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.
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
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!
At the top of your file, tell Python you would like to be able to use the
Rectangleclasses from the
from shapes import Paper, Triangle, Rectangle, Oval
To have something to draw on, you will need to create an instance of a
paper = Paper()
Create your first shape by creating an instance of a
rect1 = Rectangle()
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.
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")
You can now use the
drawmethod to draw the rectangle onto the paper.
Finally, you should use the
displaymethod of the
Paperobject to show your creation on the screen.
Save and run your code. You should see a blue rectangle appear.
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
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:
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_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.