我实际上是在为一个项目编写pacman,但是当我添加了这个部分时,我遇到了一个问题:
@Override
public void keyPressed(KeyEvent e){
dir=e.getKeyCode();
}
程序没有调用我需要的特定方法来获得正确的方向,然后将其放入更新中。
以下是我仍在处理的完整代码:
import java.awt.Graphics;
import org.game.engine.Game;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.game.engine.GameApplication;
public class Pacman extends Game {
BufferedImage pacman;
int frame;
int dir;
int x,y;
final int STEP=2;
public static void main (String[] args){
GameApplication.start(new Pacman());
}
public Pacman(){
title="Pacman";
width=height=500;
dir=KeyEvent.VK_RIGHT;
x=300;
y=200;
try {
pacman = ImageIO.read(new File("pacman.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void keyPressed(KeyEvent e){
dir=e.getKeyCode();
}
@Override
public void update() {
frame++;
if(frame>2){
frame=0;
}
**switch(dir){
case KeyEvent.VK_LEFT:
x-=STEP;
break;
case KeyEvent.VK_RIGHT:
x+=STEP;
break;**
}
if(x<0){
x=0;
}
else if(x> width -28-15){
x= width-28-15;
}
}
@Override
public void draw(Graphics g) {
g.drawImage(pacman.getSubimage(frame*30,0,28,28),x,y,null);
}
@Override
public void init() {
}
}
额外详情: 所以基本上为了运行项目,我有一个游戏引擎,可以帮助我处理游戏的形状。 为了做到这一点,我有4个班级:
1 /游戏应用程序:
package org.game.engine;
import javax.swing.JFrame;
public class GameApplication {
static public void start (Game jeu) {
JFrame fenetre=new JFrame(jeu.getTitle());
fenetre.setSize(jeu.getWidth(),jeu.getHeight());
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GameCanvas canvas=new GameCanvas(jeu);
fenetre.add(canvas);
fenetre.setVisible(true);
GameLoop loop= new GameLoop(jeu,canvas);
loop.start();
}
}
2 / GameCanvas
package org.game.engine;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JComponent;
public class GameCanvas extends JComponent{
private final Game game;
public GameCanvas(Game game) {
this.game=game;
**addKeyListener(this.game);**
requestFocus();
}
@Override
public void paintComponent(Graphics g){
game.draw(g);
}
}
3 /游戏循环
package org.game.engine;
public class GameLoop extends Thread {
private final Game game;
private final GameCanvas canvas;
public GameLoop(Game game, GameCanvas canvas) {
this.game=game;
this.canvas=canvas;
}
@Override
public void run() {
game.init();
while(!game.isOver()){
game.update();
canvas.repaint();
try {
Thread.sleep(game.getDelay());
} catch (InterruptedException e) {
}
}
}
}
最后游戏本身:
4 /游戏
package org.game.engine;
import java.awt.Graphics;
**import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;**
public abstract class Game **implements KeyListener**{
protected boolean over;
protected int delay=50;
protected int width=500;
protected int height=500;
protected String title="Mon jeu";
abstract public void update();
abstract public void draw(Graphics g);
abstract public void init();
public int getWidth(){ return width;}
public int getHeight(){return height;}
public String getTitle(){return title;}
public boolean isOver(){ return over;}
public long getDelay(){ return delay;}
**public void keyTyped(KeyEvent e){}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}**
}
1 /游戏应用程序用于进程的执行部分(布局显示等)。 2 / Game Canvas是为了绘制窗口的内容,除了它还通过以下方式调用Keylistener:addKeyListener(this.game); 3 / Game循环包含run方法中programm的主循环 最后,4 / Game包含程序的所有基本参数,例如标题大小和它也实现了Keylisteners。
答案 0 :(得分:0)
要正确处理按键,您需要执行以下操作:
可能你正在做这些事。如果是这样,你需要进一步缩小问题范围以获得合理的帮助。
答案 1 :(得分:0)
您将密钥值存储在a
中,然后检查交换机中的dir
。显然这不起作用。您似乎没有在应用程序中的任何其他位置使用a
,因此我建议您完全删除该变量以避免混淆。