到目前为止,我开始使用平台和我的角色。
我实施了重力和跳跃功能。
所以重力概念如下工作,继续下降直到玩家与他下方的物体发生碰撞。
所以,当我在我的英雄下面有一个平台时,这是有效的,但是当我想要实现一个屋顶时。 所以在我的播放器顶部和下方有一个平台。
我的引力功能一直在下降。我的地形都在pygame.Rect列表中。
我的引力函数遍历我的所有地形并检查玩家是否在地面物体上方,如果这样继续下降。
我注意到的问题是因为我的角色在我的角色之上,它一直在下降。我似乎无法想办法忽略我角色上方的瓷砖,只关注播放机下方的瓷砖并检查是否存在勾结。
一旦我发现了这个勾结问题,我相信我可以在跳起来检查屋顶碰撞并左右移动时将其想象成我们。
帮助表示感谢。
编辑: terrain是我的对象tile列表。 现在地形只有2个物体
#this is not correct way initialize just displaying my 2 object's rect
terrain = [<rect(400, 355, 50, 49)>,<rect(500, 198, 50, 49)>]
def GRAVITY(self, terrain):
'''----------break is cutting of parsing the rest of the terrain.
----------Need to search through each terrain. Keep falling until collide with an object under the hero ONLY.
'''
for i in range(len(terrain)):
print i
#stop falling when colliding with object
if terrain[i].top > self.rect.bottom:
print 'above tile while falling'
self.y += JUMPRATE
break
#continue falling if not standing on the object. Also catch when walking of an object then fall.
elif terrain[i].top <= self.rect.bottom and not self.rect.colliderect(terrain[i]):
print 'whoops missed tile'
self.y +=JUMPRATE
break
else:
print 'on tile'
self.y = self.y
break
这是玩家跳跃时调用的功能。
答案 0 :(得分:2)
在您的代码中,每个子句中都有一个break语句,因此无论列表中有多少Rect
,循环都将始终运行一次。
你可以做的是使用collided
标志,并且只在发生碰撞时中断。
def fall(self, terrain):
collided = False
for rect in terrain:
if self.rect.colliderect(rect):
collided = True
break
if not collided:
self.y += JUMPRATE
如果没有碰撞,则角色会掉落。否则,它不会移动。但是,您必须添加一些东西来处理从侧面发生碰撞的情况。此外,角色的脚会略微穿过地板,因此一旦碰撞,你应该将角色的rect.bottom
设置为地形的top
。