所以,我正在开发一个基本的 Pygame,其中一个机制是让子弹精灵跟随鼠标并爆炸。但我只有两个错误。
BUG 1:删除子弹会删除 bullet_list 中的所有子弹。我理解为什么会发生这种情况,但我不知道如何纠正它。解决了
BUG 2:移动鼠标似乎覆盖了按键检测。移动鼠标时不能移动或射击子弹。
更新
#Checking Keys
for event in pygame.event.get():
print("CAKE")
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
pPlayer.image = pDown
pPlayer.chy = 2
elif event.key == pygame.K_UP:
pPlayer.image = pUp
pPlayer.chy = -2
elif event.key == pygame.K_LEFT:
pPlayer.image = pLeft
pPlayer.chx = -2
elif event.key == pygame.K_RIGHT:
pPlayer.image = pRight
pPlayer.chx = 2
elif event.key == pygame.K_SPACE:
if pPlayer.canFire == True:
bullet = Bullet()
bullet.rect.x = int(player.x)
bullet.rect.y = int(player.y)
all_sprites_list.add(bullet)
bullet_list.add(bullet)
pPlayer.canFire = False
elif event.key == pygame.K_ESCAPE:
if gameState.togglePause == True:
if gameState.pause == True:
gameState.game = True
gameState.pause = False
elif gameState.pause == False:
gameState.game = False
gameState.pause = True
gameState.togglePause = False
elif event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN or event.key == pygame.K_UP:
pPlayer.chy = 0
elif event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
pPlayer.chx = 0
elif event.key == pygame.K_SPACE:
pPlayer.canFire = True
elif event.key == pygame.K_ESCAPE:
gameState.togglePause = True
else:
pPlayer.chx = 0
pPlayer.chy = 0
新错误!我对 pygame.event.get() 不熟悉,我可能在这里做错了,但 pygame 文档提供的示例并没有太大帮助。我不知道它是否修复了多个事件的处理,因为它似乎甚至没有注册它们。代码很少打印出“蛋糕”,而且非常延迟,当我击中空间时,很少会发射任何子弹。