EECS 493

EECS 493 Assignment 3: Asteroids Game

Total Released Due
125 points 1/23 2/8 at 11:59 PM ET

Submission Instructions

Before submitting, please ensure your website loads in Google Chrome by simply clicking and opening the index.html without using server hosting tools (e.g. VS Code Live Server).

Please submit your work to Canvas as a zip file, named a3_<uniqname>.zip. Replace <uniqname> with your uniqname: e.g. a3_zhaojer.zip, note that the angle brackets should NOT be included in your filename. Renaming (e.g., “-1”) done by Canvas is fine.

This zip file should have a single directory containing all files and directories provided in the starter code. In other words, the zip file should have the following structure.

a3_uniqname
├── index.html
├── scripts
│   ├── jquery.min.js
│   └── page.js
├── src
│   ├── arrowkeys.png
│   ├── asteroid.png
│   ├── audio
│   │   ├── collect.mp3
│   │   └── die.mp3
│   ├── frontpage_background.jpg
│   ├── pause-rocket.gif
│   ├── pause.png
│   ├── player
│   │   ├── player.gif
│   │   ├── player_down.gif
│   │   ├── player_left.gif
│   │   ├── player_right.gif
│   │   ├── player_shielded.gif
│   │   ├── player_shielded_down.gif
│   │   ├── player_shielded_left.gif
│   │   ├── player_shielded_right.gif
│   │   ├── player_shielded_up.gif
│   │   ├── player_touched.gif
│   │   └── player_up.gif
│   ├── port.gif
│   └── shield.gif
└── style
    └── index.css

Not following the upload instruction will result in a penalty.

Objective

The objective of this assignment is for you to gain practical experience in building an interactive single-page web application with HTML, CSS, and JavaScript/jQuery. Specifically, you will be creating a game called “Asteroids” as outlined in this spec. No external library, other than jQuery, is allowed.

Watch this video for an overview: https://youtu.be/FeEbwuNbsiI

Grading Breakdown

This assignment has 8 main components (denominator of 125 points):

  1. Overall Functionality - 10 points
  2. “Get Ready” Splash Screen - 5 points
  3. Asteroids - 25 points
  4. Shields and Portals - 15 points
  5. Rocket - 20 points
  6. Scoreboard - 5 points
  7. Sounds - 5 points
  8. Game Over Page - 10 points
  9. Pause - 15 points
  10. Resume - 5 points
  11. Exit - 3 points
  12. Restart - 7 points

Starter Code

You will use the starter code we provide to complete this assignment.

Download and unpack the starter files (either using the following commands or simply navigating to the link).

$ wget https://eecs493staff.github.io/a3-asteroids/starter_code.tar.gz
$ tar -xvzf starter_code.tar.gz

Here’s a brief description of each of the starter files.

index.html Some containers (divs) for the game window and the game board are defined; write your HTML code here
style/index.css Some stylings for the game window, game board, and images are defined; write your CSS code here
scripts/page.js Here is a list of things provided:
  1. Some comments describing the structure of the code
  2. Some global variables for storing game states/data
  3. An Asteroids class is defined (Asteroids that are randomly generated and travels linearly across the gameboard)
  4. Event handler for arrow key presses
  5. Additional helper functions (e.g. determining collision);
write your JS code here
scripts/jquery.min.js jQuery library source code; do NOT modify
src/ Images and audios for your website; do NOT modify

Remarks:

Helpful Resources

Before starting with this assignment, we recommend that you check out the following 2 resources that will make your progress in this assignment much easier.

Group OH

This is a recording of a group office hour session from Fall 2022, which contains an overview of both Assignment 2 and Assignment 3, how to start, and some challenging aspects students had questions about.

The video is 2 hours long: Everything, except the portion from 29:30 to 1:07:30, is relevant for this assignment. Timestamp is provided in the video description.

Recording link: https://youtu.be/hXilQo1lMjE

Blaster Game Demo

This demo is on creating a simple game with JS from many semesters ago. It goes over some common JS functions for manipulating the DOM, which are important for this current assignment too. The demo is about 45 minutes.

Blaster Game Video: (Consider downloading for better quality! Captions have been added also.) https://drive.google.com/file/d/1ttQV8_Wzm37cXrbEYuBXPAixG_UzQNFZ/view?usp=sharing

Blaster Game Starter Code: https://drive.google.com/file/d/1r5YC7vx_9l5GBzTYRpGE-5ewFbGN12Lk/view?usp=sharing

Screenshots

“Get Ready” Splash Screen

Minimal white start screen showing “Get Ready!” in bold pixel text above four arrow-key icons arranged like a directional pad. Below, the text reads “Use arrow keys to move.” A small scoreboard on the right displays “SCORE 0,” “DANGER 20,” and “LEVEL 1.”

Gameplay & Scoreboard

Gameplay screen showing a small rocket on a white background surrounded by several asteroid icons and a swirling black hole in the center. A shield icon sits near the bottom. On the right side, a scoreboard displays “SCORE 1560,” “DANGER 20,” and “LEVEL 1,” with a pause button in the top corner.

Game Over Page

