1 回答

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超11個(gè)贊
這是因?yàn)槟膇f / elif / else邏輯有些混亂。您有KEYDOWN事件的兩個(gè)路徑。如果第一個(gè)事件恰好是移動(dòng)命令,則您的代碼將首先運(yùn)行彈丸代碼(沒(méi)有找到有效的彈丸命令),然后嘗試引用不存在的彈丸對(duì)象。嘗試以下方法:
#---------- MAIN PROGRAM LOOP ----------#
while not done:
# --- Event processing
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
projectile = None # initialize
if event.key == pygame.K_UP:
projectile = ProjectileUp("LightningUp.png")
projectile.rect.x = player.rect.x+65
projectile.rect.y = player.rect.y
elif event.key == pygame.K_DOWN:
projectile = ProjectileDown("LightningDown.png")
projectile.rect.x = player.rect.x+65
projectile.rect.y = player.rect.y+100
elif event.key == pygame.K_LEFT:
projectile = ProjectileLeft("LightningLeft.png")
projectile.rect.x = player.rect.x+35
projectile.rect.y = player.rect.y+100
elif event.key == pygame.K_RIGHT:
projectile = ProjectileRight("LightningRight.png")
projectile.rect.x = player.rect.x+115
projectile.rect.y = player.rect.y+100
添加回答
舉報(bào)