-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.py
More file actions
34 lines (27 loc) · 752 Bytes
/
snake.py
File metadata and controls
34 lines (27 loc) · 752 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import direction
class Snake:
headColor = (100,60,150)
color = (100,150,20)
def __init__(self, x = 5, y = 3, length = 3):
self.direction = direction.right()
self.body = []
self.growLength = 0
for i in range(0, length):
self.body.append((x - i, y))
def getHead(self):
return self.body[0]
def grow(self, length = 1):
self.growLength += length
def move(self):
# get old position of head
position = self.getHead()
# calculate new position of head
x, y = self.direction.addTo(position)
# insert new position of head
self.body.insert(0, (x, y))
# if the snake isn't growing, get rid of last tail
if self.growLength == 0:
self.body.pop()
# otherwise decrease grow count
else:
self.growLength -= 1