0

i was working on a dino chrome clone and i keep running into this issue where colliderect returns true or prints collision way too soon , here's the code below i cant seem to see what the issue is?

import pygame
import math

pygame.init()

fps_clock = pygame.time.Clock()

WINDOW_WIDTH = 1000
WINDOW_HEIGHT = 200

gameState = False
ground_sprite = pygame.image.load("assets/ground.png")
player_character = pygame.image.load("assets/dino.png")
ground_width = ground_sprite.get_width()
ground_sprite = pygame.transform.scale(ground_sprite, (ground_width, WINDOW_HEIGHT))
cactus_sprite = pygame.image.load("assets/cactus.png")
player_character_rect = player_character.get_rect(midbottom=(200, 265))
cactus_rect = cactus_sprite.get_rect(midtop=(800, 180))
scroll = 0
tiles = math.ceil(WINDOW_WIDTH / ground_width) + 2
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("dino-game-clone")


while not gameState:
fps_clock.tick(10)

for i in range(tiles):
    screen.blit(ground_sprite, (i * ground_width + scroll - ground_width, 0))
scroll -= 10
if abs(scroll) > ground_width:
    scroll = 0

screen.blit(player_character, player_character_rect)
screen.blit(cactus_sprite, cactus_rect)

cactus_rect.x -= 7
print(player_character_rect.colliderect(cactus_rect))

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        gameState = True
pygame.display.flip()
pygame.quit()

any help would be appreciated

  • 4
    Perhaps the images are not completely cropped. Get_rect returns a rect with the width and height of the surface, not the width and height of the visible area. Perhaps you have a bunch of transparent pixels bordering your sprites. – Starbuck5 Sep 29 '22 at 16:58
  • oh thanks lmao i didnt even notice that, its working now thank u :D –  Sep 29 '22 at 17:06

0 Answers0