Pygame math.Vector2()错误

时间:2013-07-09 10:43:04

标签: pygame runtime-error game-physics

我在pygame中转换我的小物理测试代码,使用变量对将位置,速度和加速度描述为math.Vector2()。原因很明显,因为有很多与Vector2相关的方法可以很容易地找到长度,标准化,交叉产品等等。

在pygame文档中,它还支持一大堆数字操作,如vec * number,vec * = vec等。但是当我开始使用vec + = vec或vec * = vec时,我的问题就出现了。我收到了这种错误...

对象0x7fb4a1ec31a0的“malloc: * 错误:未释放指针”

如果我注释掉所有这些操作,代码运行正常,没有动画。 Vector2()是否存在错误,或者我只是错误地使用它?

import pygame, math, random

pygame.init() 

class Circle(pygame.sprite.Sprite):
    def __init__(self, screen):
        pygame.sprite.Sprite.__init__(self)
        self.screen = screen
        self.position =  pygame.math.Vector2(random.randrange(20,self.screen.get_width()), self.screen.get_height()/3)
        self.velocity = pygame.math.Vector2(0.0, 0.0)
        self.acceleration = pygame.math.Vector2(0.0, 0.1)
        self.netForce =  pygame.math.Vector2(0.0, 0.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.position

    def update(self):
        self.calcPos()
        self.checkBounds()
        self.rect.center = self.position
        self.netForce *= 0.0


    def calcPos(self): 
        self.acceleration = self.netForce
        self.velocity += self.acceleration
        self.position += self.velocity 


    def applyForce(self, force):
        force /self.mass
        self.netForce += force

    def checkBounds(self):
        if self.position[1] > self.screen.get_height():
            self.acceleration[1] *= -1.0
            self.position[1] = self.screen.get_height()
        if self.position[0] > self.screen.get_width(): 
            self.acceleration[0] *= -1.0
            self.position[0] = self.screen.get_width()
        if self.position[1] < 0: 
            self.acceleration[1] *= -1.0
        if self.position[0] < 0: 
            self.acceleration[0] *= -1.0

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 = pygame.math.Vector2(1.0, 0)
    gravity = pygame.math.Vector2(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 = gravity * circle.mass
            circle.applyForce(gravity)
            #pass 

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

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

    pygame.quit()

if __name__ == "__main__":
    main()    

1 个答案:

答案 0 :(得分:0)

或者,您可以使用Vec2d的{​​{1}}类。

它有运算符重载(可以与元组或列表一起使用),使用插槽进行性能,可选择,实现列表接口(因此它与pygame函数兼容),具有相当多的高级向量运算符(用于性能和可读性)并且内置了单元测试。

我在模拟和物理测试中使用过它,它提供了你所需要的一切。