java - 子弹不上行! Java游戏

标签 java keypress bullet

我是 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();

        }
    }

[这是当我移动船时发生的情况,工作得很好 -][1]

[这是当我按住空格时会发生的情况 -][2]

我实际上是从教程中处理这个代码的,但是由于教程代码不起作用,我决定自己做这件事,但我自己也做不到!

最佳答案

当您比较 shoot() 方法和 paintComponent 方法时,子弹不移动的原因就会显现出来。

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

当子弹离开屏幕顶部时,它会重置“子弹”。 这一切都很好,它完成了它应该做的事情。

 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,它是在游戏“绘制”到屏幕上时执行的。

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

然后覆盖子弹的 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();
    }

关于java - 子弹不上行! Java游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46463806/

相关文章:

javascript - 搜索引擎,如谷歌搜索

c++ - 如何使用 Bullet Physics 制作 Spring 约束?

java - 子弹物理,纹理球体不滚动

java - 该语句是否调用构造函数 obj=new checker[10]?

java - Java 中没有 instanceof 或 getClass() 的动态调度

jquery - jQuery keypress、keydown、keyup 黑魔法(在 Mac 上)背后的理论是什么?

Java——KeyPressed 事件后立即调用 KeyReleased

ios - 用项目符号格式化 UILabel?

java - EntityManager 删除 - IllegalArgumentException : Unknown entity: java. lang.Integer] 根本原因

java - 不使用maven安装protoc(win7、java)