Mercurial > skaapsteker
annotate skaapsteker/levelscene.py @ 324:ca428c1451a2
Hook up drawing tofu and scroll counts
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Sat, 09 Apr 2011 01:08:55 +0200 |
parents | bd137fdfe3c6 |
children | a9d760134706 |
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 |
318 | 14 from . import data |
248 | 15 from .sprites import player |
16 from .widgets.text import Text | |
251
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
17 from .widgets.bubble import DialogueWidget |
248 | 18 |
15 | 19 |
20 class LevelScene(engine.Scene): | |
21 | |
264
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
263
diff
changeset
|
22 def __init__(self, game_state, soundsystem, doorway_def=None): |
262 | 23 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
|
24 |
264
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
263
diff
changeset
|
25 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
|
26 fox = self.game_state.world.fox |
ddcfc159dc2a
Combine 'fox' and 'player' sections in game.json.
Simon Cross <hodgestar@gmail.com>
parents:
273
diff
changeset
|
27 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
|
28 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
|
29 self._player_dead = False |
251
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
30 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
|
31 |
264
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
263
diff
changeset
|
32 self.setup_player() |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
263
diff
changeset
|
33 |
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
|
34 self._level_surface = self._level.get_surface() |
111 | 35 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
|
36 self._world = physics.World() |
173 | 37 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
|
38 |
318 | 39 # Helper images for hud |
40 self._tofu = data.load_image('icons/tofu.png') | |
41 self._scroll = data.load_image('icons/haiku-scroll.png') | |
42 self._tail = data.load_image('icons/tail.png') | |
43 | |
44 | |
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
|
45 for sprite in self._level.sprites: |
50
7d411ed02eac
Initial stab at detecting collisions.
Simon Cross <hodgestar@gmail.com>
parents:
43
diff
changeset
|
46 self._world.add(sprite) |
302
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
290
diff
changeset
|
47 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
|
48 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
|
49 for sprite in npcs_and_items: |
127
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
124
diff
changeset
|
50 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
|
51 self._world.add(self._player) |
43 | 52 |
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
|
53 self._build_action_map() |
273
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
270
diff
changeset
|
54 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
|
55 |
264
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
263
diff
changeset
|
56 def setup_player(self): |
288
ddcfc159dc2a
Combine 'fox' and 'player' sections in game.json.
Simon Cross <hodgestar@gmail.com>
parents:
273
diff
changeset
|
57 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
|
58 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
|
59 self._player.set_facing(doorway.facing) |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
263
diff
changeset
|
60 self._player.set_image() |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
263
diff
changeset
|
61 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
|
62 |
265
7628467eecd9
Add double-key tap support
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
63 # Double tap stuff |
7628467eecd9
Add double-key tap support
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
64 self._last_keydown_time = None |
7628467eecd9
Add double-key tap support
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
65 self._last_keyup_time = None |
264
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
263
diff
changeset
|
66 |
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
|
67 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
|
68 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
|
69 |
244
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
70 self._quit_keys = set([K_q, K_ESCAPE]) |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
71 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
|
72 |
169
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
73 self._fast_key_map = { |
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
74 K_LEFT: action('left'), |
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
75 K_RIGHT: action('right'), |
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
76 K_UP: action('up'), |
106 | 77 } |
169
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
78 self._fast_keys_down = set() |
106 | 79 |
169
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
80 self._slow_key_map = { |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
288
diff
changeset
|
81 K_DOWN: action('down'), |
169
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
82 K_ESCAPE: self._quit, |
173 | 83 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
|
84 } |
106 | 85 if options['dvorak']: |
169
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
86 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
|
87 self._slow_key_map[K_q] = action('fire2') |
106 | 88 else: |
169
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
89 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
|
90 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
|
91 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
|
92 |
265
7628467eecd9
Add double-key tap support
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
93 self._key_tap_map = { |
7628467eecd9
Add double-key tap support
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
94 (K_LEFT, K_LEFT) : action('double_left'), |
7628467eecd9
Add double-key tap support
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
95 (K_RIGHT, K_RIGHT) : action('double_right'), |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
288
diff
changeset
|
96 (K_UP, K_UP) : action('double_up'), |
265
7628467eecd9
Add double-key tap support
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
97 } |
7628467eecd9
Add double-key tap support
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
98 |
244
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
99 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
|
100 import menuscene # avoid circular import |
244
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
101 if pause: |
262 | 102 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
|
103 else: |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
104 # 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
|
105 # states |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
106 if self._player_dead: |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
107 self._player.restore() |
262 | 108 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
|
109 |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
110 def _restart(self): |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
111 if self._player_dead: |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
112 self._player.restore() |
264
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
263
diff
changeset
|
113 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
|
114 |
173 | 115 def _toggle_pause(self): |
116 if self._paused: | |
117 self._world.thaw() | |
118 self._paused = False | |
119 else: | |
120 self._world.freeze() | |
121 self._paused = True | |
122 | |
251
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
123 def _open_dialogue(self, npc): |
302
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
290
diff
changeset
|
124 if isinstance(npc, basestring): |
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
290
diff
changeset
|
125 npc = self._npcs[npc] |
251
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
126 if npc.dsm.has_text(): |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
127 if self._dialogue is not None: |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
128 self._dialogue.close() |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
129 self._world.freeze() |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
130 self._dialogue = DialogueWidget(npc) |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
131 else: |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
132 self._close_dialogue() |
250
8d7edd77bfbf
Start hooking up NPC interactions.
Simon Cross <hodgestar@gmail.com>
parents:
248
diff
changeset
|
133 |
251
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
134 def _close_dialogue(self): |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
135 if self._dialogue is not None: |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
136 self._world.thaw() |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
137 self._dialogue.close() |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
138 self._dialogue = None |
250
8d7edd77bfbf
Start hooking up NPC interactions.
Simon Cross <hodgestar@gmail.com>
parents:
248
diff
changeset
|
139 |
170
aa154c4086cb
Replace scene .freeze and .thaw methods with .enter and .leave.
Simon Cross <hodgestar@gmail.com>
parents:
169
diff
changeset
|
140 def leave(self): |
104
12ce1d131a72
Hack in resume game option
Neil Muller <drnlmuller@gmail.com>
parents:
102
diff
changeset
|
141 """Freeze the scene, for serialization""" |
12ce1d131a72
Hack in resume game option
Neil Muller <drnlmuller@gmail.com>
parents:
102
diff
changeset
|
142 self._world.freeze() |
243
4050e77dade6
Add support for background music tracks
Neil Muller <drnlmuller@gmail.com>
parents:
206
diff
changeset
|
143 self._level.leave() |
104
12ce1d131a72
Hack in resume game option
Neil Muller <drnlmuller@gmail.com>
parents:
102
diff
changeset
|
144 |
170
aa154c4086cb
Replace scene .freeze and .thaw methods with .enter and .leave.
Simon Cross <hodgestar@gmail.com>
parents:
169
diff
changeset
|
145 def enter(self): |
104
12ce1d131a72
Hack in resume game option
Neil Muller <drnlmuller@gmail.com>
parents:
102
diff
changeset
|
146 """Unfreeze""" |
12ce1d131a72
Hack in resume game option
Neil Muller <drnlmuller@gmail.com>
parents:
102
diff
changeset
|
147 self._world.thaw() |
243
4050e77dade6
Add support for background music tracks
Neil Muller <drnlmuller@gmail.com>
parents:
206
diff
changeset
|
148 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
|
149 |
123 | 150 def draw(self, screen_surface, engine): |
111 | 151 if self._clip_rect is None: |
152 self._clip_rect = pygame.Rect((0, 0), screen_surface.get_size()) | |
15 | 153 |
251
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
154 if not self._paused and not self._dialogue: |
173 | 155 for key in self._fast_keys_down: |
156 self._fast_key_map[key]() | |
157 self._world.update() | |
169
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
158 |
111 | 159 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
|
160 |
111 | 161 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
|
162 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
|
163 self._world.draw(self._level_surface) |
251
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
164 if self._dialogue: |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
165 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
|
166 |
313 | 167 self._draw_fox_status() |
168 | |
123 | 169 fps_text_pos = self._clip_rect.left + 10, self._clip_rect.top + 10 |
183 | 170 fps_text = Text('FPS: %.1f' % engine.get_fps(), fps_text_pos, shadow='white') |
123 | 171 fps_text.draw(self._level_surface) |
172 | |
173 | 173 if self._paused: |
174 paused_text_pos = self._clip_rect.centerx - 10, self._clip_rect.centery - 10 | |
175 paused_text = Text('Paused', paused_text_pos) | |
176 paused_text.draw(self._level_surface) | |
177 | |
244
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
178 if self._player_dead: |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
179 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
|
180 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
|
181 death_text.draw(self._level_surface) |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
182 |
111 | 183 screen_surface.blit(self._level_surface, (0, 0), self._clip_rect) |
184 | |
313 | 185 def _draw_fox_status(self): |
186 """Draw the fox inventory. The tails and the item are drawn on the | |
187 left side of the screen, a health bar and collected tofu and | |
188 scroll counts are shown on the right""" | |
314 | 189 fox = self.game_state.world.fox # conveience shortcur |
313 | 190 # Draw the healt bar |
191 health_bottom = self._clip_rect.right - 30, self._clip_rect.top + 200 | |
192 bar = pygame.Rect(0, 0, constants.FoxHud.HEALTH_WIDTH, constants.FoxHud.HEALTH_HEIGHT) | |
193 bar.bottomleft = health_bottom | |
194 pygame.draw.rect(self._level_surface, constants.FoxHud.HEALTH_BACKGROUND, bar) | |
314 | 195 bar.height = int(constants.FoxHud.HEALTH_HEIGHT * float(fox.cur_health)/fox.max_health) |
313 | 196 bar.bottomleft = health_bottom |
197 pygame.draw.rect(self._level_surface, constants.FoxHud.HEALTH_FOREGROUND, bar) | |
198 | |
314 | 199 # Draw inventory |
200 my_item = fox.item | |
201 if my_item: | |
202 # Get image and resize it | |
315
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
203 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
|
204 if self._player.inventory_image is None: |
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
205 self._player.make_inventory_image() |
314 | 206 self._level_surface.blit(self._player.inventory_image, inv_pos) |
207 | |
318 | 208 # Draw tails |
209 # FIXME: Use correct tail icon | |
210 for tail in fox.tails: | |
211 tail_pos = self._clip_rect.left + 8, self._clip_rect.top + constants.FoxHud.TAIL_POSITIONS[tail] | |
212 self._level_surface.blit(self._tail, tail_pos) | |
313 | 213 |
324
ca428c1451a2
Hook up drawing tofu and scroll counts
Neil Muller <drnlmuller@gmail.com>
parents:
318
diff
changeset
|
214 # Draw scroll count |
ca428c1451a2
Hook up drawing tofu and scroll counts
Neil Muller <drnlmuller@gmail.com>
parents:
318
diff
changeset
|
215 |
ca428c1451a2
Hook up drawing tofu and scroll counts
Neil Muller <drnlmuller@gmail.com>
parents:
318
diff
changeset
|
216 pos = self._clip_rect.right - 20, self._clip_rect.top + 225 |
ca428c1451a2
Hook up drawing tofu and scroll counts
Neil Muller <drnlmuller@gmail.com>
parents:
318
diff
changeset
|
217 count = Text("%s" % fox.scrolls, pos) |
ca428c1451a2
Hook up drawing tofu and scroll counts
Neil Muller <drnlmuller@gmail.com>
parents:
318
diff
changeset
|
218 count.draw(self._level_surface) |
ca428c1451a2
Hook up drawing tofu and scroll counts
Neil Muller <drnlmuller@gmail.com>
parents:
318
diff
changeset
|
219 pos = self._clip_rect.right - 55, self._clip_rect.top + 220 |
ca428c1451a2
Hook up drawing tofu and scroll counts
Neil Muller <drnlmuller@gmail.com>
parents:
318
diff
changeset
|
220 self._level_surface.blit(self._scroll, pos) |
ca428c1451a2
Hook up drawing tofu and scroll counts
Neil Muller <drnlmuller@gmail.com>
parents:
318
diff
changeset
|
221 |
ca428c1451a2
Hook up drawing tofu and scroll counts
Neil Muller <drnlmuller@gmail.com>
parents:
318
diff
changeset
|
222 # Draw tofu count |
ca428c1451a2
Hook up drawing tofu and scroll counts
Neil Muller <drnlmuller@gmail.com>
parents:
318
diff
changeset
|
223 pos = self._clip_rect.right - 20, self._clip_rect.top + 255 |
ca428c1451a2
Hook up drawing tofu and scroll counts
Neil Muller <drnlmuller@gmail.com>
parents:
318
diff
changeset
|
224 count = Text("%s" % fox.tofu, pos) |
ca428c1451a2
Hook up drawing tofu and scroll counts
Neil Muller <drnlmuller@gmail.com>
parents:
318
diff
changeset
|
225 count.draw(self._level_surface) |
ca428c1451a2
Hook up drawing tofu and scroll counts
Neil Muller <drnlmuller@gmail.com>
parents:
318
diff
changeset
|
226 pos = self._clip_rect.right - 55, self._clip_rect.top + 250 |
ca428c1451a2
Hook up drawing tofu and scroll counts
Neil Muller <drnlmuller@gmail.com>
parents:
318
diff
changeset
|
227 self._level_surface.blit(self._tofu, pos) |
ca428c1451a2
Hook up drawing tofu and scroll counts
Neil Muller <drnlmuller@gmail.com>
parents:
318
diff
changeset
|
228 |
ca428c1451a2
Hook up drawing tofu and scroll counts
Neil Muller <drnlmuller@gmail.com>
parents:
318
diff
changeset
|
229 |
313 | 230 |
111 | 231 def _update_clip_rect(self): |
232 cr = self._clip_rect | |
233 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
|
234 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
|
235 cr.clamp_ip(lr) |
15 | 236 |
270
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
237 |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
238 def _detect_key_sequence(self, ev): |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
239 if ev.key not in self._fast_key_map: |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
240 self._key_sequence = [] |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
241 return False |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
242 |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
243 if ev.type is KEYUP: |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
244 if (not self._key_sequence |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
245 or ev.key != self._key_sequence[-1] |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
246 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
|
247 self._key_sequence = [] |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
248 return False |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
249 self._last_keyup_time = time.time() |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
250 return False |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
251 |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
252 if ev.type is KEYDOWN: |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
253 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
|
254 self._key_sequence = [] |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
255 self._key_sequence.append(ev.key) |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
256 self._last_keydown_time = time.time() |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
257 for seq in self._key_tap_map: |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
258 if seq == tuple(self._key_sequence[-len(seq):]): |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
259 self._key_sequence = self._key_sequence[-len(seq):] |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
260 return True |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
261 return False |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
262 |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
263 |
16
e2837dd40607
Show how to use dispatch on levelscene.
Simon Cross <hodgestar@gmail.com>
parents:
15
diff
changeset
|
264 def dispatch(self, ev): |
250
8d7edd77bfbf
Start hooking up NPC interactions.
Simon Cross <hodgestar@gmail.com>
parents:
248
diff
changeset
|
265 if ev.type is KEYDOWN: |
270
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
266 |
244
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
267 if self._player_dead: |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
268 if ev.key in self._restart_keys: |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
269 self._restart() |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
270 elif ev.key in self._quit_keys: |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
271 self._quit(False) |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
243
diff
changeset
|
272 return |
270
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
273 |
251
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
274 if self._dialogue: |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
275 self._dialogue.dispatch(ev) |
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
276 return |
270
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
277 |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
278 if self._detect_key_sequence(ev): |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
279 print "Detected double-tap:", self._key_sequence |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
280 action = self._key_tap_map.get(tuple(self._key_sequence)) |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
281 if action is not None: |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
282 action() |
169
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
283 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
|
284 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
|
285 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
|
286 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
|
287 action() |
270
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
288 |
169
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
289 elif ev.type is KEYUP: |
270
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
290 self._detect_key_sequence(ev) |
e47efa33903b
Fixed key sequence handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
268
diff
changeset
|
291 |
169
b7a8f4a677e1
Replace key repeating with custom tracking of fast keys.
Simon Cross <hodgestar@gmail.com>
parents:
165
diff
changeset
|
292 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
|
293 self._fast_keys_down.discard(ev.key) |
265
7628467eecd9
Add double-key tap support
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
294 |
250
8d7edd77bfbf
Start hooking up NPC interactions.
Simon Cross <hodgestar@gmail.com>
parents:
248
diff
changeset
|
295 elif engine.PlayerDied.matches(ev): |
8d7edd77bfbf
Start hooking up NPC interactions.
Simon Cross <hodgestar@gmail.com>
parents:
248
diff
changeset
|
296 self._player_dead = True |
8d7edd77bfbf
Start hooking up NPC interactions.
Simon Cross <hodgestar@gmail.com>
parents:
248
diff
changeset
|
297 elif engine.OpenDialog.matches(ev): |
251
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
298 self._open_dialogue(ev.npc) |
250
8d7edd77bfbf
Start hooking up NPC interactions.
Simon Cross <hodgestar@gmail.com>
parents:
248
diff
changeset
|
299 elif engine.CloseDialog.matches(ev): |
251
432f6997d306
More hooking up of interacting with NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
250
diff
changeset
|
300 self._close_dialogue() |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
288
diff
changeset
|
301 elif engine.ItemRepopulationEvent.matches(ev): |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
288
diff
changeset
|
302 self._world.add(ev.item) |