The Hungry Caterpillar Game project in Python is a single-player game for desktops. The project contains basic functions that demonstrate the actual gameplay. This application contains a variety of shapes and colored backgrounds. This game provides a basic example on how to program in Python programming.
Caterpillar Game Project Python
To Build a Simple Caterpillar Game Project You need to follow the below steps
Step 1. Download Python
You only need Python to run this script. You can visit here to download Python and then you need to install a package named turtle
pip install turtle
Step 2. Write the following code
Write this code in a Python file or directly copy this code and save the file with the name and .py extension e.g python Caterpillar.py
import turtle as t
import random as rd
t.bgcolor('yellow')
caterpillar = t.Turtle()
caterpillar.shape('square')
caterpillar.speed(0)
caterpillar.penup()
caterpillar.hideturtle()
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()
game_started = False
text_turtle = False
text_turtle = t.Turtle()
text_turtle.write('Press SPACE to start', align='center', font=('Arial', 18, 'bold'))
text_turtle.hideturtle()
score_turtle = t.Turtle()
score_turtle.hideturtle()
score_turtle.speed(0)
def outside_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) = caterpillar.pos()
outside = x < left_wall or x > right_Wall or y > top_wall or y < bottom_wall
return outside
def game_over():
caterpillar.color('yellow')
leaf.color('yellow')
t.penup()
t.hideturtle()
t.write('GAME OVER !', align='center', font=('Arial', 30, 'normal') )
t.onkey(start_game,'space')
def display_score(current_score):
score_turtle.clear()
score_turtle.penup()
x = (t.window_width()/2) - 70
y = (t.window_height()/2) - 70
score_turtle.setpos(x,y)
score_turtle.write(str(current_score), align='right', font=('Arial', 40, 'bold'))
def place_leaf():
leaf.hideturtle()
leaf.setx(rd.randint(-200,200))
leaf.sety(rd.randint(-200,200))
leaf.showturtle()
def start_game():
global game_started
if game_started:
return
game_started = True
score = 0
text_turtle.clear()
caterpillar_speed = 2
caterpillar_length = 3
caterpillar.shapesize(1,caterpillar_length,1)
caterpillar.showturtle()
display_score(score)
place_leaf()
while True:
caterpillar.forward(caterpillar_speed)
if caterpillar.distance(leaf) < 20:
place_leaf()
caterpillar_length = caterpillar_length + 1
caterpillar.shapesize(1,caterpillar_length,1)
caterpillar_speed = caterpillar_speed + 1
score = score + 10
display_score(score)
if outside_window():
game_over()
break
def move_up():
caterpillar.setheading(90)
def move_down():
caterpillar.setheading(270)
def move_left():
caterpillar.setheading(180)
def move_right():
caterpillar.setheading(0)
def restart_game():
start_game()
t.onkey(start_game,'space')
t.onkey(restart_game,'Up')
t.onkey(move_up,'Up')
t.onkey(move_right,'Right')
t.onkey(move_down,'Down')
t.onkey(move_left,'Left')
t.listen()
t.mainloop()
Step 3. Run the code
After saving the code double-click on the file to run the program or simply open Command Prompt and write the following command.
python Caterpillar.py
Leave a Reply