Repository navigation
Expand file tree
/
Copy pathsnake.py
More file actions
54 lines (45 loc) · 1.48 KB
/
Copy pathsnake.py
File metadata and controls
54 lines (45 loc) · 1.48 KB
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
from turtle import Turtle, down
STARTING_POSITON=[(0,0),(-20,0),(-40,0)]
MOVE_DISTANCE=10
UP=90
DOWN=270
RIGHT=0
LEFT=180
class Snake :
def __init__(self):
self.all_segments=[]
self.creat_snake()
self.head=self.all_segments[0]
self.head.color("purple")
def creat_snake(self):
for position in STARTING_POSITON:
self.add_segment(position)
# add segment to the snake body
def add_segment(self,position):
new_segment=Turtle(shape="square")
new_segment.color("white")
new_segment.penup()
new_segment.goto(position)
self.all_segments.append(new_segment)
# increase the lenght of snake
def increase_lenght(self):
self.add_segment(self.all_segments[-1].position())
# move the snake
def move(self):
for seg_num in range(len(self.all_segments)-1,0,-1):
x_cor=self.all_segments[seg_num-1].xcor()
y_cor=self.all_segments[seg_num-1].ycor()
self.all_segments[seg_num].goto(x_cor,y_cor)
self.head.forward(MOVE_DISTANCE)
def up(self):
if self.head.heading() != DOWN:
self.head.seth(90)
def down(self):
if self.head.heading() != UP:
self.head.seth(270)
def left(self):
if self.head.heading() != RIGHT:
self.head.seth(180)
def right(self):
if self.head.heading() != LEFT:
self.head.seth(0)