-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.py
68 lines (48 loc) · 1.51 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
from turtle import Turtle
STARTING_POSITIONS = [(0, 0), (-20, 0), (-40, 0)]
MOVE_DISTANCE = 20
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0
class Snake:
def __init__(self):
self.snake_main = []
self.create_snake()
self.head = self.snake_main[0]
def create_snake(self):
for position in STARTING_POSITIONS:
self.add_snake_body(position)
def add_snake_body(self, position):
snake = Turtle()
snake.shape("square")
snake.color("white")
snake.penup()
snake.goto(position)
self.snake_main.append(snake)
def extend_snake(self):
self.add_snake_body(self.snake_main[-1].position())
def move(self):
for body in range(len(self.snake_main) - 1, 0, -1):
x_body = self.snake_main[body - 1].xcor()
y_body = self.snake_main[body - 1].ycor()
self.snake_main[body].goto(x_body, y_body)
self.head.forward(MOVE_DISTANCE)
def up(self):
if self.head.heading() != DOWN:
self.head.setheading(UP)
def down(self):
if self.head.heading() != UP:
self.head.setheading(DOWN)
def right(self):
if self.head.heading() != LEFT:
self.head.setheading(RIGHT)
def left(self):
if self.head.heading()!= RIGHT:
self.head.setheading(LEFT)
def reset(self):
for body in self.snake_main:
body.goto(1000, 1000)
self.snake_main.clear()
self.create_snake()
self.head = self.snake_main[0]