annotate mamba/widgets/game.py @ 574:e6344f57886e

Try add colours to the irker messages.
author Simon Cross <hodgestar@gmail.com>
date Thu, 22 Nov 2012 00:57:15 +0200
parents 6c1d44c12454
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
119
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
1 """Display the game area."""
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
2
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
3 from pygame.rect import Rect
506
6c1d44c12454 Make space and pause also pause.
Simon Cross <hodgestar@gmail.com>
parents: 416
diff changeset
4 from pygame.locals import (KEYDOWN, K_LEFT, K_RIGHT, K_DOWN, K_UP, K_p,
6c1d44c12454 Make space and pause also pause.
Simon Cross <hodgestar@gmail.com>
parents: 416
diff changeset
5 K_SPACE, K_PAUSE)
119
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
6
199
986e72d2cb4d Rejiggered entrances and shifted directions around.
Jeremy Thurgood <firxen@gmail.com>
parents: 192
diff changeset
7 from mamba.constants import UP, DOWN, LEFT, RIGHT
119
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
8 from mamba.widgets.base import Widget
416
30ce046d08c3 Fix clicking in dead dialog bug
Stefano Rivera <stefano@rivera.za.net>
parents: 389
diff changeset
9 from mamba.engine import FlipArrowsEvent
119
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
10
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
11
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
12 class GameWidget(Widget):
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
13 def __init__(self, world, offset=(0, 0)):
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
14 self.world = world
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
15 self.actions = self.create_action_map()
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
16 rect = Rect(offset, world.get_size())
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
17 super(GameWidget, self).__init__(rect)
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
18 self.focussable = True
210
b92f705bd8ea Modal dialogs, and focus stealing bugfixes
Stefano Rivera <stefano@rivera.za.net>
parents: 199
diff changeset
19 self.add_callback(KEYDOWN, self.action_callback)
248
de3b4048e54a Hook up tile arrow rotation
Stefano Rivera <stefano@rivera.za.net>
parents: 222
diff changeset
20 self.add_callback(FlipArrowsEvent, self.flip_arrows)
119
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
21
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
22 def create_action_map(self):
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
23 actions = {}
506
6c1d44c12454 Make space and pause also pause.
Simon Cross <hodgestar@gmail.com>
parents: 416
diff changeset
24 pause = (self.world.toggle_pause, ())
307
09ec51b9d385 Queue movement commands and handle them sanely. (FSVO)
Jeremy Thurgood <firxen@gmail.com>
parents: 304
diff changeset
25 actions[K_LEFT] = (self.world.snake.send_new_direction, (LEFT,))
09ec51b9d385 Queue movement commands and handle them sanely. (FSVO)
Jeremy Thurgood <firxen@gmail.com>
parents: 304
diff changeset
26 actions[K_RIGHT] = (self.world.snake.send_new_direction, (RIGHT,))
09ec51b9d385 Queue movement commands and handle them sanely. (FSVO)
Jeremy Thurgood <firxen@gmail.com>
parents: 304
diff changeset
27 actions[K_DOWN] = (self.world.snake.send_new_direction, (DOWN,))
09ec51b9d385 Queue movement commands and handle them sanely. (FSVO)
Jeremy Thurgood <firxen@gmail.com>
parents: 304
diff changeset
28 actions[K_UP] = (self.world.snake.send_new_direction, (UP,))
506
6c1d44c12454 Make space and pause also pause.
Simon Cross <hodgestar@gmail.com>
parents: 416
diff changeset
29 actions[K_p] = pause
6c1d44c12454 Make space and pause also pause.
Simon Cross <hodgestar@gmail.com>
parents: 416
diff changeset
30 actions[K_SPACE] = pause
6c1d44c12454 Make space and pause also pause.
Simon Cross <hodgestar@gmail.com>
parents: 416
diff changeset
31 actions[K_PAUSE] = pause
119
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
32 return actions
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
33
210
b92f705bd8ea Modal dialogs, and focus stealing bugfixes
Stefano Rivera <stefano@rivera.za.net>
parents: 199
diff changeset
34 def action_callback(self, ev, widget):
b92f705bd8ea Modal dialogs, and focus stealing bugfixes
Stefano Rivera <stefano@rivera.za.net>
parents: 199
diff changeset
35 if ev.key in self.actions:
119
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
36 func, args = self.actions[ev.key]
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
37 func(*args)
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
38 return True
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
39
248
de3b4048e54a Hook up tile arrow rotation
Stefano Rivera <stefano@rivera.za.net>
parents: 222
diff changeset
40 def flip_arrows(self, ev, widget):
de3b4048e54a Hook up tile arrow rotation
Stefano Rivera <stefano@rivera.za.net>
parents: 222
diff changeset
41 self.world.level.flip_arrows()
de3b4048e54a Hook up tile arrow rotation
Stefano Rivera <stefano@rivera.za.net>
parents: 222
diff changeset
42
119
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
43 def draw(self, surface):
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
44 self.world.update()
119c0fb758c2 Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
45 self.world.draw(surface)
186
275f0be6946c Reincarnating Snakes
Neil Muller <drnlmuller@gmail.com>
parents: 119
diff changeset
46
192
926710da8854 Tell the user that he's died
Neil Muller <drnlmuller@gmail.com>
parents: 186
diff changeset
47 def restart(self):
186
275f0be6946c Reincarnating Snakes
Neil Muller <drnlmuller@gmail.com>
parents: 119
diff changeset
48 self.world.restart()
275f0be6946c Reincarnating Snakes
Neil Muller <drnlmuller@gmail.com>
parents: 119
diff changeset
49 self.actions = self.create_action_map()
192
926710da8854 Tell the user that he's died
Neil Muller <drnlmuller@gmail.com>
parents: 186
diff changeset
50 self.grab_focus()