子弹不向上移动! Java 游戏

Bullet not travelling up! Java game

我是 Java 的新手,我正在尝试让一艘船发射子弹。我想要的实际上是让飞船在按住空格键的同时发射子弹。 我已经成功地让船到处移动并发射了子弹。然而子弹就是上不去。这是我的代码 -

package learningPackage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

public class Draw extends JFrame implements Runnable {

    //Variables for the x and y coordinates, xDirection for modifying the values of x only.
    int x, y, xDirection;
    int bx, by;

    Image dbImage;
    Graphics dbGraphics;

    boolean shot;

    Rectangle bullet;


    //Thread run
    public void run() {
        try {
            while (true) {
                move();
                shoot();
                //Setting sleep to 0 will make it light-speed!
                Thread.sleep(5);

            }
        }
        catch (Exception e) {
            System.out.println("Error!");
            }
    }



    //Ship move
    //Ship moves only in one direction, x - axis
    public void move() {
        x += xDirection;

        //Collision detection
        if (x <= 10) {
            x = 10;
        }
        if (x >= 415) {
            x = 415;
        }
    }

    //KeyListeners
    public class AL extends KeyAdapter {
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();
            if (keyCode == e.VK_LEFT) {
                xDirection = -2;
            }
            if (keyCode == e.VK_RIGHT) {
                xDirection = 2;
            }
            if (keyCode == e.VK_SPACE) {
                shot = true;

            }
        }

            public void keyReleased(KeyEvent e) {
                int keyCode = e.getKeyCode();
                if (keyCode == e.VK_LEFT) {
                    xDirection = 0;
                }
                if (keyCode == e.VK_RIGHT) {
                    xDirection = 0;
                }
                if (keyCode == e.VK_SPACE) {
                    shot = false;
                }
            }
        }


        //Constructor for the game frame
        public Draw() {
            super("Game");
            setSize(500, 500);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setResizable(false);
            addKeyListener(new AL());

            x = 200;
            y = 465;


            setVisible(true);
        }

        //Double - buffering
        public void paint(Graphics g) {
            dbImage = createImage(getWidth(), getHeight());
            dbGraphics = dbImage.getGraphics();
            paintComponent(dbGraphics);
            g.drawImage(dbImage, 0, 0, this);

        }

        //All the graphics
        public void paintComponent(Graphics g) {

            bullet = new Rectangle(bx, by, 10, 10);
            g.setColor(Color.RED);
            //Ship rectangle
            g.fillRect(x, y, 75, 25);
            //Gun rectangle
            g.fillRect(x + 32, y - 15, 10, 15);

            //Setting the same values for bx and by as x and y so that the bullet will start from the Gun rectangle
            bx = x + 32;
            by = y - 15;

            if (shot == true) {

                g.setColor(Color.BLACK);
                g.fillRect(bx, by, bullet.width, bullet.height);

            }

            repaint();

        }


        public void shoot() {
            if (shot == true) {
                by = by - 2;
            }
            if (by <= -5) {
                //Resetting values
                bx = x + 32;
                by = y - 15;
                bullet = new Rectangle(bx, by, 10, 10);
                shot = false;
            }
        }

        //Main method
        public static void main(String[] args) {
            Draw gameTry = new Draw();
            Thread t1 = new Thread(gameTry);
            t1.start();

        }
    }

Here's what happens when I just move the ship, working perfectly fine -

Here's what happens when I hold down space -

(很抱歉无法将图片嵌入 post 本身,我也是 Stack Overflow 的新手!)

我实际上是从教程中处理这段代码的,但是由于教程代码没有用,我决定自己做,但我自己也做不到! 帮助一定会 不胜感激!

当你比较你的 shoot() 方法和 paintComponent 方法时,子弹不动的原因就很明显了。

Shoot 检查您是否设置了 shot 布尔值,如果是,则将子弹的 y 位置向上移动 2

当子弹离开屏幕顶部时,它会重置 "bullet"。 这一切都很好,它做了它应该做的。

 public void shoot() {
        if (shot == true) {
            by = by - 2; //this is fine, it moves the bullet up
        }
        if (by <= -5) {
            //Resetting values
            bx = x + 32;
            by = y - 15;
            bullet = new Rectangle(bx, by, 10, 10);
            shot = false;
        }
    }

然后是 paintComponent,它会在您的游戏 "painted" 出现在屏幕上时执行。

它为当前位置的项目符号定义了一个矩形, 画船,

然后覆盖子弹的 x 和 y 位置,使其位于船顶。 这就是你的问题所在

    public void paintComponent(Graphics g) {
        bullet = new Rectangle(bx, by, 10, 10);
        g.setColor(Color.RED);
        g.fillRect(x, y, 75, 25);
        g.fillRect(x + 32, y - 15, 10, 15);

        //you are messing with bx and by here.
        //probably because you wanted the bullet to be in the
        //same position as the ship.

        //this means they will be put back into the same position
        //for every time your game is painted to the screen.
        //my advice is, do *not* do this here.
        bx = x + 32;
        by = y - 15;

        if (shot == true) {
            g.setColor(Color.BLACK);
            g.fillRect(bx, by, bullet.width, bullet.height);
        }
        repaint();
    }