-
Notifications
You must be signed in to change notification settings - Fork 0
/
snakes.py
68 lines (48 loc) · 1.62 KB
/
snakes.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
import turtle
MOVE_DISTANCE=20
UP=90.0
DOWN=270.0
RIGHT=0.0
LEFT=180.0
class Snake:
def __init__(self,size):
self.size=size
self.segments=[]
self.cordinates=[(-20,0),(-40,0),(-60,0),(-80,0)]
def create_snake(self):
for turtL in range(0,self.size):
tim = turtle.Turtle(shape="square")
tim.penup()
# tim.shape("square")
tim.color("white")
tim.goto(self.cordinates[turtL])
self.segments.append(tim)
self.snake_head =self.segments[0]
def add_turtle(self,turtl):
tim = turtle.Turtle(shape="square")
tim.penup()
# tim.shape("square")
tim.color("white")
tim.goto(turtl)
self.segments.append(tim)
def extend(self):
self.add_turtle(self.segments[-1].position())
def move(self):
for segment_mark in range(len(self.segments) - 1, 0, -1):
x = self.segments[segment_mark - 1].xcor()
y = self.segments[segment_mark - 1].ycor()
self.segments[segment_mark].goto(x, y)
self.segments[0].forward(MOVE_DISTANCE)
# self.segments[0].left(90)
def snake_up(self):
if self.segments[0].heading()!=DOWN:
self.segments[0].setheading(UP)
def snake_down(self):
if self.segments[0].heading() != UP:
self.segments[0].setheading(DOWN)
def snake_right(self):
if self.segments[0].heading() != LEFT:
self.segments[0].setheading(RIGHT)
def snake_left(self):
if self.segments[0].heading() != RIGHT:
self.segments[0].setheading(LEFT)