
In this lab, you will explore event-based programming by creating a simple video game. Specifically, we will use callbacks, and keyboard button events.
Create a Lab10 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 Lab10 folder.
Game classCreate 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:
Screen object.Screen class variable's width and height to be 800 by 800 with the background color blue. Screen class variable's mode to 'logo' and tracer to 0.speed instance variable.turnRate instance variable.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.
Create a makePlayer method in Game. It should
Turtle object that will be the main player that will move under keyboard control.Screen object name as necessary):Game.screen.register_shape('rocketship.gif')shape method on the Turtle object to turn it into the rocketship. The name is given by a string of the image filename, 'rocketship.gif'in this case.Turtle object.Call the makePlayer method from your constructor (after you create the background artwork), 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.
In this task, you will make the player turtle (rocketship) move under keyboard control.
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.
Create the following methods in Game (with no parameters other than self):
up: Move the player Turtle object forward by its speed and call updatedown: Move the player Turtle object backward by its speed and call updateleft: Turn the player Turtle object left by its turn rate and call updateright: Turn the player Turtle object right by its turn rate and call updatesetupEvents methodThis method should be responsible for creating your game events. In the setupEvents method, you should:
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".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:
Game.screen.onkey(self.up, "Up"), not Game.screen.onkey(self.up(), "Up")
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.
makePlayer, so that rocketships/Rocketship0.gif through rocketships/Rocketship350.gif are registered as turtle shapes. moveRight and moveLeft , so that the player's shape is set to the correctly rotated rocketship image. Determine the image name by calculating the current angle (350 - the player's heading). Use the corresponding image name as a parameter for the player'sshape method.In your screen recording demo moving the main player using key strokes. This is required documentation 1 in your report.
Document in your report how your control keys move the main player.
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.
Once you are done with each of the lab tasks, you may start on the project.
© 2021 Hannen Wolfe and Oliver Layton