Writing space invaders code, can't get bullets to destroy aliens

The Impossible Squish

I apologize in advance for this question potentially taking some effort to solve. This is a group project I've been working on for a while so the scripts are a bit complicated, and me and the two others I'm working with are all total beginners to java. Unfortunately my problem is that I don't know what the problem is, so my question is a bit vague. I'll do my best to show you the relevant bits of code and any help indicating what might be the problem would be greatly appreciated :)

This class, SpriteAlien, creates an alien and has the methods which define the alien's movement.

import javax.swing.JPanel;

public class SpriteAlien extends JPanel{

    public int hPosAlien; // alien's horizontal coordinate
    public int vPosAlien; // alien's vertical coordinate

    public int hPosBullet, vPosBullet;


    public int life; // aliens life, some may have more than one

    int dx = 1; // number of pixels the alien moves horizontally every timer cycle
    int dy = 10; // number of pixels the alien moves down when it hits the boundary
    boolean direction = true; // false - left, true - right
    boolean shoot, defeated = false;

    // constructor
    public SpriteAlien (int horizontalPos, int verticalPos){
        hPosAlien = horizontalPos;
        vPosAlien = verticalPos;
    }


    public void moveAlien()
    {   
        //This creates the alien's movement

        // if the alien has not reached the right wall and is moving right
        if ((hPosAlien <= 740) && (direction == true)) { 
            moveRight();
        }

        // if the alien has reached the right wall and is moving right
        else if ((hPosAlien >= 740) && (direction == true)) { 
            moveDown();
            moveLeft();
            direction = false;
        }

        // if the alien has not reached the right wall and is moving left
        else if ((hPosAlien <= 740) && (hPosAlien >= 0) && (direction == false)) {
            moveLeft();
        }

        // if the alien has reached the left wall and is moving left
        else if ((hPosAlien < 0) && (direction == false)) {
            moveDown();
            moveRight();
            direction = true;
        }           
    }

    // methods used for alien movement
    void moveLeft() {
        hPosAlien -= dx;
    }

    void moveRight() {
        hPosAlien += dx;
    }

    void moveDown() {
        vPosAlien += dy;
    }
}

This class, SpriteArray, creates an array of aliens that move together like a formation

// create an array of aliens to make our alien army public class SpriteArray extends Random{

SpriteAlien[] alienArmy = new SpriteAlien[10]; // an array of alien objects
    int index; // index of the array 
    int x, y; // coordinates of the alien

    // constructor
    SpriteArray(){
        // nested loop to position aliens like 2 dimensional arrays on the screen
        for(int i = 0; i < 2; i++){
            for(int j = 0; j < 5; j++)
            {
                index = i*5 + j; // (0*1 + 0) (0*1 + 1) etc

                x = j*120 + 100; // each column is 120 pixels apart
                y = i*80 + 100; // each row is 80 pixels apart 

                alienArmy[index] = new SpriteAlien(x, y);

            }
        }
    }

