Mercurial > skaapsteker
annotate skaapsteker/levelscene.py @ 315:0fc2b9d1a9cb
Recreate inventory image on scene changes
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Sat, 09 Apr 2011 00:04:35 +0200 |
parents | f29999d1bba6 |
children | bd137fdfe3c6 |
rev | line source |
---|---|
15 | 1 """Scene wrapping a level object.""" |
2 | |
169
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
3 from pygame.locals import (KEYDOWN, KEYUP, K_DOWN, K_ESCAPE, K_LEFT, K_RIGHT, |
244
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
4 K_SEMICOLON, K_UP, K_p, K_q, K_x, K_z, K_RETURN) |
106 | 5 |
248 | 6 import pygame |
265
7628467eecd9
Add double-key tap support
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
7 import time |
248 | 8 |
106 | 9 from . import options |
248 | 10 from . import engine |
11 from . import level | |
12 from . import physics | |
265
7628467eecd9
Add double-key tap support
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
13 from . import constants |
248 | 14 from .sprites import player |
15 from .widgets.text import Text | |
251
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
16 from .widgets.bubble import DialogueWidget |
248 | 17 |
15 | 18 |
19 class LevelScene(engine.Scene): | |
20 | |
264
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
263
diff
changeset
|
21 def __init__(self, game_state, soundsystem, doorway_def=None): |
262 | 22 super(LevelScene, self).__init__(game_state, soundsystem) |
189
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
183
diff
changeset
|
23 |
264
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
263
diff
changeset
|
24 if doorway_def is not None: |
288
ddcfc159dc2a
Combine 'fox' and 'player' sections in game.json.
Simon Cross <hodgestar@gmail.com>
parents:
273
diff
changeset
|
25 fox = self.game_state.world.fox |
ddcfc159dc2a
Combine 'fox' and 'player' sections in game.json.
Simon Cross <hodgestar@gmail.com>
parents:
273
diff
changeset
|
26 fox.level, fox.doorway = doorway_def.split('.') |
ddcfc159dc2a
Combine 'fox' and 'player' sections in game.json.
Simon Cross <hodgestar@gmail.com>
parents:
273
diff
changeset
|
27 self._level = level.Level(self.game_state.world.fox.level, soundsystem) |
244
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
28 self._player_dead = False |
251
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
29 self._dialogue = None |
189
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
183
diff
changeset
|
30 |
264
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
263
diff
changeset
|
31 self.setup_player() |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
263
diff
changeset
|
32 |
40
fa1bb94cfa76
Re-factor drawing to blit to a clip rect in the One True Level Surface before blitting that to the screen.
Simon Cross <hodgestar@gmail.com>
parents:
16
diff
changeset
|
33 self._level_surface = self._level.get_surface() |
111 | 34 self._clip_rect = None |
40
fa1bb94cfa76
Re-factor drawing to blit to a clip rect in the One True Level Surface before blitting that to the screen.
Simon Cross <hodgestar@gmail.com>
parents:
16
diff
changeset
|
35 self._world = physics.World() |
173 | 36 self._paused = False |
40
fa1bb94cfa76
Re-factor drawing to blit to a clip rect in the One True Level Surface before blitting that to the screen.
Simon Cross <hodgestar@gmail.com>
parents:
16
diff
changeset
|
37 |
189
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
183
diff
changeset
|
38 for sprite in self._level.sprites: |
50
7d411ed02eac
Initial stab at detecting collisions.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
39 self._world.add(sprite) |
302
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
290
diff
changeset
|
40 npcs_and_items = game_state.create_sprites(self._level.name) |
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
290
diff
changeset
|
41 self._npcs = dict((s.name, s) for s in npcs_and_items if hasattr(s, 'dsm')) |
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
290
diff
changeset
|
42 for sprite in npcs_and_items: |
127
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
124
diff
changeset
|
43 self._world.add(sprite) |
189
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
183
diff
changeset
|
44 self._world.add(self._player) |
43 | 45 |
102
2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
46 self._build_action_map() |
273
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
270
diff
changeset
|
47 self._key_sequence = [] |
102
2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
48 |
264
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
263
diff
changeset
|
49 def setup_player(self): |
288
ddcfc159dc2a
Combine 'fox' and 'player' sections in game.json.
Simon Cross <hodgestar@gmail.com>
parents:
273
diff
changeset
|
50 doorway = self._level.doorways[self.game_state.world.fox.doorway] |
264
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
263
diff
changeset
|
51 self._player = player.Player(self.game_state.world, self._soundsystem) |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
263
diff
changeset
|
52 self._player.set_facing(doorway.facing) |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
263
diff
changeset
|
53 self._player.set_image() |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
263
diff
changeset
|
54 self._player.set_pos(doorway._starting_tile_pos) |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
263
diff
changeset
|
55 |
265
7628467eecd9
Add double-key tap support
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
56 # Double tap stuff |
7628467eecd9
Add double-key tap support
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
57 self._last_keydown_time = None |
7628467eecd9
Add double-key tap support
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
58 self._last_keyup_time = None |
264
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
263
diff
changeset
|
59 |
102
2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
60 def _build_action_map(self): |
169
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
61 action = lambda s: getattr(self._player, 'action_%s' % s) |
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
62 |
244
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
63 self._quit_keys = set([K_q, K_ESCAPE]) |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
64 self._restart_keys = set([K_x, K_z, K_RETURN]) |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
65 |
169
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
66 self._fast_key_map = { |
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
67 K_LEFT: action('left'), |
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
68 K_RIGHT: action('right'), |
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
69 K_UP: action('up'), |
106 | 70 } |
169
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
71 self._fast_keys_down = set() |
106 | 72 |
169
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
73 self._slow_key_map = { |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
288
diff
changeset
|
74 K_DOWN: action('down'), |
169
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
75 K_ESCAPE: self._quit, |
173 | 76 K_p: self._toggle_pause, |
169
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
77 } |
106 | 78 if options['dvorak']: |
169
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
79 self._slow_key_map[K_SEMICOLON] = action('fire1') |
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
80 self._slow_key_map[K_q] = action('fire2') |
106 | 81 else: |
169
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
82 self._slow_key_map[K_x] = action('fire1') |
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
83 self._slow_key_map[K_z] = action('fire2') |
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
84 self._slow_key_map[K_q] = self._quit |
102
2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
85 |
265
7628467eecd9
Add double-key tap support
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
86 self._key_tap_map = { |
7628467eecd9
Add double-key tap support
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
87 (K_LEFT, K_LEFT) : action('double_left'), |
7628467eecd9
Add double-key tap support
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
88 (K_RIGHT, K_RIGHT) : action('double_right'), |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
288
diff
changeset
|
89 (K_UP, K_UP) : action('double_up'), |
265
7628467eecd9
Add double-key tap support
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
90 } |
7628467eecd9
Add double-key tap support
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
91 |
244
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
92 def _quit(self, pause=True): |
102
2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
93 import menuscene # avoid circular import |
244
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
94 if pause: |
262 | 95 engine.ChangeScene.post(menuscene.MenuScene(self.game_state, self._soundsystem, self)) |
244
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
96 else: |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
97 # FIXME: When starting the game, we shoudl ensure we have sane |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
98 # states |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
99 if self._player_dead: |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
100 self._player.restore() |
262 | 101 engine.ChangeScene.post(menuscene.MenuScene(self.game_state, self._soundsystem, None)) |
244
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
102 |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
103 def _restart(self): |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
104 if self._player_dead: |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
105 self._player.restore() |
264
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
263
diff
changeset
|
106 engine.ChangeScene.post(LevelScene(self.game_state, self._soundsystem)) |
104
12ce1d131a72
Hack in resume game option
Neil Muller <drnlmuller@gmail.com>
parents:
102
diff
changeset
|
107 |
173 | 108 def _toggle_pause(self): |
109 if self._paused: | |
110 self._world.thaw() | |
111 self._paused = False | |
112 else: | |
113 self._world.freeze() | |
114 self._paused = True | |
115 | |
251
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
116 def _open_dialogue(self, npc): |
302
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
290
diff
changeset
|
117 if isinstance(npc, basestring): |
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
290
diff
changeset
|
118 npc = self._npcs[npc] |
251
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
119 if npc.dsm.has_text(): |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
120 if self._dialogue is not None: |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
121 self._dialogue.close() |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
122 self._world.freeze() |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
123 self._dialogue = DialogueWidget(npc) |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
124 else: |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
125 self._close_dialogue() |
250
8d7edd77bfbf
Start hooking up NPC interactions.
Simon Cross <hodgestar@gmail.com>
parents:
248
diff
changeset
|
126 |
251
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
127 def _close_dialogue(self): |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
128 if self._dialogue is not None: |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
129 self._world.thaw() |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
130 self._dialogue.close() |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
131 self._dialogue = None |
250
8d7edd77bfbf
Start hooking up NPC interactions.
Simon Cross <hodgestar@gmail.com>
parents:
248
diff
changeset
|
132 |
170
aa154c4086cb
Replace scene .freeze and .thaw methods with .enter and .leave.
Simon Cross <hodgestar@gmail.com>
parents:
169
diff
changeset
|
133 def leave(self): |
104
12ce1d131a72
Hack in resume game option
Neil Muller <drnlmuller@gmail.com>
parents:
102
diff
changeset
|
134 """Freeze the scene, for serialization""" |
12ce1d131a72
Hack in resume game option
Neil Muller <drnlmuller@gmail.com>
parents:
102
diff
changeset
|
135 self._world.freeze() |
243
4050e77dade6
Add support for background music tracks
Neil Muller <drnlmuller@gmail.com>
parents:
206
diff
changeset
|
136 self._level.leave() |
104
12ce1d131a72
Hack in resume game option
Neil Muller <drnlmuller@gmail.com>
parents:
102
diff
changeset
|
137 |
170
aa154c4086cb
Replace scene .freeze and .thaw methods with .enter and .leave.
Simon Cross <hodgestar@gmail.com>
parents:
169
diff
changeset
|
138 def enter(self): |
104
12ce1d131a72
Hack in resume game option
Neil Muller <drnlmuller@gmail.com>
parents:
102
diff
changeset
|
139 """Unfreeze""" |
12ce1d131a72
Hack in resume game option
Neil Muller <drnlmuller@gmail.com>
parents:
102
diff
changeset
|
140 self._world.thaw() |
243
4050e77dade6
Add support for background music tracks
Neil Muller <drnlmuller@gmail.com>
parents:
206
diff
changeset
|
141 self._level.enter() |
88
bf4adb40ca3c
Rough outline of a player object, completely with statuesque flying fox
Neil Muller <drnlmuller@gmail.com>
parents:
50
diff
changeset
|
142 |
123 | 143 def draw(self, screen_surface, engine): |
111 | 144 if self._clip_rect is None: |
145 self._clip_rect = pygame.Rect((0, 0), screen_surface.get_size()) | |
15 | 146 |
251
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
147 if not self._paused and not self._dialogue: |
173 | 148 for key in self._fast_keys_down: |
149 self._fast_key_map[key]() | |
150 self._world.update() | |
169
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
151 |
111 | 152 self._update_clip_rect() |
40
fa1bb94cfa76
Re-factor drawing to blit to a clip rect in the One True Level Surface before blitting that to the screen.
Simon Cross <hodgestar@gmail.com>
parents:
16
diff
changeset
|
153 |
111 | 154 self._level_surface.set_clip(self._clip_rect) |
40
fa1bb94cfa76
Re-factor drawing to blit to a clip rect in the One True Level Surface before blitting that to the screen.
Simon Cross <hodgestar@gmail.com>
parents:
16
diff
changeset
|
155 self._level.draw(self._level_surface) |
fa1bb94cfa76
Re-factor drawing to blit to a clip rect in the One True Level Surface before blitting that to the screen.
Simon Cross <hodgestar@gmail.com>
parents:
16
diff
changeset
|
156 self._world.draw(self._level_surface) |
251
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
157 if self._dialogue: |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
158 self._dialogue.draw(self._level_surface) |
40
fa1bb94cfa76
Re-factor drawing to blit to a clip rect in the One True Level Surface before blitting that to the screen.
Simon Cross <hodgestar@gmail.com>
parents:
16
diff
changeset
|
159 |
313 | 160 self._draw_fox_status() |
161 | |
123 | 162 fps_text_pos = self._clip_rect.left + 10, self._clip_rect.top + 10 |
183 | 163 fps_text = Text('FPS: %.1f' % engine.get_fps(), fps_text_pos, shadow='white') |
123 | 164 fps_text.draw(self._level_surface) |
165 | |
173 | 166 if self._paused: |
167 paused_text_pos = self._clip_rect.centerx - 10, self._clip_rect.centery - 10 | |
168 paused_text = Text('Paused', paused_text_pos) | |
169 paused_text.draw(self._level_surface) | |
170 | |
244
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
171 if self._player_dead: |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
172 death_text_pos = self._clip_rect.centerx - 100, self._clip_rect.centery - 100 |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
173 death_text = Text("You've died.\nPress Escape to exit or Return to restart the level", death_text_pos) |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
174 death_text.draw(self._level_surface) |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
175 |
111 | 176 screen_surface.blit(self._level_surface, (0, 0), self._clip_rect) |
177 | |
313 | 178 def _draw_fox_status(self): |
179 """Draw the fox inventory. The tails and the item are drawn on the | |
180 left side of the screen, a health bar and collected tofu and | |
181 scroll counts are shown on the right""" | |
314 | 182 fox = self.game_state.world.fox # conveience shortcur |
313 | 183 # Draw the healt bar |
184 health_bottom = self._clip_rect.right - 30, self._clip_rect.top + 200 | |
185 bar = pygame.Rect(0, 0, constants.FoxHud.HEALTH_WIDTH, constants.FoxHud.HEALTH_HEIGHT) | |
186 bar.bottomleft = health_bottom | |
187 pygame.draw.rect(self._level_surface, constants.FoxHud.HEALTH_BACKGROUND, bar) | |
314 | 188 bar.height = int(constants.FoxHud.HEALTH_HEIGHT * float(fox.cur_health)/fox.max_health) |
313 | 189 bar.bottomleft = health_bottom |
190 pygame.draw.rect(self._level_surface, constants.FoxHud.HEALTH_FOREGROUND, bar) | |
191 | |
314 | 192 # Draw inventory |
193 my_item = fox.item | |
194 if my_item: | |
195 # Get image and resize it | |
315
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
196 inv_pos = self._clip_rect.left + 8, self._clip_rect.top + 32 |
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
197 if self._player.inventory_image is None: |
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
198 self._player.make_inventory_image() |
314 | 199 self._level_surface.blit(self._player.inventory_image, inv_pos) |
200 | |
313 | 201 |
202 | |
111 | 203 def _update_clip_rect(self): |
204 cr = self._clip_rect | |
205 lr = self._level_surface.get_rect() | |
150
a5ca417ee39a
Centre camera one tile higher then the midbottom of the player, to avoid bouncing around due to animation size mismatches.
Jeremy Thurgood <firxen@gmail.com>
parents:
145
diff
changeset
|
206 cr.center = self._player.collide_rect.move(0, -level.TILE_SIZE[0]).midbottom |
124
fde3d25cb4b9
Replace custom clamping code with a call to .clamp_ip.
Simon Cross <hodgestar@gmail.com>
parents:
123
diff
changeset
|
207 cr.clamp_ip(lr) |
15 | 208 |
270
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
209 |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
210 def _detect_key_sequence(self, ev): |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
211 if ev.key not in self._fast_key_map: |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
212 self._key_sequence = [] |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
213 return False |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
214 |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
215 if ev.type is KEYUP: |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
216 if (not self._key_sequence |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
217 or ev.key != self._key_sequence[-1] |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
218 or (time.time() - self._last_keydown_time) > constants.DOUBLE_TAP_TIME): |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
219 self._key_sequence = [] |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
220 return False |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
221 self._last_keyup_time = time.time() |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
222 return False |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
223 |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
224 if ev.type is KEYDOWN: |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
225 if self._last_keyup_time is None or (time.time() - self._last_keyup_time) > constants.DOUBLE_TAP_TIME: |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
226 self._key_sequence = [] |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
227 self._key_sequence.append(ev.key) |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
228 self._last_keydown_time = time.time() |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
229 for seq in self._key_tap_map: |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
230 if seq == tuple(self._key_sequence[-len(seq):]): |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
231 self._key_sequence = self._key_sequence[-len(seq):] |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
232 return True |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
233 return False |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
234 |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
235 |
16
e2837dd40607
Show how to use dispatch on levelscene.
Simon Cross <hodgestar@gmail.com>
parents:
15
diff
changeset
|
236 def dispatch(self, ev): |
250
8d7edd77bfbf
Start hooking up NPC interactions.
Simon Cross <hodgestar@gmail.com>
parents:
248
diff
changeset
|
237 if ev.type is KEYDOWN: |
270
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
238 |
244
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
239 if self._player_dead: |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
240 if ev.key in self._restart_keys: |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
241 self._restart() |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
242 elif ev.key in self._quit_keys: |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
243 self._quit(False) |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
244 return |
270
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
245 |
251
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
246 if self._dialogue: |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
247 self._dialogue.dispatch(ev) |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
248 return |
270
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
249 |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
250 if self._detect_key_sequence(ev): |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
251 print "Detected double-tap:", self._key_sequence |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
252 action = self._key_tap_map.get(tuple(self._key_sequence)) |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
253 if action is not None: |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
254 action() |
169
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
255 if ev.key in self._fast_key_map: |
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
256 self._fast_keys_down.add(ev.key) |
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
257 action = self._slow_key_map.get(ev.key) |
102
2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
258 if action is not None: |
2e913a89e69d
Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
259 action() |
270
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
260 |
169
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
261 elif ev.type is KEYUP: |
270
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
262 self._detect_key_sequence(ev) |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
263 |
169
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
264 if ev.key in self._fast_key_map: |
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
265 self._fast_keys_down.discard(ev.key) |
265
7628467eecd9
Add double-key tap support
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
266 |
250
8d7edd77bfbf
Start hooking up NPC interactions.
Simon Cross <hodgestar@gmail.com>
parents:
248
diff
changeset
|
267 elif engine.PlayerDied.matches(ev): |
8d7edd77bfbf
Start hooking up NPC interactions.
Simon Cross <hodgestar@gmail.com>
parents:
248
diff
changeset
|
268 self._player_dead = True |
8d7edd77bfbf
Start hooking up NPC interactions.
Simon Cross <hodgestar@gmail.com>
parents:
248
diff
changeset
|
269 elif engine.OpenDialog.matches(ev): |
251
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
270 self._open_dialogue(ev.npc) |
250
8d7edd77bfbf
Start hooking up NPC interactions.
Simon Cross <hodgestar@gmail.com>
parents:
248
diff
changeset
|
271 elif engine.CloseDialog.matches(ev): |
251
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
272 self._close_dialogue() |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
288
diff
changeset
|
273 elif engine.ItemRepopulationEvent.matches(ev): |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
288
diff
changeset
|
274 self._world.add(ev.item) |