CSCI 150 - Foundations of Computer Science
Spring 2012
Homework 12: Asteroids
In this assignment, you will use Pygame
to create your own version of the classic video game Asteroids.
Reading Assignment
- Read Chapter 10, pages 154-164.
Program Requirements
- A triangular spaceship should be placed in the middle of the viewing area.
- When the left arrow key is pressed, it rotates left.
- When the right arrow key is pressed, it rotates right.
- When the space bar (
K_SPACE) is pressed, it fires a
circular missile. The missile originates from the tip of the
spaceship and moves in the direction that the triangle is facing.
- Each asteroid originates somewhere on the edge of the screen
- The total number of asteroids should be specified when calling the
function that starts the game.
- For each asteroid to be created:
- Randomly select its heading.
- Set its initial position to be (0, 0)
- When missiles cross the edge of the screen, they should leave the game.
- When asteroids cross the edge of the screen, they should wrap around
to the opposite edge.
- When a missile strikes an asteroid:
- Both the missile and asteroid should vanish.
- A new asteroid should be randomly generated and added to the game.
- When an asteroid strikes the spaceship, both should vanish and the game
ends.
Grading
- Completing the above list of features will suffice to earn a grade of
B on the assignment.
- To earn an A, you must add
at least one non-trivial additional feature.
Implementation Ideas
- Import Circles and Triangles from the modules you created in the
previous assignment. Feel free to modify those
classes as necessary.
- Asteroids and missiles are Circle objects; the spaceship is a Triangle object.
- Use lists to store the currently visible asteroids and missiles.
- On each iteration of the event loop:
- Check for a timer event; if found, update the positions of the asteroids and missiles.
- Check for keyboard events; if found, update the position of the spaceship, and introduce new missiles as needed.
- Check for collisions between each asteroid and the spaceship
- Check for collisions between each missile and each asteroid
- For each asteroid that goes off the screen, move it to the opposite side
- Here is an example (from class) of an event loop
integrating both timer-based and keyboard-based inputs.