Sub programs - decomposing and abstracting to make large programs easier to write and understand
Writing complex programs is easier if we use sub programs to break down different sections.
Subprograms are small programs that are written within a larger, main program. The purpose of a subprogram is to perform a specific task. This task may need to be done more than once at various points in the main program. In this exercise, you will create a Games Zone where people can choose which game to play. We will go this by creating 3 sub programs:
Download the starting code below. Copy and paste it into Thonny and save it with a suitable name. Follow the instructions written in comments to create the Game Zone program. When it is working, attach it to an email to your teacher. |
game_zone.txt | |
File Size: | 1 kb |
File Type: | txt |
Dice game with turtle graphics
In this exercise you will fix 4 errors to complete a working dice game with turtle graphics.
You will then answer some questions about the code. Download the code below. Copy and paste it into a new Thonny file and save it with a relevant name. There are 4 errors to fix. Look for the lines ____ and read the instructions in the comments. The errors are around line 3, line 32, line 52 and line 92. When the code is working, attach it to an email to your teacher.
|
Turtle drawing game
In this exercise you will be given code that uses Turtle to play an addictive drawing game.
Then you will be given the challenge to improve the game by adding a sub program to change the colour of the line when the space bar is pressed. You will then be free to epxeriment - perhaps add a second turtle so 2 people can draw at once. Perhaps change the colour on different key presses - eg. R for red, B for blue etc. It will be up to you! Download the code below and copy and paste it into a new Thonny file. Save it with a suitable name. Adding and using a sub program
3. Now you need to add the sub program to change the turtle pen colour. The code is below. Put it in the section with the other sub programs.
4. Finally you need to call this sub program on the press of the space key. Work out where to put it and then test your game.
|
Experiment!
Perhaps add a second turtle so 2 people can draw at once. Perhaps change the colour on different key presses - eg. R for red, B for blue etc. It will be up to you!