The purpose of this challenge is to understand how subroutines (functions/procedures) are used in Python to create sections of code that can be called/reused many times within a program. Subroutines mean that a section of code can be identified using a name (identifier). This enables us to add more instructions to the already existing functions used in Python such as print or input.
To create a new subroutine in Python we have to declare it first using the keyword def in Python.
The code for the subroutine is then indented to the right.
A subroutine can also take parameters. In our case, our drawPlayer() subroutine will take four parameters:
- color: (string value) The color name such as “red” or hexadecimal code such as “#FF0000”.
- x: (integer value) The x coordinate of the player between -140 to 140,
- y: (integer value) The y coordinate of the player between -195 to 195,
- label: (string value) The text (name of player or position) to display.
X and Y coordinates? Quadrant?
Check the above picture. Can you state three facts about X and Y coordinates and about quadrants?
The canvas we are drawing on (using Python Turtle) is 400 pixels wide by 400 pixels high.
Look at the canvas below to understand how (x,y) coordinates work:
So to define this function we will now be using the following code with the 4 parameters:
We will then be able to position our players by calling the drawPlayer() subroutine as follows:
Your task consists of adding all 11 players to your formation using the code provided below. We have already started the formation for you by adding the goal keeper and the two centre-back players.
Your formation should include 11 players including:
- 1 goal keeper,
- 2 to 4 fullback players,
- 2 to 4 midfield players,
- 2 to 4 forward players.
Extension Task #1
Add a parameter to the drawPlayer() subroutine to take the squad number of the player.
Change the content of the subroutine to display the squad number on the circle for the player. The output of your program should look like this:
Extension Task #2
During a football games, players can be issued a yellow card or a red card.
Add two parameters to your subroutine to specify if a player has been given a yellow or red card.
Change the text color of the player name or position to:
- Yellow for players who have received a yellow card,
- Red for players who have received a red card,
- Grey (#CCCCCC) for all other players.