comparison mamba/snake.py @ 128:646bb61c9ea6

The snake is a li(n)e!
author Simon Cross <hodgestar@gmail.com>
date Sun, 11 Sep 2011 22:00:18 +0200
parents 625b22f92efa
children c533b7c9cbe8
comparison
equal deleted inserted replaced
127:2b50fd1e4ca1 128:646bb61c9ea6
1 """The player snake object.""" 1 """The player snake object."""
2 2
3 from pygame.sprite import Group 3 from pygame.sprite import Group
4 from pygame.locals import BLEND_MULT 4 from pygame.locals import BLEND_MULT
5 from pygame.draw import aalines
5 6
6 from mamba.sprites import BaseSprite 7 from mamba.sprites import BaseSprite, tile_sizify
7 from mamba import mutators 8 from mamba import mutators
8 9
9 10
10 class Snake(object): 11 class Snake(object):
11 12
12 UP, DOWN, LEFT, RIGHT = [(0, -1), (0, 1), (-1, 0), (1, 0)] 13 UP, DOWN, LEFT, RIGHT = [(0, -1), (0, 1), (-1, 0), (1, 0)]
14 SPEED = 20.0 # pixels / s
13 15
14 def __init__(self, tile_pos, orientation): 16 def __init__(self, tile_pos, orientation):
15 self.segments = self.create_segments(tile_pos, orientation) 17 self.segments = self.create_segments(tile_pos, orientation)
16 self.segment_group = Group() 18 self.segment_group = Group()
17 self.segment_group.add(*self.segments) 19 self.segment_group.add(*self.segments)
18 self.set_orientation(orientation) 20 self.set_orientation(orientation)
21 self.pos = tile_sizify(tile_pos)
22 self.length = 3.0
19 23
20 head = property(fget=lambda self: self.segments[0]) 24 head = property(fget=lambda self: self.segments[0])
21 tail = property(fget=lambda self: self.segments[-1]) 25 tail = property(fget=lambda self: self.segments[-1])
22 26
23 def create_segments(self, tile_pos, orientation): 27 def create_segments(self, tile_pos, orientation):
27 Body((x + dx, y + dy)), 31 Body((x + dx, y + dy)),
28 Tail((x + 2 * dx, y + 2 * dy)), 32 Tail((x + 2 * dx, y + 2 * dy)),
29 ] 33 ]
30 34
31 def draw(self, surface): 35 def draw(self, surface):
32 self.segment_group.draw(surface) 36 # self.segment_group.draw(surface)
37 x, y = self.pos
38 aalines(surface, (0, 0, 255), False, [(x, y), (x + 60, y)])
33 39
34 def update(self, dt): 40 def update(self, dt):
35 pass 41 x, y = self.pos
42 ox, oy = self.orientation
43 ds = self.SPEED * dt
44 self.pos = (x + ox * ds, y + oy * ds)
36 45
37 def set_orientation(self, orientation): 46 def set_orientation(self, orientation):
38 self.orientation = orientation 47 self.orientation = orientation
39 #self.orientation = orientation 48 #self.orientation = orientation
40 #for segment in self.segments: 49 #for segment in self.segments:
41 # segment.set_orientation(self) 50 # segment.set_orientation(self)
42 print orientation
43 51
44 52
45 class Segment(BaseSprite): 53 class Segment(BaseSprite):
46 54
47 GREEN = mutators.Overlay("tiles/common/snake/green.png", BLEND_MULT) 55 GREEN = mutators.Overlay("tiles/common/snake/green.png", BLEND_MULT)