gpt4 book ai didi

java - 如何同时使用按键监听器和定时器?

转载 作者:行者123 更新时间:2023-12-01 11:40:25 25 4
gpt4 key购买 nike

我正在尝试让游戏变得蛇形,但我似乎无法让键盘控件与图形显示同时运行。如何在计时器方法中实现按键监听器?

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();
}
}

这是我的键盘控制类

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;
}
}
}

最佳答案

  1. 您需要注册KeyControls到能够生成 KeyEvents 的东西
  2. 不需要 KeyControlsJFrame 延伸,这只会让事情变得困惑。
  3. 考虑使用按键绑定(bind) API 而不是 KeyListener ,它可以让您更好地控制组件在触发关键事件之前所需的焦点级别。请参阅How to Use Key Bindings了解更多详情
  4. 避免覆盖 paint顶级容器,如 JFrame ,就您而言,当框架更新时,这将导致闪烁。相反,创建一个从类似 JPanel 扩展的自定义类。并覆盖它的 paintComponent方法并将您的自定义绘画放置在那里。封装 Timer 可能是谨慎的做法以及此类中的键绑定(bind)。看看Painting in AWT and SwingPerforming Custom Painting了解更多详情

关于java - 如何同时使用按键监听器和定时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29573698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com