Mercurial > mamba
annotate mamba/snake.py @ 225:af95fbc8ef5c
Snakes should be impermeable. So now they are.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Wed, 14 Sep 2011 23:02:45 +0200 |
parents | c3018764942a |
children | ec23ecbbd1c6 |
rev | line source |
---|---|
78 | 1 """The player snake object.""" |
2 | |
221 | 3 import random |
4 | |
188
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
5 from pygame.sprite import Group, spritecollide |
110 | 6 |
199
986e72d2cb4d
Rejiggered entrances and shifted directions around.
Jeremy Thurgood <firxen@gmail.com>
parents:
198
diff
changeset
|
7 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
|
8 from mamba.sprites import BaseSprite |
213 | 9 from mamba.engine import SnakeDiedEvent, LevelCompletedEvent |
98 | 10 from mamba import mutators |
78 | 11 |
12 | |
217
874eb26aad10
No more mile-long snake to start with.
Jeremy Thurgood <firxen@gmail.com>
parents:
213
diff
changeset
|
13 INITIAL_SEGMENT_COUNT = 4 |
874eb26aad10
No more mile-long snake to start with.
Jeremy Thurgood <firxen@gmail.com>
parents:
213
diff
changeset
|
14 |
874eb26aad10
No more mile-long snake to start with.
Jeremy Thurgood <firxen@gmail.com>
parents:
213
diff
changeset
|
15 |
110 | 16 class Snake(object): |
78 | 17 |
18 def __init__(self, tile_pos, orientation): | |
110 | 19 self.segments = self.create_segments(tile_pos, orientation) |
201
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
20 self.pending_segments = [] # segments waiting to be added |
110 | 21 self.segment_group = Group() |
178 | 22 self.segment_group.add(*reversed(self.segments)) |
78 | 23 self.set_orientation(orientation) |
178 | 24 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
|
25 self.frac_ds = 0.0 |
181
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
178
diff
changeset
|
26 self.mutation = None |
225
af95fbc8ef5c
Snakes should be impermeable. So now they are.
Jeremy Thurgood <firxen@gmail.com>
parents:
223
diff
changeset
|
27 self.coiled = True |
78 | 28 |
110 | 29 head = property(fget=lambda self: self.segments[0]) |
30 tail = property(fget=lambda self: self.segments[-1]) | |
31 | |
32 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
|
33 tx, ty = tile_pos |
110 | 34 dx, dy = orientation |
178 | 35 segments = [] |
217
874eb26aad10
No more mile-long snake to start with.
Jeremy Thurgood <firxen@gmail.com>
parents:
213
diff
changeset
|
36 for cls in [Head] + [Body] * INITIAL_SEGMENT_COUNT + [Tail]: |
178 | 37 segments.append(cls((tx, ty))) |
38 return segments | |
110 | 39 |
201
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
40 def add_segment(self, segment=None): |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
41 if segment is None: |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
42 segment = Body((0, 0)) |
205
2b5956bedf64
Set colour of default new segment to colour of snake head.
Simon Cross <hodgestar@gmail.com>
parents:
203
diff
changeset
|
43 segment.set_colour(self.head.colour) |
201
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
44 self.pending_segments.append(segment) |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
45 |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
46 def remove_segment(self, segment=None): |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
47 if segment is None: |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
48 if len(self.segments) < 3: |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
49 return |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
50 segment = self.segments[-2] |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
51 try: |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
52 idx = self.segments.index(segment) |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
53 except IndexError: |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
54 return |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
55 self.shiftup_segments(idx, segment.get_tile_state()) |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
56 del self.segments[idx] |
202
498c1d659a94
Remove a segment when driving over the small rat.
Simon Cross <hodgestar@gmail.com>
parents:
201
diff
changeset
|
57 segment.kill() |
201
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
58 |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
59 def shiftup_segments(self, idx, state): |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
60 for segment in self.segments[idx:]: |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
61 next_state = segment.get_tile_state() |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
62 segment.shift_tile(state) |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
63 state = next_state |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
64 |
110 | 65 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
|
66 self.segment_group.draw(surface) |
98 | 67 |
181
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
178
diff
changeset
|
68 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
|
69 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
|
70 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
|
71 ds = int(ds) |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
72 while True: |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
73 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
|
74 shifted, ds = self.head.shift_head(ds) |
218
10804ae58066
Open mouth when eating things
Stefano Rivera <stefano@rivera.za.net>
parents:
217
diff
changeset
|
75 if shifted: |
225
af95fbc8ef5c
Snakes should be impermeable. So now they are.
Jeremy Thurgood <firxen@gmail.com>
parents:
223
diff
changeset
|
76 self.coiled = False |
221 | 77 self.head.shifted_tile() |
218
10804ae58066
Open mouth when eating things
Stefano Rivera <stefano@rivera.za.net>
parents:
217
diff
changeset
|
78 else: |
177
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
79 break |
203
8009742103e7
Add a segment when driving over the big rat.
Simon Cross <hodgestar@gmail.com>
parents:
202
diff
changeset
|
80 if self.pending_segments: |
8009742103e7
Add a segment when driving over the big rat.
Simon Cross <hodgestar@gmail.com>
parents:
202
diff
changeset
|
81 new_segment = self.pending_segments.pop(0) |
8009742103e7
Add a segment when driving over the big rat.
Simon Cross <hodgestar@gmail.com>
parents:
202
diff
changeset
|
82 self.segments.insert(1, new_segment) |
8009742103e7
Add a segment when driving over the big rat.
Simon Cross <hodgestar@gmail.com>
parents:
202
diff
changeset
|
83 self.segment_group.add(new_segment) |
8009742103e7
Add a segment when driving over the big rat.
Simon Cross <hodgestar@gmail.com>
parents:
202
diff
changeset
|
84 new_segment.shift_tile(tile_state) |
206 | 85 for segment in self.segments[2:]: |
86 segment.shift_tile(segment.get_tile_state()) | |
203
8009742103e7
Add a segment when driving over the big rat.
Simon Cross <hodgestar@gmail.com>
parents:
202
diff
changeset
|
87 else: |
8009742103e7
Add a segment when driving over the big rat.
Simon Cross <hodgestar@gmail.com>
parents:
202
diff
changeset
|
88 self.shiftup_segments(1, tile_state) |
201
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
89 if not self.check_uturn(): |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
90 self.head.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
|
91 |
225
af95fbc8ef5c
Snakes should be impermeable. So now they are.
Jeremy Thurgood <firxen@gmail.com>
parents:
223
diff
changeset
|
92 self.check_self_crash() |
177
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
93 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
|
94 segment.shift_pixels(ds) |
188
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
95 world.interact(segment) |
145
20b2cedf0f1d
Snake movement now turn
Gideon Visser <gideon@gideonvisser.com>
parents:
141
diff
changeset
|
96 |
201
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
97 def check_uturn(self): |
198
2876658d0b98
Don't allow the snake to reverse onto itself immediately.
Simon Cross <hodgestar@gmail.com>
parents:
195
diff
changeset
|
98 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
|
99 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
|
100 head_x, head_y = self.head.tile_pos |
201
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
101 if (head_x + self.orientation[0] == neck_x and |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
102 head_y + self.orientation[1] == neck_y): |
198
2876658d0b98
Don't allow the snake to reverse onto itself immediately.
Simon Cross <hodgestar@gmail.com>
parents:
195
diff
changeset
|
103 # 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
|
104 # 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
|
105 # deaths are allowed though. |
201
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
106 return True |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
107 return False |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
108 |
78f9cafb0819
Fix uturns. Start of work on adding and removing segments.
Simon Cross <hodgestar@gmail.com>
parents:
199
diff
changeset
|
109 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
|
110 self.orientation = orientation |
98 | 111 |
225
af95fbc8ef5c
Snakes should be impermeable. So now they are.
Jeremy Thurgood <firxen@gmail.com>
parents:
223
diff
changeset
|
112 def check_self_crash(self): |
af95fbc8ef5c
Snakes should be impermeable. So now they are.
Jeremy Thurgood <firxen@gmail.com>
parents:
223
diff
changeset
|
113 if self.coiled: |
af95fbc8ef5c
Snakes should be impermeable. So now they are.
Jeremy Thurgood <firxen@gmail.com>
parents:
223
diff
changeset
|
114 return |
af95fbc8ef5c
Snakes should be impermeable. So now they are.
Jeremy Thurgood <firxen@gmail.com>
parents:
223
diff
changeset
|
115 collides = spritecollide(self.head, self.segment_group, False) |
af95fbc8ef5c
Snakes should be impermeable. So now they are.
Jeremy Thurgood <firxen@gmail.com>
parents:
223
diff
changeset
|
116 if [s for s in collides if s not in self.segments[:2]]: |
af95fbc8ef5c
Snakes should be impermeable. So now they are.
Jeremy Thurgood <firxen@gmail.com>
parents:
223
diff
changeset
|
117 self.crash() |
af95fbc8ef5c
Snakes should be impermeable. So now they are.
Jeremy Thurgood <firxen@gmail.com>
parents:
223
diff
changeset
|
118 |
181
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
178
diff
changeset
|
119 def crash(self): |
186 | 120 SnakeDiedEvent.post() |
181
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
178
diff
changeset
|
121 |
213 | 122 def exit_level(self): |
123 LevelCompletedEvent.post() | |
124 | |
181
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
178
diff
changeset
|
125 def mutate(self, mutation): |
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
178
diff
changeset
|
126 self.mutation = mutation |
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
178
diff
changeset
|
127 |
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
178
diff
changeset
|
128 def can_swim(self): |
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
178
diff
changeset
|
129 return self.mutation == 'amphibious' |
061d711ba570
Interact with the world.
Jeremy Thurgood <firxen@gmail.com>
parents:
178
diff
changeset
|
130 |
98 | 131 |
110 | 132 class Segment(BaseSprite): |
133 | |
195
d488731886dc
Wean colour overlays off image colour patches.
Simon Cross <hodgestar@gmail.com>
parents:
188
diff
changeset
|
134 GREEN = mutators.SNAKE_GREEN |
125
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
135 BLUE = mutators.BLUE |
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
136 RED = mutators.RED |
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
137 YELLOW = mutators.YELLOW |
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
138 |
131 | 139 _detail_mutators = () |
140 | |
182
0a955d2536f0
Painted Jezebel! (Assuming you name your snake Jezebel.)
Jeremy Thurgood <firxen@gmail.com>
parents:
181
diff
changeset
|
141 is_head = False |
0a955d2536f0
Painted Jezebel! (Assuming you name your snake Jezebel.)
Jeremy Thurgood <firxen@gmail.com>
parents:
181
diff
changeset
|
142 |
110 | 143 def __init__(self, image_name, tile_pos): |
144 super(Segment, self).__init__() | |
153
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
145 self.set_base_image(image_name) |
205
2b5956bedf64
Set colour of default new segment to colour of snake head.
Simon Cross <hodgestar@gmail.com>
parents:
203
diff
changeset
|
146 self.colour = self.GREEN |
199
986e72d2cb4d
Rejiggered entrances and shifted directions around.
Jeremy Thurgood <firxen@gmail.com>
parents:
198
diff
changeset
|
147 self.orientation = UP |
125
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
148 |
131 | 149 self.make_images() |
125
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
150 self.update_image() |
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
151 self.set_tile_pos(tile_pos) |
188
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
152 self.on_tiles = [] |
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
153 |
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
154 def filter_collisions(self, group): |
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
155 collide = [] |
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
156 tiles = spritecollide(self, group, False) |
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
157 for tile in tiles: |
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
158 if tile not in self.on_tiles: |
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
159 collide.append(tile) |
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
160 self.on_tiles = tiles |
d76c069164c0
Much better interaction detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
187
diff
changeset
|
161 return collide |
125
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
162 |
153
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
163 def set_base_image(self, image_name): |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
164 self._base_image = "/".join(["snake", image_name]) |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
165 |
131 | 166 def make_images(self): |
110 | 167 self._images = {} |
168 for orientation, muts in [ | |
199
986e72d2cb4d
Rejiggered entrances and shifted directions around.
Jeremy Thurgood <firxen@gmail.com>
parents:
198
diff
changeset
|
169 (RIGHT, (mutators.RIGHT,)), |
986e72d2cb4d
Rejiggered entrances and shifted directions around.
Jeremy Thurgood <firxen@gmail.com>
parents:
198
diff
changeset
|
170 (LEFT, (mutators.LEFT,)), |
986e72d2cb4d
Rejiggered entrances and shifted directions around.
Jeremy Thurgood <firxen@gmail.com>
parents:
198
diff
changeset
|
171 (UP, (mutators.UP,)), |
986e72d2cb4d
Rejiggered entrances and shifted directions around.
Jeremy Thurgood <firxen@gmail.com>
parents:
198
diff
changeset
|
172 (DOWN, (mutators.DOWN,)), |
110 | 173 ]: |
205
2b5956bedf64
Set colour of default new segment to colour of snake head.
Simon Cross <hodgestar@gmail.com>
parents:
203
diff
changeset
|
174 all_muts = (self.colour,) + self._detail_mutators + muts |
141 | 175 self._images[orientation] = self.load_image(self._base_image, |
176 all_muts) | |
125
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
177 |
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
178 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
|
179 self.image = self._images[self.orientation] |
110 | 180 |
181 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
|
182 self.orientation = orientation |
125
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
183 self.update_image() |
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
184 |
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
185 def set_colour(self, colour_overlay): |
205
2b5956bedf64
Set colour of default new segment to colour of snake head.
Simon Cross <hodgestar@gmail.com>
parents:
203
diff
changeset
|
186 self.colour = colour_overlay |
131 | 187 self.make_images() |
125
625b22f92efa
the snake is green
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
122
diff
changeset
|
188 self.update_image() |
110 | 189 |
177
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
190 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
|
191 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
|
192 |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
193 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
|
194 """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
|
195 |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
196 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
|
197 """ |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
198 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
|
199 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
|
200 self.orientation = orientation |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
201 |
221 | 202 def shifted_tile(self): |
203 pass | |
204 | |
177
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
205 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
|
206 """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
|
207 dx, dy = self.orientation |
e0573297b17c
Snake that moves (my god that was an epic struggle).
Simon Cross <hodgestar@gmail.com>
parents:
153
diff
changeset
|
208 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
|
209 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
|
210 |
223
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
211 def shift_head(self, ds): |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
212 """Shift the head a number of pixels in the direction of it |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
213 orientation. |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
214 """ |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
215 dx, dy = self.orientation |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
216 TX, TY = TILE_SIZE |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
217 rx, ry = self.rect.left, self.rect.top |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
218 tx, ty = self.tile_pos |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
219 # WARNING: Tri-state logic ahead |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
220 # (Tri-state logic is the mamba's natural habitat) |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
221 if dx != 0: |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
222 newdx = rx + ds * dx |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
223 newtx = newdx / TX |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
224 if newtx > tx: |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
225 self.set_tile_pos((tx + 1, ty)) |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
226 ds = newdx - self.rect.left |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
227 return True, ds |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
228 elif newtx < tx - 1: |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
229 self.set_tile_pos((tx - 1, ty)) |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
230 ds = self.rect.left - newdx |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
231 return True, ds |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
232 else: |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
233 newdy = ry + ds * dy |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
234 newty = newdy / TY |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
235 if newty > ty: |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
236 self.set_tile_pos((tx, ty + 1)) |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
237 ds = newdy - self.rect.top |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
238 return True, ds |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
239 elif newty < ty - 1: |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
240 self.set_tile_pos((tx, ty - 1)) |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
241 ds = self.rect.top - newdy |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
242 return True, ds |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
243 return False, ds |
c3018764942a
Move shift_head onto base segment so that head is less special (in preparation for snake disappearing down the exit).
Simon Cross <hodgestar@gmail.com>
parents:
221
diff
changeset
|
244 |
110 | 245 |
246 class Head(Segment): | |
153
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
247 CLOSED = "snake-head" |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
248 OPEN = "snake-head-mouth-open-r" |
212
e768056b0588
Refactor tile image loading stuff. More magic is good, right?
Jeremy Thurgood <firxen@gmail.com>
parents:
206
diff
changeset
|
249 EYE = mutators.Overlay("snake/snake-head-eye-r") |
e768056b0588
Refactor tile image loading stuff. More magic is good, right?
Jeremy Thurgood <firxen@gmail.com>
parents:
206
diff
changeset
|
250 TONGUE = mutators.Overlay("snake/snake-head-tongue-r") |
153
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
251 |
182
0a955d2536f0
Painted Jezebel! (Assuming you name your snake Jezebel.)
Jeremy Thurgood <firxen@gmail.com>
parents:
181
diff
changeset
|
252 is_head = True |
0a955d2536f0
Painted Jezebel! (Assuming you name your snake Jezebel.)
Jeremy Thurgood <firxen@gmail.com>
parents:
181
diff
changeset
|
253 |
110 | 254 def __init__(self, tile_pos): |
153
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
255 self._detail_mutators = (self.EYE,) |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
256 super(Head, self).__init__(self.CLOSED, tile_pos) |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
257 |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
258 def mouth_open(self): |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
259 self.set_base_image(self.OPEN) |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
260 self.make_images() |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
261 self.update_image() |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
262 |
218
10804ae58066
Open mouth when eating things
Stefano Rivera <stefano@rivera.za.net>
parents:
217
diff
changeset
|
263 def mouth_close(self): |
153
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
264 self.set_base_image(self.CLOSED) |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
265 self.make_images() |
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
266 self.update_image() |
110 | 267 |
131 | 268 def tongue_out(self): |
153
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
269 self._detail_mutators = (self.EYE, self.TONGUE) |
131 | 270 self.make_images() |
271 self.update_image() | |
272 | |
273 def tongue_in(self): | |
153
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
274 self._detail_mutators = (self.EYE,) |
131 | 275 self.make_images() |
276 self.update_image() | |
277 | |
221 | 278 def shifted_tile(self): |
279 if random.random() < 0.02: | |
280 self.mouth_open() | |
281 self.tongue_out() | |
282 else: | |
283 self.mouth_close() | |
284 self.tongue_in() | |
285 | |
110 | 286 |
287 class Body(Segment): | |
288 def __init__(self, tile_pos): | |
153
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
289 super(Body, self).__init__("snake-body", tile_pos) |
110 | 290 |
291 | |
292 class Tail(Segment): | |
293 def __init__(self, tile_pos): | |
153
9c47bf162ea1
snake has plastic surgery
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
145
diff
changeset
|
294 super(Tail, self).__init__("snake-tail", tile_pos) |