Mercurial > mamba
annotate mamba/world.py @ 578:1306f7d8ed35
Add support for serving zip file containing all levels.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Sun, 06 Oct 2013 22:47:45 +0200 |
parents | 48e39d1ad5ae |
children |
rev | line source |
---|---|
78 | 1 """World state (level, plus snake, plus other sprites).""" |
2 | |
122
45dd79e9ba1b
Rudimentary FPS and clock ticking.
Simon Cross <hodgestar@gmail.com>
parents:
119
diff
changeset
|
3 import time |
45dd79e9ba1b
Rudimentary FPS and clock ticking.
Simon Cross <hodgestar@gmail.com>
parents:
119
diff
changeset
|
4 |
78 | 5 from mamba.snake import Snake |
317
e89702425a73
Add support for playing music to levels
Neil Muller <drnlmuller@gmail.com>
parents:
286
diff
changeset
|
6 from mamba.sound import play_music, pause_music, resume_music, stop_sound |
78 | 7 |
8 | |
9 class World(object): | |
10 def __init__(self, level): | |
11 self.level = level | |
285
cacc03748580
Move world initialisation to restart
Stefano Rivera <stefano@rivera.za.net>
parents:
243
diff
changeset
|
12 self.restart() |
110 | 13 |
111 | 14 def get_size(self): |
15 return self.level.get_size() | |
16 | |
110 | 17 def draw(self, surface): |
111 | 18 self.level.draw(surface) |
110 | 19 self.snake.draw(surface) |
119
119c0fb758c2
Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
111
diff
changeset
|
20 |
243 | 21 def get_sprite(self, sprite_id): |
22 return self.level.extra_sprites[sprite_id] | |
23 | |
119
119c0fb758c2
Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
111
diff
changeset
|
24 def update(self): |
122
45dd79e9ba1b
Rudimentary FPS and clock ticking.
Simon Cross <hodgestar@gmail.com>
parents:
119
diff
changeset
|
25 prev = self._prev |
45dd79e9ba1b
Rudimentary FPS and clock ticking.
Simon Cross <hodgestar@gmail.com>
parents:
119
diff
changeset
|
26 self._prev = time.time() |
191
454f7552e863
Pause the world, I may want to get off
Neil Muller <drnlmuller@gmail.com>
parents:
188
diff
changeset
|
27 if self._paused: |
454f7552e863
Pause the world, I may want to get off
Neil Muller <drnlmuller@gmail.com>
parents:
188
diff
changeset
|
28 return |
122
45dd79e9ba1b
Rudimentary FPS and clock ticking.
Simon Cross <hodgestar@gmail.com>
parents:
119
diff
changeset
|
29 if prev is None: |
45dd79e9ba1b
Rudimentary FPS and clock ticking.
Simon Cross <hodgestar@gmail.com>
parents:
119
diff
changeset
|
30 return |
45dd79e9ba1b
Rudimentary FPS and clock ticking.
Simon Cross <hodgestar@gmail.com>
parents:
119
diff
changeset
|
31 dt = self._prev - prev |
45dd79e9ba1b
Rudimentary FPS and clock ticking.
Simon Cross <hodgestar@gmail.com>
parents:
119
diff
changeset
|
32 |
181
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
33 self.snake.update(dt, self) |
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
34 |
182
0a955d2536f0
Painted Jezebel! (Assuming you name your snake Jezebel.)
Jeremy Thurgood <firxen@gmail.com>
parents:
181
diff
changeset
|
35 def interact(self, segment): |
188
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
186
diff
changeset
|
36 tiles = segment.filter_collisions(self.level.sprites) |
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
186
diff
changeset
|
37 for tile in tiles: |
243 | 38 tile.interact(self, segment) |
186 | 39 |
191
454f7552e863
Pause the world, I may want to get off
Neil Muller <drnlmuller@gmail.com>
parents:
188
diff
changeset
|
40 def pause(self): |
317
e89702425a73
Add support for playing music to levels
Neil Muller <drnlmuller@gmail.com>
parents:
286
diff
changeset
|
41 pause_music() |
286
71bc946ce1f1
Check if paused to avoid displaing two MessageBoxes on the screen if we manage to stack up SnakeDied events
Stefano Rivera <stefano@rivera.za.net>
parents:
285
diff
changeset
|
42 was_paused, self._paused = self._paused, True |
71bc946ce1f1
Check if paused to avoid displaing two MessageBoxes on the screen if we manage to stack up SnakeDied events
Stefano Rivera <stefano@rivera.za.net>
parents:
285
diff
changeset
|
43 return was_paused == False |
191
454f7552e863
Pause the world, I may want to get off
Neil Muller <drnlmuller@gmail.com>
parents:
188
diff
changeset
|
44 |
454f7552e863
Pause the world, I may want to get off
Neil Muller <drnlmuller@gmail.com>
parents:
188
diff
changeset
|
45 def resume(self): |
317
e89702425a73
Add support for playing music to levels
Neil Muller <drnlmuller@gmail.com>
parents:
286
diff
changeset
|
46 resume_music() |
286
71bc946ce1f1
Check if paused to avoid displaing two MessageBoxes on the screen if we manage to stack up SnakeDied events
Stefano Rivera <stefano@rivera.za.net>
parents:
285
diff
changeset
|
47 was_paused, self._paused = self._paused, False |
71bc946ce1f1
Check if paused to avoid displaing two MessageBoxes on the screen if we manage to stack up SnakeDied events
Stefano Rivera <stefano@rivera.za.net>
parents:
285
diff
changeset
|
48 return was_paused == True |
191
454f7552e863
Pause the world, I may want to get off
Neil Muller <drnlmuller@gmail.com>
parents:
188
diff
changeset
|
49 |
389
48e39d1ad5ae
Add pause button (mostly for creating screenshots).
Simon Cross <hodgestar@gmail.com>
parents:
317
diff
changeset
|
50 def toggle_pause(self): |
48e39d1ad5ae
Add pause button (mostly for creating screenshots).
Simon Cross <hodgestar@gmail.com>
parents:
317
diff
changeset
|
51 if self._paused: |
48e39d1ad5ae
Add pause button (mostly for creating screenshots).
Simon Cross <hodgestar@gmail.com>
parents:
317
diff
changeset
|
52 self.resume() |
48e39d1ad5ae
Add pause button (mostly for creating screenshots).
Simon Cross <hodgestar@gmail.com>
parents:
317
diff
changeset
|
53 else: |
48e39d1ad5ae
Add pause button (mostly for creating screenshots).
Simon Cross <hodgestar@gmail.com>
parents:
317
diff
changeset
|
54 self.pause() |
48e39d1ad5ae
Add pause button (mostly for creating screenshots).
Simon Cross <hodgestar@gmail.com>
parents:
317
diff
changeset
|
55 |
186 | 56 def restart(self): |
317
e89702425a73
Add support for playing music to levels
Neil Muller <drnlmuller@gmail.com>
parents:
286
diff
changeset
|
57 stop_sound() |
186 | 58 self.level.restart() |
317
e89702425a73
Add support for playing music to levels
Neil Muller <drnlmuller@gmail.com>
parents:
286
diff
changeset
|
59 play_music(self.level.background_track) |
186 | 60 tile_pos, orientation = self.level.get_entry() |
61 self.snake = Snake(tile_pos, orientation) | |
62 self._prev = None | |
191
454f7552e863
Pause the world, I may want to get off
Neil Muller <drnlmuller@gmail.com>
parents:
188
diff
changeset
|
63 self._paused = False |