滑动精灵
答
操纵像“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
+0
谢谢 这个工程! – bluesplay106
+0
如果您对答案满意,请随时接受。 –
+0
等待,实际上我希望这个人滑动(更像滑冰),直到它碰到一个物体。这段代码就像一个快速幻灯片。对不起,我没有正确看待它。 – bluesplay106
确保使用的[摩擦系数]低值(http://en.wikipedia.org/wiki/Friction#Coefficient_of_friction)。或者至少低于“正常行走”的价值。 –
我真的没有冰的摩擦系数 我应该。我的意思是没有一种方法可以让精灵滑下来。我的意思是我没有写任何复杂的东西。如果精灵在地面上,它会走路,当它不在时,它会滑动。我正在做一个期限项目,所以我要去简单大声笑 – bluesplay106