2016-05-29 34 views
0

我正在爲學校做一個python編碼項目,我需要編寫一個pygame來從特定的位置發射子彈。Pygame Bullets(python 3.2.5)

我使用此代碼試過,

if (event.type == pygame.KEYDOWN):  
     if (event.key == pygame.K_SPACE):  
      screen.blit(projectile,(projectilex,projectiley)) 

彈丸的x座標等於玩家的x座標,它從拍攝。所以像:

projectilex = playerx 

它只出現在屏幕上1秒然後消失。我需要從玩家的x開始,並將它的值增加到y,直到它達到屏幕的頂部,然後消失。任何幫助?

注:我必須使用python 3.2.5,我無法升級到更新的版本。

回答

1

screen.blit只顯示單幀持續時間的內容。你需要畫出每一幀的子彈。使用單獨的變量來跟蹤項目符號的X和Y位置以及是否顯示項目符號。例如,

if (event.type == pygame.KEYDOWN):  
    if (event.key == pygame.K_SPACE):  
     show_projectile = True 
     projectile_x = player_x 
     projectile_y = player_y 
# ... 
if show_projectile: 
    screen.blit(projectile, (projectile_x, projectile_y)) 
0

嘗試在你的代碼中插入這樣的:

pygame.key.set_repeat (1, 50) 

這允許代碼repeate KEY_DOWN檢查:1 =延遲,50 =時間重複

所以:

pygame.key.set_repeat (1, 50) 

if (event.type == pygame.KEYDOWN):  
     if (event.key == pygame.K_SPACE):  
      screen.blit(projectile,(projectilex,projectiley))