I have finished coding my flappy bird remake and now I've gotten to the process of tinkering with my code and trying to get rid of bugs. But there seems to be a problem with my collisions. I have coded it so that when a collision has been detected between the player and the obstacle it kills the program. But I am now faced with the issue of when the program is running and player is between two obstacles not even close the program kills itself. But it doesn't always happen. Only like 50% of the time. Which is strange. Can you have look at my collisions and point out to me what I have done wrong. This is my first attempt and project with pygame, so I am not the best.
#Imports
import sys, pygame
from pygame import mixer
from pygame.locals import *
import time
import random
from random import randint
import time
#Built in pygame functions + Stuff
pygame.init()
pygame.mixer.init()
pygame.display.set_caption('Floppy Disk')
FRAMERATE = 60
clock = pygame.time.Clock()
#Screen Stuff
Screen = pygame.display.set_mode((500,750))
background_image = pygame.image.load("circuit.jpeg").convert()
#Sound Track
##playlist = list()
##playlist.append("Midnight City.mp3")
##playlist.append("Midnight City.mp3")
##playlist.append("Midnight City.mp3")
##
##pygame.mixer.music.load(playlist.pop())
##pygame.mixer.music.queue(playlist.pop())
##pygame.mixer.music.set_endevent(pygame.USEREVENT)
##pygame.mixer.music.play()
#Floppy Disk
disk = pygame.image.load("floppy1.jpg")
disk_x = 50
disk_y = 300
disk_y_change = 0
score = 0
oldscore = [0]
Score_font = pygame.font.Font('freesansbold.ttf', 32)
def display_disk(x, y):
Screen.blit(disk, (x,y))
Obstacle_Width = 70
Obstacle_Height = random.randint(150, 450)
Obstacle_colour = (0,200,10)
Obstacle_x_change = -4
Obstacle_x = 500
def display_obstacle(height):
pygame.draw.rect(Screen, Obstacle_colour, (Obstacle_x, 0, Obstacle_Width, height))
bottom_y = height+200
bottom_height = 635-bottom_y
pygame.draw.rect(Screen, Obstacle_colour, pygame.Rect(Obstacle_x, bottom_y, Obstacle_Width, bottom_height))
def Collision (Obstacle_x, Obstacle_Height, disk_y, bottom_height):
if Obstacle_x >= 70 and Obstacle_x <= (40 + 64):
if disk_y <= (Obstacle_Height -30) or disk_y >= (bottom_height - 34):
return True
return False
def Score(score):
display = Score_font.render(f"Score: {score}", True, (181, 26, 18, 0.86))
display1 = Score_font.render(f"OldScore: {oldscore[0]}", True, (181, 26, 18, 0.86))
Screen.blit(display, (10,10))
Screen.blit(display1, (10,35))
#Shutdown of code
shuton = True
while shuton:
Screen.fill((0,0,0))
Screen.blit(background_image, (0,0))
clock.tick(60)
print(disk_y)
for event in pygame.event.get():
if event.type == pygame.QUIT:
shuton = False
sys.exit()
pygame.quit()
##
## if event.type == pygame.USEREVENT:
## if len(playlist) > 0:
## pygame.mixer.music.queue(playlist.pop())
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
disk_y_change = -8
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
disk_y_change = 3
disk_y += disk_y_change
if disk_y <= 0:
disk_y = 0
if disk_y >= 571:
bird_y = 571
Obstacle_x += Obstacle_x_change
if Obstacle_x <= -10:
Obstacle_x = 500
Obstacle_Height = random.randint(200, 400)
score += 1
display_obstacle(Obstacle_Height)
# if disk.rect.colliderect(obstacle)
collision = Collision(Obstacle_x, Obstacle_Height, disk_y, Obstacle_Height + 150)
if collision:
oldscore.append(score)
print(f"Score was: {score}")
pygame.quit()
sys.exit()
display_disk(disk_x, disk_y)
Score(score)
pygame.display.update()
pygame.quit()