使用一个键盘进行双人游戏

时间:2017-11-14 17:30:23

标签: java swing keyboard

我正在制作一个游戏,我正在制作一个项目。它主要包括两个使用WASD移动和E停止的玩家,第二个玩家使用de箭头键移动和SHIFT停止。

我遇到的主要问题是当两个球员同时移动火箭时,当一个火箭向左或向右转动时,另一个玩家根本无法向两侧移动,或者它向前移动但是缓慢地向前移动同样的问题,它不能向左或向右移动。

有关控件的问题或我在代码中没有考虑的任何问题的想法吗?

这是游戏的一些课程:

GameDraw Class,它具有玩家的动作和绘制火箭。

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

class GameDraw extends JComponent{
            static Jugador rocket1 = new Jugador();
            static Jugador rocket2 = new Jugador();
            int width = GUI.width;
            int height = GUI.height;

            public GameDraw() {}

            public void paint(Graphics g) {
                Graphics2D graphicsSet = (Graphics2D) g;
                AffineTransform id = new AffineTransform();
                AffineTransform id2 = new AffineTransform();
                graphicsSet.setColor(Color.LIGHT_GRAY);
                graphicsSet.fillRect(0, 0, getWidth(), getHeight());
                graphicsSet.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                graphicsSet.setPaint(Color.blue);

                //Player 1 Controls//
                if(GUI.keyHeld_D == true && GUI.keyHeldCode == Controls.D) {
                    rocket1.increaseRotAngle();
                }
                else if(GUI.keyHeld_A == true && GUI.keyHeldCode == Controls.A) {
                    rocket1.decreaseRotAngle();
                }
                else if(GUI.keyHeld_W == true && GUI.keyHeldCode == Controls.W) {
                    rocket1.setMovingAngle(rocket1.getRotationAngle());
                    rocket1.increaseXVel(rocket1.rocketXMoveAngle(rocket1.getMovingAngle())*0.1);
                    rocket1.increaseYVel(rocket1.rocketYMoveAngle(rocket1.getMovingAngle())*0.1);   
                }
                else if(GUI.keyHeld_S == true && GUI.keyHeldCode == Controls.S) {
                    rocket1.setMovingAngle(rocket1.getRotationAngle());
                    rocket1.decreaseXVel(rocket1.rocketXMoveAngle(rocket1.getMovingAngle())*0.1);
                    rocket1.decreaseYVel(rocket1.rocketYMoveAngle(rocket1.getMovingAngle())*0.1);
                }
                else if(GUI.keyHeld_E == true && GUI.keyHeldCode == Controls.E) {
                    rocket1.stopRocket();
                }


                //Player 2 Controls//
                if(GUI.keyHeld_RIGHT == true && GUI.keyHeldCode == Controls.RIGHT) {
                    rocket2.increaseRotAngle();
                }
                else if(GUI.keyHeld_LEFT == true && GUI.keyHeldCode == Controls.LEFT) {
                    rocket2.decreaseRotAngle();
                }
                else if(GUI.keyHeld_UP == true && GUI.keyHeldCode == Controls.UP) {
                    rocket2.setMovingAngle(rocket2.getRotationAngle());
                    rocket2.increaseXVel(rocket2.rocketXMoveAngle(rocket2.getMovingAngle())*0.1);
                    rocket2.increaseYVel(rocket2.rocketYMoveAngle(rocket2.getMovingAngle())*0.1);   
                }
                else if(GUI.keyHeld_DOWN == true && GUI.keyHeldCode == Controls.DOWN) {
                    rocket2.setMovingAngle(rocket2.getRotationAngle());
                    rocket2.decreaseXVel(rocket2.rocketXMoveAngle(rocket2.getMovingAngle())*0.1);
                    rocket2.decreaseYVel(rocket2.rocketYMoveAngle(rocket2.getMovingAngle())*0.1);
                }
                else if(GUI.keyHeld_SHIFT == true && GUI.keyHeldCode == Controls.SHIFT) {
                    rocket2.stopRocket();
                }

                rocket1.movement();
                graphicsSet.setTransform(id);
                graphicsSet.translate(rocket1.getXCenter(), rocket1.getYCenter());
                graphicsSet.rotate(Math.toRadians(rocket1.getRotationAngle()));
                graphicsSet.draw(rocket1);

                rocket2.movement();
                graphicsSet.setTransform(id2);
                graphicsSet.translate(rocket2.getXCenter(), rocket2.getYCenter());
                graphicsSet.rotate(Math.toRadians(rocket2.getRotationAngle()));
                graphicsSet.draw(rocket2);
            }
        }

