对象未添加到pymun中的空间

2024-06-16 12:26:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图用pymunk和pygame编写一个程序。该计划(截至目前)只是一个汽车驾驶一些障碍。我正在尝试使用pymunk来检查汽车和任何障碍物是否发生碰撞,如果发生碰撞,我将调用其他函数。我遇到的问题是车和/或障碍物似乎没有添加到空间中。你知道吗

这是我正在运行的代码,我为我的疏忽道歉。你知道吗

谢谢

编辑: 我的目标是使用pymunk检测障碍物和汽车之间的碰撞,以返回类似“游戏结束”的内容。问题是,我不知道是否障碍物和/或汽车被添加到pymunk空间。你知道吗

import pygame as pg
import pymunk
import random
import numpy as np

pg.init()

display_width = 1500
display_height = 1000

black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
yellow = (255, 255, 100)

gameDisplay = pg.display.set_mode((display_width, display_height))
clock = pg.time.Clock()

carImg = pg.image.load('photoshop_car.jpg')
carImg.set_colorkey(black)

rectangles = []

space = pymunk.Space()

def draw_car(x, y):
    pg.draw.circle(gameDisplay, blue, (x, y), 25)

def forward_movement_x(theta_degrees, movement_speed, currentx):

    theta_radians = theta_degrees * (np.pi / 180)
    delta_x = movement_speed * np.sin(theta_radians)
    return delta_x

def forward_movement_y(theta_degrees, movement_speed, currenty):
    theta_radians = theta_degrees * (np.pi / 180)
    delta_y = movement_speed * np.cos(theta_radians)
    return delta_y

def draw_obs(num_obs):
    for i in range(num_obs):
        obstaclex = random.randrange(0, display_width)
        obstacley = random.randrange(0, display_height)
        obstacle_width = random.randrange(50, 75)
        obstacle_height = random.randrange(50, 75)
        rectangles.append((obstaclex, obstacley, obstacle_width, obstacle_height))
        return rectangles

def obs_to_space(space):
    for i in rectangles:
        body = pymunk.Body(body_type = pymunk.Body.STATIC)
        obstacle_in_space = pymunk.Poly.create_box(body, size = (i[0], i[1]))
        body.position = (i[0], display_height - i[1])
        space.add(obstacle_in_space)

def car_to_space(x, y):
    mass = 10
    radius = 25
    moment = pymunk.moment_for_circle(mass, 0, radius)
    body = pymunk.Body(mass, moment)
    body.position = (x, y)
    shape = pymunk.Circle(body, radius)
    space.add(body, shape)

def coll_begin(arbiter, space, data):
    print("begin")
    return True

def coll_pre(arbiter, space, data):
    print("pre")
    return True

def coll_post(arbiter, space, data):
    print("pre")


def coll_separate(arbiter, space, data):
    print("pre")


def game_loop():
    gameExit = False
    draw_obs(5)

    x = (display_width * .5)
    y = (display_height * .5)
    car_rotation = 0
    rotate_speed = 0
    car_speed = 10
    car_direction = True

    obs_to_space(space)

    while not gameExit:

        gameDisplay.fill(pg.Color("black"))

        for i in range(len(rectangles)):
            pg.draw.rect(gameDisplay, red, rectangles[i])

        for event in pg.event.get():
            if event.type == pg.QUIT:
                gameExit = True
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_LEFT:
                    rotate_speed = 5
                if event.key == pg.K_RIGHT:
                    rotate_speed = -5
            if event.type == pg.KEYUP:
                if event.key == pg.K_LEFT or event.key == pg.K_RIGHT:
                    rotate_speed = 0

        car_rotation = car_rotation + rotate_speed

        x = x - forward_movement_x(car_rotation, car_speed, x)
        y = y - forward_movement_y(car_rotation, car_speed, y)
        x = int(x)
        y = int(y)

        car_to_space(x, y)

        draw_car(x, y)

        pg.display.update()
        clock.tick(30)

game_loop()
pg.quit()
quit()

Tags: eventdefdisplaybodyspacewidthcarpg
2条回答

有多个问题使代码无法按预期工作。你知道吗

  1. obs\u to\u space只将形状添加到空间,而不是实体。你知道吗

    通过添加主体来修复。

  2. 车到车的空间称为每一帧。这意味着,每一帧新的车身和汽车形状被添加到空间。(因此,一段时间后,空间将容纳100辆汽车:)

    您可以通过在开始时添加一次汽车形状,然后在while循环的每次迭代中更新其位置来修复此问题。

  3. 你没有任何代码来查看Pymunk是否记录了汽车和障碍物之间的任何碰撞。你知道吗

    有不同的方法来处理这个问题。从你的代码看来,你想自己处理汽车的运动,只使用Pymunk的碰撞。在这种情况下,最简单的方法可能是根本不将汽车添加到空间中,而是使用space.shape_query的每个帧来查看汽车的形状是否与空间中的另一个形状碰撞。你知道吗

    另一方面,如果您只使用Pymunk进行基本的碰撞检测,那么使用Pygame中内置的碰撞检测可能会更容易。

你看不到这辆车的原因是你在画完车后打电话给gameDisplay.fill(pg.Color("black"))。你知道吗

还要注意的是,在while循环中调用了很多不应该调用的代码。例如,您在每个周期都要重新绘制黑色背景。你知道吗

这里有些东西你应该搬走。如果有疑问,请在函数中放置print语句,以查看调用它们的频率。你知道吗

def game_loop():
    gameExit = False
    draw_obs(5)

    x = int(display_width * .5)
    y = int(display_height * .5)
    car_rotation = 0
    rotate_speed = 0
    car_speed = 10
    car_direction = True

    gameDisplay.fill(pg.Color("black"))
    obs_to_space(space)
    draw_car(x, y)

    for i in range(len(rectangles)):
        pg.draw.rect(gameDisplay, red, rectangles[i])

    while not gameExit:
        ...

我不打算破译代码来查看是否确实添加了障碍物,但是您可以打印space._get_shapes()来查看空间中的形状。你知道吗

相关问题 更多 >