forked from clear-code-projects/FlappyBird_Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
261 lines (210 loc) · 8.16 KB
/
main.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import pygame, sys, os
from random import randint, choice
import random
pygame.init()
size = (576,1024)
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
#sounds settings
pygame.mixer.set_num_channels(8)
pygame.mixer.pre_init(44100,-16,2,512)
pygame.mixer.init()
#sky background
class Sky(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load('assets/background-day.png').convert_alpha()
self.image = pygame.transform.scale2x(self.image)
self.rect = self.image.get_rect(topleft=(0,0))
#Ground in movement
class Ground(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load('assets/base.png').convert_alpha()
self.rect = self.image.get_rect(topleft=(0,890))
def initial_position(self):
self.rect.x = 576
def ground_movement(self):
self.rect.left -= 1
if self.rect.right == 0:
self.rect.left = 576
def update(self):
self.ground_movement()
class Pipe(pygame.sprite.Sprite):
def __init__(self,position, is_flipped=False):
super().__init__()
self.image = pygame.image.load('assets/pipe-green.png').convert_alpha()
self.image = pygame.transform.scale2x(self.image)
if is_flipped:
self.flip_pipe()
self.rect = self.image.get_rect(bottomleft=(600,position))
else:
self.rect = self.image.get_rect(topleft=(600,position))
def flip_pipe(self):
self.image = pygame.transform.rotate(self.image,180)
def movement_pipe(self):
self.rect.x -= 9
def destroy_pipes(self):
if self.rect.right < 0:
self.kill()
def update(self):
self.movement_pipe()
self.destroy_pipes()
class Bird(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.down_flap = pygame.image.load('assets/redbird-downflap.png').convert_alpha()
self.down_flap = pygame.transform.scale2x(self.down_flap)
self.up_flap = pygame.image.load('assets/redbird-upflap.png').convert_alpha()
self.up_flap = pygame.transform.scale2x(self.up_flap)
self.fly_sound = pygame.mixer.Sound('sound/sfx_wing.wav')
self.fly_sound.set_volume(0.5)
self.image = pygame.image.load('assets/redbird-midflap.png').convert_alpha()
self.image = pygame.transform.scale2x(self.image)
self.rect = self.image.get_rect(topleft=(50,512))
#bird object initiate with force gravity action.
self.gravity = 0
self.jumping = False
def gravity_force_for_bird(self):
self.gravity += 1
self.rect.y += self.gravity
if not self.jumping:
self.image = self.up_flap
def jump_bird(self):
self.gravity = -20
self.image = self.down_flap
if not pygame.mixer.Channel(0).get_busy():
pygame.mixer.Channel(0).play(self.fly_sound)
def player_input(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
self.jumping = True
self.jump_bird()
else:
self.jumping = False
def rotate_bird(self):
#method for rotate a base image.
angle = -self.gravity * 2 #positive gravity bird look to sky #negative gravity bird look to ground
self.image = pygame.transform.rotate(self.image,angle=angle)
self.rect = self.image.get_rect(center=self.rect.center)
def update(self):
self.player_input()
self.gravity_force_for_bird()
self.rotate_bird()
class Score(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.path_style_font = 'assets/fonts/04B_19.TTF'
self.text_font = pygame.font.Font(self.path_style_font,25)
self.score = ""
self.image = self.text_font.render(self.score,False,'black')
self.rect = self.image.get_rect(topleft=(50,20))
self.time_life_game = 0
self.tick_sound = pygame.mixer.Sound('sound/sfx_point.wav')
self.tick_sound.set_volume(0.2)
def measure_time(self):
# '''Mide la duracion de una partida.'''
# self.time_life_game = int((pygame.time.get_ticks()-start_time)/1000)
# self.image = self.text_font.render(f"Score: {str(self.time_life_game)}",False,'black')
# #self.tick_sound.play()
self.time_life_game = int((pygame.time.get_ticks()-start_time)/1000)
self.image = self.text_font.render(f"Score: {str(self.time_life_game)}",False,'black')
if not pygame.mixer.Channel(1).get_busy():
pygame.mixer.Channel(1).play(self.tick_sound)
def update(self):
self.measure_time()
#Instances
sky = Sky()
ground = Ground()
ground_2 = Ground()
ground_2.initial_position()
bird = Bird()
score = Score()
#Sprite Groups
sky_group = pygame.sprite.GroupSingle()
sky_group.add(sky)
ground_group = pygame.sprite.Group()
ground_group.add(ground)
ground_group.add(ground_2)
player_group = pygame.sprite.GroupSingle()
player_group.add(bird)
obstacle_group = pygame.sprite.Group()
#obstacles will be added on game loop dinamically
score_group = pygame.sprite.GroupSingle()
score_group.add(score)
#Timer for create random pipes obstalces.
obstacle_timer = pygame.USEREVENT + 1
pygame.time.set_timer(obstacle_timer,1200)
pipe_gap = -300
#collison manager
def detect_collisions():
'''Return true if bird collide with a pipe'''
#Collisions detect
if pygame.sprite.spritecollide(player_group.sprite,obstacle_group,False):
#after collide remove all sprites from obstacle group
collisions_sound.play()
obstacle_group.empty()
return True
else:
return False
def fall_detection():
if player_group.sprite.rect.top >= 1024:
fall_sound.play()
return True
return False
##collision sound efect
collisions_sound = pygame.mixer.Sound('sound/sfx_hit.wav')
fall_sound = pygame.mixer.Sound('sound/sfx_die.wav')
#Game control Variables.
game_active = False
start_time = 0 #declarar para medir el tiempo desde que se inicia la partida
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if game_active:
if event.type == obstacle_timer:
#set position before instantiate pipes
#random position for bottom pipe
topleft_pipe_position = choice([400,500,600,750]) #topleft
pipe = Pipe(topleft_pipe_position)
#get the y position relative to bottom pipe position
top_pipe_position = pipe.rect.topleft[1]
top_pipe_position += pipe_gap
pipe_2 = Pipe(top_pipe_position,is_flipped=True)
obstacle_group.add(pipe)
obstacle_group.add(pipe_2)
else:
#logic for restart game
if event.type == pygame.KEYDOWN:
if event.key ==pygame.K_r:
player_group.sprite.rect.topleft = (50,512)
player_group.sprite.gravity = 0
game_active = True
start_time = pygame.time.get_ticks()
if game_active:
#draw and update all scenario
#detect collisions
ground_group.update()
obstacle_group.update()
sky_group.draw(screen)
obstacle_group.draw(screen)
ground_group.draw(screen)
score_group.draw(screen)
score_group.update()
#draw and update player
player_group.draw(screen)
player_group.update()
#print(player_group.sprite.jumping) Como acceder a un objeto sprite dentro de un grupo
collisions = detect_collisions()
fall = fall_detection()
if collisions or fall:
#collisions_sound.play()
game_active = False
else:
#IMPLEMENTAR EL ESTADO DE GAME OVER.
screen.fill('yellow')
score_group.draw(screen)
pygame.display.update()
clock.tick(60)