[英]Cannot Get Pyglet To Render
我正在嘗試構建一個小的API,以簡化使用pyglet時的某些事情。 我希望它與Processing語法相似,因此我可以在純python環境中使用我的Python Processing代碼,而不是對其進行處理。
我已經獲得一些代碼來繪制矩形,但是僅當我復制/粘貼時才起作用。 我目前使用的模型似乎沒有任何作用。
這是我正在使用的App類的重要部分:
import pyglet
class App(pyglet.window.Window):
def __init__(self, w=1280, h=720, fullscreen = False, resizable = True):
super(App, self).__init__(w, h, fullscreen = fullscreen, resizable=resizable)
self.size = self.width, self.height = w, h
self.alive = 1
def on_draw(self):
self.render()
def setBinds(self, drawFunction):
self.drawFunction = drawFunction
def render(self):
self.clear()
self.drawFunction()
self.flip()
def run(self):
while self.alive == 1:
self.render()
event = self.dispatch_events()
def Rect(self, x, y, w, h):
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2f', [x, y, w, y, w, h, x, h]))
這是運行應用程序的代碼。 它應該打開一個窗口,然后每幀在屏幕上繪制一個矩形。
from processing import App
def draw(dt=0):
width, height = APP.width, APP.height
APP.Rect(100,100,100,100)
if __name__ == "__main__":
APP = App()
APP.setBinds(drawFunction=draw)
APP.run()
但是,它不會在屏幕上繪制任何內容。 沒有錯誤顯示或任何東西,只有黑屏。 使用簡化代碼(例如)時,我已經獲得pyglet.grapics.draw函數在屏幕上繪制內容:
import pyglet
from pyglet.window import mouse
window = pyglet.window.Window()
@window.event
def on_draw():
window.clear()
x, y, dx, dy = 100, 100, 20, 30
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2f', [x, y, dx, y, dx, dy, x, dy]))
pyglet.app.run()
我不確定這兩組代碼之間的區別是什么使其無法正常工作
當您使用以下參數調用Rect
方法時
APP.Rect(100,100,100,100)
然后指令
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2f', [x, y, w, y, w, h, x, h]))
不繪制矩形,因為所有頂點坐標都相同。
要繪制矩形,您必須分別通過x+w
和y+h
來計算右頂點和頂頂點坐標:
x1, y1, x2, y2 = x, y, x+w, y+h
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2f', [x1, y1, x2, y1, x2, y2, x1, y2]))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.