Python 根据类别更改身体颜色
我有一个Box2D世界,我正试图改变身体的颜色,这取决于它所属的类别 我有动态实体(汽车类和行人类)和静态实体(地面区域类和建筑类) 我有这些渲染功能:Python 根据类别更改身体颜色,python,colors,pygame,rendering,box2d,Python,Colors,Pygame,Rendering,Box2d,我有一个Box2D世界,我正试图改变身体的颜色,这取决于它所属的类别 我有动态实体(汽车类和行人类)和静态实体(地面区域类和建筑类) 我有这些渲染功能: 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):
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)
但不幸的是,静态实体具有相同的颜色,而动态实体具有相同的颜色(根据字典颜色)。我试着这样改变它:
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)}
但由于未定义isinstance()
中的body
参数,因此它不起作用
你有什么办法让我解决这个问题吗
非常感谢你的帮助
另外,我知道在render()
函数中使用全局变量是不好的,但我无法想出任何其他工作方式,因此请不要指出这一点,这不是我的问题所在
更新在您想要为实例添加代码的注释中,就是这样(一个类用于静态主体-建筑,一个类用于动态主体-行人,其他类的定义类似)
这是步行班
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,
请使用
Car
的代码更新您的问题。如您所愿,请参阅代码更新谢谢,这是一个好的观点,但当我尝试运行它时,我在函数中遇到了一个错误-例如-\u draw\u circle(),对象b2Body没有属性颜色,因为您可以看到该类从技术上由两部分组成,属性,如颜色、尺寸、位置,然后有一个称为self.body的物理属性,它是一个Box2D实体,具有一些质量、惯性等。你有什么办法可以解决这个问题吗?谢谢你能在某种设置中为它们指定一个颜色属性吗?这是我问题的另一部分。Box2D与GUI无关,它不关心颜色
class Car():
color = (133, 187, 101, 0)
# etc for all bodies that need a color.
pygame.draw.circle(screen, body.color,