微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pyglet方法中的形状

如何解决pyglet方法中的形状

为什么在render_rect()方法中创建的矩形“ test2”不渲染?我怀疑这与在调用render_rect()后不触发on_draw()或其他事情有关,但我不太清楚。如何在方法中正确使用pyglet中的形状,子图形等?

from pyglet import shapes

WHITE = (255,255,255)

HEIGHT = 1080
WIDTH = 720


window = pyglet.window.Window(HEIGHT,WIDTH)
background = pyglet.graphics.Batch()


test = shapes.Rectangle(200,200,50,color=WHITE,batch=background) # Renders successfuly

def render_rect():
    test2 = shapes.Rectangle(300,300,batch=background)        # Does not render
render_rect()


@window.event
def on_draw():
    window.clear()
    background.draw()


pyglet.app.run()

在我的other question中,这似乎是命名引用的问题,所以如果我这样做

def render_rect():
    test2 = shapes.Rectangle(300,batch=background)
    return test2
test = render_rect()

有效。但是,如果要在一种方法中定义多个形状怎么办?像这样:

def render_rects():
    test2 = shapes.Rectangle(300,batch=background)
    test3 = shapes.Rectangle(400,batch=background)
    test4 = shapes.Rectangle(500,batch=background)
render_rects()

我是否真的必须返回列表中的所有形状并在方法之外逐一命名,还是有一种更简单的方法

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。