Skip to content

Commit e625d80

Browse files
committed
Add project #Caterpillar_Game project
1 parent e11ea1a commit e625d80

File tree

5 files changed

+405
-260
lines changed

5 files changed

+405
-260
lines changed

Caterpillar_Game/Caterpillar.py

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

Caterpillar_Game/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
![Star Badge](https://img.shields.io/static/v1?label=%F0%9F%8C%9F&message=If%20Useful&style=style=flat&color=BC4E99)
2+
![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)
3+
[![View My Profile](https://img.shields.io/badge/View-My_Profile-green?logo=GitHub)](https://github.com/ndleah)
4+
[![View Repositories](https://img.shields.io/badge/View-My_Repositories-blue?logo=GitHub)](https://github.com/ndleah?tab=repositories)
5+
6+
# Caterpillar
7+
<p align="center">
8+
<img src="https://static.wikia.nocookie.net/pixar/images/e/ec/Heimlich.png/revision/latest?cb=20170807224005" width=40% height=40%>
9+
10+
## 🛠️ Description
11+
12+
A simple Caterpillar game built in python.
13+
14+
## ⚙️ Languages or Frameworks Used
15+
```bash
16+
pip install tk
17+
```
18+
19+
## 🌟 How to run
20+
Running the script is really simple! Just open a terminal in the folder where your script is located and run the following command:
21+
22+
```sh
23+
python Caterpillar.py
24+
```
25+
## 📺 Demo
26+
<p align="center">
27+
<img src="https://github.com/ndleah/python-mini-project/blob/main/IMG/caterpillar.gif" width=70% height=70%>
28+
29+
## 🤖 Author
30+
[Leah Nguyen](https://github.com/ndleah)

IMG/caterpillar.gif

379 KB
Loading

0 commit comments

Comments
 (0)