CS 1410 | Syllabus | Assignments | Schedule | Canvas Course | [print]
CS 1410: Asteroids Part 2
Nearly everyone has played or at least heard of the famous arcade game Asteroids. But, if you have not, you can play it here. The game involves a player-controlled spaceship that can turn left or right, accelerate forward, and shoot bullets. A collection of rocks (asteroids) move through space, potentially on a collision course with the spaceship. If a collision between the spaceship and a rock occurs, then the spaceship is destroyed. If a bullet collides with a rock, the rock and bullter are both destroyed. The objective of the game is to eliminate all of the rocks, by successfully shooting them from the spaceship, before a devastating collision incident occurs.
Assignment
Your assignment is to recreate a simple Asteroids game using Python and Pygame. The assignment will consist of two sequential parts. For this second part, you are required to add implementions the following features of the game:
A bullet must fire from the ship and travel in the direction the ship is facing at the time. The bullet will only stay active for a limited amount of time after being fired. Then, it should disappear.
Collisions between any rock and the bullet will cause the bullet and the rock to disappear.
Collisions between any rock and the ship will cause the game to end.
All rocks being destroyed will cause the game to end.
Stars will be displayed, and they will twinkle, becoming brighter and dimmer.
For part 2 of the assignment, only the above additional functionality is required. You are welcome to continue working on additional features once you complete the requirements for part 2, but it is your responsibility to complete the requirements for part 2 of the assignment first, and submit it by the due date.
For this assignment, you are required to demonstrate use of the object oriented principles inheritance, polymorphism and aggregation when implementing the classes that have been designed for you to represent the game and its various components.
You should use your solution to part 1 of the assignment as a starting point.
Required Classes
The required classes are listed in the UML Diagram. Not all of the methods will be described in detail here. For example, most getter methods will not be listed. However, if they are in the UML diagram, they are required. If you have questions about the required functionality, please ask questions in class or in the discussion forums.
Only changes to part 1 classes are listed here.
Movable Class
Movable Data Members
mActiveboolean value, whether the object is active or not.
Movable Methods
__init__initializesmActiveto true.getActivereturns the value of themActivedata membersetActiveupdates the data member to the value in the parameter.hitsreturns true if this object’s “circular” area overlaps with that of the other object. Use the circle collision technique discussed in class.getRadiusis an abstract method. It shouldraise NotImplementedError.
Rotatable Class
Rotatable Data Members
- No changes
Rotatable Methods
- No changes
Polygon Class
Polygon Data Members
- No changes
Polygon Methods
getRadiusFor each point in the original polygon, calculate the distance from the origin. Return the average of these distances. If there are no points, return 0.
Ship Class
Ship Data Members
- No changes
Ship Methods
fireCreates a bullet and returns it. The bullet should be created with the location of the ship’s rotated and translated first point. Also, the bullet will have the same velocity components as the ship and the same rotation as the ship.
Rock Class
Rock Data Members
- No changes
Rock Methods
- No changes
Circle Class
Circle Data Members
mRadiusis a number measured in pixels. The radius of the circle.mColoris a PyGame color, a 3-tuple of integers in the range 0-255 describing the red, green and blue channels of the color.
Circle Methods
__init__uses constructor chaining to initialize theRotatabledata members, and sets the object’s radius from the paramater, and sets the color to white.setRadiusupdates the data member, but only if the new value is at least 1.setColorupdates the data member. It assumes the new color is valid.drawUses the PyGame functions to draw the circle described by theMovableposition and the radius.
Bullet Class
Bullet Data Members
mAgethe number of seconds the bullet has been in existence.
Bullet Methods
__init__uses constructor chaining to initialize allCircledata members. Bullets all have a radius of 3. Initializes the age of the bullet to 0. Accelerates the bullet 100.0 units. Moves the bullet 0.1 seconds worth of movement. Without this, it will hit the ship.setAgeupdates the data member, assuming the parameter is correct.evolvemoves the bullet. Addsdtto the age of the bullet. If the bullet is more than 6 seconds old, makes it inactive.
Star Class
Star Data Members
mBrightnessan integer value. The current star brightness, in the range 0 to 255.
Star Methods
__init__uses constructor chaining to initialize allCircledata members. Stars have no speed, rotation of 0, and radius of 2. Initializes the brightness to a random value.setBrightnessupdates the data member, but only if the new brightness is within the range specified above. If the brightness changes, update the color as well.evolveTries to changes the brightness by adding 10, subtracting 10, or doing nothing, each with equal probability.
Asteroids Class
Asteroids Data Members
mBulletsa list of allBulletobjects active in the gamemStarsa list of allStarobjects createdmObjectsa list of all objects active in the game, includingBullets andStars.
Asteroids Methods
__init__Create 20Stars in random locations and an emptyBulletlist. Store theStars in the correct lists.fireIf the maximum number of active bullets (3) has not been reached, create a new bullet, add it to the appropriate lists. Refer to theShipandBulletclasses.evolveAllObjectsCall evolve on all objects.collideShipAndBulletsCheck and handle collisions between bullets and ship.collideShipAndRocksCheck and handle collisions between rocks and ship.collideRocksAndBulletsCheck and handle collisions between bullets and rocks.removeInactiveObjectsRemoves all inactive objects from all lists.evolveBe sure that all objects evolve. If any bullet collides with the ship, the ship and bullet should become inactive. If any bullet collides with any rock, the rock and bullet should become inactive. If any rock collides with the ship, the ship and bullet should become inactive. Remove any inactive rocks and bullets from the lists. Use the methods outlined above, in the correct order.drawBe sure that all objects draw, but only if they are active.
main
game_logicAdd a key press to call thefiremethod. Be sure to checknewkeys, notkeys.
Hints
Refer to the Pygame documentation to understand which parameters are necessary when calling each of the Pygame draw methods. Specifically, you should be interested in
pygame.drawandpygame.Rect.When creating colors, use a helpful tool to determine the RGB values. Here are two good options: color.adobe.com and colorpicker.com
Last Updated 09/27/2019

