2014-04-19 87 views
1

基本上我試圖讓敵人隨機在地圖上移動。我已經實現了這一點,然而隨機移動只發生在牆壁上(使它看起來更自然)。有沒有更好的方法來做到這一點?或者我應該移動我的地圖,讓角色更頻繁地撞牆?PyGame Sprite隨機運動

def update(self, walls, player): 
    # Check if moving left/right caused a collision 
    self.rect.x += self.x_speed 
    walls_hit = pygame.sprite.spritecollide(self, walls, False) 
    for wall in walls_hit: 
     if self.x_speed > 0: 
      self.rect.right = wall.rect.left 
      tmp = random.randint(0, 1) 
      print(tmp) 
      if tmp == 0: 
       self.x_speed = 0 
       self.y_speed = 3 
      else: 
       self.x_speed = 0 
       self.y_speed = -3 
     else: 
      self.rect.left = wall.rect.right 
      tmp = random.randint(0, 1) 
      print(tmp) 
      if tmp == 0: 
       self.x_speed = 0 
       self.y_speed = 3 
      else: 
       self.x_speed = 0 
       self.y_speed = -3 

    # Check if moving up/down caused a collision 
    self.rect.y += self.y_speed 
    walls_hit = pygame.sprite.spritecollide(self, walls, False) 
    for wall in walls_hit: 
     if self.y_speed > 0: 
      self.rect.bottom = wall.rect.top 
      tmp = random.randint(0, 1) 
      print(tmp) 
      if tmp == 0: 
       self.y_speed = 0 
       self.x_speed = 3 
      else: 
       self.y_speed = 0 
       self.x_speed = -3 
     else: 
      self.rect.top = wall.rect.bottom 
      tmp = random.randint(0, 1) 
      print(tmp) 
      if tmp == 0: 
       self.y_speed = 0 
       self.x_speed = 3 
      else: 
       self.y_speed = 0 
       self.x_speed = -3 

如果它有助於下面的代碼是我的地圖(1的牆壁= 0的地板=)

GRID = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 
     [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], 
     [1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1], 
     [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], 
     [1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1], 
     [1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1], 
     [1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1], 
     [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], 
     [1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1], 
     [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], 
     [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] 

任何幫助,將不勝感激!

回答

0

這真的取決於你想要的隨機移動的類型。有很多選擇:

  • 隨機移動的每一個「步驟」(可以看起來生澀/不自然)。具體改變你的代碼的一種方法是在速度更多的變化/漸進的增量(它看起來像你的性格只能移動+/- 3)在每一個方向)

例如:

self.x_speed = 6*random.random()-3 
# this will set the x_speed to a float between -3 and 3 

self.y_speed += random.random() - .5 
# this will increment y_speed by a number in between .5 and -.5 
# this is a more gradual speed change and a way to make it look more natural 
  • 我個人很喜歡你當前的實現,當你撞牆時,速度正在變化。我想無論你選擇哪種隨機移動,你的角色應該改變方向,當你撞牆時。

  • 隨機運動每x步驟(這看起來更自然比隨機移動的每一步)

例如:

# within your function, you can see the number of steps 
step = 0 
if step >= 5: #every 5 steps 
    #set random movement of whatever type you want 
    self.x_speed = 6*random.random()-3 
    self.y_speed = 6*random.random()-3 
    step = 0 
step += 1 

您也可以使step閾值的隨機數,就像如果你想隨機改變方向每5-10步(除了當你打牆時),你可以改變上面的if語句如下:

threshold = random.randrange(5,11) 
step = 0 
if step >= threshold: #every 5 steps 
    #set random movement of whatever type you want 
    self.x_speed = 6*random.random()-3 
    self.y_speed = 6*random.random()-3 
    step = 0 
    threshold = random.randrange(5,11) 
step += 1 

祝您的遊戲順利!