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 use the shapes classes to create a masterpiece of your own.
Did you complete the task on the previous step and display two rectangles on your drawing? Here is an example solution, if you would like to review how it could be done.
The documentation for the shapes classes can be found here. By reading the class documentation, you can work out what attributes and methods are available to you for each object.
In this snippet from the documentation, you can see that the Oval class has a method randomize(), which will randomly choose a value for each of the attributes.
class Oval(Shape)
Oval(width=50, height=50, x=None, y=None, color='black')
randomize(self, smallest=20, largest=200)
Randomly generates width, height, position and colour for a shape. You can specify
the smallest and largest random size that will be generated. If not specified, the
generated shape will default to a random size between 20 and 200.
Args:
smallest (int): The smallest the shape can be. Defaults to 20
largest (int): The largest the shape can be. Defaults to 200.
Explore the shapes class documentation. Use what you find out from this about other classes and methods to:
Create an Oval and call the randomize() method on it before calling the draw() method to display it.
Create a drawing using rectangles, ovals, and triangles.
Tip: The Triangle class is a little different to Rectangle and Oval classes, because to create a triangle you need to specify the coordinates of the three points that will be the three corners of the triangle. For example, this code creates a right-angled triangle with corners at (5,5), (100,5), and (100,200):
tri = Triangle(5, 5, 100, 5, 100, 200)