Let’s Build a Castle

In this challenge we are going to build different medieval castles by customising some Python code.

Our first castle is already made for us and looks as follows:

You can see, in the trinket window below, the Python code used to create this castle.

The code uses a range of pre-defined functions to draw the different building blocks of the castle (walls, towers, doors, flags and loopholes). These functions can be accessed in the extra file (tab) called shapes.py. However for the purpose of this challenge, you will not need to access the code of these functions.

The main functions that you will have to reuse to cutomise the look and feel of your castle are:

  • drawWall()
  • drawTower()
  • drawDoor()
  • drawLoophole()
  • drawFlag()

Each of these functions is using some parameters to change the position, width and height of the different building blocks and to change the colour of the flag. Let’s investigate these functions and their parameters.

drawWall()drawTower()drawFlag()drawDoor()drawLoophole()(X,Y) Coordinates?Colour Codes?
The drawWall() function uses the following 6 parameters:

So the following code would be used to create a wall at position (-150,0). The wall would be 300 pixels wide, 150 pixels high and would have battlements (crenellation).

The drawTower() function uses the following 6 parameters:

So the following code would be used to create a tower at position (-100,0). The tower would be 50 pixels wide, 150 pixels high and would have battlements (crenellation).

The drawFlag() function uses the following 6 parameters:So the following code would be used to create a flag at position (120,120). The flag would be 30 pixels wide, 40 pixels high and be of a red colour.
The drawDoor() function uses the following 5 parameters:
So the following code would be used to create a door at position (-100,0). The door would be 50 pixels wide, 150 pixels high.
The drawDoor() function uses the following 5 parameters:
So the following code would be used to create a loophole at position (-100,0). The loophole would be 10 pixels wide, 30 pixels high.

(X,Y) Coordinates?


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:


Colour Codes


#FF0000#FFFF00#0000AA#00AA00

Python Code

Here is the Python code to create the castle.

Your Task

Your task is to edit the code from line 16 to 27 to re-create the following castles. To do so you will have to tweak the parameters of the functions used to create the castle. You may also need to remove or add some extra lines of code to add extra elements (towers, flags, loopholes, etc…)

Extension Task

Access the code in the shapes.py file (tab).
Create two more functions as follows:

  • drawSun() to draw a yellow circle at a given (x,y) set of coordinates.
  • drawTree() to draw a tree at a given (x,y) set of coordinates.
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 4.2 / 5. Vote count: 223

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: