Lab 7: Sprites and pygame (part II)

Next we'll work on extending our GameObject class and event handling to allow the user to

Because we have a nice clean structure to our program, the only things that need to change from the version we created in part I are the GameObject class and the definition and call to the processEvents routine.


Changes to the main routine

To accomodate the new, fancier event processing for our game, we'll need to make a couple of small changes to our main routine:

Thus our new global variable section now looks like this:

And our new main routine now looks like this:

Changing the ship constructor

Our new, fancier ship controls mean that we have to keep track of a bit more information about each ship (game object), which means we need to spruce up our constructor a bit:

Changing the ship constructor

Our new, fancier ship controls also mean that our update routine needs to be a bit more complex:

Changing the ship selected

Eventually we might want to have the currently selected ship look differently (e.g. outline it, highlight it, etc) so the player can see visually which one is currently "it".

That means we should include a routine in the GameObject class to change/store whether or not the current ship is selected.

This routine would get called by the processEvents routine whenever a player hit the TAB key, and here I'm assuming processEvents would send along a flag specifying whether this ship was now selected or deselected.

Changing the ship facing

We will also need a routine that changes the direction of the ship when instructed to turn left or right.

This routine would get called by the processEvents routine whenever a player hit the LEFT or RIGHT arrow, and here I'm assuming processEvents would send along a letter (l or r) to indicate which direction to turn.

Changing the ship velocity

Needless to say, we also need a routine that alters the ship's velocity when instructed to do so (again by ProcessEvents).

Here I'm assuming processEvents would send along an = or - character specifying whether the ship should be sped up or slowed down.

Changing the ship size

Finally, we need a routine that alters the ship's size (scaling or zoom factor) when instructed to do so (again by ProcessEvents).

Here I'm assuming processEvents would send along an = or - character specifying whether the ship should be scaled up or down.

Event processing routine

Now, the event processing routine needs a major overhaul to respond to all the different events, calling the right GameObject routine in each case:

Complete version

Hopefully the game is now fully functional, a complete copy of the code is given here: