Hello all:

The task is to:

Create a row of Alien attackers, class RowOfAlien, patterned after the partially filled array. Put five Aliens in the row. When the row hits the left or right of the screen, the Aliens snake downwards (see demo). Note that this is different from the video game where the entire row bounces as one unit.

Left and right keys move the canon. The canon fires automatically (as in version 1) but the bullet is fired from the current position of the canon.Note that you must press space bar a couple of times to get keyboard interaction working.

To make it easier to debug your program, print a message in the console when each of the following happens:

* An alien is hit by a bullet
* The left or right keys are pressed

3) Modify GameDemo to:

* Play the game multiple times.
* Display the outcome of each game.
* At the conclusion of play, show the total number of games won and lost


So far I have this small chunk of code

public class RowOfAlien {

	// CONSTANTS
	public final int EMPTY = -1;
	

	// VARIABLES
	private Alien[] row;
	private int topIndex;

	public RowOfAlien(int numberOfAliens) {
		row = new Alien[numberOfAliens];
		topIndex = EMPTY;
	}
	
	public void add( Alien alien ) {
		// Put your code here
	}

	public void draw(Graphics g) {
		// Put your code here
	}


	public void move() {
		// Put your code here
	}
	
	public void remove(int index) {
		// Put your code here
	}
}

But its saying that there is already an error on the line that contains public RowofAliens

Any suggestions to help me start?

Recommended Answers

All 5 Replies

That looks like your constructor. I usually don't use public in front of a constructor but i don't see that it should hurt. Maybe it's the notion that you can't have a constructor with an argument ( int) without having a default constructor with no arguments, even if you don't use it or have code in it. Seem to recall something like that.

Mike

Ok I deleted the public part and it worked, but now I need help with figuring out how to make 5 aliens to show up in one row.

Member Avatar for coil

What does your Alien class look like? I assume that your Alien constructor takes in at least an xpos and ypos, so if you want 5 aliens to be in the same row, then simply give them the same ypos coordinate.

This is my alien code:

import java.awt.Graphics;
import java.awt.Image;

/**
 * Implementation Plan
 * 
 * 1. Setup Java project and get skeleton running. x2. Write the code for
 * Alien.move member function so that the alien moves from left to right and
 * disappears off the right hand side of the JApplet window. 3. Refine
 * Alien.move so that the alien bounces back and forth across the first row 4.
 * When the alien hits the left or right side of the window, have it drop down
 * one row. 5. Terminate the program and print "DONE" in the console pane when
 * the alien reaches the bottom.
 * 
 */
class Alien {

	// CONSTANTS
	private final String ALIEN_ICON = "alien.gif";
	private final String EXPLOSION_WAV = "explosion.wav";// Sound played when
															// hit by bullet
	
	// Object that plays the explosion sound.
	// This is analgous to SPRITE. One handles graphics details
	// the other audio details.
	private final Sound EXPLOSION;

	private final int MAX_WIDTH; // Size of canvas on which Alien is moving
	private final int MAX_HEIGHT;

	// Get the sound to play when hit by missile

	// Initial speed at which the alien moves horizontally
	private final double INITIAL_X_SPEED = 1.0; // NOT an int - must be double

	private final Sprite SPRITE; // Graphical representation of alien

	// VARIABLES
	private double upperLeftX, upperLeftY; // Position of alien on canvas
	

	/*
	 * Create a new alien object - Constructor
	 */
	
	
	public Alien(int theX, int theY, int theMaxWidth, int theMaxHeight) {
		upperLeftX = theX;
		upperLeftY = theY;
		MAX_WIDTH = theMaxWidth;
		MAX_HEIGHT = theMaxHeight;

		// Get the graphical representation of the alien
		SPRITE = SpriteStore.get().getSprite(ALIEN_ICON);
		EXPLOSION = SoundStore.get().getSound(EXPLOSION_WAV);
	}

	public void draw(Graphics g) {
		SPRITE.draw(g, (int) upperLeftX, (int) upperLeftY);
	}

	boolean isRight = true;
	public boolean alienAtBottom = false;

	public void move() {

		if (isRight == true) {
			upperLeftX = (upperLeftX + INITIAL_X_SPEED);
		}
		// Allows alien to move right
		if (upperLeftX == MAX_WIDTH - SPRITE.getWidth()) {
			isRight = false;
			upperLeftY = upperLeftY + SPRITE.getHeight();
		}
		// Allows alien to move right and down
		if (isRight == false) {
			upperLeftX = (upperLeftX - INITIAL_X_SPEED);
		}
		// Allows alien to move right, down, and left
		if (upperLeftX == 0) {
			isRight = true;
			upperLeftY = (upperLeftY + SPRITE.getHeight());
		}
		// Allows alien to move right, down, left, and down continuously
		if ((upperLeftX + SPRITE.getWidth() >= MAX_WIDTH)
				|| (upperLeftY + SPRITE.getHeight() >= MAX_HEIGHT)) {
			alienAtBottom = true;
			upperLeftY = upperLeftY - SPRITE.getHeight();
			// Alien stops once it reaches the bottom
		}
	}

	// The graphic for a bullet is small so we will treat it as a
	// single point represented by its coordinates for its upper left corner.
	// The alien is treated as a rectangle represented by the coordinates of
	// its upper left corner, getHeight(), and getWidth().
	// The hitBy member function determines if the point representing the
	// missile is inside the rectangle representing the alien.
	public boolean hitBy(Bullet m) {
		if ((upperLeftX < m.getUpperLeftX())

		&& (((m.getUpperLeftX() < upperLeftX + getWidth())))

		&& ((upperLeftY < m.getUpperLeftY()) &&

		((m.getUpperLeftY() < upperLeftY + getHeight())))) {

			EXPLOSION.playSound();// Sound played when bullet hits alien

			return true;
		}
		return false;
	}
	// GETTERS AND SETTERS
	public int getHeight() {
		return SPRITE.getHeight();
	}

	public int getWidth() {
		return SPRITE.getWidth();
	}

	public double getUpperLeftX() {
		return upperLeftX;
	}

	public void setUpperLeftX(int x) {
		upperLeftX = x;
	}

	public double getUpperLeftY() {
		return upperLeftY;
	}

	public void setUpperLeftY(int y) {
		upperLeftY = y;
	}
}
Member Avatar for coil

So when you add each Alien to the array and call the constructor, give each Alien the same y coordinate.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.