在示例 pong.py 中使用 World-Entity-Applicator 模式渲染多边形
sdl2.sdlgfx.filledPolygonRGBA(renderer.sdlrenderer, x_array, y_array, vertex_count, *color)
Applicator 不会在其组件集中选择自定义实体。
我写了下面的代码来重现这个问题。我期待它在“这里应该列出多边形:”之后打印出一些东西,但它什么也没打印出来。
import sdl2.ext
class Polygon(sdl2.ext.Entity):
def __init__(self, world, x, y, vertices):
self.position = (x, y)
self.vertices = vertices
class PolygonApplicator(sdl2.ext.Applicator):
def __init__(self):
super().__init__()
self.componenttypes = (Polygon,)
def process(self, world, componentsets):
print("There should be polygons listed here:")
for polygon in componentsets:
# Draw the polygon entity here
print(polygon)
def run():
sdl2.ext.init()
window = sdl2.ext.Window("Polygon Example", size=(640, 480))
window.show()
world = sdl2.ext.World()
world.add_system(PolygonApplicator())
polygon = Polygon(world, 100, 100, [(0, 0), (50, 0), (50, 50), (0, 50)])
running = True
while running:
events = sdl2.ext.get_events()
for event in events:
if event.type == sdl2.SDL_QUIT:
running = False
break
world.process()
window.refresh()
sdl2.ext.quit()
if __name__ == "__main__":
run()