By using this site, you agree to our Privacy Policy and our Terms of Use. Close

Game Class (this is the last I'll upload, the rest deal with things like receiving keyboard input, displaying the bullets and other things that shouldn't be relevant)

import java.util.Iterator;
import java.util.Random;
public class Game {

static final Integer numberAliensCol = 10;

static final Integer numberAliensRows = 3;

static final Double alienGapCol = (SpaceInvaders.xcanvas - 2.0 * Alien.width)
/ numberAliensCol;

static final Double alienGapRow = (SpaceInvaders.ycanvas - 3.0 * Alien.height)
/ numberAliensRows;

static final Double shipStartx = SpaceInvaders.xcanvas / 2.0;

static final Double shipStarty = 1.0 * SpaceInvaders.ycanvas
- SpaceShip.height;

static Random random = new Random();

Double xsize, ysize;

SpaceShip ship;

Bodies missiles, aliens, bullets;

Boolean movingLeft;

Boolean lost;

public Game(Double xs, Double ys) {

lost = false;
xsize = xs;
ysize = ys;
ship = new SpaceShip(shipStartx, shipStarty);

aliens = new Bodies();
missiles = new Bodies();
bullets = new Bodies();
for (Integer i = 0; i for (Integer j = 0; j aliens.add(new Alien((i + 1) * alienGapCol, j * alienGapRow));
}
}
movingLeft = true;
}

public void draw(GameComponent canvas) {
// Drawing the current games state involves drawing all of
// the components that make up the game.
for (Body b : aliens) {
b.draw(this, canvas);
}
for (Body b : missiles) {
b.draw(this, canvas);
}
for (Body b : bullets) {
b.draw(this, canvas);
}
ship.draw(this, canvas);
}

public Boolean noAliens() {
return aliens.size() == 0;
}

public void step(GameComponent canvas) {

// Each of the components are stepped forward one step in time.
for (Body b : aliens) {
b.step(this, canvas);
}
for (Body b : missiles) {
b.step(this, canvas);
}
for (Body b : bullets) {
b.step(this, canvas);
}
ship.step(this, canvas);

// When a missile hits an alien both are removed.
Iterator alienIterator = aliens.iterator();
while (alienIterator.hasNext()) {
Alien a = (Alien) alienIterator.next();
Iterator missileIterator = missiles.iterator();
Boolean removed = false;
while (missileIterator.hasNext() && !removed) {
Missile m = (Missile) missileIterator.next();
if (m.collide(a)) {
missileIterator.remove();
alienIterator.remove();
removed = true;
}
}
}

// When a bullet hits the ship the games is lost. Also bullets that drop off the screen
// are removed from the simulation.
Iterator bulletsIterator = bullets.iterator();
while (bulletsIterator.hasNext()) {
Bullet b = (Bullet) bulletsIterator.next();
if (b.collide(ship)) {
lost = true;
}
if (b.offscreen()) {
bulletsIterator.remove();
}
}


// Missile that go off the screen are also removed.
Iterator missileIterator = missiles.iterator();
Boolean removed = false;
while (missileIterator.hasNext() && !removed) {
Missile m = (Missile) missileIterator.next();
if (m.offscreen())
missileIterator.remove();
}



}
}