2011-11-29 99 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