[英]how to use keylistener and timer at the same time?
I am trying to make the game snake but I cant seem to get the keyboard controls to run the same time as the graphics display. 我试图使游戏成为蛇,但是我似乎无法使键盘控件与图形显示同时运行。 how can i implement keylistener inside the timer method? 我如何在timer方法内实现keylistener?
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;
public class Game extends JFrame {
private static final long serialVersionUID = 7607561826495057981L;
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice();
int width = gd.getDisplayMode().getWidth();
int height = gd.getDisplayMode().getHeight();
private Timer timer;
private int xCor = 400, yCor = 150;
private int speed = 1, move = 1;
private final int top = 0, bottom = height - 10, right = width - 10,
left = 0;
private boolean north = false, south = false, east = true, west = false;
public Game() {
setTitle("Snake");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(width, height);
setResizable(false);
init();
setVisible(true);
}
public void init() {
timer = new Timer(speed, new TimerListener());
timer.start();
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.black);
g.fillRect(xCor, yCor, 10, 10);
}
private class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent f) {
if (south) {
if (xCor >= left && xCor <= right && yCor >= top
&& yCor <= bottom) {
yCor += move;
}
}
if (north) {
if (xCor >= left && xCor <= right && yCor >= top
&& yCor <= bottom) {
yCor -= move;
}
}
if (west) {
if (xCor >= left && xCor <= right && yCor >= top
&& yCor <= bottom) {
xCor -= move;
}
}
if (east) {
if (xCor >= left && xCor <= right && yCor >= top
&& yCor <= bottom) {
xCor += move;
}
}
repaint();
}
}
public static void main(String[] args) {
new Game();
}
}
this is my keyboard control class 这是我的键盘控制课
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class KeyControls extends JFrame implements KeyListener {
private static final long serialVersionUID = 2227545284640032216L;
private boolean north = false, south = false, east = true, west = false;
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
int i = e.getKeyChar();
if (i == KeyEvent.VK_W && !south) {
north = true;
east = false;
west = false;
south = false;
}
if (i == KeyEvent.VK_S && !north) {
north = false;
east = false;
west = false;
south = true;
}
if (i == KeyEvent.VK_A && !west) {
north = false;
east = true;
west = false;
south = false;
}
if (i == KeyEvent.VK_D && !east) {
north = false;
east = false;
west = true;
south = false;
}
}
}
KeyControls
to something that is capable of generating KeyEvents
您需要将KeyControls
注册到能够生成KeyEvents
东西上 KeyControls
to extend from JFrame
, that's just going to confuse things. 无需从JFrame
扩展KeyControls
, KeyControls
事情变得混乱。 KeyListener
, it will give you better control over the level of focus a component needs to have before it triggers a key event. 考虑使用键绑定API而不是KeyListener
,它将使您更好地控制组件在触发键事件之前需要具有的焦点级别。 See How to Use Key Bindings for more details 有关更多详细信息,请参见如何使用键绑定 。 paint
of top level containers like JFrame
, in your case, this is going to cause flickering when ever the frame is updated. 在您的情况下,请避免覆盖诸如JFrame
类的顶级容器的paint
,这将在框架更新时引起闪烁。 Instead, create a custom class extending from something like JPanel
and override it's paintComponent
method and place you custom painting there. 相反,创建一个从JPanel
类扩展的自定义类,并覆盖它的paintComponent
方法,然后在此处自定义绘画。 It would probably be prudent to encapsulate the Timer
and key bindings within this class as well. 同样,最好将Timer
和键绑定封装在此类中。 Have a look at Painting in AWT and Swing and Performing Custom Painting for more details 有关更多详细信息,请查看AWT中的绘画以及“摇摆和执行自定义绘画” 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.