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