Pyglet:在游戏循环中获取事件

时间:2015-02-26 14:28:32

标签: python pyglet

我希望能够访问“游戏循环”中的鼠标和键盘事件。要做到这一点,我使用一个包含一个名为' lastevent'的变量的类,一个返回它的方法,称为' getevent'和一个Pyglet窗口,其on_mouse_press / release方法被覆盖。

这是我使用的代码:

import pyglet 

class mypygletwindow:

    def __init__(self):
        self.winHandle = pyglet.window.Window()
        self.winHandle.on_mouse_press   = self._onPygletMousePress
        self.winHandle.on_mouse_release = self._onPygletMouseRelease
        self.winHandle.on_key_press     = self._onPygletKeyPress
        self.winHandle.on_key_release   = self._onPygletKeyRelease
        self.winHandle.getevent         = self.getevent

        self.lastevent = {}

    def _onPygletMousePress(self, x,y,button,modifiers):
        print 'press', button, (x,y)
        self.lastevent = {'type':'MOUSEDOWN', 'button': button, 'pos': (x,y)}

    def _onPygletMouseRelease(self,x,y, button, modifiers):
        print 'release', button, (x,y)
        self.lastevent = {'type':'MOUSEUP', 'button': button, 'pos': (x,y)}

    def _onPygletKeyPress(self, symbol, modifiers):
        print 'press', symbol, modifiers
        self.lastevent = {'type':'KEYDOWN', 'symbol': symbol}

    def _onPygletKeyRelease(self, symbol, modifiers):
        print 'release', symbol, modifiers
        self.lastevent = {'type':'KEYUP', 'symbol': symbol}

    def getevent(self):
        out = self.lastevent
        self.lastevent = {}
        return out

if __name__ == '__main__':

    w = mypygletwindow().winHandle

    while not w.has_exit:
        dt = pyglet.clock.tick()
        w.dispatch_events()
        w.clear()

        # get events
        e = w.getevent()
        if e != {}:
            print e
        w.flip()

在代码的底部,我使用' getevent'在游戏中循环播放#39;目标是获得发生的所有事件。然而,当鼠标按压与其相应释放之间的时间非常短时,仅获得鼠标释放。我能理解' on_mouse_press / release'调用方法比调用getevent的方法更快。

我如何才能获得游戏循环中的所有事件?

1 个答案:

答案 0 :(得分:0)

import pyglet
from time import time, sleep

class Window(pyglet.window.Window):
    def __init__(self, refreshrate):
        super(Window, self).__init__(vsync = False)
        self.frames = 0
        self.framerate = pyglet.text.Label(text='Unknown', font_name='Verdana', font_size=8, x=10, y=10, color=(255,255,255,255))
        self.last = time()
        self.alive = 1
        self.refreshrate = refreshrate

    def on_draw(self):
        self.render()

    def on_key_press(self, symbol, modkey):
        print('Key Down:',symbol)

    def on_key_release(self, symbol, modkey):
        print('Key Released:',symbol)

    def on_mouse_release(self, x, y, button, modifiers):
        print('Mouse Released:', x, y, button)

    def on_mouse_press(self, x, y, button, modifiers):
        print('Mouse Down:', x, y, button)

    def on_mouse_motion(self, x, y, dx, dy):
        pass

    def render(self):
        self.clear()
        if time() - self.last >= 1:
            self.framerate.text = str(self.frames)
            self.frames = 0
            self.last = time()
        else:
            self.frames += 1
        self.framerate.draw()
        self.flip()

    def on_close(self):
        self.alive = 0

    def run(self):
        while self.alive:
            self.render()
            event = self.dispatch_events() # <-- This is the event queue
            sleep(1.0/self.refreshrate)

win = Window(23) # set the fps
win.run()

看看这是否更有意义。 它是一个受到批评的版本。