-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv3helpers.py
95 lines (77 loc) · 2.5 KB
/
v3helpers.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from turtle import Turtle
import random
# head class
class Head(Turtle):
def __init__(self):
Turtle.__init__(self)
self.color("white")
self.shape("square")
self.speed(0)
self.direction = "stop"
self.penup()
self.goto(0, 100)
# def functions to change direction of head
def go_up(self):
if self.direction != "down":
self.direction = "up"
def go_down(self):
if self.direction != "up":
self.direction = "down"
def go_left(self):
if self.direction != "right":
self.direction = "left"
def go_right(self):
if self.direction != "left":
self.direction = "right"
# move towards the direction head is facing
def move(self):
if self.direction == "up":
x, y = self.xcor(), self.ycor()
self.goto(x + 0, y + 20)
if self.direction == "down":
x, y = self.xcor(), self.ycor()
self.goto(x + 0, y - 20)
if self.direction == "left":
x, y = self.xcor(), self.ycor()
self.goto(x + -20, y + 0)
if self.direction == "right":
x, y = self.xcor(), self.ycor()
self.goto(x + 20, y + 0)
def reset_head(self):
self.goto(0, 100)
self.direction = "stop"
self.speed(0)
# body class
class Body(Turtle):
def __init__(self):
Turtle.__init__(self)
self.color("yellow")
self.shape("square")
self.speed(0)
self.penup()
# food class
class Food(Turtle):
def __init__(self):
Turtle.__init__(self)
self.color("blue")
self.shape("circle")
self.speed(0)
self.penup()
#self.goto(0, 0)
# food reinitialize at random spot
def food_random(self):
x, y = random.randint(-290, 290), random.randint(-290, 290)
self.goto(x, y)
# pen class
class Pen(Turtle):
def __init__(self, high_score):
Turtle.__init__(self)
self.goto(0, 330)
self.color("white")
self.shape("square")
self.penup()
self.hideturtle()
self.write("Score: 0 High Score: {}".format(high_score), align="center", font=("Arial", 24, "normal"))
def update_score(self, score, high_score):
self.clear()
self.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Arial", 24, "normal"))