I am trying to create a game using pygame in which there is a treasure in the middle, and zombies are trying to attack it.
Here's my code:
import pygame
from sys import exit
from random import randint
pygame.init()
clock = pygame.time.Clock()
screen_width = 1000
screen_height = 660
screen = pygame.display.set_mode((screen_width, screen_height))
# Treasure
treasure = pygame.image.load(r"file path").convert_alpha()
treasure_surf = pygame.transform.scale(treasure, (10, 10))
treasure_rect = treasure_surf.get_rect(center = (500, 330))
# Zombie
zombie_vel = 1
zombie = pygame.image.load(r"file path").convert_alpha()
zombie_surf = pygame.transform.scale(zombie, (100, 100))
zombie_rect = zombie_surf.get_rect(center = (randint(0, 1000), randint(0, 600)))
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
    if zombie_rect.centerx < 500:
        zombie_rect.centerx += zombie_vel
    if zombie_rect.centerx > 500:
        zombie_rect.centerx -= zombie_vel
    if zombie_rect.centery < 330:
        zombie_rect.centery += zombie_vel
    if zombie_rect.centery > 330:
        zombie_rect.centery -= zombie_vel
    if zombie_rect.colliderect(treasure_rect):
        zombie_rect.centerx = randint(0, 1000)
        zombie_rect.centery = randint(0, 600)
    screen.fill('DarkGreen')
    screen.blit(treasure_surf, treasure_rect)
    screen.blit(zombie_surf, zombie_rect)
        
    pygame.display.update()
    clock.tick(60)
In order to detect the collision of the zombie and treasure, I used the colliderect() method, however, it triggers before the two rectangles acutally collide.
Is there an issue with my code, because I wasn't able to find one with my images? Thanks in advance.