我想制作碰撞检测脚本。在
当我运行脚本时Pygame总是说图像发生冲突。在
当我使用{“rect = cat1.img.get_rect()” then “rect.colliderect(another rect of other image)}时,它会打印出”crash”,而且这两种方法都是这样。在
我在两个精灵中使用了cat.png。在
问题
cat.png不好吗?在
我没有使用正确的脚本吗?在
我的电脑奇怪吗?在
这是我的脚本和cat.png。在

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)`
Original: https://blog.csdn.net/weixin_31486289/article/details/113507774
Author: 徐佳昇
Title: python判断图片类型_pygame图像Python碰撞检测(图像类型:png)
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/782211/
转载文章受原作者版权保护。转载请注明原作者出处!