python - Pygame:我怎样才能纠正下落的球向上移动?
问题描述
下面代码中的块在重力作用下下落。当我运行此代码时,它们正在从屏幕底部移动到顶部。我试图调整重力功能,但它仍然从下到上移动,这与重力相反。然而,当我用另一台笔记本电脑进行测试时,代码运行良好。我目前正在学习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()
解决方案
问题很可能是在 pymunk 5.7 和 6.0 之间进行的更改。在旧版本中,pymunk 处理 y 的方向与 pygame 在使用pymunk_utils
. 为了简化事情,这被改变了,所以 Pymunk 和 Pygame 都做同样的事情。
要恢复旧行为,您可以positive_y_is_up
在 pygame_util 中设置为 True: pymunk.pygame_util.positive_y_is_up = True
。但是,如果您没有很多旧代码,我建议您修改位置和重力。这将使您更容易推断是否需要直接使用 pygame 绘制任何东西。(要转换你做的converted_y_value = surface.get_height() - old_y_value
)
转换后的代码将如下所示:
import random
import sys
from random import choice, seed
import pygame
import pymunk
import pymunk.pygame_util
from pygame.locals import *
def add_circular_weight(space,mass=1,x=150,y=150):
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)
#print(pymunk.pygame_util.positive_y_is_up)
#print(pymunk.version)
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 > 550:
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()
推荐阅读
- trimesh - Trimesh:在裁剪网格时保留纹理
- java - Chronicle 映射和队列是否是应用程序 JVM 堆的一部分
- angular - NullInjectorError:Angular jasmine 测试中没有 CROSS_STORAGE_CLIENT 的提供者
- javascript - 无法读取未定义的属性“indexOf”。当我重新加载 div 内容绑定到表格但第一次单击时表格内容显示在表格主体之外
- reactjs - 状态更新时表为空,需要刷新
- python - 如何覆盖 VSCode 的 Jupyter Notebook 中输出的默认背景颜色?
- api - 在 FB CAPI 中返回错误 190(过期的访问令牌)但令牌有效
- python - Python:fuzzywuzzy,输出第一个值是正确的,其他都是NaN
- android - 根据android中的矩形坐标裁剪捕获的图像
- shell - Unix 中的月份格式