2016-12-18 56 views
0

所以這裏是我的問題,我在使我的盜賊喜歡遊戲的開始,我堅持讓敵人追逐玩家,它必須是簡單明瞭的東西,但我的技能在相當低的......因此,這裏是我的怪獸級怪物蔡斯pygame

class monster(): 
    def __init__(self , mright, mleft, mup, mdown): 
    self.mright = pygame.image.load(mright).convert_alpha() 
    self.mleft = pygame.image.load(mleft).convert_alpha() 
    self.mup = pygame.image.load(mup).convert_alpha() 
    self.mdown = pygame.image.load(mdown).convert_alpha() 

    self.sqrx = 7 
    self.sqry = 2 
    self.x = 210 
    self.y = 60 
    self.direction = self.mdown 

    char = character(right , left , up , down , Level) 

def chase(self,direction): 
    if self.sqrx > char.sqrx : 
     self.sqrx -= 1 
     self.x = self.sqrx * size_sprite 
    self.direction = self.mleft 

    if self.sqrx < char.sqrx : 
     self.sqrx += 1 
     self.x = self.sqrx * size_sprite 
    self.direction = self.mright 

    if self.sqry > char.sqry : 
     self.sqry -= 1 
     self.y = self.sqry * size_sprite 
    self.direction = self.mup 

    if self.sqry < char.sqry : 
     self.sqry += 1 
     self.y = self.sqry * size_sprite 
    self.direction = self.mdown 

而且遊戲循環

while gameon: 


    pygame.time.Clock().tick(30) 

    for event in pygame.event.get(): 

     if event.type == QUIT: 
      gameon = 0 
      on = 0 

     elif event.type == KEYDOWN: 

      if event.key == K_ESCAPE: 
       gameon = 0 

      elif event.key == K_RIGHT: 
       char.move('right') 
      elif event.key == K_LEFT: 
       char.move('left') 
      elif event.key == K_UP: 
       char.move('up') 
      elif event.key == K_DOWN: 
       char.move('down') 

    level.display(window) 
    window.blit(monst.direction , (monst.x , monst.y)) 
    window.blit(char.direction , (char.x , char.y)) 
    pygame.display.flip() 
+0

您在代碼中有錯誤的縮進。在'chase'中你總是設置'self.direction = self.mdown' – furas

+0

@furas self.direction只是字符 – Haza

+0

的精靈,但是'chase(self,direction):'always set' self.direction = self.mdown ' - 不管你使用什麼'方向'。 (順便說一下:「評論」是對所有代碼發表評論的地方,不僅僅是答案/解決方案) – furas

回答

0

像上面提到的人 - 你的設置方向的壓痕導致它調用的所有4調用每次調用chase。這基本上總是把方向= self.mdown

def chase(self,direction): 
    if self.sqrx > char.sqrx : 
     self.sqrx -= 1 
     self.x = self.sqrx * size_sprite 
     self.direction = self.mleft 
1

你應該在主循環使用chase() - 看到代碼註釋

while gameon: 

    pygame.time.Clock().tick(30) 

    # --- events --- 

    for event in pygame.event.get(): 

     if event.type == QUIT: 
      gameon = 0 
      on = 0 

     elif event.type == KEYDOWN: 

      if event.key == K_ESCAPE: 
       gameon = 0 

      elif event.key == K_RIGHT: 
       char.move('right') 
      elif event.key == K_LEFT: 
       char.move('left') 
      elif event.key == K_UP: 
       char.move('up') 
      elif event.key == K_DOWN: 
       char.move('down') 
     # - object events --- 

     # char.handle_event(event) 

    # --- updates --- 

    # HERE put all functions which change elements 
    #  (but don't depend on events) like: 
    # - chasing player, 
    # - moving mosters, 
    # - detecting collisions, 
    # - animating other elements (ie. fire, water, etc.) 

    # --- draws all elements --- 

    level.display(window) 
    window.blit(monst.direction , (monst.x , monst.y)) 
    window.blit(char.direction , (char.x , char.y)) 
    pygame.display.flip() 

看到一些templates of PyGame code


在MOSTER我會用char(播放器實例)作爲參數

def __init__(self , char, ...): 
    self.char = char 

有機會獲得其位置的所有時間

或至少

def chase(self, direction, char): 

有機會獲得,當你調用chase()


BTW球員:代替更可讀的gameon = 0gameon = False(和gameon = True


你可以在Moster()創建draw()並做類(class Monster():)和lower_case名變量

monst.draw(window) 

,而不是

window.blit(monst.direction , (monst.x , monst.y)) 

使用CamelCase名(moster = Moster() )。它使代碼更具可讀性。請參閱PEP 8 -- Style Guide for Python Code

+0

坦克爲您提供建議!我會嘗試糾正我的錯誤,並嘗試你的例子。 – Haza

+0

所以我沒有更多的縮進問題和更清晰的代碼感謝你,但我無法弄清楚如何使用我剛剛創建的類,在我使用「For event in」的主循環中使我的角色移動很簡單,但我不知道如何在我的循環中使用我的chase()函數提示? – Haza

+0

部分'---更新---'使用'monst.chase()'。但可能你必須修改這個函數,因爲我不確定它是否符合你的期望。 – furas