Tuesday 3 June 2014

Pyglet - Keymapping and Sound effects

For any real game however you probably want some conditional events - like controlling an object or causing something to happen when a button is pressed. In the below example we map three keys; a, left arrow and the left mouse button to 3 separate actions.


A couple of things to note:

  • to ensure the sound effect plays at the same time as the key is clicked we load the resource into memory instead of streaming the audio file as we did in the earlier example.
  • for keymapping the 'modifiers' field indicates where Caps, Shift or Ctrl is held.
  • Finally for mouse events and x and y is returned giving the location of the mouse cursor in the window when pressed.

Challenge!

Using this sounds effect, create a game that creates yellow ovals at the mirrored mouse location when the left button is pressed. I.e. if a mouse click was at (200, 100) in a 600x800 window then the 'mirrored' location should be at (600-200, 800-100).

No comments:

Post a Comment