import pygame from pygame.locals import * import random from debug import debug pygame.init() screen_h = 936 screen_l = 864 screen = pygame.display.set_mode((screen_l, screen_h)) clock = pygame.time.Clock() pygame.display.set_caption("Flappy Burb") bg = pygame.image.load("FlappyBurb/Assets/bg.png") ground = pygame.image.load("FlappyBurb/Assets/ground.png") button_img = pygame.image.load("FlappyBurb/Assets/restart.png") font = pygame.font.SysFont('Bauhaus 93', 90) white = (255,255,255) ground_scroll = 0 scroll_speed = 4 flying = False game_over = False pipe_gap = 180 pipe_freq = random.randint(100, 200) last_pipe = pygame.time.get_ticks() - pipe_freq pipe_height = 0 score = 0 high_score = 0 pass_pipe = False def draw_text(text, font, text_col, x, y): img = font.render(text, True, text_col) screen.blit(img, (x, y)) def reset_game(): pipe_group.empty() flappy.rect.x = 100 flappy.rect.y = int(screen_h / 2) flappy.vel = 0 flappy.index = 0 flappy.counter = 0 flappy.image = flappy.images[flappy.index] score = 0 return score class Bird(pygame.sprite.Sprite): def __init__(self, x, y): pygame.sprite.Sprite.__init__(self) self.images = [] self.index = 0 self.counter = 0 for num in range(1,4): img = pygame.image.load(f"FlappyBurb/Assets/bird{num}.png") self.images.append(img) self.image = self.images[self.index] self.rect = self.image.get_rect() self.rect.center = [x, y] self.vel = 0 self.clicked = False def update(self): if flying == True: self.vel += 0.5 if self.vel > 12: self.vel = 12 if self.rect.bottom < 768: self.rect.y += int(self.vel) if game_over == False: if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False: self.clicked = True self.vel = -9 if pygame.mouse.get_pressed()[0] == 0: self.clicked = False if self.rect.top < 0: self.rect.top = 0 self.vel = 0 self.counter += 1 plap_cooldown = 8 if self.counter > plap_cooldown: self.counter = 0 self.index += 1 if self.index >= len(self.images): self.index = 0 self.image = self.images[self.index] self.image = pygame.transform.rotate(self.images[self.index], self.vel * -2) else: self.image = pygame.transform.rotate(self.images[self.index], -60) class Pipe(pygame.sprite.Sprite): def __init__(self, x, y, position): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load("FlappyBurb/Assets/pipe.png") self.rect = self.image.get_rect() if position == 1: self.image = pygame.transform.flip(self.image, False, True) self.rect.bottomleft = [x, y - int(pipe_gap / 2)] if position == -1: self.rect.topleft = [x, y + int(pipe_gap / 2)] def update(self): self.rect.x -= scroll_speed if self.rect.right < 0: self.kill() class Button(): def __init__(self, x, y, image): self.image = image self.rect = self.image.get_rect() self.rect.topleft = (x, y) def draw(self): action = False pos = pygame.mouse.get_pos() if self.rect.collidepoint(pos): if pygame.mouse.get_pressed()[0] == 1: action = True screen.blit(self.image, (self.rect.x, self.rect.y)) return action pipe_group = pygame.sprite.Group() bird_group = pygame.sprite.Group() flappy = Bird(100, int(screen_h / 2)) bird_group.add(flappy) button = Button(screen_l // 2 - 50, screen_h // 2 - 50, button_img ) running = True while running: screen.blit(bg, (0,0)) screen.blit(ground, (ground_scroll, 768)) bird_group.draw(screen) bird_group.update() pipe_group.draw(screen) if len(pipe_group) > 0: if bird_group.sprites()[0].rect.left > pipe_group.sprites()[0].rect.left\ and bird_group.sprites()[0].rect.right < pipe_group.sprites()[0].rect.right\ and pass_pipe == False: pass_pipe = True if pass_pipe == True: if bird_group.sprites()[0].rect.left > pipe_group.sprites()[0].rect.right: score += random.randint(1,1) pass_pipe = False draw_text(str(score), font, white, int(screen_l / 2), 40) if pygame.sprite.groupcollide(bird_group, pipe_group, False, False): game_over = True if flappy.rect.bottom >= 760: game_over = True flying = False if game_over == False and flying == True: time_now = pygame.time.get_ticks() if time_now - last_pipe > pipe_freq : pipe_height = random.randint(-350, 150) pipe_freq = random.randint(1100, 1500) bot_pipe = Pipe(screen_l, int(screen_h / 2) + pipe_height, -1) top_pipe = Pipe(screen_l, int(screen_h / 2) + pipe_height, 1) pipe_group.add(bot_pipe) pipe_group.add(top_pipe) last_pipe = time_now ground_scroll -= scroll_speed if abs(ground_scroll) > 35: ground_scroll = 0 pipe_group.update() if game_over == True: if button.draw() == True: game_over = False if score > high_score: # Only update high_score if the current score is higher high_score = score score = reset_game() for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.MOUSEBUTTONDOWN and flying == False and game_over == False: flying = True debug(f"Game over = {game_over}") debug(f"Bird Vel = {-flappy.vel}", 55, 15) debug(f"Pipe freq = {pipe_freq}", 105, 15) debug(f"Pipe y-pos diff = {-pipe_height}", 155, 15) debug(f"Score = {score}", 205, 15) debug(f"High score = {high_score}", 255, 15) pygame.display.update() clock.tick(60) pygame.quit()