I am using pygame to make a flappy bird game. Everything is going well but when I try to end the game my if statement is being completely ignored.
import pygame
import time
import sys
import random
import neat
#initialize pygame
pygame.init()
#sys vars
screen = pygame.display.set_mode((1080, 720)) #set width and height for game window {Try and find a
call for finding the comps defaults}
clock = pygame.time.Clock()
############
#game vars
Floor_x = 0
gravity = 0.25
bird_movement = 0
start = 0
PIPE_LIST = []
SPAWNPIPE = pygame.USEREVENT
pygame.time.set_timer(SPAWNPIPE, 1200)
Pipe_Height = [500, 550, 600, 650, 700, 750]
game_active = True;
###########
#load surfaces
Bg_day = pygame.image.load('Assets/background-day.png').convert() #create a surface to be displayed
on the display surface
Bg_day = pygame.transform.scale(Bg_day, (1080, 720))
Bg_night = pygame.image.load('Assets/background-night.png').convert()
Bg_night = pygame.transform.scale(Bg_night, (1080, 720))
Bg_floor = pygame.image.load('Assets/base.png').convert()
Bg_floor = pygame.transform.scale(Bg_floor, (1080, 720))
Game_Anims_Bird_midFlap = pygame.image.load('Assets/redbird-midflap.png').convert()
Game_Anims_Bird_midFlap = pygame.transform.scale2x(Game_Anims_Bird_midFlap)
Bird_HitBox = Game_Anims_Bird_midFlap.get_rect(center = (100,355))
Bg_ToUse = Bg_day
Game_Pipes_Red = pygame.image.load('Assets/pipe-red.png').convert()
Game_Pipes_Red = pygame.transform.scale2x(Game_Pipes_Red)
############
#FUNCTIONS
def Draw_Bg(x):
screen.blit(Bg_ToUse,(0,0)) #display our created surface in the specified coords
screen.blit(Bg_floor,(x,650))
screen.blit(Bg_floor,(x + 1080,650))
if x <= -1080:
x = 0
def CreatePipe():
PipeHeight = random.choice(Pipe_Height)
top_pipe = Game_Pipes_Red.get_rect(midtop = (1090,PipeHeight))
bottom_pipe = Game_Pipes_Red.get_rect(midbottom = (1090,PipeHeight - 225))
return bottom_pipe, top_pipe
def Pipe_Movement(Pipes):
for pipe in Pipes:
pipe.centerx -= 5
return Pipes
def Draw_Pipes(Pipes):
for pipe in Pipes:
if pipe.bottom >= 720:
screen.blit(Game_Pipes_Red, pipe)
else:
screen.blit(pygame.transform.flip(Game_Pipes_Red, False, True), pipe)
def CollisionDetection(Pipes):
for pipe in Pipes:
if Bird_HitBox.colliderect(pipe):
game_active = False
print('Game over')
if Bird_HitBox.centery >= 720 or Bird_HitBox.centery <= -30:
print('out of bounds')
game_active = False
############
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:
if start == 0 and game_active == True:
start += 1
print('game start')
bird_movement = 0
bird_movement -= 7
if event.key == pygame.K_RETURN:
if Bg_ToUse == Bg_day:
Bg_ToUse = Bg_night
else:
Bg_ToUse = Bg_day
if event.type == SPAWNPIPE and start != 0 and game_active == True:
if start != 0:
PIPE_LIST.extend(CreatePipe())
if game_active != False: #this is the problematic statement it will
#run even when game_active is set to false
if start != 0: # game is active d raw everything
Floor_x -= 1;
bird_movement += gravity
Bird_HitBox.centery += bird_movement
Draw_Bg(Floor_x)
screen.blit(Game_Anims_Bird_midFlap,Bird_HitBox)
CollisionDetection(PIPE_LIST)
PIPE_LIST = Pipe_Movement(PIPE_LIST)
Draw_Pipes(PIPE_LIST)
if Floor_x <= -1080:
Floor_x = 0
pygame.display.update()
clock.tick(120)
else: #draw floor and bird
Draw_Bg(Floor_x)
screen.blit(Game_Anims_Bird_midFlap,Bird_HitBox)
Floor_x -= 1;
pygame.display.update()
clock.tick(120)
#floor function to make it go forever
else:
print('game_active == false')
PIPE_LIST = []
bird_movement = 0
while game_active == False:
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:
PIPE_LIST = []
The issue is when the bird collides with a pipe or goes out of bounds, game_active is set to false but the if statement still runs.
edit - added a comment where the problematic if statement is