我的精灵在空中行走,因为它认为它正在接触平台,但实际上没有……?

My sprite is walking on air because it thinks it is touching a platform but it's not..?

我一直在努力让我的代码正常工作,但我的 sprite 没有受到重力的影响,当我尝试一些 print() 来查看出了什么问题时,我的 sprite 似乎认为它在触摸平台在任何时候只要触摸一次..? Image to the problem

代码如下:

tx   = 64
ty   = 64

class Player(pygame.sprite.Sprite):
  def __init__(self):
    pygame.sprite.Sprite.__init__(self)
    self.image = pygame.image.load('player.png').convert()
    self.movex = 0 # move along X
    self.movey = 0 # move along Y
    self.isJumping = True
    self.isFalling = True

  def gravity(self):
    if self.isJumping == True:
      self.movey += 3.2

  def update(self):
    platform_hit_list = pygame.sprite.spritecollide(self, platform_list, False)
    for p in platform_hit_list:
      self.isJumping = False  # stop jumping
      self.movey = 0

      if self.rect.bottom <= p.rect.bottom:
          self.rect.bottom = p.rect.top
      else:
          self.movey += 3.2
    
    self.rect.x = self.rect.x + self.movex  
    self.rect.y = self.rect.y + self.movey

class Level:
  def platform(lvl, tilesX, tilesY):
    platform_list = pygame.sprite.Group()
    platformLocation = []
    i = 0
    if lvl == 1:
      platformLocation.append((1000, height - tilesY - 100, 0))
      while i < len(platformLocation):
        j = 0
        while j <= platformLocation[i][2]:
          platform = Platform((platformLocation[i][0] + (j * tilesX)), platformLocation[i][1], tilesX, tilesY, 'platform.png')
          platform_list.add(platform)
          j = j + 1
        print('run' + str(i) + str(platformLocation[i]))
        i = i + 1
    return platform_list

platform_list = Level.platform(1, tx, ty)

对问题的一些解决方案和解释感到满意

您显示的代码让播放器以 self.isJumping = True 开头,但是一旦它在 update 函数中设置为 False(当发生碰撞时),它永远不会得到再次设置为 True。这就是为什么如果你走出平台的边缘就不会摔倒的原因。

您可能希望在循环碰撞平台之前将 isJumping 设置为 True,这样如果没有碰撞,它将保持 True

def update(self):
    self.isJumping = True        # we're in the air by default
    platform_hit_list = pygame.sprite.spritecollide(self, platform_list, False)
    for p in platform_hit_list:
        self.isJumping = False   # unless we collide with something
        ...

如果您的游戏中可能存在多种碰撞(例如行走或跳入墙壁的侧面),您可能需要比这更复杂的碰撞响应代码,因为只有平台正好靠近墙壁的碰撞玩家的脚应该能让他们停止掉落(否则跳到墙上或天花板上会很奇怪)。

tx   = 64
ty   = 64

class Player(pygame.sprite.Sprite):
  def __init__(self):
    pygame.sprite.Sprite.__init__(self)
    self.image = pygame.image.load('player.png').convert()
    self.movex = 0 # move along X
    self.movey = 0 # move along Y
    self.isJumping = True
    self.isFalling = True

  def gravity(self):
    if self.isJumping == True:
      self.movey += 3.2

def update(self):
    self.isJumping = True   
    platform_hit_list = pygame.sprite.spritecollide(self, platform_list, False)
    for p in platform_hit_list:
        self.isJumping = False 
        self.movey = 0

      if self.rect.bottom <= p.rect.bottom:
          self.rect.bottom = p.rect.top
      else:
          self.movey += 3.2
    
    self.rect.x = self.rect.x + self.movex  
    self.rect.y = self.rect.y + self.movey

class Level:
  def platform(lvl, tilesX, tilesY):
    platform_list = pygame.sprite.Group()
    platformLocation = []
    i = 0
    if lvl == 1:
      platformLocation.append((1000, height - tilesY - 100, 0))
      while i < len(platformLocation):
        j = 0
        while j <= platformLocation[i][2]:
          platform = Platform((platformLocation[i][0] + (j * tilesX)), platformLocation[i][1], tilesX, tilesY, 'platform.png')
          platform_list.add(platform)
          j = j + 1
        print('run' + str(i) + str(platformLocation[i]))
        i = i + 1
    return platform_list

platform_list = Level.platform(1, tx, ty)