【问题标题】:Pygame: How can i correct falling balls from moving upwards?Pygame:我怎样才能纠正下落的球向上移动?
【发布时间】: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


【解决方案1】:

问题很可能是在 pymunk 5.7 和 6.0 之间进行的更改。在旧版本中,pymunk 在使用pymunk_utils 绘制空间时,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()

【讨论】:

  • 谢谢。这已经奏效了。我之前更新了 pymunk。
猜你喜欢
  • 1970-01-01
  • 2020-02-22
  • 1970-01-01
  • 1970-01-01
  • 2022-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-06
相关资源
最近更新 更多