我希望我的精灵滑动就像他在冰上。 所以,如果他是在地面上那么他就可以正常走路,但是当他接触冰他会滑动,直到我们阻止了他。 有谁知道如何可以做到这一点? 谢谢
Answer 1:
操纵像“的摩擦系数的Sprite运动朝向目标 ”的例子(以下修改):
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
文章来源: Sliding sprites