-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.py
72 lines (50 loc) · 1.5 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
70
71
72
from turtle import Turtle
from time import sleep
STEPS = 20
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0
POSITIONS = [(20,0), (0,0), (-20,0)]
class Snake:
def __init__(self):
self.snake_body = []
self.create_snake()
self.head = self.snake_body[0]
def create_snake(self):
for position in POSITIONS:
self.add_part(position)
def add_part(self, position):
part = Turtle("square")
part.color("white")
part.penup()
part.goto(position)
self.snake_body.append(part)
def extend(self):
new_pos = self.snake_body[-1].position()
self.add_part(new_pos)
def move(self):
for i in range(len(self.snake_body)-1, 0, -1):
new_x = self.snake_body[i-1].xcor()
new_y = self.snake_body[i-1].ycor()
self.snake_body[i].goto(new_x,new_y)
self.head.forward(STEPS)
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 left(self):
if self.head.heading() != RIGHT:
self.head.setheading(LEFT)
def right(self):
if self.head.heading() != LEFT:
self.head.setheading(RIGHT)
def reset(self):
for part in self.snake_body:
part.hideturtle()
self.snake_body.clear()
sleep(0.2)
self.create_snake()
self.head = self.snake_body[0]