-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.py
62 lines (48 loc) · 1.45 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
from turtle import Turtle
import random
STARTING_POSITIONS = [(0, 0), (-20, 0), (-40, 0)]
MOVE_DIS = 20
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0
class Snake:
def __init__(self):
self.segments = []
self.create_snake()
self.head = self.segments[0]
def create_snake(self):
for position in STARTING_POSITIONS:
self.add_seg(position)
def add_seg(self, position):
seg = Turtle("circle")
seg.penup()
seg.goto(position)
self.change_color(seg)
self.segments.append(seg)
def extend(self):
# add one seg to snake.
self.add_seg(self.segments[-1].position())
def move(self):
for seg_num in range(len(self.segments)-1, 0, -1):
new_x = self.segments[seg_num - 1].xcor()
new_y = self.segments[seg_num - 1].ycor()
self.segments[seg_num].goto(new_x, new_y)
self.head.forward(MOVE_DIS)
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 change_color(self, seg):
R = random.random()
B = random.random()
G = random.random()
seg.color(R, G, B)