没有将对象添加到pymunk中的空间

时间:2018-04-12 00:25:50

标签: python pymunk

我正在尝试使用pymunk和pygame编写程序。该计划(截至目前)只是一辆带有一些障碍的汽车。我正在尝试使用pymunk检查汽车和任何障碍物是否发生碰撞,如果他们这样做,我将会调用其他一些功能。我遇到的问题是汽车和/或障碍似乎没有被添加到太空。

这是我现在正在运行的代码,对于邋iness道歉。

谢谢

编辑: 我的目标是使用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()

2 个答案:

答案 0 :(得分:0)

您无法看到汽车的原因是因为您在开车后致电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()以查看空间中的形状。

答案 1 :(得分:0)

有多个问题导致代码无法按预期运行。

  1. obs_to_space只将形状添加到空间,而不是身体。

    通过添加正文进行修复。

  2. 每帧调用car_to_space。这意味着每个车架都会将新车身和车身形状添加到空间中。 (所以一段时间后,这个空间将包含100辆汽车:)

    您可以通过在开头添加一次汽车形状然后在while循环中每次迭代更新其位置来解决此问题。

  3. 你没有任何代码可以真正看出Pymunk是否记录了汽车和障碍物之间的任何碰撞。

    有不同的方法来处理这个问题。从您的代码中看起来您想要自己处理汽车的移动,并且只使用Pymunk进行碰撞。在这种情况下,最简单的可能是根本不将汽车添加到空间,而是使用space.shape_query每个框架来查看汽车的形状是否在空间中碰到另一个形状。

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

相关问题