[Java2Dゲーム開発]キーボード入力/矢印キーを監視して人を制御する



Monitor Keyboard Input Arrow Keys Control People



最終効果:
画像


ゲームを制御するにはキーボードを使用する必要があるので、ここにモニターのキーボードボタンがあります



モニターボタンは非常にシンプルで、KeyListenerインターフェースを実装するだけです。
キーボード入力を聞くことができると仮定すると、Playerクラスのボタンをどのように聞くのですか?

@Override public void onTick() { if(Press the left arrow key) Shift left if(Press the right arrow key) Right shift }

解決策は、クラスInputを作成し、ゲームウィンドウのキーボード入力をリッスンし、押されたキーを記録してから、メソッドgetKeyDownを実装することです。呼び出しが渡されると、リッスンするボタン、メソッドはボタンが押されたかどうかを返します。



@Override public void onTick() { if(Input.getKeyDown(Left arrow)) Shift left if(Input.getKeyDown(Right arrow)) Right shift }

では、ボタンのステータス(押されているかどうか)をどのように記録しますか?
これらのデータはすべてButton - stateフォームKey - valueフォームはHashMapで記録できます

Key - value Button A - press Button B - not pressed Button C - press ...

HashMapの使用方法:

public static void main(String[] args) { HashMap<Integer, Boolean> map = new HashMap<>(300) map.put(0, true)//HashMap.put(key, valua) Add a pair of key values ​​to the HashMap (if there is the same key, replace it already) map.put(22, true) map.put(666, false) System.out.println(map.get(666))//HashMap.get(key) Get the value of the corresponding key System.out.println(map.get(0)) map.put(0, false)/ / Modify the value of the key 0 is false System.out.println(map.get(0)) }

出力:



false true false

新しい入力クラス

package com.game.input import java.awt.event.KeyEvent import java.awt.event.KeyListener import java.util.HashMap public class Input implements KeyListener { private static HashMap<Integer, Boolean> keys public final static int KEY_COUNTS = 300/ / The number of keys stored public void init() { keys = new HashMap<Integer, Boolean>(KEY_COUNTS) for(int i = 0 i < KEY_COUNTS i++) { keys.put(i, false) } } @Override public void keyPressed(KeyEvent key)//When there is a button press { keys.put(key.getKeyCode(), true)/ / Set the corresponding button status to true } @Override public void keyReleased(KeyEvent key)/ / When the button is released { keys.put(key.getKeyCode(), false)/ / Set the corresponding button status to false } @Override public void keyTyped(KeyEvent key) { } public static boolean getKeyDown(int keyCode) { return keys.get(keyCode)/ / Returns the status of the corresponding button } }

GameクラスでInputクラスを初期化します

//Game class constructor public Game(int windowWidth, int windowHeight, int fps) { width = windowWidth height = windowHeight this.fps = fps windowTitle = 'Game' backgroundColor = Color.BLACK gameObjects = new ArrayList<GameObject>() createWindow() render = new RenderThread(this) render.start() / / Initialize the input device Input input = new Input() input.init() this.addKeyListener(input)/ / Set the keyboard listener of the game window }

Playerクラスのボタンを聞いて、対応する動きをします

private int speed = 4//Moving speed @Override public void onTick() { if(Input.getKeyDown(KeyEvent.VK_LEFT))//When the left arrow key is pressed this.transfer(-1 * speed, 0)/ / left shift (n-axis negative direction) if(Input.getKeyDown(KeyEvent.VK_RIGHT)) this.transfer(1 * speed, 0)//Shift right (x-axis positive direction) if(Input.getKeyDown(KeyEvent.VK_UP)) this.transfer(0, -1 * speed)//Upward (y-axis negative direction) if(Input.getKeyDown(KeyEvent.VK_DOWN)) this.transfer(0, 1 * speed)/ / Move down (y-axis negative direction) }

JavaSwingの座標系は次のようになっていることに注意してください
スイング
これの代わりに
画像
したがって、y軸座標を+1ではなく-1まで移動します

実行結果:
画像