Game over screen for a space-themed Asteroids game, showing a purple dialog box centered on a starry galaxy background. The box displays “GAME OVER! YOU HAVE TO FIX YOUR SPACESHIP!” followed by the score “1720” and a “Start Over” button. The top banner reads “Asteroids” with simple asteroid icons on each side.

Pause Screen

Dimmed gameplay view with a centered pause menu showing “Game Paused,” a rocket image, and buttons for Resume, Restart, and Exit.

Restart Confirmation

Dimmed game screen with scattered cartoon asteroids and a small rocket on the right. A centered light-gray dialog box asks, “Are you sure you want to restart?” Below it are two buttons labeled “Yes” and “No.” The right-side HUD still shows SCORE 520, DANGER 20, LEVEL 1, with a pause icon in the top corner.

Restarting

Dimmed gameplay screen with scattered hand-drawn asteroids. In the center, small white text reads “Restarting…” The right HUD still shows SCORE 520, DANGER 20, and LEVEL 1, along with a small pause icon.

The remaining pages are the same as Assignment 2.

Requirements

We outline the requirements for each of the components below. Everything listed in this section, unless labeled as “Suggested”, is required. The demo video & screenshots may be helpful in understanding the game; however, your game does NOT need to look exactly like them.

General

Overall Functionality (10 points)

“Get Ready” Splash Screen (5 points)

Main Components

Required

Suggested Style

Asteroids (25 points)

Shields and Portals (15 points)

Rocket (20 points)

Scoreboard (5 points)

Sounds (5 points)

Game Over Page (10 points)

Main Components

Required

Pause (15 points)

Resume (5 points)

Exit (3 points)

Restart (7 points)

Hints

Changing Item Size

If you are trying to set the width and height for asteroid elements but it’s not working, double check what element you’re actually setting the width and height for. If you are setting the width and height on the “div” containing the image, this doesn’t actually affect the image size; it’s simply a container for the image. You likely will need to set the width and height for the image element itself.

Event Listeners

If you think your event listeners aren’t being triggered, one common error occurs when event listeners are created (and attached) before relevant DOM objects exist.

A way around this is to create event listeners that are attached to the body, or another element that exists immediately on the page, and then filter events for a given selector. For example, if I have a UI where I expect items to get added to the page dynamically, and I want to have a “delete” button next to each dynamically added element, I might create the event listener this way:

$("body").on("click", ".deleteX", function(event){
 
});

Here, the “body” is listening for all click events, and is essentially only passing them to the callback if the item clicked actually had the “deleteX” class. See the “selector” arg here: https://api.jquery.com/on/

setInterval and Collision Detection

All programming projects from the core EECS classes are considered “serial” (or “synchronous”), where the code is executed sequentially. For instance, in the example code below where both functions are serial/synchronous, loadPage() will only begin execution after getData() has finished. This is also known as getData() “blocking” the program from executing loadPage(). If getData() takes one minute to finish, then loadPage() has to wait one minute to begin execution. Imagine sitting in front of your computer and waiting one whole minute for a page to load; that’s not the desired scenario.

getData();
loadPage();

JavaScript has “Asynchronous Function”, which is a function that can be exeucted without blocking the program from running other code/functions. Using the same example but suppose getData() is asynchronous, then loadPage() can start executing before getData() has finished, thus allowing programs to start long-running tasks (like getData()) and continue working on other tasks (like loadPage()).

When completing the Asteroids section of this assignment, you will use setInterval, which is an asynchronous function, to

  1. repeatedly spawn asteroids, and
  2. move each asteroid across the screen.

So how exactly is asynchronous function useful here? Without going too detailed (but feel free to really think about this more), the usage of setInterval here allows multiple asteroids on the screen to move “at the same time” (not exactly the case but it is a good intuition).

In other words, each asteroid has its own spawn function that is moving it. For collision detection, instead of having one for loop that checks the collision between the rocket and every asteroid on the screen, use a simple if condition within the setInterval (move) function that is moving each asteroid, and have that check the collision between this asteroid and the rocket. As such, you are exploiting the asynchronous nature of JavaScript.

FAQ

Is it OK if the asteroids spawn outside the game window?

Yes.

Is the assignment graded for style?

If “style” refers to coding style, then no. However, make sure you are following the rules listed under the General section in the spec. If “style” refers to CSS style, then yes, at least for some pages.

Can I use <some JS/CSS functions> to achieve <some behavior>?

Yes, as long as it does not involve any external libraries (other than jQuery).

What happens when a rocket carrying a shield comes into contact with another shield spawning on the game board?

Up to you! The simplest solution is to do nothing.

How can I make the rocket move “smoothly”?

I recommend using a setInterval() which constantly checks (e.g. every 20 ms) whether UP, DOWN, LEFT, RIGHT variables (set by the keypress event handlers) are true, and moves the rocket accordingly if so.

Is it OK if the shielded rocket images appear smaller/larger than the non-shielded images?

Yes.

Is it OK if there is a gap between the asteroid and the rocket when they collide?

Yes.

Does it matter where the rocket spawns?

No.

Acknowledgments

Original spec written by Zirui Zhao zhaojer@umich.edu. Updated by the EECS 493 team.

This document is licensed under a Creative Commons Attribution-NonCommercial 4.0 License. You’re free to copy and share this document, but not to sell it. You may not share source code provided with this document.