-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.py
61 lines (49 loc) · 1.74 KB
/
snake.py
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
58
59
60
61
from turtle import Turtle
from food import Food
INITIAL_POS = [(0, 0), (-20, 0), (-40, 0)]
MOVE_DISTANCE = 20
UP = 90
DOWN = 270
RIGHT = 0
LEFT = 180
class Snake(Food):
def __init__(self): # Create basic 3 blocked snake
self.snake_size = []
self.create_snake()
self.head = self.snake_size[0]
def create_snake(self):
for snake in INITIAL_POS:
self.add_segment(snake)
def add_segment(self, position):
segment = Turtle(shape="square")
segment.color("white")
segment.penup()
segment.goto(position)
self.snake_size.append(segment)
def reset_snake(self):
for old_snake_box in self.snake_size:
old_snake_box.goto(1000, 1000)
self.snake_size.clear()
self.create_snake()
self.head = self.snake_size[0]
def extend_tail(self):
self.add_segment(self.snake_size[-1].position())
def move(self): # Make the snake move forward
for seg_num in range(len(self.snake_size) - 1, 0, -1):
prev_xcor = self.snake_size[seg_num - 1].xcor()
prev_ycor = self.snake_size[seg_num - 1].ycor()
# Making the last snake box to goto the position of second last snake box
self.snake_size[seg_num].goto(prev_xcor, prev_ycor)
self.head.forward(MOVE_DISTANCE)
def up(self):
if self.head.heading() != DOWN:
self.head.setheading(UP) # north
def down(self):
if self.head.heading() != UP:
self.head.setheading(DOWN) # south
def left(self):
if self.head.heading() != RIGHT:
self.head.setheading(LEFT) # west
def right(self):
if self.head.heading() != LEFT:
self.head.setheading(RIGHT) # east