【发布时间】:2021-02-05 15:27:38
【问题描述】:
下面代码中的块在重力作用下下落。当我运行此代码时,它们正在从屏幕底部移动到顶部。我试图调整重力功能,但它仍然从下到上移动,这与重力相反。然而,当我用另一台笔记本电脑进行测试时,代码运行良好。我目前正在学习 pygame。
import sys, random
import pygame
from pygame.locals import *
import pymunk
import pymunk.pygame_util
from random import seed
from random import choice
def add_circular_weight(space,mass=1,x=150,y=550):
radius = 5 + mass/10
inertia = pymunk.moment_for_circle(mass, 0, radius, (0,0))
body = pymunk.Body(mass, inertia)
body.position = x, y
shape = pymunk.Circle(body, radius, (0,0))
space.add(body, shape)
return shape
def main():
masses=[10,20,30,40,50,60,70,80,90,100]
pygame.init()
screen = pygame.display.set_mode((600, 600))
pygame.display.set_caption("Experiments")
clock = pygame.time.Clock()
space = pymunk.Space()
space.gravity = (0.0, -900.0)
balls = []
draw_options = pymunk.pygame_util.DrawOptions(screen)
ticks_to_next_ball = 10
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit(0)
elif event.type == KEYDOWN and event.key == K_ESCAPE:
sys.exit(0)
'''if event.key == pg.K_a:
self.body.angular_velocity = 5.5
elif event.key == pg.K_w:
self.body.apply_impulse_at_local_point(Vec2d(0, 900))'''
ticks_to_next_ball -= 1
if ticks_to_next_ball <= 0:
ticks_to_next_ball = 25
ball_shape = add_circular_weight(space, mass=choice(masses), x = random.randint(120,380))
balls.append(ball_shape)
screen.fill((255,255,255))
balls_to_remove = []
for ball in balls:
if ball.body.position.y < 150:
balls_to_remove.append(ball)
for ball in balls_to_remove:
space.remove(ball, ball.body)
balls.remove(ball)
space.debug_draw(draw_options)
space.step(1/50.0)
pygame.display.flip()
clock.tick(5)
if __name__ == '__main__':
main()
【问题讨论】:
-
抱歉,我无法重现该问题。代码似乎运行良好。
标签: python shapes gravity pymunk