在Pygame平台游戏中遇到烦人的精灵抖动(尚未解决,但已关闭)

时间:2019-03-04 00:52:57

标签: python pygame

我正在尝试在Pygame中制作平台游戏。作为白痴,我没有使用类,这可能会使我的工作更轻松。这是针对学校的项目,需要2天,因此我没有时间实施课程。我正在尝试使播放器与地面碰撞,但是当它碰到地面时,我会出现精灵抖动(例如,精灵会“轻微反弹”)

以下是功能(touchingGround处理地面碰撞):

def touching(entityDat,entityRect,rlist,cpos):
    entityRect.center = (entityDat[0] - cpos[0],entityDat[1] - cpos[1])
    entityDat[4] = False
    for i in rlist:
        if entityRect.colliderect(i):
            entityDat[4] = True
            return True
def touchingGround(entityDat,entityRect,rlist,cpos,gravity):
    while touching(entityDat,entityRect,rlist,cpos):
        if entityDat[5]:
            entityDat[1] += gravity/10
        else:
            entityDat[1] -= gravity/10
        entityDat[3] = 0
    if touching(entityDat,entityRect,rlist,cpos):
        if entityDat[5]:
            entityDat[1] += entityDat[3]
        else:
            entityDat[1] -= entityDat[3]
        entityDat[3] = 0
def walk(entityDat,entityRect,rlist,cpos):
    entityDat[0] += entityDat[2]
    if touching(entityDat,entityRect,rlist,cpos):
        entityDat[0] -= entityDat[2]
        entityDat[2] = 0

这是移动处理程序:

if not moveLock:
        #player movement
        touching(playerDat,playerRect,rlist,cpos)
        if jump or playerDat[3] < 4:
            playerDat[3] += gravity
        elif not touching(playerDat,playerRect,rlist,cpos):
            playerDat[3] += gravity * 2
        touchingGround(playerDat,playerRect,rlist,cpos,gravity)
        playerDat[1] += playerDat[3]
        playerDat[2] *= 0.7
        if moveRight:
            playerDat[2] += 3
        if moveLeft:
            playerDat[2] -= 3
        if playerDat[2] > -0.5 or playerDat[2] < 0.5:
            walk(playerDat,playerRect,rlist,cpos)
        if touching(playerDat,playerRect,rlist,cpos) and jump == True:
            playerDat[3] = 20
            jump = False
        playerRect.center = (playerDat[0] - cpos[0],playerDat[1] - cpos[1])

这是playerDat的所有值表示什么的键:

playerDat = [0:xPos,1:yPos,2:xMomentum,3:yMomentum,4:touchingGround,5:goingUp?,6:direction,7:frame]

对不起,如果我应该包含更多内容,则还是要感谢。

1 个答案:

答案 0 :(得分:1)

这并不是全部相关的代码,因此只能猜测一下,但从我的看到来看:

  • 您为什么在while touching(...):中有一个touchingGround?这不是您的所有代码,但实际上看起来应该是if touching(...):。是因为那是您实现重力的方式,以使精灵“掉落”吗?如果真是这样,您就不应该那样做,while循环将在很大程度上阻止任何其他更新。您应该在主while循环(包含事件的循环)中更新每帧的y位置。

  • 通常,如果子画面“抖动”,则意味着它正在来回运动,这又意味着其位置正在更改,绘制,再次更改,再次绘制。这就是为什么您应该在实际绘制精灵的地方包含循环的原因。最有可能发生的事情是您在循环中多次检查并更改了精灵的位置,这使其抖动。