I want to make the Collision Detection scripts.
When I run my scripts Pygame
always say the images are in collision.
It prints "crash" when I use {"rect = cat1.img.get_rect()" then "rect.colliderect(another rect of other image)}
and this way both.
I used cat.png
in two sprites.
Questions
Is cat.png
bad?
Am I not using the right scripts?
Is my computer weird?
Here are my scripts and the cat.png.
import pygame, sys, time
from pygame.locals import *
pygame.init()
Fps = 100
fpsClock = pygame.time.Clock()
Displaysurf = pygame.display.set_mode((300, 300))# full is 1900, 1000
pygame.display.set_caption('Animation')
white = (255, 255, 255)
class cat:
def __init__(self, x, y):
self.img = pygame.image.load('cat.png')
self.x = x
self.y = y
self.rect = self.img.get_rect()
def draw(self):
Displaysurf.blit(self.img, (self.x, self.y))
def colde(self, sprite1, sprite2):
col = pygame.sprite.collide_rect(sprite1, sprite2)
if col == True:
print("crash!")
cat1 = cat(10, 10)
cat2 = cat(100, 100)
while True:
Displaysurf.fill(white)
cat1.draw()
cat2.draw()
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
cat1.y -= 3
if keys[pygame.K_DOWN]:
cat1.y += 3
if keys[pygame.K_LEFT]:
cat1.x -= 3
if keys[pygame.K_RIGHT]:
cat1.x += 3
cat1.colde(cat1, cat2)
pygame.display.update()
fpsClock.tick(Fps)`