lundi 27 juin 2016

Python and Pyglet input


Total python beginner here. I'm trying to make a game with the Python library Pyglet. It's been working great until I got to the part where I need the input. I've followed the examples and docs and my code looks something like this. I will only focus on the mouse input for now as the keyboard input seems to work in the same way. import pyglet import button from pyglet.window import key class GameWindow(pyglet.window.Window): def __init__(self): super(GameWindow, self).__init__() self.button = button.Button() def on_draw(self): self.clear() def on_mouse_press(self, x, y, button, modifiers): self.button.on_mouse_press(x, y, button, modifiers) print("mouse pressed") def on_key_press(self, symbol, modifiers): print("key pressed") def on_mouse_motion(self, x, y, dx, dy): print("mouse moved") def update(dt): pass #initializes window and keyboard window = GameWindow() keys = key.KeyStateHandler() window.push_handlers(keys) pyglet.clock.schedule_interval(GameWindow.update, 1/60.0) if __name__ == '__main__': pyglet.app.run() The Code works just fine but my question is this: Is this the correct way to handle input? Should I really send the parameters manually like that to my button class? Is it not possible for my button class to override the function for input so the button gathers the input itself? Is it possible to create a window like I am and gather the input in another unrelated class like my Button class for example? as of now I have to send the input to the buttons, the chat window, the physicsentities and to some other places. im afraid im doing it backwards and it will bite me in the ass later.

Aucun commentaire:

Enregistrer un commentaire