我有一个Box2D World,我正在尝试根据其所属的类来更改主体的颜色。
我有动态物体(汽车和行人类别)和静态物体(地面和建筑物类别)并且我具有以下渲染功能:
def fix_vertices(vertices):
return [(int(SCREEN_OFFSETX + v[0]), int(SCREEN_OFFSETY - v[1])) for v in vertices]
def _draw_polygon(polygon, screen, body, fixture):
transform = body.transform
vertices = fix_vertices([transform * v * PPM for v in polygon.vertices])
pygame.draw.polygon(
screen, [c / 2.0 for c in colors[body.type]], vertices, 0)
pygame.draw.polygon(screen, colors[body.type], vertices, 1)
def _draw_circle(circle, screen, body, fixture):
position = fix_vertices([body.transform * circle.pos * PPM])[0]
pygame.draw.circle(screen, colors[body.type],
position, int(circle.radius * PPM))
colors = {dynamicBody: (133, 187, 101, 0), staticBody: (15, 0, 89, 0)}
def render():
global screen, PPM, running
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
# The user closed the window or pressed escape
running = False
# Zoom In/Out
elif event.type == KEYDOWN and event.key == pygame.K_KP_PLUS:
PPM += 5
elif event.type == KEYDOWN and event. key == pygame.K_KP_MINUS:
if PPM <= 5:
PPM -= 0
else:
PPM -= 5
elif event.type == VIDEORESIZE:
screen = pygame.display.set_mode(event.dict['size'], HWSURFACE | DOUBLEBUF | RESIZABLE)
screen.fill((255, 255, 255, 255))
# Draw the world
b2CircleShape.draw = _draw_circle
b2PolygonShape.draw = _draw_polygon
for body in box2world.bodies:
for fixture in body.fixtures:
fixture.shape.draw(screen, body, fixture)
# Flip the screen and try to keep at the target FPS
pygame.display.flip() # Update the full display Surface to the screen
pygame.time.Clock().tick(FPS)
以及主游戏循环中的这个
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), HWSURFACE | DOUBLEBUF | RESIZABLE)
colors = {dynamicBody: (133, 187, 101, 0), staticBody: (15, 0, 89, 0)}
render()
但是不幸的是,静态物体具有相同的颜色,而动态物体具有相同的颜色(根据字典的颜色)。我试图这样更改它:
colors = {dynamicBody: (133, 187, 101, 0) if isinstance(body, Pedestrian), else (0,0,0,0), staticBody: (15, 0, 89, 0), if isinstance(body, Building), else (121,121,121,0)}
但是它没有用,因为未定义body
中的isinstance()
参数。
您有任何想法我该如何解决?
非常感谢您的帮助。
P.S。我知道在render()
函数中使用全局变量不是很好,但是我无法提出任何其他工作方式,因此请不要指出这一点,这不是我的问题。
UPDATE在注释中,您想添加实例的代码,因此在这里(一个类是静态物体-建筑物,一个类是动态物体-行人,另一个类的定义类似)
from Box2D import b2Filter, b2FixtureDef, b2PolygonShape
from Box2D.b2 import edgeShape
import numpy as np
CAR_CATEGORY = 0x0002
PEDESTRIAN_CATEGORY = 0x0004
BUILDING_CATEGORY = 0x0008
CAR_GROUP = 2
PEDESTRIAN_GROUP = -4
BUILDING_GROUP = -8
class Building():
BUILDING_FILTER = b2Filter(categoryBits=BUILDING_CATEGORY,
maskBits=PEDESTRIAN_CATEGORY + CAR_CATEGORY,
groupIndex=BUILDING_GROUP,
)
def __init__(self, box2world, shape, position, color=(0,0,150,0), sensor=None):
self.box2world = box2world
self.shape = shape
self.position = position
self.color = color
if sensor is None:
sensor = False
self.sensor = sensor
self.body = self.box2world.CreateStaticBody(position=position,
angle=0.0,
fixtures=b2FixtureDef(
shape=b2PolygonShape(box=(self.shape)),
density=1000,
friction=1000,
filter=Building.BUILDING_FILTER))
self.body.userData = {'obj': self}
self.diagonal = np.sqrt(self.shape[0]**2 + self.shape[1]**2)
这是步行者课
from Box2D import b2Filter, b2FixtureDef, b2PolygonShape, b2CircleShape, b2Vec2
import numpy as np
CAR_CATEGORY = 0x0002
PEDESTRIAN_CATEGORY = 0x0004
BUILDING_CATEGORY = 0x0008
CAR_GROUP = 2
PEDESTRIAN_GROUP = -4
class Pedestrian():
def __init__(self, box2world, random_movement, ped_velocity, color=(110,0,0,0), position=None):
if position is None:
position = [5, 5]
self.random_movement = random_movement
self.color = color
self.ped_velocity = ped_velocity
self.position = position
self.box2world = box2world
self.nearest_building = 0
self.body = self.box2world.CreateDynamicBody(position=position,
angle=0.0,
fixtures=b2FixtureDef(
shape=b2CircleShape(radius=1),
density=2,
friction=0.3,
filter=b2Filter(
categoryBits=PEDESTRIAN_CATEGORY,
maskBits=CAR_CATEGORY + BUILDING_CATEGORY,
groupIndex=PEDESTRIAN_GROUP)))
self.current_position = [self.body.position]
self.body.userData = {'obj': self}
在我看来,对于每个需要颜色的类,您都可以在类本身内部直接分配一个:
class Car():
color = (133, 187, 101, 0)
# etc for all bodies that need a color.
然后在您需要时:
pygame.draw.circle(screen, body.color,