添加键盘功能后,我仍然在Pyglet中出错:
我正在用python开发游戏引擎,并且正在尝试Pyglet并打开gl
from pyglet.gl import *
import pyglet
import numpy as np
class Triangle:
def __init__(self):
self.vertices = pyglet.graphics.vertex_list(3, ('v3f', [-0.6,-0.6,0.0, 0.6,-0.6,0.0, 0.0,0.6,0.0]),
('c3B', [255,0,0, 0,255,0, 0,0,255]))
class Quad:
def __init__(self):
self.indices = [0,1,2,2,3,0]
self.vertex = [-0.5,-0.5,0.0, 0.5,-0.5,0.0, 0.5,0.5,0.0, -0.5,0.5,0.0]
self.colour = [0.99,0.10,0.150, 0.9,0.155,0.100, 0.9,0.50,0.20, 0.100,0.50,0.20]
self.vertices = pyglet.graphics.vertex_list_indexed(4, self.indices,('v3f',self.vertex),('c3f', self.colour))
class gameWindow(pyglet.window.Window):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.set_minimum_size(320,240)
glClearColor(.128, .128, .128, .128)
self.triangle = Triangle()
self.quad = Quad()
def on_draw(self):
self.clear()
self.quad.vertices.draw(GL_TRIANGLES)
self.triangle.vertices.draw(GL_TRIANGLES)
def on_resize(self, width, height):
glViewport(0, 0, width, height)
def on_key_press(symbol, modifiers):
if symbol == pyglet.window.key.W:
print('A key was pressed')
if __name__ == "__main__":
window = gameWindow(640,480,"GAME WINDOW", resizable=True)
pyglet.app.run()
然后我在添加on_key_pressed函数之后突然出现了这些错误:
Traceback (most recent call last):
File "C:\Users\rohan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyglet\event.py", line 367, in dispatch_event
if getattr(self, event_type)(*args):
TypeError: on_key_press() takes 2 positional arguments but 3 were given
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "_ctypes/callbacks.c", line 234, in 'calling callback function'
File "C:\Users\rohan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyglet\window\win32\__init__.py", line 639, in f
result = event_handler(msg, wParam, lParam)
File "C:\Users\rohan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyglet\window\win32\__init__.py", line 710, in _event_key
self.dispatch_event(ev, symbol, modifiers)
File "C:\Users\rohan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyglet\window\__init__.py", line 1232, in dispatch_event
if EventDispatcher.dispatch_event(self, *args) != False:
File "C:\Users\rohan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyglet\event.py", line 371, in dispatch_event
event_type, args, getattr(self, event_type))
File "C:\Users\rohan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pyglet\event.py", line 421, in _raise_dispatch_exception
(event_type, len(args), descr))
TypeError: on_key_press event was dispatched with 2 arguments, but handler on_key_press at C:\Users\rohan\AppData\Local\Programs\Python\Python36-32\q.py:28 has an incompatible function signature
我在网上找不到任何东西。
答案 0 :(得分:0)
您忘记了self
的定义中的on_key_press()
参数。
你有这个:
def on_key_press(symbol, modifiers):
但是您应该改用它:
def on_key_press(self, symbol, modifiers):