GUI类

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class GUI extends JFrame {
    public static int width = 1000;
    public static int height = 600;
    public static boolean keyHeld_W = false, keyHeld_A = false, keyHeld_S = false, keyHeld_D = false, keyHeld_E = false, keyHeld_UP = false, keyHeld_LEFT = false, keyHeld_DOWN = false, keyHeld_RIGHT = false, keyHeld_SHIFT = false;
    public static int keyHeldCode;

    public GUI() {
        this.setTitle("ROCKET FOOTBALL");
        this.setSize(width, height);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.addKeyListener(new KeyAdapter() {

            public void keyTyped(KeyEvent e) {}

            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                keyHeldCode = keyCode;
                //Player 1 Controls//
                if(keyHeldCode == Controls.W) {
                    Jugador.interaction_W = true;
                    keyHeld_W = true;
                }
                if(keyHeldCode == Controls.A) {
                    Jugador.interaction_A = true;
                    keyHeld_A = true;
                }
                if(keyHeldCode == Controls.S) {
                    Jugador.interaction_S = true;
                    keyHeld_S = true;
                }
                if(keyHeldCode == Controls.D) {
                    Jugador.interaction_D = true;
                    keyHeld_D = true;
                }
                if(keyHeldCode == Controls.E) {
                    Jugador.interaction_E = true;
                    keyHeld_E = true;
                }

                    //Player 2 Controls//
                if(keyHeldCode == Controls.UP) {
                    Jugador.interaction_UP = true;
                    keyHeld_UP = true;
                }
                if(keyHeldCode == Controls.LEFT) {
                    Jugador.interaction_LEFT = true;
                    keyHeld_LEFT = true;
                }
                if(keyHeldCode == Controls.DOWN) {
                    Jugador.interaction_DOWN = true;
                    keyHeld_DOWN = true;
                }
                if(keyHeldCode == Controls.RIGHT) {
                    Jugador.interaction_RIGHT = true;
                    keyHeld_RIGHT = true;
                }
                if(keyHeldCode == Controls.SHIFT) {
                    Jugador.interaction_SHIFT = true;
                    keyHeld_SHIFT = true;
                }
            }

            public void keyReleased(KeyEvent e) {
                //Player 1//
                    if(keyHeldCode == Controls.W) {
                        keyHeld_W = false;
                    }
                    if(keyHeldCode == Controls.A) {
                        keyHeld_A = false;
                    }
                    if(keyHeldCode == Controls.S) {
                        keyHeld_S = false;
                    }
                    if(keyHeldCode == Controls.D) {
                        keyHeld_D = false;
                    }
                    if(keyHeldCode == Controls.E) {
                        keyHeld_E = false;
                    }

                    //Player 2//
                    if(keyHeldCode == Controls.UP) {
                        keyHeld_UP = false;
                    }
                    if(keyHeldCode == Controls.LEFT) {
                        keyHeld_LEFT = false;
                    }
                    if(keyHeldCode == Controls.DOWN) {
                        keyHeld_DOWN = false;
                    }
                    if(keyHeldCode == Controls.RIGHT) {
                        keyHeld_RIGHT = false;
                    }
                    if(keyHeldCode == Controls.SHIFT) {
                        keyHeld_SHIFT = false;
                    }
                }
            });
        GameDraw gamePanel = new GameDraw();
        this.add(gamePanel, BorderLayout.CENTER);
        ScheduledThreadPoolExecutor ex = new ScheduledThreadPoolExecutor(2);
        ex.scheduleAtFixedRate(new RepaintTheBoard(this), 0L,20L, TimeUnit.MILLISECONDS);
        this.setResizable(false);
        this.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:0)

  

当一枚火箭向左或向右转动时,其他玩家根本无法移动

只能为最后按下的键生成KeyEvents。所以基本上你需要在按下时跟踪一个键,你需要使用一个Timer来安排动画。因此,每次Timer触发时,都会检查当前按下的键并调用相应的操作。

  

还使用键绑定

是的,密钥绑定是首选方法,因为它可以消除焦点问题。

查看Motion Using the Keyboard中的KeyboardAnimation示例。该示例对一个玩家使用4个箭头键,对另一个玩家使用WASD,但有一些区别:

  1. 您可以同时按下两个键以进行对角线移动
  2. 您需要释放按键才能停止动作