【问题标题】:Pygame - Sprite bounces through edgePygame - 精灵通过边缘反弹
【发布时间】:2013-07-08 00:30:48
【问题描述】:

我正在用精灵做一些基本的物理工作,这个是重力加上一些额外的阻力,因此理论上精灵应该停留在屏幕的顶部边缘。然而,问题是随着它越来越接近地面,精灵振动得越来越快,最终“挤压”在边界上。

有什么好的方法可以阻止这种情况?如果需要,我可以 pst 代码,但没有什么不寻常的,我只是认为它需要一种解决方法来降低速度,因为它越来越接近静止。

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()    

【问题讨论】:

    标签: pygame collision-detection physics


    【解决方案1】:

    我真的需要查看代码,但如果我正确理解您想要的内容,您可以根据到顶部的距离获得速度,例如,如果 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()
    

    最后一行是唯一必要的改变,每个循环都会将球推过它的边界,这会在球没有向上的力时引起问题,这使得球无法通过它的下界,同样可以为其他人做事。

    【讨论】:

    • 我的措辞很糟糕。我不想让它慢下来,我只想让它停下来休息。这是代码,检查一下,你会看到会发生什么。
    • 伙计,我已经这样做了,但我在 'self.dy *= -1.05' 之前有 'self.y=self.screen.get_height()' 这就是它不起作用的原因。谢谢大家看看,它的工作原理。非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    相关资源
    最近更新 更多