

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Based on a handout by Patrick Young.
/*
import acm.program.; import acm.graphics.; import java.awt.; import java.awt.event.;
public class UfoGame extends GraphicsProgram {
/** Size and speed of UFO */ private static final int UFO_WIDTH = 40; private static final int UFO_HEIGHT = UFO_WIDTH / 2; private static final int UFO_SPEED = 5;
/** Size and speed of bullets */ private static final int BULLET_SPEED = 10; private static final int BULLET_DIAM = 5;
/** Animation cycle delay */ private static final int DELAY = 10;
public void run() { setup(); while (!gameOver()) { moveUFO(); moveBullet(); checkForCollisions(); pause(DELAY); } }
/** setup UFO and add mouse listeners */ private void setup() { ufo = new GRect(UFO_WIDTH, UFO_HEIGHT); ufo.setFilled(true); add(ufo, getWidth(), 0); // UFO starts at top right ufoToLeft = true; addMouseListeners(); }
/** determines if game is over -- true if either
/** when mouse is clicked create bullet, unless a bullet
/** moves UFO, if UFO has moved to edge of screen, moves
/** moves bullet */ private void moveBullet() { if (bullet != null) { bullet.move(0, -BULLET_SPEED); } }
/** checks for bullet interaction with the world