Want to keep learning?

This content is taken from the Raspberry Pi Foundation & National Centre for Computing Education's online course, Object-oriented Programming in Python: Create Your Own Adventure Game. Join the course to learn more.

Skip to 0 minutes and 3 seconds Now, you are going to draw some shapes using objects. The article below contains a link to some starter code, which you can copy into a new Python program. And this will give you the blueprints for paper, triangle, oval, and rectangle objects. So the first thing I’m going to do is I’m going to do just that. I’m going to take the starter code, I’m going to paste that into a new Python program, and I’m going to save that as shapes.py.

Skip to 0 minutes and 35 seconds Then, again, I’m going to write a new program. This time I’m going to save it as mydrawing. So this is going to be where I create my masterpiece. Now, the first thing I need to do is I need to import the shape objects from the shapes program that we just created. So I’m going to say from shapes import Paper, Triangle, Rectangle, and Oval. And once I’ve imported those classes, I’m going to create a new Paper object. This is going to be where my shapes are going to appear– where they’re going to be displayed. And the first shape I’m going to create is going to be a rectangle. So it’s rectangle number one, and I use the Rectangle class.

Skip to 1 minute and 31 seconds And I’m going to use the setter methods of the Rectangle objects to define a width, a height, and a colour. So I’m going to say rect1, and I’m going to say set width. And I want my rectangle to be 200 wide, and I’m going to set its height to be 100. Finally, I want my rectangle to be blue. So I’m going to say set colour, and I’m going to pass blue. Now, once I’ve set up my rectangle objects, I’ve defined its attributes by using the setter methods, I can then use the draw method to tell it to draw on the piece of paper.

Skip to 2 minutes and 18 seconds Now, once I’m happy with my drawing and I’ve added all the shapes that I want, I can use my Paper object to tell it to display.

Skip to 2 minutes and 29 seconds Now, let’s have a look at that drawing. So if I run my program, what I should see is my paper window appears. And it’s got a blue rectangle on it, which is width of 200, height of 100. Now, your task is to create a second rectangle that has a different, width, height, and colour. Now, here are some points to note– firstly, when you create your next object– your new rectangle– you need to do it before the paper is displayed. Because, otherwise, you won’t see your rectangle on the piece of paper. The second one is when you create your second rectangle, what you’re going to see is it’s going to be on top of the other one.

Skip to 3 minutes and 13 seconds Because they’re at the same position on the piece of paper. Now, we can change the position of our shape objects by using the set_x and set_y methods.

Skip to 3 minutes and 33 seconds So have a go. Think about how you approached solving this task. Perhaps did you make some mistakes or did you find anything difficult? Please share your experiences in the comments below.

Drawing shapes to apply your understanding of methods and attributes

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.

Share this video:

This video is from the free online course:

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

Raspberry Pi Foundation