gpt4 book ai didi

python - Pyglet - 游戏运行缓慢,对事件和东西感到困惑

转载 作者:行者123 更新时间:2023-11-28 16:47:16 25 4
gpt4 key购买 nike

好的,我正在用 Pyglet 制作一个小游戏/原型(prototype),我对事件感到困惑。游戏运行不佳,通过分析我知道这是因为 on_draw() 由于 pyglet.clock.schedule_interval() 而每秒被调用 60 次。我不完全知道为什么 on_draw() 当前正在使用它可以使用的所有 CPU,知道这一点会很高兴。通过更多分析,我知道绘制 100 个 Sprites 也需要大量 CPU,比我认为它应该占用的更多(我什至不知道它应该占用 CPU 还是只占用 GPU)。

  1. 默认情况下 on_draw() 做什么?我可以避免任何无用的额外内容吗?
  2. 我怎样才能使 schedule_interval() 不触发 on_draw()
  3. 有什么有效的绘图/blitting 方法吗?

部分代码:

screen = pyglet.window.Window(800, 600, vsync=False, caption="Project")

tileimage = pyglet.image.create(50, 50, pyglet.image.SolidColorImagePattern((0, 255, 0, 255)))

class tileclass(pyglet.sprite.Sprite):
def __init__(self, x, y):
pyglet.sprite.Sprite.__init__(self, tileimage)
self.x = x
self.y = y
self.rect = pygame.Rect(self.x - self.width / 2, self.y - self.height / 2, self.width, self.height)
self.image.anchor_x = self.width // 2
self.image.anchor_y = self.height // 2

tiles = []

x = 25
y = 25
for i in range(100):
tiles.append(tileclass(x, y))
if x == 475:
x = 25
y+=50
else:
x+=50

@screen.event
def on_draw():
screen.clear()
for t in tiles:
t.draw()
fps.draw()

pyglet.clock.schedule_interval(update, 1/60) #Logic stuff

谢谢。

最佳答案

尝试将您的 Sprite 添加到一个批处理中并绘制它。在 pyglet 中,绘制调用很慢,因为这就是 OpenGL 的工作方式。基本上你想尽量减少你做的抽奖次数,并尽可能多地将东西放在一起。通过创建一个批处理,然后将所有 Sprite 添加到其中,最后只绘制该批处理,我对您的示例进行了巨大改进。

http://pyglet.org/doc/programming_guide/displaying_images.html#sprites

这与 pygame 形成鲜明对比,在 pygame 中,小图像比大图像便宜,并且您通常通过绘制较少的像素来加快游戏速度(通常只重绘自上一帧以来发生变化的屏幕部分等等)。

关于python - Pyglet - 游戏运行缓慢,对事件和东西感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12429972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com