0

I have two sprites, each an instance of class player. I have a function that checks if they have collided on line 28. The function will trigger when the sprites aren't actually touching, as shown in the included code snippet. I have heard that this might be the result of the png image being larger than what is actually shown. Could this be the problem, and is there a fix?

Code:

import pygame
pygame.init()
window = pygame.display.set_mode((500, 500))

class Player(pygame.sprite.Sprite):
  def __init__(self, image, where):
    super().__init__()
    self.image = image
    self.rect = self.image.get_rect()
    self.rect.topleft = where

player_1 = Player(pygame.image.load("WuDwZ.png"), (150, 150))
player_2 = Player(pygame.image.load("WuDwZ.png"), (65, 155))

sprite_group_1 = pygame.sprite.Group()
sprite_group_1.add(player_1)

sprite_group_2 = pygame.sprite.Group()
sprite_group_2.add(player_2)

game_loop = True
while game_loop == True:

  def collide():
    if pygame.sprite.spritecollide(player_2, sprite_group_1, False):
      print('hello')

  window.fill((0, 0, 0))
  sprite_group_2.draw(window)
  sprite_group_1.draw(window)
  collide()
  pygame.display.update()

PNG that I am using:

the popo
  • 49
  • 3

0 Answers0