Skip main navigation

How to Draw Shapes in Python

You are now going to draw some shapes using objects. Note: Unfortunately, you won’t be able to complete this activity if you are using an online IDE such as Trinket or Repl.it.1. Copy this shapes code, paste it into a new Python file, and save it as shapes.py.

You are now going to draw some shapes using objects.

Note: Unfortunately, you won’t be able to complete this activity if you are using an online IDE such as Trinket or Repl.it.

1. Copy this shapes code, paste it into a new Python file, and save it as shapes.py.

The script contains shape classes that I have written for you; they are the blueprints for Paper and Triangle, Oval, and Rectangle shapes.

2. In the same folder in which you just saved the shapes.py file, create a new Python file and save it as my_drawing.py. This will be where you create your masterpiece!

3. At the top of your file, tell Python you would like to be able to use the Paper, Triangle, Oval, and Rectangle classes from the shapes file:

 

from shapes import Paper, Triangle, Rectangle, Oval

 

4. To have something to draw on, you will need to create an instance of a Paper object.

 

paper = Paper()

 

5. Create your first shape by creating an instance of a Rectangle object.

 

rect1 = Rectangle()

 

6. To set the attributes of the rectangle object, you can use some special methods called setters. The name ‘setters’ comes from the fact that the purpose of these methods is to set values.

 

7. Use the setter methods to give your rectangle object a width, height, and colour.

 

rect1.set_width(200)
rect1.set_height(100)
rect1.set_color("blue")

 

8. You can now use the draw method to draw the rectangle onto the paper.

 

rect1.draw()

 

9. Finally, you should use the display method of the Paper object to show your creation on the screen.

 

paper.display()

 

10. Save and run your code. You should see a blue rectangle appear.

 

 

 

a blue rectangle displayed in a window on the screen

 

The blue rectangle is an instance of the Rectangle class; it was made with the blueprint common to all Rectangles. You used setter methods to customise the attributes of this instance of the object before drawing it onto the Paper object.

 

The attributes of the Rectangle object, such as its size and colour, define how it will be drawn on the screen.

 

The methods allow you to interact with the object, such as changing the attributes using setters or instructing it to be drawn on the screen.

 

Creating a Second Rectangle

 

Create another instance of the class Rectangle. This time, set the attributes of the rectangle to be:

 

 

    • width = 50

 

    • height = 150

 

    • color = “yellow”

 

 

Note: Put the code to create and draw your second Rectangle before the code to display the paper, for example:

 

# put the code to create the 2nd Rectangle here

paper.display()

 

You will notice that the first rectangle will be drawn underneath the second one you drew.

 

Controlling the Position of the New Rectangle

 

Can you move the position of the new rectangle using the set_x() and set_y() methods to define the coordinates of the top left point of the rectangle? Drawing the new rectangle at x = 100 and y = 100 should move it out of the way.

 

How did you approach solving this task? Did you make any mistakes or find anything difficult? Share your experiences in the comments.

 

Remember to use three tilde characters (~~~) at the start and end of your code, as in this example comment text:

 

I created this program.

~~~
print("This is my code")
~~~

Refer to our ‘Sharing code on FutureLearn’ guide for more information.

This article is from the free online

Object-oriented Programming in Python: Create Your Own Adventure Game

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education