2013-07-08 77 views
0

我正在做一些基本的物理東西與精靈,這是一個額外的拖動重力,使精靈在理論上應該停留在屏幕的頂部邊緣。然而,問題在於,當它靠近地面時,精靈振動得越來越快,最終在邊界上「擠壓」。Pygame - 雪碧彈跳通過邊緣

最好的辦法來阻止它?如果需要,我可以編碼,但沒有什麼不尋常的地方,我只是認爲它需要一個解決方法來降低速度,因爲它接近靜止。

在此先感謝

import pygame, math, random 

pygame.init() 

class Circle(pygame.sprite.Sprite): 
    def __init__(self, screen): 
     pygame.sprite.Sprite.__init__(self) 
     self.screen = screen 
     self.dx = 0 
     self.dy = 0 
     self.ddx = 0 
     self.ddx = 0 
     self.forceX = 0 
     self.forceY = 0 
     self.x = random.randrange(20,self.screen.get_width()) 
     self.y = self.screen.get_height()/2 
     self.radius = random.randrange(5,30) 
     self.image = pygame.Surface((self.radius*2,self.radius*2)) 
     self.image.set_colorkey((0,0,0)) 
     self.image.set_alpha(120) 
     self.mass = self.radius/15.0 
     pygame.draw.circle(self.image, (175,255,0), (self.radius,self.radius), self.radius) 
     self.image = self.image.convert_alpha() 
     self.rect = self.image.get_rect() 
     self.rect.center = (self.x, self.y) 

    def update(self): 
     self.calcPos() 
     self.checkBounds() 
     self.rect.centerx = self.x 
     self.rect.centery = self.y 
     self.forceX = 0 
     self.forceY = 0 


    def calcPos(self): 
     self.ddx = self.forceX 
     self.ddy = self.forceY 
     self.dy += self.ddy 
     self.dx += self.ddx 
     self.x += self.dx 
     self.y += self.dy 

    def applyForce(self, force): 
     self.forceX += force[0]/self.mass 
     self.forceY += force[1]/self.mass 

    def checkBounds(self): 
     if self.y > self.screen.get_height(): 
      self.dy *= -1.05 
     if self.x > self.screen.get_width(): 
      self.dx *= -1.05 
     if self.y < 0: 
      self.dy *= -1.05 
     if self.x < 0: 
      self.dx *= -1.05 

def main(): 
    screen = pygame.display.set_mode((600,400)) 
    background = pygame.Surface((screen.get_size())) 
    background.fill((150,150,150)) 
    background = background.convert() 

    circleGRP = pygame.sprite.Group() #Add balls 
    for x in range(10): 
     circleGRP.add(Circle(screen)) 

    wind = (1, 0) 
    gravity = (0, 1.0) 

    clock = pygame.time.Clock() 
    mainLoop = True 

    while mainLoop: 
     clock.tick(30) #Clock 
     for event in pygame.event.get(): #Key events 
      if event.type == pygame.QUIT: 
       mainLoop = False 
      elif event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_ESCAPE: 
        mainLoop = False 
      elif event.type == pygame.MOUSEBUTTONDOWN: #Add wind 
       if pygame.mouse.get_pressed()[0]: 
        for circle in circleGRP: 
         circle.applyForce(wind) 

#----------------------------------------------------------------------------     
     for circle in circleGRP: #Add gravity 
      gravity = (0, 1.0) 
      gravity = tuple([each * circle.mass for each in gravity]) 
      circle.applyForce(gravity) 

      circleX = circle.dx * -1 #Add friction 
      circleY = circle.dy * -1 
      friction = (circleX/80* circle.mass* (circle.radius/5), circleY/80* circle.mass* (circle.radius/5)) 
      circle.applyForce(friction) 

#----------------------------------------------------------------------------  
     circleGRP.update() 
     screen.blit(background, (0,0)) 
     circleGRP.draw(screen) 
     pygame.display.flip() 

    pygame.quit() 

if __name__ == "__main__": 
    main()  

回答

0

我真的需要看到的代碼,但如果我知道你想正確,你可以根據頂端的距離有velovity什麼,例如如果y你的精靈的位置:

ydir=y/50 
y+=int(ydir) 

編輯:

def checkBounds(self): 
    if self.y > self.screen.get_height(): 
     self.dy *= -1.05 
     self.y=self.screen.get_height() 

的最後一行是隻有必要的變化,每一個循環都會將球推向一定的邊界,當球沒有向上的力時,這會造成問題,這使得球不能通過它的下界,對其他球也可以做同樣的事情。

+0

這是我的不好的措辭。我不想讓它放慢速度,我只是想讓它停下來休息。下面的代碼,檢查出來,你會看到會發生什麼。 – Aikman007

+0

男人我已經這樣做了,但在'self.dy * = -1.05'之前我有'self.y = self.screen.get_height()',這就是爲什麼它沒有工作。感謝一堆看看,它的作品。非常感謝 :) – Aikman007