如何设计多按键监听

自己写的毕设,遇到的问题,刚开始发现找的解决方法都很不如人意,就自己想办法设计了这个,请大佬指教。

键盘的监听使用KeyListener接口,重写KeyPressed、KeyReleased和KeyTyped三个方法。因为玩家有极大可能不单单只是按键盘一下而是长按甚至多个长按,所以这里不再使用KeyTyped的点击方法,这时再只是通过KeyEvent事件来监听而不做处理,只能设置多种组合的按键可能性全排列进行判断,如果后续有其他按键加入则排列重新设置很麻烦不好维护。这里按键的程序设置是:为每一个游戏中需要的按键设置一个boolean型变量并初始化为false。当多个按键按下时候,执行KeyPressed方法:先是多个判断每个按键是否被按下(注意:这里每个判断只写判断一个按键是否被按下,这是区别上述的复杂方法的核心),按下则对应的boolean型变量变为true,在多个判断执行完紧接着就是判断每个按键对应的boolean型变量是否为true,如果是真就执行相应的方法(注意:在KeyPressed的方法里每个判断都是if不是else if)。KeyPressed的方法就结束了。再者就是KeyReleased方法里的判断是KeyPressed方法里的判断方式,也是单个条件多次不同的判断,如果按键释放则对应的boolean型变量设为false

 

现设计代码↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

https://github.com/xiaobaobao007/Swing_Client/blob/master/src/Controller/KeyListen.java

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import Enity.OwnPeople;
import Client.ClientStart;
import GameStart.GameStart;

public class KeyListen implements KeyListener {
    private boolean up = false;
    private boolean left = false;
    private boolean right = false;
    private boolean down = false;
    private boolean attack = false;
    private boolean Magic_attack = false;
    private boolean space = false;

    public KeyListen() {
        super();
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W) {
            up = true;
        }
        if (e.getKeyCode() == KeyEvent.VK_D) {
            right = true;
        }
        if (e.getKeyCode() == KeyEvent.VK_A) {
            left = true;
        }
        if (e.getKeyCode() == KeyEvent.VK_S) {
            down = true;
        }
        if (e.getKeyCode() == KeyEvent.VK_J) {
            attack = true;
        }
        if (e.getKeyCode() == KeyEvent.VK_K) {
            Magic_attack = true;
        }
        if (e.getKeyCode() == KeyEvent.VK_P) {
            OwnPeople.stateController.Start_Frozen(1000);
            GameController.own.addHurtBloods(0, 10, 8, "");
        }
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            GameStart.TF = false;
        }
        if (e.getKeyCode() == KeyEvent.VK_SPACE) {
            space = true;
        }
        if (up) {
            if (OwnPeople.stateController.Move()) {
                GameController.own.setDirect(0);
                GameController.own.move(0);
            }
        }
        if (down) {
            if (OwnPeople.stateController.Move()) {

                GameController.own.setDirect(2);
                GameController.own.move(2);
            }
        }
        if (left) {
            if (OwnPeople.stateController.Move()) {

                GameController.own.setDirect(3);
                GameController.own.move(1);
            }
        }
        if (right) {
            if (OwnPeople.stateController.Move()) {
                GameController.own.setDirect(1);
                GameController.own.move(3);
            }
        }
        if (attack) {
            if (OwnPeople.stateController.Attack()) {
                ClientStart.OutStreamAll(GameController.own_cilent_id + ":0101:2:1");
                GameController.own.people_attack();
            }
        }
        if (Magic_attack) {
            if (OwnPeople.stateController.Attack()) {
                GameController.own.Magic_attack();
            }
        }
        if (space) {
            ClientStart.OutStreamAll(GameController.own_cilent_id + ":0101:3:1");
            GameController.own.pullGoods();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W) {
            up = false;
        }
        if (e.getKeyCode() == KeyEvent.VK_D) {
            right = false;
        }
        if (e.getKeyCode() == KeyEvent.VK_A) {
            left = false;
        }
        if (e.getKeyCode() == KeyEvent.VK_S) {
            down = false;
        }
        if (e.getKeyCode() == KeyEvent.VK_J) {
            attack = false;
        }
        if (e.getKeyCode() == KeyEvent.VK_K) {
            Magic_attack = false;
        }
//		if (e.getKeyCode() == KeyEvent.VK_T) {
//			System.out.println(GameController.own.getX()+"|"+GameController.own.getY());
//		}
        if (e.getKeyCode() == KeyEvent.VK_SPACE) {
            space = false;
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }
}

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值