This is the main class involved, Sprite, and the one most likely to have the error because this is where I've written the code that fires bullets and is supposed to make the aliens get destroyed. I'm aware that this piece of code is annoyingly long, so to make it easier I've put 'LOOK HERE LOOK HERE' as a comment just before the most relevant bit :P

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.util.TimerTask;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Sprite extends JPanel implements ActionListener{

    // GAME ANIMATION
    public int hPosShooter; // shooter's horizontal coordinate
    public int vPosShooter; // shooter's vertical coordinate
    public int hPosBullet; // bullet's horizontal coordinate
    public int vPosBullet = 490; // bullet's vertical coordinate
    public int movementSpeed; // shooter's speed(describe shooter movement)
    public Timer loopTime = new Timer(9, this); // used to loop through movements and repaint
    public Timer generateBullet;
    public boolean started = false; // whether or not the game has started
    public boolean fired = false; // whether or not a shot should exist
    public int numberEnemies = 1; // with more enemies this will need to be edited
    public boolean[] destroyed = new boolean[10]; //Array of boolean variables that tell whether or not the alien with the matching index has been destroyed

    public int shootingIndex;

    // display text information
    PlayerInfo pInfo = new PlayerInfo();
    JLabel scoreLabel = new JLabel("score: " + Integer.toString(pInfo.getPlayerScore()));
    JLabel lifeLabel = new JLabel("lives: " + Integer.toString(pInfo.getPlayerLives()));

    // LOAD IMAGES
    String imageNameShooter; //The shooter
    String imageNameAlien; //The alien
    String imageNameBarrier; //The barrier

    protected BufferedImage imageShooter;
    protected BufferedImage imageAlien;
    protected BufferedImage imageBarrier;


    SpriteArray enemies = new SpriteArray();



//  SpriteBarrier barrier1 = new SpriteBarrier(400, 400);
    SpriteBarrierArray barrier1 = new SpriteBarrierArray(160, 360);
    SpriteBarrierArray barrier2 = new SpriteBarrierArray(360, 360);
    SpriteBarrierArray barrier3 = new SpriteBarrierArray(560, 360);

//  int shootingIndex = enemies.AlienShoot();


    public Sprite (String imageNameShooter, String imageNameAlien, String imageNameBarrier){
        try {
            imageShooter = ImageIO.read(new File(imageNameShooter));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            imageAlien = ImageIO.read(new File(imageNameAlien));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            imageBarrier = ImageIO.read(new File(imageNameBarrier));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        scoreLabel.setHorizontalTextPosition(JLabel.RIGHT);
        lifeLabel.setHorizontalTextPosition(JLabel.LEFT);
        add(scoreLabel);
        add(lifeLabel);
    }

    public static void SpriteImage(JFrame frame, JPanel panel) throws IOException
    {          
        frame.add(panel);
    }

    // PAINT SPRITE
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(imageShooter, hPosShooter, vPosShooter, null); //Draws the shooter in the correct position

        //Draws bullet
        if (fired)
        {
            g.fillRect (hPosBullet, vPosBullet, 8, 12);
        }

        //Draws the aliens
        for(int i = 0; i < 10 ; i++ ){
            g.drawImage(imageAlien, enemies.alienArmy[i].hPosAlien, enemies.alienArmy[i].vPosAlien, null);
            if(destroyed[i] == false)
            {
                g.fillRect (enemies.alienArmy[i].hPosBullet, enemies.alienArmy[i].vPosBullet, 8, 12);
            }
        }

        //Draw the Barriers
        for(int i = 0; i < 12 ; i++ ){
            g.drawImage(imageBarrier, barrier1.barrier[i].hPosBarrier, barrier1.barrier[i].vPosBarrier, null);
            g.drawImage(imageBarrier, barrier2.barrier[i].hPosBarrier, barrier2.barrier[i].vPosBarrier, null);
            g.drawImage(imageBarrier, barrier3.barrier[i].hPosBarrier, barrier3.barrier[i].vPosBarrier, null);
        }

        loopTime.start(); // refreshes the image to make animation work
    }


    //Makes the enemies drop missiles
    void enemyAttack() {

        generateBullet = new Timer(1000, new ActionListener() {
            public void actionPerformed(ActionEvent e){
                shootingIndex = enemies.AlienShoot();
            }
        });
        generateBullet.start();
    }

    //SHOOTER MOVEMENT MECHANICS
    public void actionPerformed(ActionEvent e)
    {   
        //Conditional only allows movement once game has started
        if (started)
        {
            //This only allows movement to the right if the right border hasn't been reached
            if (hPosShooter < 733 && movementSpeed > 0)
            {
                hPosShooter = hPosShooter + movementSpeed;
            }
            //This only allows movement to the left if the left border hasn't been reached
            if (hPosShooter > 0 && movementSpeed < 0)
            {
                hPosShooter = hPosShooter + movementSpeed;
            }

        }

        //FIRE!! LOOK HERE LOOK HERE LOOK HERE!!!!!
        if (fired)
        {
            vPosBullet = vPosBullet - 6; //Bullet moves upwards
            //If alien is hit... it (SHOULD) be destroyed!
            for(int i = 0; i < 10 ; i++ ){
                if (!(destroyed[i]))
                {
                    if (vPosBullet == (enemies.alienArmy[i].vPosAlien + 20) && hPosBullet > enemies.alienArmy[i].hPosAlien && hPosBullet < (enemies.alienArmy[i].hPosAlien + 50))
                    {
                        destroyed[i] = true;
                        repaint();
                        fired = false;
                        vPosBullet = 490;
                    }
                }
            }
            for(int i=0; i<12; i++)
            {
                if (vPosBullet == barrier3.barrier[i].vPosBarrier && hPosBullet > barrier3.barrier[i].hPosBarrier && hPosBullet < (barrier3.barrier[i].hPosBarrier + 20))
                {
                    fired = false;
                    vPosBullet = 490;
                }
                if (vPosBullet == barrier2.barrier[i].vPosBarrier && hPosBullet > barrier2.barrier[i].hPosBarrier && hPosBullet < (barrier2.barrier[i].hPosBarrier + 20))
                {
                    fired = false;
                    vPosBullet = 490;
                }
                if (vPosBullet == barrier1.barrier[i].vPosBarrier && hPosBullet > barrier1.barrier[i].hPosBarrier && hPosBullet < (barrier1.barrier[i].hPosBarrier + 20))
                {
                    fired = false;
                    vPosBullet = 490;
                }
            }
        }
        //OKAY THE IMPORTANT BIT'S OVER NOW!!!

        //When bullet reaches top of screen 'fired' = false
        if (vPosBullet < 0)
        {
            fired = false;
            vPosBullet = 490;
        }

        //This bit of makes the aliens shoot missiles at the user's 'shooter'
        for(int j = 0; j < 10 ; j++ ){
            enemies.alienArmy[j].moveAlien();
            if(enemies.alienArmy[j].shoot == true)
            {
                if(enemies.alienArmy[j].vPosBullet > 600)
                {
                    enemies.alienArmy[j].shoot = false;
                }
                else
                {
                    enemies.alienArmy[j].vPosBullet += 2;
                }

            }
        }


        //The position is then reset
        repaint();
    }
}

Thank you so much for any help you can provide!

maskacovnik

I am not sure but

if (vPosBullet == (enemies.alienArmy[i].vPosAlien + 20) && hPosBullet > enemies.alienArmy[i].hPosAlien && hPosBullet < (enemies.alienArmy[i].hPosAlien + 50))

There in first test

vPosBullet == (enemies.alienArmy[i].vPosAlien + 20)

I am not sure this coords will be exact this, there should be lower than and greater than maybe.

because of this:

vPosBullet = vPosBullet - 6; //Bullet moves upwards

Maybe not soulution but you code is so long ;P

EDIT:

As @user3659404 said in comment, update the if statement to:

if (vPosBullet < (enemies.alienArmy[i].vPosAlien + 20) && vPosBullet > enemies.alienArmy[i].vPosAlien && hPosBullet > enemies.alienArmy[i].hPosAlien && hPosBullet < (enemies.alienArmy[i].hPosAlien + 50))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related