-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame.py
More file actions
196 lines (155 loc) · 6.09 KB
/
Copy pathgame.py
File metadata and controls
196 lines (155 loc) · 6.09 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import sys
import random
import pygame
def draw_floor():
screen.blit(floor, (floor_x_pos, 600))
screen.blit(floor, (floor_x_pos + 432, 600))
def create_pipe():
random_pipe_pos = random.randint(300, 550)
pipe_gap = 200 # Khoảng trống 200px
bottom_pipe = pipe_surface.get_rect(midtop = (500, random_pipe_pos))
top_pipe = pipe_surface.get_rect(midbottom = (500, random_pipe_pos - pipe_gap))
return bottom_pipe, top_pipe
def move_pipe(pipes):
new_pipes = []
for pipe in pipes:
pipe.centerx -= 5
if pipe.right < 0:
continue
new_pipes.append(pipe)
return new_pipes
def draw_pipe(pipes):
for pipe in pipes:
if pipe.bottom >= height: # Nếu là ống dưới
screen.blit(pipe_surface, pipe)
else: # Nếu là ống trên, lật ngược ảnh rồi vẽ
flip_pipe = pygame.transform.flip(pipe_surface, False, True)
screen.blit(flip_pipe, pipe)
def is_collided(pipes):
for pipe in pipes:
if bird_rect.colliderect(pipe):
return True # Va chạm với ống, game kết thúc
if bird_rect.top <= -70 or bird_rect.bottom >= 650:
return True # Va chạm với sàn/trần, game kết thúc
return False # Nếu không có va chạm, game tiếp tục
def is_through(bird, pipes):
for pipe in pipes:
if pipe.centerx == bird.centerx:
return True
return False
def rotate_bird(bird1):
new_bird = pygame.transform.rotozoom(bird1, -bird_movement*3, 1)
return new_bird
def bird_animation():
new_bird = bird_animation_list[bird_animation_index]
new_bird_rect = new_bird.get_rect(center = (100, bird_rect.centery))
return new_bird, new_bird_rect
# --- MAIN GAME ---
# Mấy dòng này để khởi tạo thôi, không cần quan tâm lắm đâu
pygame.init()
width, height = 432, 768
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
gravity = 0.65
bird_movement = 0
game_active = False
game_state = 'menu'
points = 0
# background thôi
bg = pygame.image.load('img/Copilot_20250702_223548.png').convert()
bg = pygame.transform.scale2x(bg)
# floor ý mà
floor = pygame.image.load('img/floor.png').convert()
floor = pygame.transform.scale2x(floor)
floor_x_pos = 0
# bird, hay còn gọi là chim
bird_down = pygame.transform.scale2x(pygame.image.load('img/yellowbird-downflap.png').convert_alpha())
bird_mid = pygame.transform.scale2x(pygame.image.load('img/yellowbird-midflap.png').convert_alpha())
bird_up = pygame.transform.scale2x(pygame.image.load('img/yellowbird-upflap.png').convert_alpha())
bird_animation_list = [bird_down, bird_mid, bird_up] #0 1 2
bird_animation_index = 2
bird = bird_animation_list[bird_animation_index]
bird_rect = bird.get_rect(center = (100, height//2))
bird_rotated = bird
bird_flaprate = 200
birdflap = pygame.USEREVENT + 1
pygame.time.set_timer(birdflap, bird_flaprate)
# pipes, hay còn gọi là ống cống
pipe_surface = pygame.image.load('img/pipe-green.png').convert()
pipe_surface = pygame.transform.scale2x(pipe_surface)
pipe_list = []
pipe_spawnrate = 1000
spawnpipe = pygame.USEREVENT
pygame.time.set_timer(spawnpipe, pipe_spawnrate)
# hiệu ứng âm thanh, or sound effects
sfx = {
'die': pygame.mixer.Sound('sound/sfx_die.wav'),
'hit': pygame.mixer.Sound('sound/sfx_hit.wav'),
'point': pygame.mixer.Sound('sound/sfx_point.wav'),
'swoosh': pygame.mixer.Sound('sound/sfx_swooshing.wav'),
'wing': pygame.mixer.Sound('sound/sfx_wing.wav'),
}
# graphical user interface
gui_message = pygame.transform.scale_by(pygame.image.load('img/message.png'), 1.5)
gui_message_rect = gui_message.get_rect(center = (width//2, 0.4 * height))
gui_gameover = pygame.transform.scale_by(pygame.image.load('img/gameover.png'), 1.5)
gui_gameover_rect = gui_gameover.get_rect(center = (width//2, 0.25 * height))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# cái này liên quan đến việc thoát game chim ống cống
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
if game_state == 'menu':
game_state = 'ingame'
game_active = True
elif game_state == 'ingame':
bird_movement = -9
pygame.mixer.Sound.play(sfx['wing'])
elif game_state == 'gameover':
game_state = 'menu'
pipe_list.clear()
bird_animation_index = 2
bird = bird_animation_list[bird_animation_index]
points = 0
bird_rotated = bird
bird_rect.center = (100, height//2)
bird_movement = 0
if event.type == spawnpipe and game_active:
pipe_list.extend(create_pipe())
if event.type == birdflap:
bird_animation_index = (bird_animation_index + 1) % len(bird_animation_list)
bird, bird_rect = bird_animation()
screen.blit(bg, (0, 0))
if game_active:
# --- Logic khi game đang chạy ---
# Vật lý chim
bird_movement += gravity
bird_rotated = rotate_bird(bird1=bird)
bird_rect.centery += bird_movement
if is_through(bird_rect, pipe_list):
pygame.mixer.Sound.play(sfx['point'])
points += 1
print(points) # Temporary solution
if is_collided(pipe_list):
pygame.mixer.Sound.play(sfx['hit'])
pygame.mixer.Sound.play(sfx['die'])
game_active = False
game_state = 'gameover'
# Vật lý ống cống
pipe_list = move_pipe(pipe_list)
screen.blit(bird_rotated, bird_rect)
draw_pipe(pipe_list)
draw_floor()
if game_state == 'menu':
screen.blit(gui_message, gui_message_rect)
if game_state == 'gameover':
screen.blit(gui_gameover, gui_gameover_rect)
else:
# Vật lý của sàn
floor_x_pos -= 1
if floor_x_pos <= -width:
floor_x_pos = 0
pygame.display.update()
clock.tick(60)