python - 滑动 Sprite

标签 python sprite pygame slide

我希望我的 Sprite 像在冰上一样滑动。因此,如果他在地面上,那么他可以正常行走,但当他接触冰时,他会滑动,直到有东西阻止他。 有谁知道如何才能做到这一点? 谢谢

最佳答案

像“Sprite Movement Towards a Target ”示例一样操纵摩擦系数(修改如下):

class Sprite(pygame.sprite.Sprite):
    ICE = 0.01
    LAND = 1.

    def __init__(self):
        # ...
        self.normal_friction = .95 # friction while accelerating
        self.slowing_friction = .8 # friction while slowing down

    def update(self):
        # ...
        if self.dir: # if there is a direction to move

            if self.in_ice_region():
                surface_coefficient = Sprite.ICE
            else:
                surface_coefficient = Sprite.LAND

            if self.distance_check(self.dist): # if we need to slow down
                self.speedX += (self.dir[0] * (self.speed / 2)) # reduced speed
                self.speedY += (self.dir[1] * (self.speed / 2))
                self.speedX *= surface_coefficient * self.slowing_friction # increased friction
                self.speedY *= surface_coefficient * self.slowing_friction

            else: # if we need to go normal speed
                self.speedX += (self.dir[0] * self.speed) # calculate speed from direction to move and speed constant
                self.speedY += (self.dir[1] * self.speed)
                self.speedX *= surface_coefficient * self.normal_friction # apply friction
                self.speedY *= surface_coefficient * self.normal_friction

            self.trueX += self.speedX # store true x decimal values
            self.trueY += self.speedY
            self.rect.center = (round(self.trueX),round(self.trueY)) # apply values to sprite.center

关于python - 滑动 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8306471/

相关文章:

python - 如何在 pygame 中水平翻转图像?

python - 在 csv 文件 python 中反转行的最佳方法

python - SimpleBlobDetector opencv python错误但缺少输出

python - 在 ManyToManyField 中使用交叉引用时出现 Django NameError

python - Pytorch:当参数或大小发生变化时是否可以加载模型?

xcode - 替换 Sprite 中的图像-iPhone的Cocos2d游戏开发

css - 如何防止图标/ Sprite 在流体设计中移动

python - 如何在pygame中将图像置于窗口中间?

python - Sprite 不工作/不显示(Python/Pygame)

python - 如何将 PNG 图像转换为边界(对于圆)[pygame]?