我正在导入 pixelperfect 并使用它的一个名为 check_collision(object1, object2) 的函数,这是我从http://www.pygame.org/wiki/FastPixelPerfect获得的网站, 当我使用它总是返回 false 的函数时,我不知道为什么。如果有人可以帮助我或有更好的方法来进行像素完美碰撞,我将不胜感激。
它总是返回 false 这就是问题所在。
这是代码:
import pygame, pixelperfect
from pygame.locals import *
from pixelperfect import *
screen = pygame.display.set_mode([640, 480])
screen.fill([255,255,255])
pygame.display.flip()
class Ball(object):
def __init__(self, picture, speed, loc):
self.image = pygame.image.load(picture)
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = loc
self.speed = speed
def move(self):
self.rect = self.rect.move(self.speed)
if self.rect.left < 0 or self.rect.right > screen.get_width():
self.speed[0] = - self.speed[0]
elif self.rect.top < 0 or self.rect.bottom > screen.get_height():
self.speed[1] = - self.speed[1]
ball = Ball("beach_ball.png", [5,5], [0,0])
ball.rect.center = [80, 50]
ball2 = Ball("beach_ball.png", [-5,-5], [200, 200])
ball2.rect.center = [100, 200]
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill([255,255,255])
ball.move()
ball2.move()
if check_collision(ball, ball2):
print 'hit'
screen.blit(ball.image, ball.rect)
screen.blit(ball2.image, ball2.rect)
pygame.display.flip()
clock.tick(30)
pygame.quit()