我用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);
}
}
while(game.getKeyManager().keyPressed(e)){
给我一个问题,在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;
}
}
老实说,我不知道我做错了什么,因为我比较新。如果您碰巧有一个编码射弹发射的源,这比我的想法更好,请链接它!我需要帮助!
答案 0 :(得分:1)
使用键绑定比使用键侦听器容易得多。
以下是像Atari蛇游戏这样的蛇游戏中的一些关键绑定。关键方法(双关语)是setKeyBindings方法。您可以为这些键绑定添加空格键。
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());
}
}