使用精靈作為影像的姿勢創建一個太空入侵者風格的游戲 - 試圖讓子彈在螢屏上不斷向上移動,直到它檢測到它通過 y = 0。我有一個函式可以將子彈向上移動,但是每個 MOUSEBUTTONDOWN 事件僅檢測 5 個像素。理想情況下,它需要在只觸發一個事件后繼續移動到螢屏頂部。
這是有關子彈類和事件回圈的相關代碼。
# bullet class
class Bullet(pygame.sprite.Sprite):
def __init__(self,path):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(path)
self.rect = self.image.get_rect()
def shoot(self,pixels):
self.rect.y -= pixels
def track(self):
bullet.rect.x = player.rect.x 23
和事件回圈:
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.moveLeft(5)
if event.key == pygame.K_RIGHT:
player.moveRight(5)
if event.type == pygame.MOUSEBUTTONDOWN:
bullet.shoot(5)
bullet.track()
playerGroup.update()
gameDisplay.blit(backgroundImg,(0,0))
playerGroup.draw(gameDisplay)
bulletGroup.update()
bulletGroup.draw(gameDisplay)
pygame.display.update()
clock.tick(60)
感謝任何回答我問題的人。
uj5u.com熱心網友回復:
您可以shot在專案符號類中添加一個附加屬性。它最初設定為0并在shoot呼叫方法時更新。然后該track方法相應地更新y子彈的位置。
class Bullet(pygame.sprite.Sprite):
def __init__(self,path):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(path)
self.rect = self.image.get_rect()
self.shot = 0 # bullet not shot initially
def shoot(self, pixels):
self.shot = pixels
def track(self):
self.rect.y -= self.shot
所以,只要點擊按鈕,shot屬性就會被修改,子彈會繼續移動。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/330944.html
上一篇:Elasticsearch3of280shardsfailed錯誤-有沒有人以前見過這樣的事情并且知道如何修復它?
