
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.)
Copy over a few files into your Lab10 folder:
turtle_interpreter.py, lsystem.py, and shapes.py from Project 9.mosaic.py). This will be the background artwork for your video game!Open up a Terminal (e.g. Control+` in VS Code) and change the directory (cd) to the Lab10 folder.
Note: You are welcome to create another background art scene for the game later. You can think of this as an example to get you up and running with the game quickly.
TurtleInterpreter classMake the following changes to the TurtleInterpreter class:
hideturtle method on your Turtle object. This will make the turtle invisible as it draws. This way, you won't have a turtle appearing somewhere on background of your game! Remember: your Turtle object is now a class global variable, not an instance variable.getScreen method that returns the Screen object, a class global variable.mosaic.py). Make sure that your mosaic draws as expected.hold in your mosaic function.If your mosaic takes too long to draw, you can simplify it — it does not need to be exactly the same as last week!
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:
mosaic.py) to draw your background artwork mosaic so that it fills the background of the window.TurtleInterpreter object (does not need to be an instance variable).Screen object, which you should get by calling the getScreen method that you wrote in TurtleInterpreter on the object.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): '''Turns the tracer animations on (but speeds up animations) and starts the main game loop. ''' # Call the tracer method on your `Screen` instance variable, # passing in True as the parameter to turn animations on. # Call the listen method on your `Screen` instance variable # so that keyboard presses are not registered as events # Call the mainloop method on your `Screen` instance 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, you should simply see your background artwork mosaic drawn to the screen (no game elements yet).
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):self.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.
Add instance variables for player speed (units: pixels) and turn rate (units: degrees) in the Game constructor. You might wish to create keyword arguments for these, 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 speeddown: Move the player Turtle object backward by its speedleft: Turn the player Turtle object left by its turn rateright: Turn the player Turtle object right by its turn ratesetupEvents methodThis method should be responsible for creating your game events. In the setupEvents method, you should:
Screen object. It has the following syntax:self.screen.onkey(<Method name>, <String of keyboard button>). For example self.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".turtle.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 turtle.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:
self.screen.onkey(self.up, "Up"), not self.screen.onkey(self.up(), "Up")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