Mercurial > mamba
annotate mamba/snake.py @ 119:119c0fb758c2
Move key handling into GameWidget and GameWidget into widgets.game.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Sun, 11 Sep 2011 20:56:13 +0200 |
parents | 1b9b17eb896b |
children | 45dd79e9ba1b |
rev | line source |
---|---|
78 | 1 """The player snake object.""" |
2 | |
110 | 3 from pygame.sprite import Group |
4 | |
79
f602a8c3d173
Fix bugs. Stubbed out get_entry for now.
Simon Cross <hodgestar@gmail.com>
parents:
78
diff
changeset
|
5 from mamba.sprites import BaseSprite |
98 | 6 from mamba import mutators |
78 | 7 |
8 | |
110 | 9 class Snake(object): |
78 | 10 |
110 | 11 UP, DOWN, LEFT, RIGHT = [(0, -1), (0, 1), (-1, 0), (1, 0)] |
78 | 12 |
13 def __init__(self, tile_pos, orientation): | |
110 | 14 self.segments = self.create_segments(tile_pos, orientation) |
15 self.segment_group = Group() | |
16 self.segment_group.add(*self.segments) | |
78 | 17 self.set_orientation(orientation) |
18 | |
110 | 19 head = property(fget=lambda self: self.segments[0]) |
20 tail = property(fget=lambda self: self.segments[-1]) | |
21 | |
22 def create_segments(self, tile_pos, orientation): | |
23 x, y = tile_pos | |
24 dx, dy = orientation | |
25 return [Head((x, y)), | |
26 Body((x + dx, y + dy)), | |
27 Tail((x + 2 * dx, y + 2 * dy)), | |
28 ] | |
29 | |
30 def draw(self, surface): | |
111 | 31 self.segment_group.draw(surface) |
98 | 32 |
119
119c0fb758c2
Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
118
diff
changeset
|
33 def update(self): |
119c0fb758c2
Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
118
diff
changeset
|
34 pass |
119c0fb758c2
Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
118
diff
changeset
|
35 |
78 | 36 def set_orientation(self, orientation): |
119
119c0fb758c2
Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
118
diff
changeset
|
37 self.orientation = orientation |
110 | 38 #self.orientation = orientation |
39 #for segment in self.segments: | |
40 # segment.set_orientation(self) | |
41 print orientation | |
98 | 42 |
43 | |
110 | 44 class Segment(BaseSprite): |
45 | |
46 def __init__(self, image_name, tile_pos): | |
47 super(Segment, self).__init__() | |
48 image_name = "/".join(["snake", image_name]) | |
49 self._images = {} | |
50 for orientation, muts in [ | |
51 (Snake.RIGHT, (mutators.RIGHT,)), | |
52 (Snake.LEFT, (mutators.LEFT,)), | |
53 (Snake.UP, (mutators.UP,)), | |
54 (Snake.DOWN, (mutators.DOWN,)), | |
55 ]: | |
56 self._images[orientation] = self.load_image(image_name, muts) | |
118
1b9b17eb896b
Change default snake orientation to UP for now.
Simon Cross <hodgestar@gmail.com>
parents:
111
diff
changeset
|
57 self.set_orientation(Snake.UP) |
110 | 58 self.set_tile_pos(tile_pos) |
59 | |
60 def set_orientation(self, orientation): | |
61 self.image = self._images[orientation] | |
62 | |
63 | |
64 class Head(Segment): | |
65 def __init__(self, tile_pos): | |
66 super(Head, self).__init__("snake-head-mouth-open-r", tile_pos) | |
67 | |
68 | |
69 class Body(Segment): | |
70 def __init__(self, tile_pos): | |
71 super(Body, self).__init__("snake-body-r", tile_pos) | |
72 | |
73 | |
74 class Tail(Segment): | |
75 def __init__(self, tile_pos): | |
76 super(Tail, self).__init__("snake-tail-r", tile_pos) |