-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.py
222 lines (175 loc) · 6.5 KB
/
game.py
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import pygame
import sys
import random
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 1280, 720
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Flying Head Escape")
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
clock = pygame.time.Clock()
FPS = 60
BACKGROUND_IMG = pygame.image.load("images/background.png").convert()
BACKGROUND_IMG = pygame.transform.scale(BACKGROUND_IMG, (WIDTH, HEIGHT))
FLYING_HEAD_IMG = pygame.image.load("images/flying_head.png").convert_alpha()
FLYING_HEAD_IMG = pygame.transform.scale(FLYING_HEAD_IMG, (125, 100))
COAL_TRAP_IMG = pygame.image.load("images/coal.png").convert_alpha()
COAL_TRAP_IMG = pygame.transform.scale(COAL_TRAP_IMG, (100, 75))
OBSTACLE_IMG = pygame.image.load("images/tree.png").convert_alpha()
OBSTACLE_IMG = pygame.transform.scale(OBSTACLE_IMG, (100, 125))
ACORN_IMG = pygame.image.load("images/acorn.png").convert_alpha()
ACORN_IMG = pygame.transform.scale(ACORN_IMG, (50, 50))
flying_head = pygame.Rect(100, HEIGHT // 2, 100, 100)
flying_head_velocity = 0
FLAP_STRENGTH = -8
gravity = 0.5
obstacles = []
coal_traps = []
acorns = []
SPAWN_RATE = 1500
last_spawn_time = pygame.time.get_ticks()
# Score
score = 0
font = pygame.font.Font(None, 36)
def spawn_obstacle():
y_pos = random.randint(100, HEIGHT - 100)
obstacle = pygame.Rect(WIDTH, y_pos, 50, 10)
y_pos = random.randint(100, HEIGHT - 100)
coal_trap = pygame.Rect(WIDTH + random.randint(100, 200), y_pos + random.randint(-50, 50), 30, 30)
y_pos = random.randint(100, HEIGHT - 100)
acorn = pygame.Rect(WIDTH + random.randint(200, 300), y_pos + random.randint(-100, 100), 20, 20)
obstacles.append(obstacle)
coal_traps.append(coal_trap)
acorns.append(acorn)
def draw_text(text, x, y):
render = font.render(text, True, WHITE)
screen.blit(render, (x, y))
TITLE_IMG = pygame.Surface((WIDTH, HEIGHT))
TITLE_IMG.fill((50, 50, 100))
def draw_text(text, y, center_x=True):
render = font.render(text, True, WHITE)
text_rect = render.get_rect()
if center_x:
text_rect.center = (WIDTH // 2, y)
else:
text_rect.topleft = (10, y)
screen.blit(render, text_rect)
def show_title_screen():
"""
Displays the title screen with introductory text.
"""
while True:
screen.fill(BLACK)
screen.blit(TITLE_IMG, (0, 0))
draw_text("Flying Head Escape", HEIGHT // 4)
draw_text("You are the Flying Head.", HEIGHT // 2 - 100)
draw_text("After getting tricked into eating burning coal, you are in deep pain.", HEIGHT // 2 - 50)
draw_text("As you run into the forest, there are obstacles, burning coals, and acorns.", HEIGHT // 2)
draw_text("Consume acorns to earn points. However, consuming anything else will result in death!", HEIGHT // 2 + 50)
draw_text("Reach a score of 10 to escape victorious and continue terrorizing the village.", HEIGHT // 2 + 100)
draw_text("Press SPACE to Begin, and press SPACE to jump", HEIGHT // 2 + 150)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
return
def game_over_screen():
screen.fill(BLACK)
draw_text("Game Over", HEIGHT // 2 - 50)
draw_text("Press R to Restart or Q to Quit",HEIGHT // 2 + 50)
pygame.display.flip()
waiting = True
while waiting:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
waiting = False
running = True
elif event.key == pygame.K_q:
pygame.quit()
sys.exit()
def reset_game():
global flying_head, flying_head_velocity, obstacles, coal_traps, acorns, score, last_spawn_time
flying_head = pygame.Rect(100, HEIGHT // 2, 100, 100)
flying_head_velocity = 0
obstacles = []
coal_traps = []
acorns = []
score = 0
last_spawn_time = pygame.time.get_ticks()
show_title_screen()
# Game Loop
running = True
while running:
screen.blit(BACKGROUND_IMG, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
flying_head_velocity = FLAP_STRENGTH
flying_head_velocity += gravity
flying_head.y += int(flying_head_velocity)
if flying_head.top < 0 or flying_head.bottom > HEIGHT:
draw_text("You Lost!", HEIGHT // 2)
pygame.display.flip()
pygame.time.delay(2000)
running = False
if pygame.time.get_ticks() - last_spawn_time > SPAWN_RATE:
spawn_obstacle()
last_spawn_time = pygame.time.get_ticks()
for obstacle in obstacles[:]:
obstacle.x -= 5
if obstacle.x + obstacle.width + 50 < 0:
obstacles.remove(obstacle)
for trap in coal_traps[:]:
trap.x -= 5
if trap.x + trap.width + 50 < 0:
coal_traps.remove(trap)
for acorn in acorns[:]:
acorn.x -= 5
if acorn.x + acorn.width + 50 < 0:
acorns.remove(acorn)
for obstacle in obstacles:
if flying_head.colliderect(obstacle):
draw_text("You Lost!", HEIGHT // 2)
pygame.display.flip()
pygame.time.delay(2000)
running = False
for trap in coal_traps:
if flying_head.colliderect(trap):
draw_text("You Lost!", HEIGHT // 2)
pygame.display.flip()
pygame.time.delay(2000)
running = False
for acorn in acorns:
if flying_head.colliderect(acorn):
acorns.remove(acorn)
score += 1
if score >= 10:
draw_text("You Win!", HEIGHT // 2)
pygame.display.flip()
pygame.time.delay(2000)
running = False
screen.blit(FLYING_HEAD_IMG, flying_head.topleft)
for obstacle in obstacles:
screen.blit(OBSTACLE_IMG, obstacle.topleft)
for trap in coal_traps:
screen.blit(COAL_TRAP_IMG, trap.topleft)
for acorn in acorns:
screen.blit(ACORN_IMG, acorn.topleft)
draw_text(f"Score: {score}", 10, 10)
pygame.display.flip()
clock.tick(FPS)
if not running:
reset_game()
game_over_screen()
running = True
pygame.quit()
sys.exit()