You are going to do the first part of a space invaders game. You will create a bunch of spaceships that move back and forth across the screen.

The purpose of this lab is to help you to understand exceptions and objects.


You will write a program that draws at least 4 spaceships on the screen and moves them back and forth across the window.

Your program should have:

  1. The spaceship class in a separate file.
  2. An exception handler that will draw a circle instead of an image if the image file cannot be found.
  3. When a spaceship hits one of the edges, it should reverse direction.

The program should have the same functionality whether the spaceships are drawn as GImage's or something else (GOval, GPolygon, etc.).

Think before programming

You will need to use the ACM library, so you may want review the tutorial on using the ACM library.

Start by creating a GImage that reads from a JPG file. Once you are able to read the image (and catch the exception (see Further tips for more info) if the file doesn't exist) you can move on to the next phase.

Once you have the spaceships created, move them across the screen with a simulation loop.

When you have this working, reverse direction when you get to the edge of the page.

Again, take a look at the examples.

Tip: using separate files

We require that you use a separate .java file for your Spaceship class. However, you will notice that if it is in a separate file, using the add() method won't magically work like it did before. That is because add() is a method of the GraphicsProgram class. In examples like Car, the wheel class can simply call “add(circle)” because the “wheel” class is declared inside of the “car” class, which extends “GraphicsProgram”. So the wheel automatically has access to the methods of whatever GraphicsProgram that created it.

To keep classes in separate files, do what is done in the Balls example. Notice that the final parameter of the “Ball” class constructor is a “BouncingBall” object (which extends GraphicsProgram). This allows the Ball class to invoke the “add” method of the indicated GraphicsProgram without being in the same file.

Another option (and some of you may discovered this while doing the previous graphics lab) is to make your Spaceship class extend GCompound. You can look up information about GCompounds on the ACM Graphics Page. The basic idea is that you can add GOvals and GImages to your Spaceship class, and then you can add Spaceships directly to your panel in your run method.

Pass-off procedure

When you have your program working, you will need to show it to a TA. The TA will evaluate your code based on the following criteria:

  1. As always start writing your program with small simple steps. Note that you do not have to show the TA each step individually. The TA will simply check that your program accomplishes each of these elements:
    1. Draw a spaceship with an image file (for example: JPG, PNG, GIF) (5 points)
    2. Catch the exception (and draw circles instead) if the image file doesn't exist (5 points)
    3. Move the spaceships across the screen (5 points)
    4. Reverse direction when you hit the edge (5 points)
  2. Is your code properly formatted and has a separate file for spaceship (indenting blocks, comments, spacing, reasonable names of variables)? (5 points)

In order to receive points for part 1.4, you must define an object for your spaceship and call a method on that object to make it move.

5 points extra credit: Throw exceptions when you hit the edge and have your simulation loop catch the exception and tell the object to reverse direction.

Extra Credit: Pass off early (1 point per day (excluding Sunday) up to 3 points)

Further tips

When a new GImage object is created with an invalid file name, it doesn't throw a “FileNotFoundException”. It actually throws a “acm.util.ErrorException”. That is the one you will need to catch.

For ideas on how to draw some different shapes and colors, see ACM library tips

cs-142/fall-2010/labs/spaceships.txt · Last modified: 2014/11/19 22:52 by ryancha
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0