A6: Extending the Avoider Game
Extending the Avoider Game
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
Assignment A6: Extending the Avoider Game
Due Friday by 11:59pm Points 100 Submitting an external tool
Available Apr 1 at 6am - Apr 12 at 11:59pm 12 days
Goals
The main goal of this assignment is to use class inheritance and method overriding to provide
custom behavior for a derived class.
Partners
You may optionally choose to complete this assignment with a partner. When working with a
partner, both of you must code. You should follow the pair programming protocol discussed in
class: share screens, have one person type while the other person provides feedback, and
change pair programming roles at reasonable intervals (e.g., every hour, for each programming
session, etc.). A submission where one person is unable to meaningfully describe what is going
on in the code is considered academic misconduct.
When submitting on Gradescope, you will have the ability to indicate your partner. At the top of
your file, add a comment with both partners listed as authors. Only one of the persons in the
pair needs to submit.
Working with a partner is optional. You may submit your assignment as usual if you did not
work with a partner.
I have added an A6 Partner Requests discussion board to Piazza
where you can look for a partner. Please be thoughtful in discussing time availability, goals in
the course, and programming comfort. In general, two partners with similar programming skills
works better than one who is more proficient than the other. Additionally, it is best to pair up
with the same partner as A5, since you would both have familiarity with your solution for A5.
Building on A5
You will need to build on your Bounce game from A5. If that is not working, please start now to
ask for advice on what you can do to improve the game (after finalizing your submission for
A5). Going to help hours to fix your code is an excellent way of getting feedback. If you cannot
come to help hours, a Piazza message to the teaching staff with your code and a description of
errors or issues is another excellent way of getting feedback.
Assignment Requirements
For this assignment, you will make two new classes, each of which inherits from one of the A5
classes and overrides a method in that class.
DropEnemy
Make a new class DropEnemy. A DropEnemy will act like a dropping object under the influence
of gravity, but still bounce off the sides of the window like the original Enemy class does. We will
briefly discuss how this works in an upcoming class.
1. Declare the class and have it inherit from Enemy
2. Write an init method that has the same parameters as the Enemy init. Use a call to the
super() init to initialize the Enemy part of a DropEnemy.
1. Add a self.position instance variable and set it to the center of the rectangle instance
variable. This position will be used to more accurately store the x,y position of the
DropEnemy than just the rectangle is able to do.
3. Override the move method from Enemy by implementing a move method in the DropEnemy
class. This move will do a very simple physics simulation by changing the speed of the
DropEnemy object each move so that it acts under the influence of simulated gravity. Each
move call you need to:
1. Get the separate vx and vy from the speed instance variable tuple. You likely did
something similar in the Enemy bounce method.
2. Add a small value to vy. I found +0.5 to be reasonable given the other values I used in
my game (the range of speeds, the frames per second, window size, etc.). You can
make your own choice.
3. Assign the updated vx and vy to the speed instance variable.
4. Extract the x and y from the stored position instance variable.
5. Add vx to x and vy to y.
6. Store the updated x and y into the position instance variable.
7. Assign the position instance variable to the center of the rectangle instance variable.
Note that this is in contrast to the Enemy move, which (may have) directly shifted the
rectangle instance variable with the move or move_ip method. But the outcome of this
new move method is the same. At the end of the method, the stored rectangle has
shifted.
Change the code in main to make half original Enemies and half DropEnemies. Use a different
image for DropEnemies. The Enemy and DropEnemy objects can all be stored in the same list.
You should be able to run the game at this point to see the behavior of the new objects. If it
does not work, carefully compare the list of actions your move method should have with the
code you implemented.
PowerUpRotate
Make a new class PowerUpRotate. This powerup will rotate its image over time.
1. Declare the class and have it inherit from PowerUp
This tool needs to be loaded in a new browser window
2. Write an init method that has the same parameters as the PowerUp init. Use a call to
super() init to initialize the PowerUp part of a PowerUpRotate object. Add two new instance
variables
1. angle - initialize it to 0.
2. original_image - initialize it with the image. We need to keep a good original image
around so that repeated rotating of the image doesn't lose the quality.
3. Override the draw method from PowerUp by implementing a new draw method in the
PowerUpRotate class. You will need to keep the screen parameter. The draw method is
going to rotate the image each time. Each call to draw should:
1. Use pygame.transform.rotate to rotate the original_image instance variable to a new
angle This function works like
rotated_image = pygame.transform.rotate( original image,
degrees_counterclockwise_to_rotate)
2. Assign this rotated image to self.image
3. Make a rectangle from the new rotated image and position it properly
1. Store the current rectangle instance variable center in a variable.
2. Use get_rect() on the updated self.image and store the rectangle in the rectangle
instance variable
3. Assign the rectangle center you stored in step 1 to the center of this new stored
rectangle.
4. Make a new mask for the rotated image and store it in the mask instance variable.
5. Add a small number to the angle instance variable.
6. Call the draw method in the PowerUp class using the super() call to actually draw the
image. Do not just blit it here.
Change the code in main to sometimes add a normal PowerUp and sometimes add a rotating
one. Use a random number to decide. Store both kinds of PowerUps in the same list. Use a
different image for the rotating powerup (it could be a different color, or something like that).
Submitting
Record a small video showing your game being played. Quicktime on Mac can do screen
recordings, and there are programs and websites that can record on most machines (e.g., VLC
Media, Zoom). A phone recording is also fine. Try to keep the file size small. Feel free to post
suggestions for recording software on the A6 Discussion