...

Night Town

In this class, students will learn how to draw a Night Town using the Turtle graphics library. Through this project, students will have the opportunity understand how to create a reusable function to draw a rectangle and use it in multiple occasions.
The lesson will cover the following topics:


  •     Introduction to the Turtle graphics library
  •     Understanding the While loop
  •     Understanding the For loop
  •     Create reusable function
Step 1: Let's start coding
  • In your logged-in trinket account click on New Trinket -> Python

  • Or Open the blank Python template trinket: Open New.
  • Next
    Step 2: Draw a Night Town

    Create a window

    The first step is to import the Turtle module. Place import statements at the top of your code. Next, define the screen and specify its dimensions, and set the background colour to black. The done() command should be the last statement in a Turtle graphics program. Type the code as follows:

    Create a Night Sky

    Add the following code to add stars in random positions to simulate a night sky:

    Create a function to draw a rectangle

    We define a function named rectangle that takes five parameters:x, y, w, h, and col . These parameters represent the x and y coordinates of the bottom-left corner of the rectangle, as well as its width w and height h. The function's purpose is to draw a rectangle starting from the specified coordinates and with the specified dimensions. col is the colour of the rectangle.

    Draw the Buildings

    We will use a while loop to calculate the number of buildings we can fit into our screen size. We'll draw a rectangle for each building using the rectangle function and select a random colour and building height.

    Draw windows

    Next, we will draw windows. We will use a for loop to calculate the number of windows we can fit into each building.

    Prev Next
    Step 3: The complete code
    Step 4: Save your work

  • If you don't have a Trinket account, click the down arrow and then click Link. This will give you a link that you can save and come back to later.
  • If you have a Trinket account, you can click Remix to save your own copy of the trinket.
  • Prev Next
    Complete and Share

    Submit your final project



    Copy your Trinket Url

  • Click Share->Link
  • Copy the link to share your code
  • Paste link into the field
  • Prev