Pygame: 如何纠正落球向上移动?

Pygame: How can i correct falling balls from moving upwards?

下面代码中的方块正在逆重力下落。当我 运行 这段代码时,它们从屏幕底部移动到顶部。我试图调整重力功能,但它仍然从底部移动到顶部,这与重力相反。然而,当我用另一台笔记本电脑进行测试时,代码 运行ning 很好。我目前正在学习 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_utils 绘制 space 时,pymunk 处理 y 的方向与 pygame 的处理方式相反。为了简化事情,这被改变了,所以 Pymunk 和 Pygame 做同样的事情。

要恢复旧行为,您可以将 pygame_util 中的 positive_y_is_up 设置为 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()