-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Expand file tree
/
Copy pathballoon.py
More file actions
182 lines (146 loc) · 3.98 KB
/
balloon.py
File metadata and controls
182 lines (146 loc) · 3.98 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import pgzrun
from random import randint
# Screen size
WIDTH = 800
HEIGHT = 600
# Balloon
balloon = Actor("balloon")
balloon.pos = 400, 300
# Obstacles
bird = Actor("bird-up")
bird.pos = randint(800, 1600), randint(10, 200)
house = Actor("house")
house.pos = randint(800, 1600), 460
tree = Actor("tree")
tree.pos = randint(800, 1600), 450
# Restart button (appears when game over)
restart_btn = Rect((350, 350), (100, 40))
# Game variables
bird_up = True
up = False
game_over = False
score = 0
number_of_updates = 0
scores = []
# Step 8: Update High Scores
def update_high_scores():
global score, scores
filename = "high-scores.txt"
scores = []
with open(filename, "r") as file:
line = file.readline()
high_scores = line.split()
for high_score in high_scores:
if score > int(high_score):
scores.append(str(score) + " ")
score = int(high_score)
else:
scores.append(str(high_score) + " ")
with open(filename, "w") as file:
for high_score in scores:
file.write(high_score)
# Step 9: Display High Scores
def display_high_scores():
screen.draw.text("HIGH SCORES", (340, 150), color="black")
y = 175
position = 1
for high_score in scores:
screen.draw.text(
f"Position {position}: {high_score}",
(350, y),
color="black",
)
y += 25
position += 1
# Restart button
screen.draw.filled_rect(restart_btn, "lightblue")
screen.draw.text("Restart", (restart_btn.x + 15,
restart_btn.y + 10), color="black")
# Step 10: Draw function
def draw():
screen.blit("background", (0, 0))
if not game_over:
balloon.draw()
bird.draw()
house.draw()
tree.draw()
screen.draw.text(f"Score: {score}", (700, 5))
else:
display_high_scores()
# Step 11: Mouse control
def on_mouse_down(pos):
global up
if not game_over:
up = True
balloon.y -= 50
else:
# Check if restart clicked
if restart_btn.collidepoint(pos):
restart_game()
def on_mouse_up():
global up
up = False
# Step 12: Bird flapping
def flap():
global bird_up
if bird_up:
bird.image = "bird-down"
bird_up = False
else:
bird.image = "bird-up"
bird_up = True
# Step 13: Update loop
def update():
global game_over, score, number_of_updates
if not game_over:
# Balloon gravity
if not up:
balloon.y += 1
# Bird movement
if bird.x > 0:
bird.x -= 4
if number_of_updates == 9:
flap()
number_of_updates = 0
else:
number_of_updates += 1
else:
bird.x = randint(800, 1600)
bird.y = randint(10, 200)
score += 1
number_of_updates = 0
# House movement
if house.right > 0:
house.x -= 2
else:
house.x = randint(800, 1600)
score += 1
# Tree movement
if tree.right > 0:
tree.x -= 2
else:
tree.x = randint(800, 1600)
score += 1
# Check for collision or out of bounds
if balloon.top < 0 or balloon.bottom > 560:
game_over = True
update_high_scores()
if (
balloon.collidepoint(bird.x, bird.y)
or balloon.collidepoint(house.x, house.y)
or balloon.collidepoint(tree.x, tree.y)
):
game_over = True
update_high_scores()
# Restart Game Function
def restart_game():
global game_over, score, up, balloon, bird, house, tree, number_of_updates
score = 0
up = False
game_over = False
number_of_updates = 0
balloon.pos = (400, 300)
bird.pos = (randint(800, 1600), randint(10, 200))
house.pos = (randint(800, 1600), 460)
tree.pos = (randint(800, 1600), 450)
pgzrun.go()