DEV Community

Cover image for I Need Help With My Fan Game
Python Remaker
Python Remaker

Posted on

I Need Help With My Fan Game

So I'm Making An Flappy Bird Remake Game For Fun But Everytime I Run The Code It Just Closes The Game When It Opens. Code:

import pygame
import random

pygame.init()

# game window
screen = pygame.display.set_mode((288, 512))
pygame.display.set_caption('Flappy Bird')

# game clock
clock = pygame.time.Clock()

# game font
game_font = pygame.font.Font('C:\\Users\\USER\\Downloads\\04B_19__.TTF', 20)



# game variables
gravity = 0.25
bird_movement = 0
game_over = False
game_speed = 4
score = 0
high_score = 0

# game surfaces
bg_surface = pygame.image.load('background-day.png').convert()
floor_surface = pygame.image.load('base.png').convert()
bird_surface = pygame.image.load('yellowbird-midflap.png').convert_alpha()
bird_rect = bird_surface.get_rect(center=(100, 256))
pipe_surface = pygame.image.load('pipe-green.png').convert()
pipe_list = []
SPAWNPIPE = pygame.USEREVENT
pygame.time.set_timer(SPAWNPIPE, 1200)

# game functions
def create_pipe():
    random_pipe_pos = random.choice([200, 300, 400])
    bottom_pipe = pipe_surface.get_rect(midtop=(350, random_pipe_pos))
    top_pipe = pipe_surface.get_rect(midbottom=(350, random_pipe_pos - 150))
    return bottom_pipe, top_pipe

def move_pipes(pipes):
    for pipe in pipes:
        pipe.centerx -= game_speed
    visible_pipes = [pipe for pipe in pipes if pipe.right > -50]
    return visible_pipes

def draw_pipes(pipes):
    for pipe in pipes:
        if pipe.bottom >= 512:
            screen.blit(pipe_surface, pipe)
        else:
            flip_pipe = pygame.transform.flip(pipe_surface, False, True)
            screen.blit(flip_pipe, pipe)

def check_collision(pipes):
    global game_over
    for pipe in pipes:
        if bird_rect.colliderect(pipe):
            game_over = True
            return False
    if bird_rect.top <= -100 or bird_rect.bottom >= 450:
        game_over = True
        return False
    return True

def rotate_bird(bird):
    new_bird = pygame.transform.rotozoom(bird, -bird_movement * 3, 1)
    return new_bird

def score_display():
    score_surface = game_font.render(f'Score: {score}', True, (255, 255, 255))
    score_rect = score_surface.get_rect(center=(144, 50))
    screen.blit(score_surface, score_rect)

    high_score_surface = game_font.render(f'High Score: {high_score}', True, (255, 255, 255))
    high_score_rect = high_score_surface.get_rect(center=(144, 480))
    screen.blit(high_score_surface, high_score_rect)

    # game loop
    while True:
        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_SPACE and not game_over:
                    bird_movement = 0
                    bird_movement -= 6
                    flap_sound.play()
                if event.key == pygame.K_SPACE and game_over:
                    game_over = False
                    pipe_list.clear()
                    bird_rect.center = (50, 256)
                    bird_movement = 0
                    score = 0

        screen.blit(bg_surface, (0, 0))

        # check if player has passed through pipes
        if len(pipe_list) > 0:
            if bird_rect.left > pipe_list[0].rect.left and not pipe_list[0].passed:
                pipe_list[0].passed = True
                score_sound.play()

        # spawn pipes
        if len(pipe_list) == 0 or pipe_list[-1].rect.right < 100:
            pipe_list.extend(create_pipe())

        # bird movement
        bird_movement += gravity
        rotated_bird = rotate_bird(bird_surface)
        bird_rect.centery += bird_movement
        screen.blit(rotated_bird, bird_rect)

        # pipe movement
        pipe_list = move_pipes(pipe_list)
        draw_pipes(pipe_list)

        # score
        score += 0.01
        score_display()

        # update high score
        if score > high_score:
            high_score = score

        # collision
        check_collision(pipe_list)

        # floor movement
        screen.blit(floor_surface, (0, 450))
        if not game_over:
            game_speed += 0.002
        else:
            # display game over screen
            game_over_surface = game_font.render('Press SPACE to revive', True, (255, 255, 255))
            game_over_rect = game_over_surface.get_rect(center=(144, 256))
            screen.blit(game_over_surface, game_over_rect)

        # floor movement
        floor_x_pos = -100
        screen.blit(floor_surface, (floor_x_pos, 450))
        screen.blit(floor_surface, (floor_x_pos + 288, 450))
        if floor_x_pos <= -288:
            floor_x_pos = 0

        # update screen
        pygame.display.update()
        clock.tick(60)
        pygame.time.delay()
pygame.quit()
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
andypiper profile image
Andy Piper

Where in the code do you think you start the game?

I'm seeing you init the pygame library, then define a lot of functions, and then immediately call pygame.quit().

Collapse
 
citronbrick profile image
CitronBrick

please add
(triple backtick) python

before all your code blocks.
It will make reading easier for you & others.