Mercurial > boomslang
comparison pyntnclick/widgets/game.py @ 555:c0474fe18b96 pyntnclick
Copy in widgets from mamba (currently unused)
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Sat, 11 Feb 2012 14:09:46 +0200 |
parents | |
children | f9f04cb35697 |
comparison
equal
deleted
inserted
replaced
554:99a1420097df | 555:c0474fe18b96 |
---|---|
1 """Display the game area.""" | |
2 | |
3 from pygame.rect import Rect | |
4 from pygame.locals import (KEYDOWN, K_LEFT, K_RIGHT, K_DOWN, K_UP, K_p, | |
5 K_SPACE, K_PAUSE) | |
6 | |
7 from mamba.constants import UP, DOWN, LEFT, RIGHT | |
8 from mamba.widgets.base import Widget | |
9 from mamba.engine import FlipArrowsEvent | |
10 | |
11 | |
12 class GameWidget(Widget): | |
13 def __init__(self, world, offset=(0, 0)): | |
14 self.world = world | |
15 self.actions = self.create_action_map() | |
16 rect = Rect(offset, world.get_size()) | |
17 super(GameWidget, self).__init__(rect) | |
18 self.focussable = True | |
19 self.add_callback(KEYDOWN, self.action_callback) | |
20 self.add_callback(FlipArrowsEvent, self.flip_arrows) | |
21 | |
22 def create_action_map(self): | |
23 actions = {} | |
24 pause = (self.world.toggle_pause, ()) | |
25 actions[K_LEFT] = (self.world.snake.send_new_direction, (LEFT,)) | |
26 actions[K_RIGHT] = (self.world.snake.send_new_direction, (RIGHT,)) | |
27 actions[K_DOWN] = (self.world.snake.send_new_direction, (DOWN,)) | |
28 actions[K_UP] = (self.world.snake.send_new_direction, (UP,)) | |
29 actions[K_p] = pause | |
30 actions[K_SPACE] = pause | |
31 actions[K_PAUSE] = pause | |
32 return actions | |
33 | |
34 def action_callback(self, ev, widget): | |
35 if ev.key in self.actions: | |
36 func, args = self.actions[ev.key] | |
37 func(*args) | |
38 return True | |
39 | |
40 def flip_arrows(self, ev, widget): | |
41 self.world.level.flip_arrows() | |
42 | |
43 def draw(self, surface): | |
44 self.world.update() | |
45 self.world.draw(surface) | |
46 | |
47 def restart(self): | |
48 self.world.restart() | |
49 self.actions = self.create_action_map() | |
50 self.grab_focus() |