gpt4 book ai didi

java - Java 游戏中的 key 管理器

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

我正在用 Java 创建一个游戏,但在按下空格键时尝试发射的射弹出现问题。我有一个关于如何执行此操作的想法,但我遇到了问题。

public class GameState extends State{

private Player player;
private World world;
private Bullet bullet;


public GameState(Game game){
super(game);
player = new Player(game, 420, 420);
world = new World(game, "res/worlds/world1.txt");
bullet = new Bullet(game, 420, 420, 0, 0);

}



@Override
public void tick() {
world.tick();
bullet.tick();
player.tick();


while(game.getKeyManager().keyPressed(e)){
bullet.setX(player.getX());
bullet.setY(player.getY());
}
}

@Override
public void render(Graphics g) {

final int height = 64, width = 64;


world.render(g);
bullet.render(g);
player.render(g);



}



}

<strong>while(game.getKeyManager().keyPressed(e)){</strong>给我带来了 e 下的红线问题。它无法解析为变量,但老实说我不知道​​如何处理它。这也是我的 KeyManager 代码。

public class KeyManager implements KeyListener{

private boolean[] keys;
public boolean up, down, left, right, action;


public KeyManager(){
keys = new boolean[256];

}


public void tick(){

up = keys[KeyEvent.VK_W];
down = keys[KeyEvent.VK_S];
left = keys[KeyEvent.VK_A];
right = keys[KeyEvent.VK_D];
action = keys[KeyEvent.VK_SPACE];


}


@Override
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
System.out.println("Pressed");

}


@Override
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;


}


@Override
public void keyTyped(KeyEvent e) {


}


public boolean[] getKeys() {
return keys;
}


public void setKeys(boolean[] keys) {
this.keys = keys;
}

}

老实说,我不知道我做错了什么,因为我相对较新。如果您碰巧有一个比我的想法更好的射弹发射编码源,请链接它!我需要帮助!

最佳答案

使用键绑定(bind)比使用键监听器要容易得多。

以下是贪吃蛇游戏(如 Atari 贪吃蛇游戏)上下文中的一些键绑定(bind)。关键方法(双关语)是 setKeyBindings 方法。您可以将空格键添加到这些键绑定(bind)中。

package com.ggl.snake.game.view;

import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.NumberFormat;

import javax.swing.BoxLayout;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ggl.snake.game.controller.ArrowAction;
import com.ggl.snake.game.model.SnakeGameModel;
import com.ggl.snake.game.runnable.GameRunnable;

public class SnakeGameFrame {

private static final NumberFormat NF = NumberFormat.getInstance();

private ControlPanel controlPanel;

private GameRunnable gameRunnable;

private GridPanel gridPanel;

private JFrame frame;

private SnakeGameModel model;

public SnakeGameFrame(SnakeGameModel model) {
this.model = model;
createPartControl();
}

private void createPartControl() {
frame = new JFrame();
// frame.setIconImage(getFrameImage());
frame.setTitle("Retro Snake Game");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent event) {
exitProcedure();
}
});

JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.LINE_AXIS));

gridPanel = new GridPanel(model);
mainPanel.add(gridPanel);

controlPanel = new ControlPanel(this, model);
mainPanel.add(controlPanel.getPanel());

frame.add(mainPanel);
frame.pack();

setKeyBindings(gridPanel);

frame.setLocationByPlatform(true);
frame.getRootPane().setDefaultButton(controlPanel.getStartButton());
frame.setVisible(true);

gameRunnable = new GameRunnable(this, model);
new Thread(gameRunnable).start();
}

private void setKeyBindings(JPanel gridPanel) {
InputMap inputMap = gridPanel
.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(KeyStroke.getKeyStroke("W"), "up arrow");
inputMap.put(KeyStroke.getKeyStroke("S"), "down arrow");
inputMap.put(KeyStroke.getKeyStroke("A"), "left arrow");
inputMap.put(KeyStroke.getKeyStroke("D"), "right arrow");

inputMap.put(KeyStroke.getKeyStroke("UP"), "up arrow");
inputMap.put(KeyStroke.getKeyStroke("DOWN"), "down arrow");
inputMap.put(KeyStroke.getKeyStroke("LEFT"), "left arrow");
inputMap.put(KeyStroke.getKeyStroke("RIGHT"), "right arrow");

inputMap = gridPanel.getInputMap(JPanel.WHEN_FOCUSED);
inputMap.put(KeyStroke.getKeyStroke("UP"), "up arrow");
inputMap.put(KeyStroke.getKeyStroke("DOWN"), "down arrow");
inputMap.put(KeyStroke.getKeyStroke("LEFT"), "left arrow");
inputMap.put(KeyStroke.getKeyStroke("RIGHT"), "right arrow");

gridPanel.getActionMap().put("up arrow",
new ArrowAction(model, new Point(0, -1)));
gridPanel.getActionMap().put("down arrow",
new ArrowAction(model, new Point(0, 1)));
gridPanel.getActionMap().put("left arrow",
new ArrowAction(model, new Point(-1, 0)));
gridPanel.getActionMap().put("right arrow",
new ArrowAction(model, new Point(1, 0)));
}

private void exitProcedure() {
gameRunnable.setRunning(false);
frame.dispose();
System.exit(0);
}

public void repaintGridPanel() {
gridPanel.repaint();
}

public void setScoreText() {
controlPanel.setScoreText(NF.format(model.getScore()));
}

public void setPauseButton() {
controlPanel.setPauseButton(model.isGameActive());
}
}

关于java - Java 游戏中的 key 管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29902970/

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