问题
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