我正试图让我的“鱼”随着WASD键移动。当我运行这个程序时,鱼会出现,但是当我按下按键时它不会移动。我看了看,但我找不到问题。请帮忙。
public class RunGame extends JFrame{
public RunGame(){
initRunGame();
}
private void initRunGame(){
add(new Board());
setResizable(false);
pack();
setTitle("Game");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
RunGame rg=new RunGame();
rg.setVisible(true);
}
});
}
}
public class Board extends JPanel implements ActionListener{
private Timer timer;
private boolean inGame;
private final int B_WIDTH=500, B_HEIGHT=400;
private final int DELAY=15;
private MoveableSprite fish;
private final int FISH_W=10, FISH_H=5;
public Board(){initBoard();}
private void initBoard(){
addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.WHITE);
inGame=true;
setPreferredSize(new Dimension(B_WIDTH,B_HEIGHT));
fish=new MoveableSprite(B_WIDTH/2,B_HEIGHT/2,FISH_W,FISH_H,this);
timer=new Timer(DELAY, this);
timer.start();
}
private class TAdapter extends KeyAdapter{
@Override
public void keyReleased(KeyEvent e){
fish.keyReleased(e);
}
@Override
public void keyPressed(KeyEvent e){
fish.keyReleased(e);
}
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
if(inGame){
drawObjects(g);
}else{
drawGameOver(g);
}
}
private void drawObjects(Graphics g){
Graphics2D gd=(Graphics2D)g;
gd.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
if(fish.isVisible()){
gd.setColor(fish.color);
gd.fill(fish.getShape());
}
}
private void drawGameOver(Graphics g){
Graphics2D gd=(Graphics2D)g;
gd.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
String message="GAME OVER";
Font small=new Font("Helvetica", Font.BOLD, 14);
FontMetrics fm=getFontMetrics(small);
gd.setColor(Color.BLACK);
gd.setFont(small);
gd.drawString(message, B_WIDTH-fm.stringWidth(message), B_HEIGHT/2);
}
@Override
public void actionPerformed(ActionEvent e){
inGame();
updateFish();
repaint();
}
private void inGame(){
if(!inGame){timer.stop();}
}
private void updateFish(){
if(fish.isVisible()){
fish.move();
}
}
}
public class MoveableSprite extends Sprite{
private int dx, dy;
private Board b;
public MoveableSprite(int x, int y, int w, int h, Board b){
super(x,y,w,h);
this.b=b;
setColor(Color.BLUE);
}
public void move(){
x+=dx; y+=dy;
if(x<0){x=0;}
if(y<0){y=0;}
if(x>b.getWidth()){x=b.getWidth();}
if(y>b.getHeight()){y=b.getHeight();}
updateShape();
}
private void updateShape(){
shape=new Ellipse2D.Double(getX(),getY(),w,h);
}
public void keyPressed(KeyEvent e){
int key=e.getKeyCode();
if(key==KeyEvent.VK_A){dx=-1;}
if(key==KeyEvent.VK_D){dx=1;}
if(key==KeyEvent.VK_W){dy=-1;}
if(key==KeyEvent.VK_S){dy=1;}
}
public void keyReleased(KeyEvent e){
int key=e.getKeyCode();
if(key==KeyEvent.VK_A||key==KeyEvent.VK_D){dx=0;}
if(key==KeyEvent.VK_W||key==KeyEvent.VK_S){dy=0;}
}
}
public class Sprite {
protected int x, y, w, h;
private boolean isVis;
protected Shape shape;
protected Color color;
public Sprite(int x, int y, int w, int h){
this.x=x; this.y=y; this.w=w; this.h=h;
isVis=true; color=Color.BLACK;
initSprite();
}
public void setColor(Color c){
color=c;
}
private void initSprite(){
shape=new Ellipse2D.Double(x,y,w,h);
updateDimensions();
}
public int getX(){return x;}
public int getY(){return y;}
public boolean isVisible(){return isVis;}
public Rectangle getBounds(){
return new Rectangle(shape.getBounds());
}
public Shape getShape(){
return shape;
}
protected void updateDimensions(){
Rectangle r=getBounds();
w=(int) r.getWidth();
h=(int) r.getHeight();
}
}
到目前为止,这是所有代码。谢谢你的帮助!
答案 0 :(得分:0)
我怀疑它是因为这种复制粘贴错误:
private class TAdapter extends KeyAdapter{
...
@Override
public void keyPressed(KeyEvent e){
fish.keyReleased(e); <-- Whoops - should be keyPressed
}
}
由于keyReleased()
方法将dx或dy设置为零,因此ActionListener永远不会得到非零dx或dy。
当您通过阅读代码尝试查找问题时,可以隐藏这些类型的复制/粘贴拼写错误。使用调试器逐步执行代码可以帮助您找到它们。