2012-12-01 25 views
0

bassically我嘗試碰撞檢測添加到下面的精靈,使用以下:添加碰撞檢測雪碧,對於pyGame

self.rect = bounds_rect 


       collide = pygame.sprite.spritecollide(self, wall_list, False) 

       if collide: 
      # yes 

        print("collide") 

但是似乎當碰撞被觸發,連續打印「碰撞」過來,結束時,而是我希望他們根本無法走過對象,有什麼幫助?

def update(self, time_passed): 
     """ Update the creep. 

      time_passed: 
       The time passed (in ms) since the previous update. 
     """ 
     if self.state == Creep.ALIVE: 
      # Maybe it's time to change the direction ? 
      # 
      self._change_direction(time_passed) 

      # Make the creep point in the correct direction. 
      # Since our direction vector is in screen coordinates 
      # (i.e. right bottom is 1, 1), and rotate() rotates 
      # counter-clockwise, the angle must be inverted to 
      # work correctly. 
      # 
      self.image = pygame.transform.rotate(
       self.base_image, -self.direction.angle) 

      # Compute and apply the displacement to the position 
      # vector. The displacement is a vector, having the angle 
      # of self.direction (which is normalized to not affect 
      # the magnitude of the displacement) 
      # 
      displacement = vec2d( 
       self.direction.x * self.speed * time_passed, 
       self.direction.y * self.speed * time_passed) 

      self.pos += displacement 

      # When the image is rotated, its size is changed. 
      # We must take the size into account for detecting 
      # collisions with the walls. 
      # 
      self.image_w, self.image_h = self.image.get_size() 
      global bounds_rect 
      bounds_rect = self.field.inflate(
          -self.image_w, -self.image_h) 

      if self.pos.x < bounds_rect.left: 
       self.pos.x = bounds_rect.left 
       self.direction.x *= -1 
      elif self.pos.x > bounds_rect.right: 
       self.pos.x = bounds_rect.right 
       self.direction.x *= -1 
      elif self.pos.y < bounds_rect.top: 
       self.pos.y = bounds_rect.top 
       self.direction.y *= -1 
      elif self.pos.y > bounds_rect.bottom: 
       self.pos.y = bounds_rect.bottom 
       self.direction.y *= -1 

      self.rect = bounds_rect 


      collide = pygame.sprite.spritecollide(self, wall_list, False) 

      if collide: 
     # yes 

       print("collide") 




     elif self.state == Creep.EXPLODING: 
      if self.explode_animation.active: 
       self.explode_animation.update(time_passed) 
      else: 
       self.state = Creep.DEAD 
       self.kill() 

     elif self.state == Creep.DEAD: 
      pass 

     #------------------ PRIVATE PARTS ------------------# 

    # States the creep can be in. 
    # 
    # ALIVE: The creep is roaming around the screen 
    # EXPLODING: 
    # The creep is now exploding, just a moment before dying. 
    # DEAD: The creep is dead and inactive 
    # 
    (ALIVE, EXPLODING, DEAD) = range(3) 

    _counter = 0 

    def _change_direction(self, time_passed): 
     """ Turn by 45 degrees in a random direction once per 
      0.4 to 0.5 seconds. 
     """ 
     self._counter += time_passed 
     if self._counter > randint(400, 500): 
      self.direction.rotate(45 * randint(-1, 1)) 
      self._counter = 0 

    def _point_is_inside(self, point): 
     """ Is the point (given as a vec2d) inside our creep's 
      body? 
     """ 
     img_point = point - vec2d( 
      int(self.pos.x - self.image_w/2), 
      int(self.pos.y - self.image_h/2)) 

     try: 
      pix = self.image.get_at(img_point) 
      return pix[3] > 0 
     except IndexError: 
      return False 

    def _decrease_health(self, n): 
     """ Decrease my health by n (or to 0, if it's currently 
      less than n) 
     """ 
     self.health = max(0, self.health - n) 
     if self.health == 0: 
      self._explode() 

    def _explode(self): 
     """ Starts the explosion animation that ends the Creep's 
      life. 
     """ 
     self.state = Creep.EXPLODING 
     pos = (self.pos.x - self.explosion_images[0].get_width()/2, 
       self.pos.y - self.explosion_images[0].get_height()/2) 
     self.explode_animation = SimpleAnimation(
      self.screen, pos, self.explosion_images, 
      100, 300) 
     global remainingCreeps 

     remainingCreeps-=1 

     if remainingCreeps == 0: 
       print("all dead") 
+1

這不正是你在做什麼?當碰撞爲真時,您輸出碰撞。 –

+0

那麼目前我只是打印'碰撞',但它看起來很快,因爲它碰撞它無限期地打印出來0.0 – Broak

回答

2

檢查碰撞只是一個檢查,看看是否兩個矩形精靈有一個公共區域。 沒有內置碰撞,無法在碰撞期間輸入玩家輸入。你必須自己寫。

當碰撞發生時,您應該想要更改玩家座標。舉個例子:

比方說我們玩馬里奧。當馬里奧狀態正在跳躍檢查碰撞。某處我們將把馬里奧的速度存儲在y軸上。當碰撞返回True時,對於任何塊,我們現在將速度設置爲0,將y設置爲塊的頂部/底部。如果它將成爲最低點,我們仍然保持跳躍,所以它可以回落到地面。

我的提示是爬行者有一些oldx和oldy的價值,當碰撞發生時返回。那樣爬行者將永遠不會進入牆壁。另一種方法是在發生碰撞時簡單地改變方向,但這可能並不總是奏效。