-
Notifications
You must be signed in to change notification settings - Fork 0
/
v3.py
112 lines (80 loc) · 2.61 KB
/
v3.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from turtle import Turtle, Screen, done
from v3helpers import Head, Body, Food, Pen
import time
import math
def main():
# set up screen
win = Screen()
win.setup(width = 800, height = 800)
win.bgcolor("black")
win.tracer(0)
win.title("Hello Snake V3")
# set up square border
square = Turtle()
square.pencolor("white")
square.shape("square")
square.shapesize(29, 29, 1)
square.fillcolor("")
# Initialize some variables
segments = []
delay = 0.1
score = 0
high_score = 0
# set up variables as classes
head = Head()
food = Food()
pen = Pen(high_score)
# window to listen on click of WASD, and execute function to change direction of head
win.listen()
win.onkey(head.go_up, "W")
win.onkey(head.go_down, "S")
win.onkey(head.go_left, "A")
win.onkey(head.go_right, "D")
while True:
# continously update the game
win.update()
time.sleep(delay)
# if head touches food,
if head.distance(food.pos()) < 15:
# food reinitialize at random spot
food.food_random()
# body increases
body = Body()
segments.append(body)
score += 10
if score > high_score:
high_score = score
# update score
pen.update_score(score, high_score)
# make snake faster
delay -= 0.001
# put body to follow head
for i in range(len(segments)-1, 0, -1):
x, y = segments[i-1].xcor(), segments[i-1].ycor()
segments[i].goto(x, y)
if len(segments) > 0:
x, y = head.xcor(), head.ycor()
segments[0].goto(x, y)
head.move()
# if head touches wall, dies and game resets
if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
time.sleep(1)
head.reset_head()
for j in segments:
j.goto(1000, 1000)
segments = []
score = 0
pen.update_score(score, high_score)
# if head touches body, dies and game resets
for k in segments:
if head.distance(k) < 15:
time.sleep(1)
head.reset_head()
for j in segments:
j.goto(1000, 1000)
segments = []
score = 0
pen.update_score(score, high_score)
done()
if __name__ == "__main__":
main()