我将我在 pygame 中的小型物理测试代码从使用变量对描述位置、速度和加速度转换为 math.Vector2()。原因很明显,因为有很多与 Vector2 相关的方法可以很容易地找到长度、归一化、叉积等等。
在 pygame 文档中,它还支持一大堆数值运算,例如 vec*number、vec*=vec 等。但是,当我开始使用 vec+=vec 或 vec*=vec 时,我的问题就出现了。我收到这种错误...
“malloc:*对象 0x7fb4a1ec31a0 的错误:未分配被释放的指针”
如果我注释掉所有这些操作,代码当然可以在没有动画的情况下运行良好。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()