-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.py
69 lines (56 loc) · 1.79 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
62
63
64
65
66
67
68
69
from turtle import Turtle
START_POSITIONS = [(0,0), (-20,0), (-40,0)]
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0
class Snake:
def __init__(self):
"""Function to initialize the Snake"""
self.boxes = []
self.create_snake()
self.head = self.boxes[0]
self.distance = 20
def create_snake(self):
"""Function to create the snake"""
for pos in START_POSITIONS:
self.add_box(pos)
def add_box(self, pos):
"""Function to add a box to the snake"""
t = Turtle("square")
t.color("white")
t.penup()
t.goto(pos)
self.boxes.append(t)
def extend(self):
"""Function to extend the snake"""
self.add_box(self.boxes[-1].position())
def reset(self):
"""Function to reset the snake"""
for box in self.boxes:
box.goto(1000, 1000)
self.boxes.clear()
self.create_snake()
self.head = self.boxes[0]
def move(self):
"""Function to move the snake"""
for box in range((len(self.boxes) - 1), 0, -1):
x, y = self.boxes[box - 1].pos()
self.boxes[box].goto(x, y)
self.head.forward(self.distance)
def forward(self):
"""Function to move the snake forward"""
if self.head.heading() != DOWN:
self.head.setheading(UP)
def backward(self):
"""Function to move the snake backward"""
if self.head.heading() != UP:
self.head.setheading(DOWN)
def left(self):
"""Function to move the snake left"""
if self.head.heading() != RIGHT:
self.head.setheading(LEFT)
def right(self):
"""Function to move the snake right"""
if self.head.heading() != LEFT:
self.head.setheading(RIGHT)