Mercurial > mamba
annotate mamba/world.py @ 267:1e8dca95c48a
Hook up hot-keys in editor for jerith
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Thu, 15 Sep 2011 12:54:16 +0200 |
parents | f9cc701a6be0 |
children | cacc03748580 |
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 |
6 | |
7 | |
8 class World(object): | |
9 def __init__(self, level): | |
10 self.level = level | |
82
136d96b1aa75
Hook up level.get_entry to world.
Simon Cross <hodgestar@gmail.com>
parents:
79
diff
changeset
|
11 tile_pos, orientation = level.get_entry() |
78 | 12 self.snake = Snake(tile_pos, orientation) |
122
45dd79e9ba1b
Rudimentary FPS and clock ticking.
Simon Cross <hodgestar@gmail.com>
parents:
119
diff
changeset
|
13 self._prev = None |
191
454f7552e863
Pause the world, I may want to get off
Neil Muller <drnlmuller@gmail.com>
parents:
188
diff
changeset
|
14 self._paused = False |
110 | 15 |
111 | 16 def get_size(self): |
17 return self.level.get_size() | |
18 | |
110 | 19 def draw(self, surface): |
111 | 20 self.level.draw(surface) |
110 | 21 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
|
22 |
243 | 23 def get_sprite(self, sprite_id): |
24 return self.level.extra_sprites[sprite_id] | |
25 | |
119
119c0fb758c2
Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
111
diff
changeset
|
26 def update(self): |
122
45dd79e9ba1b
Rudimentary FPS and clock ticking.
Simon Cross <hodgestar@gmail.com>
parents:
119
diff
changeset
|
27 prev = self._prev |
45dd79e9ba1b
Rudimentary FPS and clock ticking.
Simon Cross <hodgestar@gmail.com>
parents:
119
diff
changeset
|
28 self._prev = time.time() |
191
454f7552e863
Pause the world, I may want to get off
Neil Muller <drnlmuller@gmail.com>
parents:
188
diff
changeset
|
29 if self._paused: |
454f7552e863
Pause the world, I may want to get off
Neil Muller <drnlmuller@gmail.com>
parents:
188
diff
changeset
|
30 return |
122
45dd79e9ba1b
Rudimentary FPS and clock ticking.
Simon Cross <hodgestar@gmail.com>
parents:
119
diff
changeset
|
31 if prev is None: |
45dd79e9ba1b
Rudimentary FPS and clock ticking.
Simon Cross <hodgestar@gmail.com>
parents:
119
diff
changeset
|
32 return |
45dd79e9ba1b
Rudimentary FPS and clock ticking.
Simon Cross <hodgestar@gmail.com>
parents:
119
diff
changeset
|
33 dt = self._prev - prev |
45dd79e9ba1b
Rudimentary FPS and clock ticking.
Simon Cross <hodgestar@gmail.com>
parents:
119
diff
changeset
|
34 |
181
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
35 self.snake.update(dt, self) |
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
36 |
182
0a955d2536f0
Painted Jezebel! (Assuming you name your snake Jezebel.)
Jeremy Thurgood <firxen@gmail.com>
parents:
181
diff
changeset
|
37 def interact(self, segment): |
188
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
186
diff
changeset
|
38 tiles = segment.filter_collisions(self.level.sprites) |
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
186
diff
changeset
|
39 for tile in tiles: |
243 | 40 tile.interact(self, segment) |
186 | 41 |
191
454f7552e863
Pause the world, I may want to get off
Neil Muller <drnlmuller@gmail.com>
parents:
188
diff
changeset
|
42 def pause(self): |
454f7552e863
Pause the world, I may want to get off
Neil Muller <drnlmuller@gmail.com>
parents:
188
diff
changeset
|
43 self._paused = True |
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): |
454f7552e863
Pause the world, I may want to get off
Neil Muller <drnlmuller@gmail.com>
parents:
188
diff
changeset
|
46 self._paused = False |
454f7552e863
Pause the world, I may want to get off
Neil Muller <drnlmuller@gmail.com>
parents:
188
diff
changeset
|
47 |
186 | 48 def restart(self): |
49 self.level.restart() | |
50 tile_pos, orientation = self.level.get_entry() | |
51 self.snake = Snake(tile_pos, orientation) | |
52 self._prev = None | |
191
454f7552e863
Pause the world, I may want to get off
Neil Muller <drnlmuller@gmail.com>
parents:
188
diff
changeset
|
53 self._paused = False |