-
Notifications
You must be signed in to change notification settings - Fork 0
/
border_collapse_turtle_game.py
116 lines (99 loc) · 2.02 KB
/
border_collapse_turtle_game.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
113
114
115
116
import turtle
from random import randrange as ran
screen = turtle.Screen()
width = 500
screen.setup(width,width)
screen.bgcolor("black")
border = 200
yourspeed = 30
caught = True
movecount = 0
changespeed = 5
score = 0
def changeborder(size):
global border
b.clear()
b.home()
b.color("green")
b.write(score,font=("Arial",20,"normal"))
b.color("red")
b.penup()
b.goto(-size//2, size//2)
b.pendown()
b.speed(10)
for x in range(4):
b.forward(size)
b.right(90)
b.speed(0)
b.penup()
border = size
def gameover():
global score
t.hideturtle()
t.home()
t.write("Game Over. Score: " + str(score), font=("Arial",15,"normal"))
b = turtle.Turtle()
b.hideturtle()
b.speed(0)
b.pensize(10)
b.color("red")
b.penup()
def moveguy():
global border
global yourspeed
global caught
global score
yourspeed = yourspeed + 5
if caught:
changeborder(border + 10)
score = score + 1
else:
changeborder(border - 60)
g.goto(ran(-border//2,border//2),ran(-border//2,border//2))
caught = False
g = turtle.Turtle()
g.penup()
g.speed(9)
g.shape("circle")
g.color("blue")
t = turtle.Turtle()
t.speed(yourspeed)
t.shape("turtle")
t.color("white")
t.penup()
moveguy()
# keyboard functions
def goleft():
t.left(10)
def goright():
t.right(10)
def move():
global border
global yourspeed
global caught
global movecount
global changespeed
if t.distance(g) < 15:
caught = True
movecount = 0
changespeed = changespeed*.95
moveguy()
pos = t.pos()
xbound = (pos[0] > -border//2) and (pos[0] < border//2)
ybound = (pos[1] > -border//2) and (pos[1] < border//2)
if (not (xbound and ybound)):
gameover()
return
t.forward(1)
movecount += 1
if movecount >= yourspeed*changespeed:
movecount = 0
changespeed = changespeed*.92
caught = False
moveguy()
screen.ontimer(move,1000//yourspeed)
screen.onkey(goleft, "Left")
screen.onkey(goright, "Right")
screen.listen()
print("Use the left and right arrow keys to get the food before the border collapses!")
move()