Skip to content

Commit b7d782b

Browse files
authored
Add files via upload
1 parent 05e5ae3 commit b7d782b

File tree

1 file changed

+117
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)