Question: Pygame Part 1: The Drawable Class In this week's lecture we discussed how we can used object oriented programming concepts to make graphics applications more
Pygame
Part 1: The Drawable Class
In this week's lecture we discussed how we can used object oriented programming concepts to make graphics applications more organized and easier to create. One of the things that we'll leverage is inheritance, polymorphism, and abstract base classes.
We have included drawable.py that contains an abstract base class called (shocker) Drawable.
This class has the following methods:
- A constructor (__init__) method where you can set the x and y locations for your object.
- Accessor and mutator methods for the x and y locations.
- An abstract method called draw that takes a surface to draw on as a parameter.
Part 2: Rectangle class
Now we're going to derive from this class. The first derived class you'll want to make is Rectangle.
To instantiate the Rectangle class you need:
- The (x,y) location where the Rectangle is to be drawn
- It's width and height
- It's color
Then in the class's draw method you draw a rectangle starting at (x,y) of dimensions (width, height) on the surface in the chosen color.
NOTE: For credit you must derive from Drawable and use it's constructor.
Part 3: The Snowflake Class
Now switch roles!
Next up let's create a Snowflake!
A Snowflake will be made up of 4 lines. If the Snowflake is to be centered at (x,y) then those four lines are
- Line1: (x-5,y)=>(x+5,y)
- Line2: (x,y-5)=>(x,y+5)
- Line3: (x-5,y-5)=>(x+5,y+5)
- Line4: (x-5,y+5)=>(x+5,y-5)
To instantiate the Snowflake class you need:
- The (x) location where the Snowflake will start (it's y location will always start as 0)
And in its draw method you draw a the four lines as mentioned based on the current (x,y) location in white.
NOTE: For credit you must derive from Drawable and use it's constructor.
Part 4: The Main Application
Switch roles again!
Here's the main application:
- Draw a"ground plane". Presumably a green rectangle
- Draw a"sky plane". Presumably a blue rectangle
- Every iteration of your graphics loop
- Loop through all your drawable objects and draw them.
- If the current drawable is of type Snowflake (hint: use the isinstance function) increment the y coordinate of that Snowflake
- Spawn a new Snowflake instance (adding it to your list of drawables) at a random x location (with y initially set to 0).
Below is a sample image:
Part 5: Interactivity
Switch roles again
For the last part, allow the spacebar to toggle the animation.
when a Snowflake is spawned also assign it a maximum y value that is somewhere (randomly) between the start of the ground plane and the bottom of it. Then when a Snowflake hits its maximum y value, it no longer animates. This should make it look like the snow is sticking to the ground!
Scoring
The score for the assignment is determined as follows.
- Attendance
- Acted as Driver
- Acted as Observer
- Implemented and Demonstrated the Rectangle derived class
- Implemented and Demonstrated the Snowflake derived class
- Create the basic animation correctly
- Spacebar correctly toggles animation
- Snow "sticks" to the ground plane.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
