Write a program using an object
Start to create your object-oriented text-based adventure game
Extend other people’s classes
Create an enemy in the room
Recap Week 3
Extending your knowledge of OOP
Finish your game
End of the course
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 Paper and Triangle, Oval, and Rectangle shapes.
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 Paper, Triangle, Oval, and Rectangle classes from the shapes file:
from shapes import Paper, Triangle, Rectangle, Oval
To have something to draw on, you will need to create an instance of a Paper object.
paper = Paper()
Create your first shape by creating an instance of a Rectangle object.
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 draw method to draw the rectangle onto the paper.
rect1.draw()
Finally, you should use the display method of the Paper object to show your creation on the screen.
paper.display()
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 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.
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.
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.