编写太空入侵者代码,无法获得子弹来摧毁外星人

时间:2015-04-29 05:35:45

标签: java

我提前为这个问题道歉,可能需要付出一些努力才能解决。这是一个我已经工作了一段时间的小组项目,因此脚本有点复杂,我和我正在使用的其他两个人都是java的初学者。不幸的是我的问题是我不知道问题是什么,所以我的问题有点模糊。我将尽我所能向您展示代码的相关内容以及任何可能出现问题的帮助,我们将不胜感激:)

这个类SpriteAlien创造了一个外星人,并且有定义外星人运动的方法。

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;
    }
}

这个类SpriteArray创建了一个像阵形一样移动的外星人数组

//创建一个外星人阵列,让我们的外星军队 公共类SpriteArray扩展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);

            }
        }
    }

这是涉及的主要类,Sprite,以及最有可能出错的人,因为这是我编写了发射子弹的代码,并且应该让外星人被摧毁。我知道这段代码很烦人,所以为了让我更容易,我把'在这里看到这里'作为评论,就在最相关的位之前: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();
    }
}

非常感谢您提供的任何帮助!

1 个答案:

答案 0 :(得分:3)

我不确定但是

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

首次测试

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

我不确定这种坐标是否正确,应该低于和大于可能。

因为这个:

vPosBullet = vPosBullet - 6; //Bullet moves upwards

也许不是洗漱,但你的代码太长了; P

编辑:

正如@ user3659404在评论中所说,将if语句更新为:

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