0

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()
  • 1
    just use `pygame.Rect` and its method `.colliderect`, hard to point out the issue right now (partially because you have not provided a [mre]) also you have way too many arbitrary integers floating around – Matiiss Oct 19 '21 at 20:27
  • I highly recommend learning how to use a GUI debugger (like the free one in [PyCharm](https://www.jetbrains.com/help/pycharm/debugging-your-first-python-application.html)) to debug your own code. You're able to put a breakpoint on any line, such as the line of code where collision happens, and then inspect the values of all the variables at that point, as well as trace the path the code took to get to that point. If you don't know how to debug your own code then you're going to be at a huge disadvantage. – Random Davis Oct 19 '21 at 20:40
  • @Matiiss is there a documentation you can link me for the pygame.rect and the .colliderect method – KGGaming Fluff Oct 19 '21 at 20:42
  • 1
    @KGGamingFluff you didn't just google "pygame rect"? The documentation for both is the top result. Please at least try to put in some effort, otherwise people will be less likely to help you out: https://www.pygame.org/docs/ref/rect.html – Random Davis Oct 19 '21 at 20:45
  • @RandomDavis I looked at the pygame documentation I just wasn't understanding it. And I was wanting to see if he has one he recommended. – KGGaming Fluff Oct 19 '21 at 21:02
  • @KGGamingFluff if you don't understand something specific in the docs you can search that specific thing up (also pygame docs provide this option anyways for searching for examples) and you are likely to find some explanatory example or a tutorial that focuses on that or some other specific part of the documentation that may help you, and I recommend the official documentation (at least regarding the `pygame` library) – Matiiss Oct 19 '21 at 21:07

1 Answers1

0

This isn't an answer to your question but it should help you understand pygame.Rect object and how to use pygame.colliderect function.

import pygame
from pygame import Rect
from pygame import Vector2

pygame.init()
PDR = Rect(0, 0, 1024, 640)
PDS = pygame.display.set_mode(PDR.size)

# define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (128, 0, 0)

# create a pygame rect (size: 100x100) object in the center of the primary display surface
R1 = Rect(Vector2(PDR.center) - (50, 50), (100, 100))

exitDemo = False
while not exitDemo:
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            exitDemo = True

    mp = Vector2(pygame.mouse.get_pos()) # get the mouse location 
    
    # create pygame rect object (size: 100x100) at the mouse location
    R2 = Rect(mp - (50, 50), (100, 100))
    
    # check for collision. If true both squares will turn red
    if R1.colliderect(R2):
        color = RED
    else:
        color = WHITE

    # clear the display
    PDS.fill(BLACK)
    pygame.draw.rect(PDS, color, R1, 1) # draw a square using the R1 rect (center po)
    pygame.draw.rect(PDS, color, R2, 1) # draw a square using the R2 rect (mouse pos)

    # update the primary display surface
    pygame.display.update()

pygame.quit()
cookertron
  • 188
  • 12