-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.py
63 lines (51 loc) · 1.69 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
# CONSTANTS
SNAKE_START = [(0, 0), (-20, 0), (-40, 0)]
MOVE_FORWARD = 20
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0
class Snake:
def __init__(self):
self.snake_parts = []
self.create_snake()
self.snake_head = self.snake_parts[0]
# create the parts of the snake.
def create_snake(self):
for s in SNAKE_START:
self.add_segment(s)
def add_segment(self, s):
new_square = Turtle(shape="square")
new_square.color("white")
new_square.penup()
new_square.goto(s)
self.snake_parts.append(new_square)
def reset(self):
for seg in self.snake_parts:
seg.goto(1000, 1000)
self.snake_parts.clear()
self.create_snake()
self.snake_head = self.snake_parts[0]
def extend_snake(self):
self.add_segment(self.snake_parts[-1].position())
# get the snake parts to move smoothly together.
def move(self):
for s in range(len(self.snake_parts) - 1, 0, -1):
new_x = self.snake_parts[s - 1].xcor()
new_y = self.snake_parts[s - 1].ycor()
self.snake_parts[s].goto(new_x, new_y)
self.snake_head.forward(MOVE_FORWARD)
# directional keys so snake can change directions
def up(self):
if self.snake_head.heading() != DOWN:
self.snake_head.setheading(UP)
def down(self):
if self.snake_head.heading() != UP:
self.snake_head.setheading(DOWN)
def left(self):
if self.snake_head.heading() != RIGHT:
self.snake_head.setheading(LEFT)
def right(self):
if self.snake_head.heading() != LEFT:
self.snake_head.setheading(RIGHT)