Hey guys I need help with part 2 of my space invaders game. So far I have the missile shooting from the base but the missile is only fired once and that's it. I need to finish the code so that once the missile hits the top of the window a new missile is launched from the base. Any suggestions? Here is my code:

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

public class Bullet {
	//CONSTANTS
	private final String BULLET_ICON = "bullet.gif";
	private final int MAX_WIDTH;
	private final int MAX_HEIGHT;
	
// Initial speed at which the bullet moves vertically
	private final double INITIAL_X_SPEED = 0.5;
	
	private final Sprite SPRITE;
	
	// VARIABLES
	private double upperLeftX, upperLeftY; // Position of cannon on canvas

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

		// Get the graphical representation of the cannon
		SPRITE = SpriteStore.get().getSprite(BULLET_ICON);
	}

	public void draw(Graphics g) {
		SPRITE.draw(g, (int) upperLeftX, (int) upperLeftY);
	
}
	boolean isRight = true;
	public boolean  BulletAtTop = false;
	
	public void move(){
		if (isRight == true) {
			upperLeftY = (upperLeftY - INITIAL_X_SPEED);
		}
		
}
}

Just glancing real quick, if upperLeftY reaches 0 you hit the top and can reset to the starting Y position?

This is the code for my base

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

public class Cannon {
	// CONSTANTS
	private final String CANNON_ICON = "cannon.gif";
	private final int MAX_WIDTH; // Size of window on which Cannon is moving
	private final int MAX_HEIGHT;
	
	private final Sprite SPRITE; // Graphical representation of cannon

	// VARIABLES
	private double X, Y; // Position of cannon on canvas

	/*
	 * Create a new cannon object - Constructor
	 */
	public Cannon(int theX, int theY , int theMaxWidth, int theMaxHeight) {
		X = theX;
		Y = theY;
		MAX_WIDTH = theMaxWidth;
		MAX_HEIGHT = theMaxHeight;

		// Get the graphical representation of the cannon
		SPRITE = SpriteStore.get().getSprite(CANNON_ICON);
	}

	public void draw(Graphics g) {
		SPRITE.draw(g, (int) X, (int) Y);
	}
}

How would I reset the upperLeftY to 0 and reset to starting Y position

In the move() method? You don't reset upperLeftY to 0, you test if it is zero and if it is, then you need to move it back to it's starting value in your base.

That's what I am asking is there an example of code that you can show me off how to do that. I am very new to java.

I did this and it seems like it's mostly working:

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

class Bullet {
    // CONSTANTS
    private final String MISSILE_ICON = "missile.GIF";
    private final int MAX_WIDTH;  // Size of canvas on which missile is located
    private final int MAX_HEIGHT;

    private final double INITIAL_X_SPEED = 1;   ////********************************
    private final int DROP_DOWN_AMOUNT = 600;

    private final Sprite SPRITE;    // Graphical representation of missile

    private final double INITIAL_Y_SPEED = 1; 

    // VARIABLES
    private double upperLeftX, upperLeftY;  // Position of missile on canvas
    private int direction;

    public Bullet(int theX, int theY, int theMaxWidth,
            int theMaxHeight) {
        upperLeftX = theX;
        upperLeftY = theY;
        MAX_WIDTH = theMaxWidth;
        MAX_HEIGHT = theMaxHeight;
        direction = 1;

        SPRITE = SpriteStore.get().getSprite(MISSILE_ICON);
    }

        // Get the graphical representation of the missile

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

    private boolean hitTop() {
        final int TOP_EDGE = -150;
        return upperLeftY <= -150;
    }

    public void move() {
        upperLeftY += -direction * INITIAL_Y_SPEED;


        if(hitTop()) {
            upperLeftY += DROP_DOWN_AMOUNT;
        }

    }

}

Now I just have to figure out how to program in the collision, post on here if you figure it out please! Much love

Edited 3 Years Ago by Reverend Jim: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.