Mercurial > mamba
annotate mamba/snake.py @ 199:986e72d2cb4d
Rejiggered entrances and shifted directions around.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Wed, 14 Sep 2011 19:48:20 +0200 |
parents | 2876658d0b98 |
children | 78f9cafb0819 |
rev | line source |
---|---|
78 | 1 """The player snake object.""" |
2 | |
188
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
3 from pygame.sprite import Group, spritecollide |
110 | 4 |
199
986e72d2cb4d
Rejiggered entrances and shifted directions around.
Jeremy Thurgood <firxen@gmail.com>
parents:
198
diff
changeset
|
5 from mamba.constants import TILE_SIZE, UP, DOWN, LEFT, RIGHT |
177
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
6 from mamba.sprites import BaseSprite |
186 | 7 from mamba.engine import SnakeDiedEvent |
98 | 8 from mamba import mutators |
78 | 9 |
10 | |
110 | 11 class Snake(object): |
78 | 12 |
13 def __init__(self, tile_pos, orientation): | |
110 | 14 self.segments = self.create_segments(tile_pos, orientation) |
15 self.segment_group = Group() | |
178 | 16 self.segment_group.add(*reversed(self.segments)) |
78 | 17 self.set_orientation(orientation) |
178 | 18 self.speed = 120.0 # pixel / s |
177
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
19 self.frac_ds = 0.0 |
181
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
178
diff
changeset
|
20 self.mutation = None |
78 | 21 |
110 | 22 head = property(fget=lambda self: self.segments[0]) |
23 tail = property(fget=lambda self: self.segments[-1]) | |
24 | |
25 def create_segments(self, tile_pos, orientation): | |
177
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
26 tx, ty = tile_pos |
110 | 27 dx, dy = orientation |
178 | 28 segments = [] |
29 for cls in [Head] + [Body] * 10 + [Tail]: | |
30 segments.append(cls((tx, ty))) | |
31 return segments | |
110 | 32 |
33 def draw(self, surface): | |
177
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
34 self.segment_group.draw(surface) |
98 | 35 |
181
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
178
diff
changeset
|
36 def update(self, dt, world): |
177
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
37 ds = dt * self.speed + self.frac_ds |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
38 ds, self.frac_ds = divmod(ds, 1) |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
39 ds = int(ds) |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
40 while True: |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
41 tile_state = self.head.get_tile_state() |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
42 shifted, ds = self.head.shift_head(ds) |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
43 if not shifted: |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
44 break |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
45 self.head.set_orientation(self.orientation) |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
46 for segment in self.segments[1:]: |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
47 old_tile_state = segment.get_tile_state() |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
48 segment.shift_tile(tile_state) |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
49 tile_state = old_tile_state |
119
119c0fb758c2
Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
118
diff
changeset
|
50 |
177
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
51 for segment in self.segments: |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
52 segment.shift_pixels(ds) |
188
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
53 world.interact(segment) |
145
20b2cedf0f1d
Snake movement now turn
Gideon Visser <gideon@gideonvisser.com>
parents:
141
diff
changeset
|
54 |
78 | 55 def set_orientation(self, orientation): |
198
2876658d0b98
Don't allow the snake to reverse onto itself immediately.
Simon Cross <hodgestar@gmail.com>
parents:
195
diff
changeset
|
56 if len(self.segments) > 1: |
2876658d0b98
Don't allow the snake to reverse onto itself immediately.
Simon Cross <hodgestar@gmail.com>
parents:
195
diff
changeset
|
57 neck_x, neck_y = self.segments[1].tile_pos |
2876658d0b98
Don't allow the snake to reverse onto itself immediately.
Simon Cross <hodgestar@gmail.com>
parents:
195
diff
changeset
|
58 head_x, head_y = self.head.tile_pos |
2876658d0b98
Don't allow the snake to reverse onto itself immediately.
Simon Cross <hodgestar@gmail.com>
parents:
195
diff
changeset
|
59 if (head_x + orientation[0] == neck_x and |
2876658d0b98
Don't allow the snake to reverse onto itself immediately.
Simon Cross <hodgestar@gmail.com>
parents:
195
diff
changeset
|
60 head_y + orientation[1] == neck_y): |
2876658d0b98
Don't allow the snake to reverse onto itself immediately.
Simon Cross <hodgestar@gmail.com>
parents:
195
diff
changeset
|
61 # Don't allow the snake to go back on itself |
2876658d0b98
Don't allow the snake to reverse onto itself immediately.
Simon Cross <hodgestar@gmail.com>
parents:
195
diff
changeset
|
62 # immediately. More creative self-inflicted |
2876658d0b98
Don't allow the snake to reverse onto itself immediately.
Simon Cross <hodgestar@gmail.com>
parents:
195
diff
changeset
|
63 # deaths are allowed though. |
2876658d0b98
Don't allow the snake to reverse onto itself immediately.
Simon Cross <hodgestar@gmail.com>
parents:
195
diff
changeset
|
64 return |
119
119c0fb758c2
Move key handling into GameWidget and GameWidget into widgets.game.
Simon Cross <hodgestar@gmail.com>
parents:
118
diff
changeset
|
65 self.orientation = orientation |
98 | 66 |
181
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
178
diff
changeset
|
67 def crash(self): |
186 | 68 SnakeDiedEvent.post() |
181
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
178
diff
changeset
|
69 |
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
178
diff
changeset
|
70 def mutate(self, mutation): |
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
178
diff
changeset
|
71 self.mutation = mutation |
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
178
diff
changeset
|
72 |
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
178
diff
changeset
|
73 def can_swim(self): |
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
178
diff
changeset
|
74 return self.mutation == 'amphibious' |
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
178
diff
changeset
|
75 |
98 | 76 |
110 | 77 class Segment(BaseSprite): |
78 | |
195
d488731886dc
Wean colour overlays off image colour patches.
Simon Cross <hodgestar@gmail.com>
parents:
188
diff
changeset
|
79 GREEN = mutators.SNAKE_GREEN |
125
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
80 BLUE = mutators.BLUE |
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
81 RED = mutators.RED |
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
82 YELLOW = mutators.YELLOW |
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
83 |
131 | 84 _detail_mutators = () |
85 | |
182
0a955d2536f0
Painted Jezebel! (Assuming you name your snake Jezebel.)
Jeremy Thurgood <firxen@gmail.com>
parents:
181
diff
changeset
|
86 is_head = False |
0a955d2536f0
Painted Jezebel! (Assuming you name your snake Jezebel.)
Jeremy Thurgood <firxen@gmail.com>
parents:
181
diff
changeset
|
87 |
110 | 88 def __init__(self, image_name, tile_pos): |
89 super(Segment, self).__init__() | |
153
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
90 self.set_base_image(image_name) |
131 | 91 self._colour_overlay = self.GREEN |
199
986e72d2cb4d
Rejiggered entrances and shifted directions around.
Jeremy Thurgood <firxen@gmail.com>
parents:
198
diff
changeset
|
92 self.orientation = UP |
125
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
93 |
131 | 94 self.make_images() |
125
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
95 self.update_image() |
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
96 self.set_tile_pos(tile_pos) |
188
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
97 self.on_tiles = [] |
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
98 |
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
99 def filter_collisions(self, group): |
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
100 collide = [] |
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
101 tiles = spritecollide(self, group, False) |
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
102 for tile in tiles: |
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
103 if tile not in self.on_tiles: |
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
104 collide.append(tile) |
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
105 self.on_tiles = tiles |
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
106 return collide |
125
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
107 |
153
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
108 def set_base_image(self, image_name): |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
109 self._base_image = "/".join(["snake", image_name]) |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
110 |
131 | 111 def make_images(self): |
110 | 112 self._images = {} |
113 for orientation, muts in [ | |
199
986e72d2cb4d
Rejiggered entrances and shifted directions around.
Jeremy Thurgood <firxen@gmail.com>
parents:
198
diff
changeset
|
114 (RIGHT, (mutators.RIGHT,)), |
986e72d2cb4d
Rejiggered entrances and shifted directions around.
Jeremy Thurgood <firxen@gmail.com>
parents:
198
diff
changeset
|
115 (LEFT, (mutators.LEFT,)), |
986e72d2cb4d
Rejiggered entrances and shifted directions around.
Jeremy Thurgood <firxen@gmail.com>
parents:
198
diff
changeset
|
116 (UP, (mutators.UP,)), |
986e72d2cb4d
Rejiggered entrances and shifted directions around.
Jeremy Thurgood <firxen@gmail.com>
parents:
198
diff
changeset
|
117 (DOWN, (mutators.DOWN,)), |
110 | 118 ]: |
131 | 119 all_muts = (self._colour_overlay,) + self._detail_mutators + muts |
141 | 120 self._images[orientation] = self.load_image(self._base_image, |
121 all_muts) | |
125
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
122 |
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
123 def update_image(self): |
177
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
124 self.image = self._images[self.orientation] |
110 | 125 |
126 def set_orientation(self, orientation): | |
177
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
127 self.orientation = orientation |
125
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
128 self.update_image() |
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
129 |
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
130 def set_colour(self, colour_overlay): |
131 | 131 self._colour_overlay = colour_overlay |
132 self.make_images() | |
125
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
133 self.update_image() |
110 | 134 |
177
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
135 def get_tile_state(self): |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
136 return self.tile_pos, self.orientation |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
137 |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
138 def shift_tile(self, tile_state): |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
139 """Shift this segment to the tile the other one was on. |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
140 |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
141 Also reset the position to be the center of the tile. |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
142 """ |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
143 tile_pos, orientation = tile_state |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
144 self.set_tile_pos(tile_pos) |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
145 self.orientation = orientation |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
146 |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
147 def shift_pixels(self, distance): |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
148 """Shift this segment a number of pixels.""" |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
149 dx, dy = self.orientation |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
150 dx, dy = distance * dx, distance * dy |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
151 self.rect = self.rect.move(dx, dy) |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
152 |
110 | 153 |
154 class Head(Segment): | |
153
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
155 CLOSED = "snake-head" |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
156 OPEN = "snake-head-mouth-open-r" |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
157 EYE = mutators.Overlay("tiles/common/snake/snake-head-eye-r.png") |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
158 TONGUE = mutators.Overlay("tiles/common/snake/snake-head-tongue-r.png") |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
159 |
182
0a955d2536f0
Painted Jezebel! (Assuming you name your snake Jezebel.)
Jeremy Thurgood <firxen@gmail.com>
parents:
181
diff
changeset
|
160 is_head = True |
0a955d2536f0
Painted Jezebel! (Assuming you name your snake Jezebel.)
Jeremy Thurgood <firxen@gmail.com>
parents:
181
diff
changeset
|
161 |
110 | 162 def __init__(self, tile_pos): |
153
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
163 self._detail_mutators = (self.EYE,) |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
164 super(Head, self).__init__(self.CLOSED, tile_pos) |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
165 |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
166 def mouth_open(self): |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
167 self.set_base_image(self.OPEN) |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
168 self.make_images() |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
169 self.update_image() |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
170 |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
171 def mouth_closed(self): |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
172 self.set_base_image(self.CLOSED) |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
173 self.make_images() |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
174 self.update_image() |
110 | 175 |
131 | 176 def tongue_out(self): |
153
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
177 self._detail_mutators = (self.EYE, self.TONGUE) |
131 | 178 self.make_images() |
179 self.update_image() | |
180 | |
181 def tongue_in(self): | |
153
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
182 self._detail_mutators = (self.EYE,) |
131 | 183 self.make_images() |
184 self.update_image() | |
185 | |
177
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
186 def shift_head(self, ds): |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
187 """Shift the head a number of pixels in the direction of it |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
188 orientation. |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
189 """ |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
190 dx, dy = self.orientation |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
191 TX, TY = TILE_SIZE |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
192 rx, ry = self.rect.left, self.rect.top |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
193 tx, ty = self.tile_pos |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
194 # WARNING: Tri-state logic ahead |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
195 # (Tri-state logic is the mamba's natural habitat) |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
196 if dx != 0: |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
197 newdx = rx + ds * dx |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
198 newtx = newdx / TX |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
199 if newtx > tx: |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
200 self.set_tile_pos((tx + 1, ty)) |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
201 ds = newdx - self.rect.left |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
202 return True, ds |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
203 elif newtx < tx - 1: |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
204 self.set_tile_pos((tx - 1, ty)) |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
205 ds = self.rect.left - newdx |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
206 return True, ds |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
207 else: |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
208 newdy = ry + ds * dy |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
209 newty = newdy / TY |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
210 if newty > ty: |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
211 self.set_tile_pos((tx, ty + 1)) |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
212 ds = newdy - self.rect.top |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
213 return True, ds |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
214 elif newty < ty - 1: |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
215 self.set_tile_pos((tx, ty - 1)) |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
216 ds = self.rect.top - newdy |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
217 return True, ds |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
218 return False, ds |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
219 |
110 | 220 |
221 class Body(Segment): | |
222 def __init__(self, tile_pos): | |
153
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
223 super(Body, self).__init__("snake-body", tile_pos) |
110 | 224 |
225 | |
226 class Tail(Segment): | |
227 def __init__(self, tile_pos): | |
153
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
228 super(Tail, self).__init__("snake-tail", tile_pos) |