[英]How can I move figures only with keyboard React
export const Chessboard: React.FC = () => {
const board = [];
for (let j = horisontalAxis.length - 1; j >= 0; j -= 1) {
for (let i = 0; i < verticalAxis.length; i += 1) {
const num = j + i + 2;
let image = '';
figures.forEach(el => {
if (el.x === i && el.y === j) {
image = el.image;
}
});
board.push(<Cell key={`${i}, ${j}`} image={image} num={num} />);
}
}
return (
<div className="chessboard">{board}</div>
);
};
I am trying to make chess game that works only with keyboard.我正在尝试制作仅适用于键盘的国际象棋游戏。 How can I select figure with keyboard without mouse and make move also?如何在没有鼠标的情况下使用键盘选择图形并进行移动?
Implement a cursor.实现一个游标。 Make a state that holds the current coordinates of the cursor (x,y).创建一个保存光标当前坐标 (x,y) 的状态。 Also draw the cursor on your board (like a border around the cell).还要在板上绘制光标(如单元格周围的边框)。 Then listen for key presses of the arrow keys and use this to update the cursor position.然后监听箭头键的按键并使用它来更新光标位置。
Then you need some more keys (space, enter), and those make you select a piece at the cursor position, or put down a selected piece.然后你需要更多的键(空格,回车),这些让你在光标位置选择一块,或者放下一个选定的块。 This adds some more state (held piece, which could be null), and you need to think about how to visualize this.这会增加一些状态(持有的部分,可能为空),您需要考虑如何将其可视化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.