Game 0.5 - Animation Refactoring
To prepare for a game with user interactions, we first want to do a bit of refactoring of our animation as well as add a couple of small features.
Preliminary File Sync
New files have been added to the project. To get them, you’ll need to merge a pull request on github. We’ll walk through this in class, but if you need help stepping through it, just ask. Be sure to do the merge before making the changes described below.
Game 0.5 Feature Requirements
- The animation now includes a title with instructions to press space bar (or any specific key you want) to start.
- Pressing space bar (or your chosen key) will end the introduction animation and launch the level 1 main.
- The level 1 main prints
"Level 1"to the terminal.
Game 0.5 Specifications
The bulk of your work is to reorganized the code into modules (separate files).
- All start animation code is in a file/module named
intro.py. - All game constants are declared in a file/module named
constants.py. This module is imported intointro.pyto supply the necessary constants; explicit constant declarations are removed fromintro.py. - A new module named
level1.pyis created for level 1. For starters it only has one function,mainthat prints"Level 1!". - There is a module named
main.pythat has one function,main, that initializes pygame and the display, then launches the intromainfollowed by the level 1main
TODO
To meet your specifications, you should work through the following steps.
- Create
constants.py. Move all global constants from the currentmain.pyto this file. Import this file intomain.pyand fix constant references as needed. Run yourmain.pyto ensure your animation runs as it did before. - Rename
main.pytointro.pyand create a newmain.py. In the newmain.py, add the necessary imports, then create a functionmain(calledmain.mainfrom here on) that calls the themainfromintro.py(now calledintro.main). Add the standard “if main” conditional to the end ofmain.pysuch thatmain.mainis called when run. Runmain.pyto ensure your animation runs as before. - In
tests.pycorrect imports and/or function calls as needed so that all tests for the functions that are now inintro.pyare run when you runtests.pywith pytest. - Modify
intro.mainso that it takes a singleSurfaceTypeparameter, the display surface. Move the code that initializes pygame and the display fromintro.mainto themain.main, then pass the display surface (probably nameddisplay_surf) tointro.mainwhen you call it. Runmain.mainto ensure your animation once again runs like it did before. - Create
level1.py. Include the necessary imports and a single function,main(here on calledlevel1.main) that only prints"Level 1!"when executed. Add a call tolevel1.maininmain.mainafter the call tointro.main. - Modify
intro.mainto include the title and request to press a key to start in the animation. - Modify
intro.mainso that pressing the key will end the game loop. If everything is working, then you’ll see the Level 1! output on the terminal after you press the designated key.