Sliding sprites

霸气de小男生 提交于 2019-12-11 09:06:47

问题


I want my sprite to slide like he's on ice. So if he's on ground then he can walk normally, but when he touches ice he will slide until something stops him. Does anyone know how one can do that? Thanks


回答1:


Manipulate the friction coefficients like the "Sprite Movement Towards a Target" example (modified below):

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


来源:https://stackoverflow.com/questions/8306471/sliding-sprites

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!