0
\$\begingroup\$

So, I am making Flappy Bird and when the player dies I have the spacebar assigned to reset the game. However, it's not working and I truly have no idea why (after like 3 hours of going through tutorials and rereading the code).

import pygame
import random

# Initiating pygame
pygame.init()

# Variables:
FPS = 60

WHITE = (255, 255, 255)

ORIGIN = (0, 0)
ground_X_pos = 0

run = True
not_collided = True

BIRD_STARTING_POS = (50, 280)
bird_pos = 0
gravity = 0.3

pipe_list = []
available_pipe_height = [300, 310, 320, 330, 340, 350,
                         360, 370, 380, 390, 400, 410, 420, 430, 440, 450]
SPAWN_PIPE = pygame.USEREVENT

GAP = 200

# Setting the display window
pygame.display.set_caption("Crappy Bird")

SCREEN_DIMENSIONS = (352, 600)
SCREEN = pygame.display.set_mode(SCREEN_DIMENSIONS)

# Loading required images as surfaces
bg = pygame.image.load("assets/sprites/background-day.png").convert()
bg = pygame.transform.scale(bg, SCREEN_DIMENSIONS)

ground = pygame.image.load("assets/sprites/base.png").convert()
ground = pygame.transform.scale(ground, (352, 75))

bird = pygame.image.load("assets/sprites/yellowbird-midflap.png").convert()
bird = pygame.transform.scale(bird, (50, 34))

pipe = pygame.image.load("assets/sprites/pipe-red.png").convert()

_game_over = pygame.image.load("assets/sprites/gameover.png")
_game_over = pygame.transform.scale(_game_over, (352, 77))

# Loading required rectangles
bird_rect = bird.get_rect(center=BIRD_STARTING_POS)

# Spawning pipes
pygame.time.set_timer(SPAWN_PIPE, 1010)


# This will load all the required surfaces of entities
def update(_pipe_list):
    # Loading in background surfaces
    SCREEN.blit(bg, ORIGIN)

    # Loading in the bird
    SCREEN.blit(bird, bird_rect)

    # Loading in the pipes
    for _pipe in _pipe_list:
        if _pipe.bottom >= 600:
            SCREEN.blit(pipe, _pipe)
        else:
            _pipe_ = pygame.transform.flip(pipe, False, True)
            SCREEN.blit(_pipe_, _pipe)

    # Loading in the ground
    SCREEN.blit(ground, (ground_X_pos, 525))
    SCREEN.blit(ground, (ground_X_pos + 352, 525))


# This will spawn in pipes
def spawn_pipe():
    seed = random.choice(available_pipe_height)

    bottom_pipe = pipe.get_rect(midtop=(360, seed))
    top_pipe = pipe.get_rect(midbottom=(360, seed - GAP))

    return bottom_pipe, top_pipe


# Puts the pipe in their correct positions
def pipe_pos(_pipe_list):
    for _pipe in _pipe_list:
        _pipe.centerx -= 5

    return _pipe_list


# This checks if the bird has collided with anything or not
def collision_checker(_pipe_list):
    for _pipe in _pipe_list:
        if bird_rect.colliderect(_pipe):
            return False
        else:
            pass

        if bird_rect.bottom >= 525 or bird_rect.top <= 0:
            return False
        else:
            pass

    return True


def game_over():
    SCREEN.fill(WHITE)
    SCREEN.blit(_game_over, (0, 261.5))


# This is the main loop for running the game
while run:
    # Setting a fixed frame rate
    pygame.time.Clock().tick(FPS)

    # Checking if the game needs to be closed
    for event in pygame.event.get():  # pygame.event.get() gets all events occurring (like key press, mouse clicks, etc)
        if event.type == SPAWN_PIPE:
            pipe_list.extend(spawn_pipe())
        else:
            pass

        if event.type == pygame.QUIT:
            run = False
        else:
            pass

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and not_collided:
                bird_pos = 0
                bird_pos -= 8.25
            else:
                pass

            if event.type == pygame.K_SPACE:
                bird_pos = 0
                bird_rect.center = BIRD_STARTING_POS
                print("Debug : This works.")
                pipe_list.clear()

                not_collided = True
            else:
                pass
        else:
            pass

    # Running the game
    if not_collided:
        update(pipe_list)
        ground_X_pos -= 5

        if ground_X_pos <= -352:
            ground_X_pos = 0
        else:
            pass

        bird_pos += gravity
        bird_rect.centery += bird_pos

        pipe_list = pipe_pos(pipe_list)

        not_collided = collision_checker(pipe_list)

    #Ending the game
    if not not_collided:
        game_over()

    pygame.display.update()

pygame.quit()

It gets stuck on this screen.

help_me_please

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

if event.type == pygame.K_SPACE: is incorrect, you want to check against event.key like in the code you have written above. You'll probably want to also check and run the resetting code only if there has been a collision detected.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.