1

I am having the issue that, my colliders aren't accurate. No matter how I tweak the numbers, they wont work. The eagle, the birds collider is accurate, but the collider of the box/obstacle isn't. The rectangle I use as a collider is the same size as the image of the box, and should be centered on it, but for some reason, it isn't.

import pygame as pg # imports life juice

eagleImage = pg.image.load("playerflappybirdscaled.png") # loads fucking eagle image
eagleCollider = pg.Surface.get_bounding_rect(eagleImage) # better collider

boxImage = pg.image.load("boxcollider.png") # box image go load
boxCollider = pg.Surface.get_bounding_rect(boxImage) # makes box rect

class Box(object): # makes box
    def __init__(self, x, y):
        self.image = boxImage # image of a box
        self.collider = boxCollider # collider for said box

        self.x = x # box xpos
        self.y = y # box ypos
        self.collider.center = self.x, self.y

    def checkCollision(self, rect): # checks for collision
        self.collider.center = self.x, self.y # sets collider
        colliderToCheckFor = rect # gets other collider
        collision = boxCollider.colliderect(colliderToCheckFor) # checks for collision
        if collision: # returns car crash
            return True
        else:
            return False

    def draw(self, surface):
        surface.blit(self.image, (self.x, self.y))



class Bird(object): # bird go flyyyyyyyyyyy
    def __init__(self):
        self.image = eagleImage # bird go display
        self.collider = eagleCollider # bird gets collider

        self.x = 320 # bird posx
        self.y = 200 # bird posy

    def key_handler(self, moveDist, direction): # manhandles keys
        key = pg.key.get_pressed() # gets manhandled keys
        dist = 2 # distance in FREEDOM UNITS jk

        if key[pg.K_UP]: # key go up
            self.y -= dist
        elif key[pg.K_SPACE]: # key go up
            self.y -= dist
        elif key[pg.K_w]: # key go up
            self.y -= dist
        elif key[pg.K_a]: # key go left
            self.x -= dist

        if moveDist == 0: # checks if distance moved is wanted
            return # if not returns
        else:
            if direction == "y": # checks direction wanted
                self.y += moveDist # does shit
            elif direction == "x":
                self.x += moveDist

    def draw(self, surface): # toddler draws image
        surface.blit(self.image, (self.x, self.y))
        self.collider.center = self.x, self.y 

pg.init() # starts pygame
s = pg.display.set_mode((640, 400)) # sets screen

box = Box(320, 50)
bird = Bird() # biiiiiird
clock = pg.time.Clock() # clock for fps and shit

running = True # if runs
while running: # when runs
    event = pg.event.poll() # get event
    if event.type == pg.QUIT: # if not run go quit
        pg.quit()
        running = False
        break

    bird.key_handler(1, "y") # bird mauls keys

    s.fill((255, 0, 0)) # give screen color

    box.draw(s)

    bird.draw(s) # take drawing from toddler

    pg.display.update() # put drawing on display

    if box.checkCollision(eagleCollider) == True:
        running = False
        pg.quit()

    clock.tick(60) # tick tack

So as I have said, I have tried tweaking every number, and I see no reason why the colliders wouldn't be accurate.

Edit to highlight the difference between questions this one has been associated with. I have actually followed and implemented answers from one of these questions to the best of my ability, and as far as I can tell, I have followed what was answered there.

1 Answers1

0

See Why is my collision test always returning 'true' and why is the position of the rectangle of the image always wrong (0, 0)?.

The instruction surface.blit(self.image, (self.x, self.y)) draws self.image at the topleft position (self.x, self.y):

class Box(object): # makes box
    def __init__(self, x, y):
        self.image = boxImage # image of a box
        self.rect = self.image.get_rect(topleft = (x, y))
        self.collider = self.image.get_bounding_rect()
        self.collider.x += x
        self.collider.y += y

    def checkCollision(self, rect): # checks for collision
        collision = self.collider.colliderect(rect)
        return collision

    def draw(self, surface):
        surface.blit(self.image, self.rect)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174