gpt4 book ai didi

java - 如何设置切换 boolean 变量状态的按键检测

转载 作者:太空宇宙 更新时间:2023-11-04 09:05:26 24 4
gpt4 key购买 nike

如何设置按键检测来切换 boolean 变量 paused 的状态,其中 paused 是我的 GCTester 类的一个属性,初始状态为 false

// GCTester demonstrates how we can override the GameCore class
// to create our own 'game'. We usually need to implement at
// least 'draw' and 'update' (not including any local event handling)
// to begin the process. You should also write your own 'init'
// method that will initialise event handlers etc. By default GameCore
// will handle the 'Escape' key to quit the game but you should
// override this with your own event handler.

public class GCTester extends GameCore {

private Animation anim;
private Sprite rock;
long total; // Total time elapsed
boolean paused;

// The obligatory main method that creates
// an instance of our GCTester class and
// starts it running
public static void main(String[] args) {
GCTester gct = new GCTester();
gct.init();
// Start in windowed mode with a 800x600 screen
gct.run(false, 800, 600);
}

// Initialise the class, e.g. set up variables
// animations, register event handlers
public void init() {
total = 0;
Image player = loadImage("images/rock.png");
anim = new Animation();
anim.addFrame(player, 20);

rock = new Sprite(anim);

rock.setX(100);
rock.setY(100);
rock.setVelocityX(0.1f);
rock.setVelocityY(0.1f);
rock.show();
}

// Draw the current frame
public void draw(Graphics2D g) {
// A simple demo - note that this is not
// very efficient since we fill the screen
// on every frame.
g.setColor(Color.black);
g.fillRect(0, 0, 800, 600);
g.setColor(Color.yellow);
g.drawString("Time Expired:" + total, 30, 50);

rock.draw(g);
}

// Update any sprites and check for collisions
public void update(long elapsed) {
if (paused) return;
total += elapsed;
if (total > 60000) stop();
rock.update(elapsed);
}

public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ESCAPE)
stop(); // Call GameCore stop method to stop the game loop
else {
if (keyCode == KeyEvent.VK_S) rock.stop();
if (keyCode == KeyEvent.VK_G) rock.setVelocityX(0.1f);

e.consume();
}
}
}

最佳答案

您应该使用窗口组件在屏幕上显示您的应用程序。假设它是一个JFrame(或者它可以是一个JDialog)。创建一个将“监听”关键操作的 JComponent,将其添加到您的 jFrame 或您使用的任何其他上部窗口组件中:

JComponent controlComp = new JComponent() {};
controlComp.setBounds(0, 0, 800, 600); // your bounds
controlComp.setFocusTraversalKeysEnabled(false);
jFrame.add(controlComp);

ActionListener keyEscListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
paused = !paused;
// your logic for Escape
}
};

// tie KeyStroke and keyboard action to the controlComp
KeyStroke keyEsc = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
controlComp.registerKeyboardAction(keyEscListener, keyEsc, JComponent.WHEN_FOCUSED);

此方法需要为每个要监听的按键创建单独的 Listener 和 KeyStroke 对象

关于java - 如何设置切换 boolean 变量状态的按键检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60264891/

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