我的精灵在空中行走,因为它认为它正在触摸平台,但事实并非如此..?



我一直在努力让我的代码正常工作,但我的精灵没有受到重力的影响,当我尝试一些print((来查看出了什么问题时,我的精灵似乎认为它在触摸平台时一直在触摸它。。?问题的图像

这是代码:

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)

最新更新