Mercurial > nagslang
annotate nagslang/screens/area.py @ 269:9fcdb106424a
Add first draft of sound support.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Thu, 05 Sep 2013 00:54:26 +0200 |
parents | 9d05f0c4bca3 |
children | 933c2f9371ca |
rev | line source |
---|---|
18 | 1 """Display a game area.""" |
2 | |
20 | 3 import pygame |
21 | 4 import pymunk |
5 import pymunk.pygame_util | |
20 | 6 |
266 | 7 from nagslang.constants import COLLISION_TYPE_PLAYER, CALLBACK_COLLIDERS |
261
db7c8e74efb4
(really rubbish) bullets
Stefano Rivera <stefano@rivera.za.net>
parents:
258
diff
changeset
|
8 from nagslang.events import ScreenChange, DoorEvent, FireEvent |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
9 from nagslang.level import Level |
65
a99ac95a2940
Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents:
64
diff
changeset
|
10 from nagslang.protagonist import Protagonist |
a99ac95a2940
Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents:
64
diff
changeset
|
11 from nagslang.screens.base import Screen |
261
db7c8e74efb4
(really rubbish) bullets
Stefano Rivera <stefano@rivera.za.net>
parents:
258
diff
changeset
|
12 from nagslang.game_object import Bullet |
269
9fcdb106424a
Add first draft of sound support.
Simon Cross <hodgestar@gmail.com>
parents:
266
diff
changeset
|
13 from nagslang.sound import sound |
18 | 14 |
83 | 15 |
34 | 16 class ControlKeys(object): |
100
96bdfadeb461
Cleaner direction key management.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
17 direction_keys = { |
96bdfadeb461
Cleaner direction key management.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
18 (0, 1): set([pygame.locals.K_UP, pygame.locals.K_w]), |
96bdfadeb461
Cleaner direction key management.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
19 (0, -1): set([pygame.locals.K_DOWN, pygame.locals.K_s]), |
96bdfadeb461
Cleaner direction key management.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
20 (-1, 0): set([pygame.locals.K_LEFT, pygame.locals.K_a]), |
96bdfadeb461
Cleaner direction key management.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
21 (1, 0): set([pygame.locals.K_RIGHT, pygame.locals.K_d]), |
96bdfadeb461
Cleaner direction key management.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
22 } |
96bdfadeb461
Cleaner direction key management.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
23 |
34 | 24 def __init__(self): |
25 self.keys_down = set() | |
26 | |
27 def key_down(self, key): | |
28 self.keys_down.add(key) | |
29 | |
30 def key_up(self, key): | |
31 self.keys_down.discard(key) | |
32 | |
33 def handle_event(self, ev): | |
34 if ev.type == pygame.locals.KEYDOWN: | |
35 self.key_down(ev.key) | |
36 elif ev.type == pygame.locals.KEYUP: | |
37 self.key_up(ev.key) | |
38 | |
100
96bdfadeb461
Cleaner direction key management.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
39 def get_direction(self): |
96bdfadeb461
Cleaner direction key management.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
40 dx, dy = 0, 0 |
96bdfadeb461
Cleaner direction key management.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
41 for (tx, ty), keys in self.direction_keys.iteritems(): |
96bdfadeb461
Cleaner direction key management.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
42 if self.keys_down & keys: |
96bdfadeb461
Cleaner direction key management.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
43 dx += tx |
96bdfadeb461
Cleaner direction key management.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
44 dy += ty |
96bdfadeb461
Cleaner direction key management.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
45 return (dx, dy) |
96bdfadeb461
Cleaner direction key management.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
46 |
34 | 47 |
107 | 48 class Drawables(object): |
49 def __init__(self): | |
50 self._drawables = {} | |
51 | |
52 def add(self, drawable): | |
53 self._drawables.setdefault(drawable.zorder, []).append(drawable) | |
54 | |
55 def remove(self, drawable): | |
56 self._drawables[drawable.zorder].remove(drawable) | |
57 | |
58 def get_drawables(self): | |
59 for zorder in sorted(self._drawables): | |
60 for drawable in self._drawables[zorder]: | |
61 yield drawable | |
62 | |
63 __iter__ = get_drawables | |
64 | |
65 | |
18 | 66 class AreaScreen(Screen): |
21 | 67 |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
68 def setup(self): |
190
97627a999042
Don't render the old scene with the protagnist's new position during scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
188
diff
changeset
|
69 self._disable_render = False # Avoid redrawing on scene changes |
34 | 70 self.keys = ControlKeys() |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
71 self._level = Level(self.name) |
145
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
143
diff
changeset
|
72 self._level.load(self.space) |
107 | 73 self._drawables = Drawables() |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
74 self.add_walls() |
188
3894cfe15823
Better collision handling, potentially locked doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
180
diff
changeset
|
75 self._add_collision_handlers() |
180
026297a03963
Add DoorEvent and tweak ScreenChange to keep more state when the player goes through a door
Neil Muller <drnlmuller@gmail.com>
parents:
147
diff
changeset
|
76 if self.protagonist is not None: |
026297a03963
Add DoorEvent and tweak ScreenChange to keep more state when the player goes through a door
Neil Muller <drnlmuller@gmail.com>
parents:
147
diff
changeset
|
77 # We do things this way to avoid extra pymunk |
026297a03963
Add DoorEvent and tweak ScreenChange to keep more state when the player goes through a door
Neil Muller <drnlmuller@gmail.com>
parents:
147
diff
changeset
|
78 # juggling to move objects between spaces |
026297a03963
Add DoorEvent and tweak ScreenChange to keep more state when the player goes through a door
Neil Muller <drnlmuller@gmail.com>
parents:
147
diff
changeset
|
79 old_protagonist = self.protagonist |
026297a03963
Add DoorEvent and tweak ScreenChange to keep more state when the player goes through a door
Neil Muller <drnlmuller@gmail.com>
parents:
147
diff
changeset
|
80 self.add_protagonist() |
026297a03963
Add DoorEvent and tweak ScreenChange to keep more state when the player goes through a door
Neil Muller <drnlmuller@gmail.com>
parents:
147
diff
changeset
|
81 self.protagonist.copy_state(old_protagonist) |
026297a03963
Add DoorEvent and tweak ScreenChange to keep more state when the player goes through a door
Neil Muller <drnlmuller@gmail.com>
parents:
147
diff
changeset
|
82 else: |
026297a03963
Add DoorEvent and tweak ScreenChange to keep more state when the player goes through a door
Neil Muller <drnlmuller@gmail.com>
parents:
147
diff
changeset
|
83 self.add_protagonist() |
145
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
143
diff
changeset
|
84 self.add_game_objects() |
269
9fcdb106424a
Add first draft of sound support.
Simon Cross <hodgestar@gmail.com>
parents:
266
diff
changeset
|
85 sound.play_music("POL-cyber-factory-short.ogg") |
21 | 86 |
188
3894cfe15823
Better collision handling, potentially locked doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
180
diff
changeset
|
87 def _collision_pre_solve_handler(self, space, arbiter): |
3894cfe15823
Better collision handling, potentially locked doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
180
diff
changeset
|
88 gobj = arbiter.shapes[1].physicser.game_object |
256
2a0bad886956
Collision handlers get the protagonist.
Jeremy Thurgood <firxen@gmail.com>
parents:
247
diff
changeset
|
89 result = gobj.collide_with_protagonist(self.protagonist) |
192
3dc2b6290e66
Document collision handler a little better.
Jeremy Thurgood <firxen@gmail.com>
parents:
191
diff
changeset
|
90 # The collision handler must return `True` or `False`. We don't want to |
3dc2b6290e66
Document collision handler a little better.
Jeremy Thurgood <firxen@gmail.com>
parents:
191
diff
changeset
|
91 # accidentally reject collisions from handlers that return `None`, so |
3dc2b6290e66
Document collision handler a little better.
Jeremy Thurgood <firxen@gmail.com>
parents:
191
diff
changeset
|
92 # we explicitly check for `False` and treate everything else as `True`. |
188
3894cfe15823
Better collision handling, potentially locked doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
180
diff
changeset
|
93 if result is False: |
3894cfe15823
Better collision handling, potentially locked doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
180
diff
changeset
|
94 return False |
3894cfe15823
Better collision handling, potentially locked doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
180
diff
changeset
|
95 return True |
3894cfe15823
Better collision handling, potentially locked doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
180
diff
changeset
|
96 |
3894cfe15823
Better collision handling, potentially locked doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
180
diff
changeset
|
97 def _add_collision_handlers(self): |
3894cfe15823
Better collision handling, potentially locked doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
180
diff
changeset
|
98 for collision_type in CALLBACK_COLLIDERS: |
3894cfe15823
Better collision handling, potentially locked doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
180
diff
changeset
|
99 self.space.add_collision_handler( |
3894cfe15823
Better collision handling, potentially locked doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
180
diff
changeset
|
100 COLLISION_TYPE_PLAYER, collision_type, |
3894cfe15823
Better collision handling, potentially locked doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
180
diff
changeset
|
101 pre_solve=self._collision_pre_solve_handler) |
3894cfe15823
Better collision handling, potentially locked doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
180
diff
changeset
|
102 |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
103 def add_walls(self): |
21 | 104 self.walls = [] |
105 body = pymunk.Body() | |
84
ef8e799477e0
Point pymunk at a screen surface, not the display to make things saner
Neil Muller <drnlmuller@gmail.com>
parents:
83
diff
changeset
|
106 body.position = (0, 0) |
53 | 107 walls = self._level.get_walls() |
108 for wall in walls: | |
109 corners = wall | |
110 corner = corners[-1] | |
111 for next_corner in corners: | |
112 wall = pymunk.Segment(body, corner, next_corner, 5) | |
113 wall.elasticity = 1.0 | |
114 self.walls.append(wall) | |
115 corner = next_corner | |
21 | 116 self.space.add(*self.walls) |
117 | |
145
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
143
diff
changeset
|
118 def add_game_objects(self): |
191 | 119 for drawable in self._level.drawables: |
145
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
143
diff
changeset
|
120 self._drawables.add(drawable) |
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
143
diff
changeset
|
121 |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
122 def add_protagonist(self): |
93
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
92
diff
changeset
|
123 self.protagonist = Protagonist(self.space, (350, 300)) |
107 | 124 self._drawables.add(self.protagonist) |
21 | 125 |
18 | 126 def handle_event(self, ev): |
20 | 127 if ev.type == pygame.locals.KEYDOWN: |
128 if ev.key == pygame.locals.K_ESCAPE: | |
129 ScreenChange.post('menu') | |
97
c177cdc41477
Add WASD controls, switch to "c" for form change.
Jeremy Thurgood <firxen@gmail.com>
parents:
93
diff
changeset
|
130 if ev.key == pygame.locals.K_c: |
47
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
131 self.protagonist.toggle_form() |
238
28d906fc2ab1
Add a world object to collect some stats
Neil Muller <drnlmuller@gmail.com>
parents:
211
diff
changeset
|
132 self.world.transformations += 1 |
261
db7c8e74efb4
(really rubbish) bullets
Stefano Rivera <stefano@rivera.za.net>
parents:
258
diff
changeset
|
133 if ev.key == pygame.locals.K_SPACE: |
db7c8e74efb4
(really rubbish) bullets
Stefano Rivera <stefano@rivera.za.net>
parents:
258
diff
changeset
|
134 self.world.attacks += 1 |
db7c8e74efb4
(really rubbish) bullets
Stefano Rivera <stefano@rivera.za.net>
parents:
258
diff
changeset
|
135 self.protagonist.attack() |
180
026297a03963
Add DoorEvent and tweak ScreenChange to keep more state when the player goes through a door
Neil Muller <drnlmuller@gmail.com>
parents:
147
diff
changeset
|
136 elif DoorEvent.matches(ev): |
026297a03963
Add DoorEvent and tweak ScreenChange to keep more state when the player goes through a door
Neil Muller <drnlmuller@gmail.com>
parents:
147
diff
changeset
|
137 self.protagonist.set_position(ev.dest_pos) |
026297a03963
Add DoorEvent and tweak ScreenChange to keep more state when the player goes through a door
Neil Muller <drnlmuller@gmail.com>
parents:
147
diff
changeset
|
138 if ev.destination != self.name: |
026297a03963
Add DoorEvent and tweak ScreenChange to keep more state when the player goes through a door
Neil Muller <drnlmuller@gmail.com>
parents:
147
diff
changeset
|
139 # Go to anther screen |
190
97627a999042
Don't render the old scene with the protagnist's new position during scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
188
diff
changeset
|
140 self._disable_render = True |
238
28d906fc2ab1
Add a world object to collect some stats
Neil Muller <drnlmuller@gmail.com>
parents:
211
diff
changeset
|
141 self.world.rooms += 1 |
180
026297a03963
Add DoorEvent and tweak ScreenChange to keep more state when the player goes through a door
Neil Muller <drnlmuller@gmail.com>
parents:
147
diff
changeset
|
142 ScreenChange.post(ev.destination, self.protagonist) |
190
97627a999042
Don't render the old scene with the protagnist's new position during scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
188
diff
changeset
|
143 return |
180
026297a03963
Add DoorEvent and tweak ScreenChange to keep more state when the player goes through a door
Neil Muller <drnlmuller@gmail.com>
parents:
147
diff
changeset
|
144 # else we're teleporting within the screen, and just the |
026297a03963
Add DoorEvent and tweak ScreenChange to keep more state when the player goes through a door
Neil Muller <drnlmuller@gmail.com>
parents:
147
diff
changeset
|
145 # position change is enough |
261
db7c8e74efb4
(really rubbish) bullets
Stefano Rivera <stefano@rivera.za.net>
parents:
258
diff
changeset
|
146 elif FireEvent.matches(ev): |
db7c8e74efb4
(really rubbish) bullets
Stefano Rivera <stefano@rivera.za.net>
parents:
258
diff
changeset
|
147 bullet = Bullet(self.space, ev.source, ev.impulse) |
db7c8e74efb4
(really rubbish) bullets
Stefano Rivera <stefano@rivera.za.net>
parents:
258
diff
changeset
|
148 self._drawables.add(bullet) |
34 | 149 self.keys.handle_event(ev) |
18 | 150 |
87 | 151 def _calc_viewport(self, level_surface, display_surface): |
152 level_size = level_surface.get_size() | |
153 display_size = display_surface.get_size() | |
93
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
92
diff
changeset
|
154 protagnist_pos = self.protagonist.physicser.get_render_position( |
127
fe1f0bb4ecf0
Handle case of display larger than the level better
Neil Muller <drnlmuller@gmail.com>
parents:
107
diff
changeset
|
155 level_surface) |
87 | 156 x_wide = display_size[0] // 2 |
157 y_wide = display_size[1] // 2 | |
127
fe1f0bb4ecf0
Handle case of display larger than the level better
Neil Muller <drnlmuller@gmail.com>
parents:
107
diff
changeset
|
158 if display_size[0] > level_size[0]: |
fe1f0bb4ecf0
Handle case of display larger than the level better
Neil Muller <drnlmuller@gmail.com>
parents:
107
diff
changeset
|
159 x = -(display_size[0] - level_size[0]) // 2 |
fe1f0bb4ecf0
Handle case of display larger than the level better
Neil Muller <drnlmuller@gmail.com>
parents:
107
diff
changeset
|
160 elif protagnist_pos[0] < x_wide: |
87 | 161 x = 0 |
162 elif protagnist_pos[0] > level_size[0] - x_wide: | |
163 x = level_size[0] - display_size[0] | |
164 else: | |
165 x = protagnist_pos[0] - x_wide | |
127
fe1f0bb4ecf0
Handle case of display larger than the level better
Neil Muller <drnlmuller@gmail.com>
parents:
107
diff
changeset
|
166 if display_size[1] > level_size[1]: |
fe1f0bb4ecf0
Handle case of display larger than the level better
Neil Muller <drnlmuller@gmail.com>
parents:
107
diff
changeset
|
167 y = -(display_size[1] - level_size[1]) // 2 |
fe1f0bb4ecf0
Handle case of display larger than the level better
Neil Muller <drnlmuller@gmail.com>
parents:
107
diff
changeset
|
168 elif protagnist_pos[1] < y_wide: |
87 | 169 y = 0 |
170 elif protagnist_pos[1] > level_size[1] - y_wide: | |
171 y = level_size[1] - display_size[1] | |
172 else: | |
173 y = protagnist_pos[1] - y_wide | |
174 return pygame.rect.Rect(x, y, display_size[0], display_size[1]) | |
175 | |
18 | 176 def render(self, surface): |
190
97627a999042
Don't render the old scene with the protagnist's new position during scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
188
diff
changeset
|
177 if self._disable_render: |
97627a999042
Don't render the old scene with the protagnist's new position during scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
188
diff
changeset
|
178 return |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
179 background = self._level.get_background() |
84
ef8e799477e0
Point pymunk at a screen surface, not the display to make things saner
Neil Muller <drnlmuller@gmail.com>
parents:
83
diff
changeset
|
180 mysurface = background.copy() |
105
0131e4606e1a
List of drawables in area.
Jeremy Thurgood <firxen@gmail.com>
parents:
100
diff
changeset
|
181 for drawable in self._drawables: |
0131e4606e1a
List of drawables in area.
Jeremy Thurgood <firxen@gmail.com>
parents:
100
diff
changeset
|
182 drawable.render(mysurface) |
87 | 183 render_rect = self._calc_viewport(mysurface, surface) |
184 surface.blit(mysurface, (0, 0), render_rect) | |
191 | 185 for overlay in self._level.overlay_drawables: |
186 if overlay.is_visible(): | |
211
434b5a3aaaff
Pass display offset to overlays for when the display is larger than the level
Neil Muller <drnlmuller@gmail.com>
parents:
192
diff
changeset
|
187 overlay.render(surface, render_rect.topleft) |
243 | 188 self.render_health_bar(surface) |
34 | 189 |
190 def tick_protagonist(self): | |
100
96bdfadeb461
Cleaner direction key management.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
191 dx, dy = self.keys.get_direction() |
47
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
192 self.protagonist.set_direction(dx, dy) |
34 | 193 |
194 def tick(self, seconds): | |
195 self.tick_protagonist() | |
143
deac6a4008e7
Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents:
140
diff
changeset
|
196 for drawable in self._drawables: |
deac6a4008e7
Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents:
140
diff
changeset
|
197 drawable.animate() |
243 | 198 |
34 | 199 super(AreaScreen, self).tick(seconds) |
243 | 200 |
258 | 201 def render_health_bar(self, surface, damage_experienced=None): |
243 | 202 rect = pygame.Rect(50, 500, 110, 50) |
258 | 203 if damage_experienced: |
204 health_box_colour = pygame.color.THECOLORS['red'] | |
205 else: | |
206 health_box_colour = pygame.color.THECOLORS['white'] | |
257 | 207 pygame.draw.rect(surface, health_box_colour, rect, 0) |
246 | 208 if self.protagonist.in_human_form(): |
243 | 209 health_colour = pygame.color.THECOLORS['red'] |
210 else: | |
211 health_colour = pygame.color.THECOLORS['purple'] | |
246 | 212 rect = pygame.Rect(55, 505, self.protagonist.get_health_level(), 40) |
243 | 213 pygame.draw.rect(surface, health_colour, |
246 | 214 rect, 0) |