-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.py
More file actions
57 lines (52 loc) · 1.46 KB
/
Snake.py
File metadata and controls
57 lines (52 loc) · 1.46 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# This class represents the Snake
class Snake:
x = []
y = []
direction = 0
length = 0
speed = 0
# init for our snake
def __init__(self, length, speed):
self.speed = speed
self.x.append(0)
self.y.append(0)
self.length += 1
for i in range(1, length):
self.feed()
def update(self):
# Update position of the body
for i in range(self.length-1, 0, -1):
self.x[i] = self.x[i-1]
self.y[i] = self.y[i-1]
# Update position of the head
if self.direction == 0:
self.x[0] += self.speed
elif self.direction == 1:
self.x[0] -= self.speed
elif self.direction == 2:
self.y[0] -= self.speed
elif self.direction == 3:
self.y[0] += self.speed
def move(self, direction):
if direction == 'R' and self.direction != 1:
self.direction = 0
elif direction == 'L' and self.direction != 0:
self.direction = 1
elif direction == 'U' and self.direction != 3:
self.direction = 2
elif direction == 'D' and self.direction != 2:
self.direction = 3
def feed(self):
if self.direction == 0:
self.x.append(self.x[self.length-1]-self.speed)
self.y.append(self.y[self.length-1])
if self.direction == 1:
self.x.append(self.x[self.length-1]+self.speed)
self.y.append(self.y[self.length-1])
if self.direction == 2:
self.x.append(self.x[self.length-1])
self.y.append(self.y[self.length-1]+self.speed)
if self.direction == 3:
self.x.append(self.x[self.length-1])
self.y.append(self.y[self.length-1]-self.speed)
self.length += 1