-
Notifications
You must be signed in to change notification settings - Fork 0
/
caterpillar.py
144 lines (116 loc) · 3.25 KB
/
caterpillar.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import random as r
import turtle as t
#Create a catapillar turtle
t.bgcolor('orange')
cplr = t.Turtle()
cplr.shape('square')
cplr.color('blue')
cplr.speed(0)
cplr.penup()
cplr.hideturtle()
#Create a leaf turtle
leaf = t.Turtle()
leaf_shape = (0, 0), (14, 2), (18, 6), (20, 20), (6, 18), (2, 14)
t.register_shape('leaf', leaf_shape)
leaf.shape('leaf')
leaf.color('green')
leaf.penup()
leaf.hideturtle()
leaf.speed(0)
game_started = False
outside = False
#Add some text
text_t = t.Turtle()
text_t.write('Press SPACE to start', align='center', font=('Arial', 16, 'bold'))
text_t.hideturtle()
score_t = t.Turtle()
score_t.hideturtle()
score_t.speed(0)
def outside_window():
#Stay inside the window
left_wall = -t.window_width()/2
right_wall = t.window_width()/2
top_wall = t.window_height()/2
bottom_wall = -t.window_height()/2
(x, y) = cplr.pos()
outside = x > right_wall or x < left_wall or y > top_wall or y < bottom_wall
return outside
def game_over():
#cplr.color('orange')
#leaf.color('orange')
t.penup()
#t.hideturtle()
t.write('GAME OVER!', align='center', font=('Arial', 50, 'bold italic'))
def display_score(current_score):
score_t.clear()
score_t.penup()
x = (t.window_width()/2) - 50
y = (t.window_height()/2) - 50
score_t.setpos(x, y)
score_t.write(str(current_score), align='right', font=('Arial', 50, 'bold'))
def place_leaf():
leaf.ht()
leaf.setx(r.randint(-200, 200))
leaf.sety(r.randint(-200, 200))
leaf.st()
#Game starter
def start_game():
global game_started
global outside
if game_started:
return
#Setup variables for game starting
game_started = True
score = 0
text_t.clear()
cplr_speed = 2
cplr_length = 3
cplr.shapesize(1, cplr_length, 1)
cplr.showturtle()
display_score(score)
place_leaf()
#Get Moving
while True:
cplr.forward(cplr_speed)
if cplr.distance(leaf) < 20:
place_leaf()
cplr_length = cplr_length + 1
cplr.shapesize(1, cplr_length, 1)
cplr_speed = cplr_speed + 1
score = score + 10
display_score(score)
if outside_window():
game_over()
game_started = False
outside = False
t.clear()
cplr.setx(0)
cplr.sety(0)
text_t.write('Press SPACE to start', align='center', font=('Arial', 16, 'bold'))
t.listen()
t.mainloop()
# start_game()
def up_key():
# check if catapillar is moving left or right
if cplr.heading() == 0 or cplr.heading() == 180:
cplr.setheading(90)
def down_key():
# check if catapillar is moving left or right
if cplr.heading() == 0 or cplr.heading() == 180:
cplr.setheading(270)
def left_key():
# check if catapillar is moving up or down
if cplr.heading() == 90 or cplr.heading() == 270:
cplr.setheading(180)
def right_key():
# check if catapillar is moving up or down
if cplr.heading() == 90 or cplr.heading() == 270:
cplr.setheading(0)
#Bind and Listen
t.onkey(start_game, 'space')
t.onkey(up_key, 'Up')
t.onkey(down_key, 'Down')
t.onkey(left_key, 'Left')
t.onkey(right_key, 'Right')
t.listen()
t.mainloop()