0

I wanted to make a game where you could pick up items and use them, and I used the colliderect function to detect the collision, but the function is returning true even when they aren't colliding at all. I think the get_rect() function is the problem here, but I don't know for sure. How can I fix this?

Code:

import pygame, os
from data.scripts.playerclass import Player
from data.scripts.images import ghost_imgs, button_imgs, item_imgs
from data.scripts.button import Button
from data.scripts.items import Item
pygame.init()

clock = pygame.time.Clock()

RED = (255, 0, 0)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
COLORONE = (235, 100, 110)

WIDTH, HEIGHT = 900, 400
font = pygame.font.SysFont('arial', 20)
#Creating Buttons
start_button = Button(300,170,button_imgs['start_button'],3)
exit_button = Button(300,275,button_imgs['exit_button'],3)
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Ghost Game - BETA 0.1")
p = Player(ghost_imgs['notghost'], 80, 90)
I = Item(item_imgs['book'], 30, 30, 1)
def redrawGameWindow():
    win.fill((COLORONE))


def main():
    run = True
    ghoststage = False
    start_clicked = False
    while run:
        playerbox = p.img.get_rect()
        itembox = I.img.get_rect()
       
        clock.tick(30)

        # KEY PRESS ACTIONS
        keys = pygame.key.get_pressed()
        if keys[pygame.K_a]:
            p.x -= 3
        if keys[pygame.K_d]:
            p.x += 3
        if keys[pygame.K_s]:
            if(ghoststage):
                p.y += 3
        if keys[pygame.K_w]:
            if(ghoststage):
                if(p.y > -1):
                    p.y -= 3

        if start_clicked == False:
           #drawing button if it hasn't been clicked yet
          if start_button.draw(win):
                #Drawing Character if Button is Clicked
            win.blit(I.img, (I.x, I.y))
            win.blit(p.img, (p.x, p.y))
            start_clicked = True
        else:
              win.blit(I.img, (I.x, I.y))
              win.blit(p.img, (p.x, p.y))
                
        if start_clicked == False:
            if exit_button.draw(win):   
                run = False      
              
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_g:
                    if not ghoststage:
                        ghoststage = True
                        p.changeImg(ghost_imgs['ghost'])
                    else:
                        ghoststage = False
                        p.changeImg(ghost_imgs['notghost'])
                if event.key == pygame.K_p:
                    if not ghoststage:
                        collide = playerbox.colliderect(itembox)
                        if collide:
                            print('hello')


        pygame.display.update()  

        redrawGameWindow()

    pygame.quit()

if __name__ == "__main__":
    main()

0 Answers0