()
| 173 | |
| 174 | |
| 175 | def main(): |
| 176 | pygame.init() |
| 177 | |
| 178 | display_surface = pygame.display.set_mode((W_WIDTH, W_HEIGHT)) |
| 179 | pygame.display.set_caption("Flappy Bird by PMN") |
| 180 | |
| 181 | clock = pygame.time.Clock() |
| 182 | score_font = pygame.font.SysFont(None, 32, bold=True) # default font |
| 183 | images = load_images() |
| 184 | |
| 185 | # the bird stays in the same x position, so bird.x is a constant |
| 186 | # center bird on screen |
| 187 | bird = Bird( |
| 188 | 50, |
| 189 | int(W_HEIGHT / 2 - Bird.HEIGHT / 2), |
| 190 | 2, |
| 191 | (images["bird-wingup"], images["bird-wingdown"]), |
| 192 | ) |
| 193 | |
| 194 | pipes = deque() |
| 195 | |
| 196 | frame_clock = 0 # this counter is only incremented if the game isn't paused |
| 197 | score = 0 |
| 198 | done = paused = False |
| 199 | while not done: |
| 200 | clock.tick(FPS) |
| 201 | |
| 202 | # Handle this 'manually'. If we used pygame.time.set_timer(), |
| 203 | # pipe addition would be messed up when paused. |
| 204 | if not (paused or frame_clock % msec_to_frames(PipePair.ADD_INTERVAL)): |
| 205 | pp = PipePair(images["pipe-end"], images["pipe-body"]) |
| 206 | pipes.append(pp) |
| 207 | |
| 208 | for e in pygame.event.get(): |
| 209 | if e.type == QUIT or (e.type == KEYUP and e.key == K_ESCAPE): |
| 210 | done = True |
| 211 | break |
| 212 | elif e.type == KEYUP and e.key in (K_PAUSE, K_p): |
| 213 | paused = not paused |
| 214 | elif e.type == MOUSEBUTTONUP or ( |
| 215 | e.type == KEYUP and e.key in (K_UP, K_RETURN, K_SPACE) |
| 216 | ): |
| 217 | bird.ms_to_up = Bird.UP_DURATION |
| 218 | |
| 219 | if paused: |
| 220 | continue # don't draw anything |
| 221 | |
| 222 | # check for collisions |
| 223 | pipe_collision = any(p.collides_with(bird) for p in pipes) |
| 224 | if pipe_collision or 0 >= bird.y or bird.y >= W_HEIGHT - Bird.HEIGHT: |
| 225 | done = True |
| 226 | |
| 227 | for x in (0, W_WIDTH / 2): |
| 228 | display_surface.blit(images["background"], (x, 0)) |
| 229 | |
| 230 | while pipes and not pipes[0].visible: |
| 231 | pipes.popleft() |
| 232 |
no test coverage detected