2011-11-29 113 views
1

我想让我的精灵像滑冰一样滑动。所以如果他在地面上,那么他可以正常行走,但是当他接触冰块时,他会滑动,直到阻止他。 有谁知道如何做到这一点? 由于滑动精灵

+0

确保使用的[摩擦系数]低值(http://en.wikipedia.org/wiki/Friction#Coefficient_of_friction)。或者至少低于“正常行走”的价值。 –

+0

我真的没有冰的摩擦系数 我应该。我的意思是没有一种方法可以让精灵滑下来。我的意思是我没有写任何复杂的东西。如果精灵在地面上,它会走路,当它不在时,它会滑动。我正在做一个期限项目,所以我要去简单大声笑 – bluesplay106

回答

1

操纵像“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