如何为我的游戏添加键盘支持?

时间:2021-08-08 09:17:56

I have been trying to make a Pong clone as sort of a beginner's project, and I've hit a road block. I have tried various methods to implement keyboard support, but none have worked.

我一直试图将Pong克隆作为一个初学者的项目,我已经遇到了障碍。我已经尝试了各种方法来实现键盘支持,但没有一种方法有效。

For reference here is my Pong.java(the main class): http://pastebin.com/8d7BqK4x

这里是我的Pong.java(主类)的参考:http://pastebin.com/8d7BqK4x

Here is the Board.java(Graphics): http://pastebin.com/N9fLMzFT

这是Board.java(图形):http://pastebin.com/N9fLMzFT

1 个解决方案

#1


0  

Just some general advice, you can add keyboard listeners and then interpret the input, here's a link with some advice on keyboard listeners: http://download.oracle.com/javase/tutorial/uiswing/events/keylistener.html. For a game, I recommend using a "key pressed" event.

只是一些一般性的建议,你可以添加键盘监听器然后解释输入,这里是一个关于键盘监听器的一些建议的链接:http://download.oracle.com/javase/tutorial/uiswing/events/keylistener.html。对于游戏,我建议使用“按下按键”事件。

#1


0  

Just some general advice, you can add keyboard listeners and then interpret the input, here's a link with some advice on keyboard listeners: http://download.oracle.com/javase/tutorial/uiswing/events/keylistener.html. For a game, I recommend using a "key pressed" event.

只是一些一般性的建议,你可以添加键盘监听器然后解释输入,这里是一个关于键盘监听器的一些建议的链接:http://download.oracle.com/javase/tutorial/uiswing/events/keylistener.html。对于游戏,我建议使用“按下按键”事件。