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).

Pyglet - placing images, text and playing background music

Continuing on our Pyglet introduction you can use the basic environment we set up earlier to place external images (in a few different formats), display text and to add some atmospheric background music. This code is pretty self explanatory so I'll just post the lot and let you dig through it yourself - but one thing to notice is that by itself pyglet/Python can't decode and play .mp3 audio files. It requires an external library called AVBin. So if you get an error when trying to play .mp3 files try installing the AVBin library (see the link below for details).


There are a few other commands in here that are worth pointing out:

  • image.blit(0,0) - the (0,0) is the location of the centre of the image  in x and y co-codinates. 'Blitting' is the process of drawing an image into our window. Otherwise we only load it into memory and never display it.
  • we also use the window.height and the window.width commands to return the current height/width of the window which is a far nicer way of defining the location of things. (N.B. the height and width methods also work on image objects!)

Challenge!

Find some nice atmospheric music for your game (and yes check it's Creative Commons licenced for you to use first!) and setup a background image you want to use and display it in the centre of your image in such a way that if you resize your window it appears in the the same place. I highly recommend checking out the wonderful local composer Rhian Sheehan's Soundcloud for inspiration (and yes you can download some of his .mp3s but make sure to attribute them if you use them!). 

References


Stellarium - Intro to basic scripting

I am a space nut. I love everything about space, from how weird and mysterious it is, to how vastly, hugely, mind-bogglingly big it is to Douglas Adams. It's difficult to explain at the best of times and something like a static powerpoint simply never does it justice. In my travels I have discovered quite a few programmes for exploring the universe, including the weighty Celestia, but my favourite in terms of ease of use is currently Stellarium. It's free and open source and a great way to get into exploring the sky in some detail. Also it comes with a scripting interface and a host of complex examples - so in this tut I'm going to describe the very basics of setting up and automating a Stellarium script.

Installation and Use

Download and install Stellarium here. Run the program and it will show you the stars from Paris for the current date. Have a play and get used to the controls (a full list is here), but some useful basics:
  • A - toggles the presence of the atmosphere
  • P - toggles the labels of the planets
  • Ctrl + Up/Down - zooms in and out
  • Space centres on the selected object
  • F12 - displays the command window (for scripting)
Expanding the bottom left corner pane reveals a spanner, select it and navigate across the the scripts tab to find all the loaded demo scripts. Finally, to edit or create new scripts you will find them in the Stellarium directory's 'scripts' folder and you should be able to edit them with any text editor (if you keep them here they will automatically appear in the scripts list).

Time and Place

The most confusing thing (for me!) about planetarium software is getting the time and place correct that you're looking to replicate. In Stellarium there are two commands for setting and getting time and position data:


These commands return the current time and location in Stellarium and set those as the current location. Nothing too magic here, but you can also pass the set commands any date or location and it will display what is visible from those coordinates.
i.e. These are the stars at 5am on the day I was born, as seen from Wellington, New Zealand:

Setting focus

Because of the aforementioned hugeness of space, it's inadvisable to to jetting off without knowing roughly where you want to go. For that reason, the easiest way to script is to select your target object, turn to face it and them move directly towards it. In Stellarium we can do that with the commands:


The second line of the above sets Stellarium to keep tracking the object as it moves through space. Then we can zoom in and out with:



with the core.wait(5) command setting the script to pause between commands, for 5 seconds (it's always a good idea to do this to allow the camera to complete one motion before starting another). We can direct the zoom to a specific scale with:


Challenge!

Generate a script that centres on and then zooms to the planets in order, then speeds up time to show the rotation of each planet, then finally zooms to our nearest star (Proxima Centauri) and looks back to see our sun from there.

As as per usual here's a full functional script that centres on the moon and then zooms to show Jupiter and the Galilean moons as an example.