-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.py
67 lines (51 loc) · 1.67 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
MOVE_DISTANCE = 20
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0
SNAKE_COLOR = "aqua"
colors = ["Pink", "PaleVioletRed", "HotPink", "DeepPink", "MediumVioletRed"] # PINKS
#colors = ["Red", "Orange", "Yellow", "Lime", "Cyan", "Blue", "Indigo"] # RAINBOW
COLOR_PALETTE = colors + colors[-2:0:-1]
NUMBER_OF_COLORS = len(COLOR_PALETTE)
class Snake:
def __init__(self):
self.body = []
for i in range(3):
self.add_body_segment()
self.body[i].setx(-20 * i)
def add_body_segment(self):
new_seg = Turtle()
self.body.append(new_seg)
new_seg.shape("square")
body_len = len(self.body)
new_seg.color(COLOR_PALETTE[body_len % NUMBER_OF_COLORS])
new_seg.penup()
def move(self):
for i in range(len(self.body) - 1, 0, -1):
self.body[i].goto(self.body[i - 1].pos())
self.body[0].forward(MOVE_DISTANCE)
def up(self):
if self.body[0].heading() != DOWN:
self.body[0].setheading(UP)
def down(self):
if self.body[0].heading() != UP:
self.body[0].setheading(DOWN)
def left(self):
if self.body[0].heading() != RIGHT:
self.body[0].setheading(LEFT)
def right(self):
if self.body[0].heading() != LEFT:
self.body[0].setheading(RIGHT)
def extend(self):
self.add_body_segment()
self.body[-1].goto(self.body[-2].pos())
def reset(self):
for segment in self.body:
segment.hideturtle()
segment.clear()
self.body = []
for i in range(3):
self.add_body_segment()
self.body[i].setx(-20 * i)