为什么我的 pymunk 程序太慢了?
Why is my pymunk program is way too slow?
我的 pymunk 程序太慢了。每次我 运行 程序加载时需要 5 秒。这是我的代码。提前致谢。
import pymunk # Import pymunk..
import pygame
pygame.init()
display = pygame.display.set_mode((800,800))
clock = pygame.time.Clock()
space = pymunk.Space() # Create a Space which contain the simulation
FPS = 30
def convert_coordinates(point):
return point[0], 800-point[1]
running = True
space.gravity = 0,-1000 # Set its gravity
# Set the position of the body
body = pymunk.Body()
shape = pymunk.Circle(body, 10)
body.position = (400,800)
shape.density = 1
space.add(body,shape)
while running: # Infinite loop simulation
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
clock.tick(FPS)
space.step(1/FPS)
display.fill((255,255,255))
x,y = convert_coordinates(body.position)
sprite = pygame.draw.circle(display,(255,0,0), (int(x),int(y)),10)
pygame.quit()
这是Indentation的事情。您必须在应用程序循环而不是事件循环中绘制场景:
while running: # Infinite loop simulation
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# <--- INDENTATION
clock.tick(FPS)
space.step(1/FPS)
display.fill((255,255,255))
x,y = convert_coordinates(body.position)
sprite = pygame.draw.circle(display,(255,0,0), (int(x),int(y)),10)
pygame.display.update()
pygame.quit()
我的 pymunk 程序太慢了。每次我 运行 程序加载时需要 5 秒。这是我的代码。提前致谢。
import pymunk # Import pymunk..
import pygame
pygame.init()
display = pygame.display.set_mode((800,800))
clock = pygame.time.Clock()
space = pymunk.Space() # Create a Space which contain the simulation
FPS = 30
def convert_coordinates(point):
return point[0], 800-point[1]
running = True
space.gravity = 0,-1000 # Set its gravity
# Set the position of the body
body = pymunk.Body()
shape = pymunk.Circle(body, 10)
body.position = (400,800)
shape.density = 1
space.add(body,shape)
while running: # Infinite loop simulation
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
clock.tick(FPS)
space.step(1/FPS)
display.fill((255,255,255))
x,y = convert_coordinates(body.position)
sprite = pygame.draw.circle(display,(255,0,0), (int(x),int(y)),10)
pygame.quit()
这是Indentation的事情。您必须在应用程序循环而不是事件循环中绘制场景:
while running: # Infinite loop simulation
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# <--- INDENTATION
clock.tick(FPS)
space.step(1/FPS)
display.fill((255,255,255))
x,y = convert_coordinates(body.position)
sprite = pygame.draw.circle(display,(255,0,0), (int(x),int(y)),10)
pygame.display.update()
pygame.quit()