【发布时间】:2012-01-11 15:42:17
【问题描述】:
我的目标是让我的程序能够在球精灵撞击任何一个“g_ball”精灵时检测到碰撞。代码明显检测到碰撞,我有“打印”语句来测试它……但即使没有任何精灵接触,它也会不断地打印“进度”。代码如下:
while 1:
screen.blit(background, (0,0))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_a:
m_x = -4
s+=1
elif event.key == K_d:
m_x = +4
s2+=1
if event.type == KEYUP:
if event.key == K_a:
m_x = 0
elif event.key == K_d:
m_x = 0
x+= m_x
y+= m_y
ball = pygame.sprite.Sprite()
ball.image = pygame.image.load('red_ball.png').convert()
ball.rect = ball.image.get_rect()
ball.image.set_colorkey((white))
screen.blit(ball.image,(x,y))
if x > 640:
x = 0
if x < 0:
x = 640
g_ball = pygame.sprite.Sprite()
g_ball.image = pygame.image.load('green_ball.png').convert()
g_ball.rect = g_ball.image.get_rect()
g_ball.image.set_colorkey(white)
screen.blit(g_ball.image,(50,t))
t+=5
if t > 521:
t = 0
collision = pygame.sprite.collide_rect(ball, g_ball)
if collision == True:
print ('PROGRESS!!!')
【问题讨论】:
标签: python pygame collision sprite