Lab 09: Event-based programming

In this lab, you will explore event-based programming by creating a simple video game. Specifically, we will use callbacks, and keyboard button events.


Tasks

L0. The setup

Create a Lab9 folder on your computer where you store CS151 projects and labs, hopefully somewhere that is backed up (e.g. in Dropbox, Google Drive, Microsoft One Drive, etc.)

Open up a Terminal (e.g. Control+` in VS Code) and change the directory (cd) to the Lab9 folder.

L1. Make your Game class

L1a. Create the constructor

Create a new file asteroids.py and in it define a class called Game.

Create a constructor with no parameters other than the usual self. In it, write code that does the following:

  1. Outside of the constructor, create a global class variable for Screen object.
  2. In the constructor, setup the Screen class variable's width and height to be 800 by 800 with the background color blue.
  3. In the constructor, set the Screen class variable's tracer to 0.
  4. In the constructor, assign 20 to a speed instance variable.
  5. In the constructor, assign 10 to an turnRate instance variable.

L1b. Create the play method

Copy-and-paste the following play method into your Game class. Fill it in according to the instructions in the comments. This is the method you will call when you want to start interactive gameplay.

def play(self):
    '''Starts the main game loop.
    '''

    # Call the listen method on your `Screen` class variable
    # so that keyboard presses are not registered as events

    # Call the update method on your `Screen` class variable

    # Call the mainloop method on your `Screen` class variable.

Create a main function outside the Game class that creates a Game object and calls the play method on it. Call the main function from top-level code in the usual if __name__ == '__main__' conditional statement.

Run asteroids.py. If everything is working so far, a blank screen should appear.


L2. Create a main player

Create a makePlayer method in Game. It should

Call the makePlayer method from your constructor, assigning the returned Turtle object to an instance variable called something like self.player.

Run your game. You should now see your rocketship in the scene.


L3. Control the main player and make the game interactive

In this task, you will make the player turtle (rocketship) move under keyboard control.

L3a. Parameterize the main player controls

Use the instance variables for player speed (units: pixels) and turn rate (units: degrees) in the Game constructor, giving them default values of 20 and 10, respectively.

L3b. Create movement methods

Create the following methods in Game (with no parameters other than self):

L3c. Create the setupEvents method

This method should be responsible for creating your game events. In the setupEvents method, you should:

  1. Add keyboard callback events to the four movement methods you just created. Here, callback means that we will instruct our game to move the player by calling one of movement methods when we press the appropriate keyboard button. To add a callback, we call the onkeypress method of the Screen object. It has the following syntax:
    Game.screen.onkey(<Method name>, <String of keyboard button>). For example Game.screen.onkeypress(self.up, "Up") will move the main player up with you press the up arrow key. To assign the three other arrow keys, use the strings: "Down", "Left", and "Right".
  2. Add a callback to call the Screen object's bye function when the user presses the "q" key (You could instead use the built-in Python function quit that we have used before, but the Screen object's bye works more reliably).

Call the setupEvents method at the end of the Game constructor.

Run your game. You should now be able to move your player around the scene with the arrow keys (and change the heading with the left/right keys)!

Notes:

L3d. Update turtle image on rotation

To have the rocket ship rotate, update makePlayer, moveRight and moveLeft to use multiple rocketship images. The idea would be that if the rocket turns by 10 degree intervals, then every time it turns change the shape of the rocket by choosing the associated file.

  1. Download the collection of rocketships zip file, and unzip it in your lab folder.
  2. Update makePlayer, so that rocketships/Rocketship0.gif through rocketships/Rocketship350.gif are registered as turtle shapes.
  3. Update moveRight and moveLeft , so that the player's shape is set to the correctly rotated rocketship image. Determine the image name using the player's heading. Use the corresponding image name as a parameter for the player's shape method.

L4. Record a video of your interface

In your screen recording demo moving the main player using key strokes.

I suggest using the screen recording feature of Quicktime on macOS or Xbox Game Bar on Windows 10.

If you have trouble including the video in your Google Doc report, no problem, just include it in your project folder.


Submit Lab Code and Video on Google Classroom

Upload and submit asteroids.py and your screen recording on Google Classroom.

Get started with the project

Once you are done with each of the lab tasks, you may start on the project.


© 2021 Hannen Wolfe and Oliver Layton