Skip to 0 minutes and 2 secondsTo apply this principle, let's draw some shapes. First download the code and save it as shapes.py.
Skip to 0 minutes and 11 secondsThis file contains the shape classes, which we have written for you. These are the basic blueprints for how to create a triangle, oval, and rectangle.
Skip to 0 minutes and 27 secondsIn the same folder as 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.
Skip to 0 minutes and 43 secondsAt the start of your file, tell Python you would like to be able to use the triangle, oval, and rectangle classes from the shapes file.
Skip to 0 minutes and 55 secondsNow let's create a rectangle object, where we'll use some special methods called setters to set the attributes of the object. The name setters comes from the fact that the purpose of these methods is to set the values. Finally, let's call another method to draw the rectangle object. Save and run your code, you should see a blue rectangle appear on the screen. This object is an instance of the rectangle class, it was made with the blueprint common to all rectangles, and then we customise the attributes of this instance of the object.
Drawing shapes to apply your understanding of methods and attributes
Let’s draw some shapes!
This exercise is the only one which will not work in Trinket. You can install Python 3 on your computer and use the IDLE code editor to complete this task.
Copy the code here, paste it into a blank Python file, and save it as shapes.py.
The script contains shape classes we have written for you – they are the blueprints for a
Rectangle. Your turn to write your own class is coming up next week.
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
Rectangle classes from the shapes file:
from shapes import Triangle, Rectangle, Oval
Now, let’s create an instance of a
rect1 = Rectangle()
We will use some special methods called setters to set the attributes of the object. The name setters comes from the fact that the purpose of these methods is to set values.
rect1.set_width(200) rect1.set_height(100) rect1.set_color("blue")
Finally, let’s call another method to draw the
Rectangle object we have created:
Save and run your code. You should see a blue rectangle appear. This object is an instance of the
Rectangle class – it was made with the blueprint common to all
Rectangles, and we customised the attributes of this instance of the object.
Can you create another instance of a the class
Rectangle, but set the attributes of this one as 50
height, and yellow
Your original rectangle will be underneath the second one you drew. 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 100, 100 should move it out of the way.
The documentation for these classes is available. If you see an error message when the page first loads, please refresh your browser. If you are able to read class documentation, you will be able to work out what attributes and methods are available to you for each object. Look at this screenshot:
Here we can see that the
Oval class has a method
randomise() which will randomly choose a value for each the attributes. Create an
Oval and call the
randomise() method on it before calling the
draw() method to display it.
Have a look at the documentation to find out what other methods are available to you. Use this to create your own masterpiece of art using triangles, ovals and rectangles.
© CC BY-SA 4.0