2013-07-10 111 views
1

我想讓一個精靈在pygame中投擲手榴彈。我想讓它向前移動一點,然後停下來。我的問題是讓手榴彈順利前進。下面的代碼所做的是讓它立即移動到intrest點,而不是順利移動。在Python中循環

[手榴彈類]

#Grenade Class 
class Explosive(pygame.sprite.Sprite): 
    def __init__(self, location): 
     pygame.sprite.Sprite.__init__(self) 
     self.pos = location 
     self.image = Nade 
     self.rect = self.image.get_rect() 
     self.rect.right = self.image.get_rect().right 
     self.rect.left = self.image.get_rect().left 
     self.rect.top = self.image.get_rect().top 
     self.rect.bottom = self.image.get_rect().bottom 
     self.rect.center = location 
    def move(self): 
     if Player.direction == 0: 
      self.rect.centery = self.rect.centery - 5 
     if Player.direction == 180: 
       self.rect.centery = self.rect.centery + 5 
     if Player.direction == 90: 
      self.rect.centerx = self.rect.centerx + 5 
     if Player.direction == 270: 
      self.rect.centerx = self.rect.centerx - 5 

[投擲手榴彈(主迴路)

if event.key == pygame.K_e and grenadeNum > 0: 
      Grenade = Explosive([Player.rect.centerx, Player.rect.centery]) 
      for i in range(1, 10) 
       Grenade.move() 
+0

就每秒速度而言。那麼你也會希望儘可能快的FPS。無論FPS如何,手榴彈總是會移動例如:每秒200像素。您可以根據時間使用更簡單的移動,但也可以查看[gaffer的固定時間步驟](http://gafferongames.com/game-physics/fix-your-timestep/) – ninMonkey

回答

0

你需要在每次移動後重繪屏幕,我覺得代碼:

pygame.display.update() 

所以你的循環應該是這樣的:

for i in range(1, 10) 
    Grenade.move() 
    pygame.display.update() 

這可能不是太高效,所以一定要尋找更有效的方法,例如,如果手榴彈是場景中唯一移動的物體,那麼你將只想重繪它。

+0

除此之外還有其他方法可以重新繪製屏幕'pygame.display.update()' – pepper5319

+0

'pygame.display.flip()'相當於沒有參數的'pygame.display.update()'。如果您使用某些精靈組,他們可以只更新屏幕的髒部分。但你仍然調用翻轉或更新來更新。 – ninMonkey

1

雖然您確實需要更新顯示屏,但還有第二個問題。現在,你並沒有讓它一直等待,所以它會毫不拖延地完成所有十個動作。這將會非常突然,所以您可能需要導入時間並在其中添加time.sleep()。因爲將手榴彈放在自己的「for」循環中,如果你還有其他事情會停止。如果你有一個時間循環來管理所有事情,可以在手榴彈上打一個「更新」功能,他們會有專櫃來確定是否應該在這一輪中移動。如果其他精靈需要做的事情,然後你可以調用他們的方法。最後,只是爲了加快程序,你可以只更新部分的屏幕,在精靈移動。當你把所有這些組合起來,它是沿着這些路線的東西:你要定義你的運動

import time 

grenades = pygame.sprite.RenderUpdates()  #initializes a pygame group for sprites 

class Explosive(pygame.sprite.Sprite): 
def __init__(self, location): 
    pygame.sprite.Sprite.__init__(self) 
    self.pos = location 
    self.image = Nade 
    self.rect = self.image.get_rect() 
    self.rect.right = self.image.get_rect().right 
    self.rect.left = self.image.get_rect().left 
    self.rect.top = self.image.get_rect().top 
    self.rect.bottom = self.image.get_rect().bottom 
    self.rect.center = location 
    self.move_counter = 10    #adds a counter for how many rounds left of moving the grenade has 
def update(self): 
    if self.move_counter > 0:   #checks that self still has turns left to move 
     <insert screen variable name>.fill(<insert background color>,self.rect) #erases self in old location 
     if Player.direction == 0: 
      self.rect.centery = self.rect.centery - 5 
     if Player.direction == 180: 
       self.rect.centery = self.rect.centery + 5 
     if Player.direction == 90: 
      self.rect.centerx = self.rect.centerx + 5 
     if Player.direction == 270: 
      self.rect.centerx = self.rect.centerx - 5 
     pygame.display.update(self.draw((screen))) #draws self in new location 
     self.move_counter -= 1   #subtracts one from the counter to mark that another turn of moving has passed by 

while True: 
    grenades.update() 
    #anything else you want to do in real time here 
    time.sleep(0.1)