Skip to content

Commit 0d1d2c4

Browse files
committedMay 29, 2021
Play with this Caterpillar
1 parent a4b026f commit 0d1d2c4

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
 

‎Caterpillar.py

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import turtle as t
2+
import random as rd
3+
4+
t.bgcolor('yellow')
5+
6+
caterpillar = t.Turtle()
7+
caterpillar.shape('square')
8+
caterpillar.speed(0)
9+
caterpillar.penup()
10+
caterpillar.hideturtle()
11+
12+
leaf = t.Turtle()
13+
leaf_shape = ((0,0),(14,2),(18,6),(20,20),(6,18),(2,14))
14+
t.register_shape('leaf',leaf_shape)
15+
leaf.shape('leaf')
16+
leaf.color('green')
17+
leaf.penup()
18+
leaf.hideturtle()
19+
leaf.speed()
20+
21+
game_started = False
22+
text_turtle = t.Turtle()
23+
text_turtle.write('Press SPACE to start',align='center',font=('Arial',16,'bold'))
24+
text_turtle.hideturtle()
25+
26+
score_turtle = t.Turtle()
27+
score_turtle.hideturtle()
28+
score_turtle.speed(0)
29+
30+
def outside_window():
31+
left_wall = -t.window_width()/2
32+
right_wall = t.window_width()/2
33+
top_wall = t.window_height()/2
34+
bottom_wall = -t.window_height()/2
35+
(x,y) = caterpillar.pos()
36+
outside = x < left_wall or x > right_wall or y < bottom_wall or y > top_wall
37+
return outside
38+
39+
def game_over():
40+
caterpillar.color('yellow')
41+
leaf.color('yellow')
42+
t.penup()
43+
t.hideturtle()
44+
t.write('GAME OVER!',align='center' , font=('Aerial',30,'normal'))
45+
46+
def display_score(current_score):
47+
score_turtle.clear()
48+
score_turtle.penup()
49+
x = (t.window_width() / 2)-50
50+
y = (t.window_height() / 2)-50
51+
score_turtle.setpos(x,y)
52+
score_turtle.write(str(current_score) , align = 'right',font=('Arial',40,'bold'))
53+
54+
def place_leaf():
55+
leaf.hideturtle()
56+
leaf.setx(rd.randint(-200,200))
57+
leaf.sety(rd.randint(-200,200))
58+
leaf.showturtle()
59+
60+
def start_game():
61+
global game_started
62+
if game_started:
63+
return
64+
game_started = True
65+
66+
score = 0
67+
text_turtle.clear()
68+
69+
caterpillar_speed = 2
70+
caterpillar_length = 3
71+
caterpillar.shapesize(1,caterpillar_length,1)
72+
caterpillar.showturtle()
73+
display_score(score)
74+
place_leaf()
75+
76+
while True:
77+
caterpillar.forward(caterpillar_speed)
78+
if caterpillar.distance(leaf)<20:
79+
place_leaf()
80+
caterpillar_length = caterpillar_length + 1
81+
caterpillar.shapesize(1,caterpillar_length,1)
82+
caterpillar_speed = caterpillar_speed + 1
83+
score = score + 10
84+
display_score(score)
85+
if outside_window():
86+
game_over()
87+
break
88+
89+
def move_up():
90+
if caterpillar.heading() == 0 or caterpillar.heading() == 180:
91+
caterpillar.setheading(90)
92+
93+
def move_down():
94+
if caterpillar.heading() == 0 or caterpillar.heading() == 180:
95+
caterpillar.setheading(270)
96+
97+
def move_left():
98+
if caterpillar.heading() == 90 or caterpillar.heading() == 270:
99+
caterpillar.setheading(180)
100+
101+
def move_right():
102+
if caterpillar.heading() == 90 or caterpillar.heading() == 270:
103+
caterpillar.setheading(0)
104+
105+
t.onkey(start_game,'space')
106+
t.onkey(move_up,'Up')
107+
t.onkey(move_right,'Right')
108+
t.onkey(move_down,'Down')
109+
t.onkey(move_left,'Left')
110+
t.listen()
111+
t.mainloop()

0 commit comments

Comments
 (0)
Please sign in to comment.