0

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.

R_C
  • 35
  • 5
  • Read about [PyGame collision with masks](https://stackoverflow.com/questions/57455811/pygame-collision-with-masks/57499484#57499484) and [How to get the correct dimensions for a pygame rectangle created from an image](https://stackoverflow.com/questions/65361582/how-to-get-the-correct-dimensions-for-a-pygame-rectangle-created-from-an-image/65361896#65361896). – Rabbid76 Feb 12 '22 at 07:13
  • it works good for me, i have changed the images to pygame.Surface because I don't have those images – Pratik Agrawal Feb 12 '22 at 10:12
  • @Pratik Agrawal, thanks it was probably a mistake with the image I used for my zombie. I changed that to a pygame.Surface and it worked just fine. So instead of using my previous image, I'm just using a surface filled with purple. However, is it possible for me to make that surface from a rectangle to a circle? – R_C Feb 12 '22 at 15:10
  • No. Read [How do I detect collision in pygame?](https://stackoverflow.com/questions/29640685/how-do-i-detect-collision-in-pygame/65064907#65064907) – Rabbid76 Feb 12 '22 at 15:12
  • While displaying the rect do pygame.draw.rect(screen, color, rect, 0,25). Add the corner radius and that should change the rectangle to circle, although there are ways to draw a circle and for that check the documentation – Pratik Agrawal Feb 13 '22 at 16:10

0 Answers0