Java键盘监听器网格游戏



我正在制作一款玩家在网格中移动角色的游戏,如果用户试图将角色移动到特定类型的空间,那么它就可以被移动到。网格是由一个类组成的,我知道这个类叫做PGrid,所以网格是由PGrid组成的。问题是keyListener根本不起作用,甚至没有打印出"hi"。下面是PGame和moveGrid的代码,其中PGame处理moveGrid的东西,但moveGrid绘制网格(因为它是JPanel)。我尝试将keylistener从PGame移动到moveGrid,但它不起作用。

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
//where all the different classes are put together
public class PGame implements KeyListener{ //may want to move the listener to moveGrid
private static moveGrid panel = new moveGrid(8,8); //taking something from moveGrid
private static PActor pguy = new PActor("Bill", 2, 2);
private boolean shallmove = false;
private int newx, newy;
public static void main(String[] args){
    //create a new frame 
    JFrame frame = new JFrame("PGame"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    //moveGrid panel = new moveGrid(8,8); //taking something from moveGrid
    frame.getContentPane().add(panel); 
    frame.pack(); 
    frame.setVisible(true);
    panel.requestFocus();
    //creating a "path" of places able to move to
    moveGrid.pgrids.get(0).changeType(1);
    moveGrid.pgrids.get(1).changeType(1);
    moveGrid.pgrids.get(2).changeType(1);
    moveGrid.pgrids.get(3).changeType(1);
    moveGrid.pgrids.get(10).changeType(1);
    moveGrid.pgrids.get(11).changeType(1);
    moveGrid.pgrids.get(19).changeType(1);
    moveGrid.pgrids.get(27).changeType(1);
    //moveGrid.pgrids.get(4).changeType(2);
    //start our pguy out in a position
    PGrid pguystart = new PGrid(2,0,0);
    moveGrid.pgrids.set(0,pguystart);
    panel.repaint();
}

public void keyPressed(KeyEvent e) {
    //here test if the grid can be updated
    //Test:
    pguy.canMove(3,3);
    pguy.Move(4,3,3);
    if(e.getKeyCode() == KeyEvent.VK_UP){
        newx = pguy.getx();
        newy = pguy.gety() - 1;
        shallmove = pguy.canMove(newx,newy);
        System.out.println("Hi");
    }else if(e.getKeyCode() == KeyEvent.VK_DOWN){
        newx = pguy.getx();
        newy = pguy.gety() + 1;
        shallmove = pguy.canMove(newx,newy);
    } else if(e.getKeyCode() == KeyEvent.VK_LEFT){
        newx = pguy.getx() - 1;
        newy = pguy.gety();
        shallmove = pguy.canMove(newx,newy);
    }else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
        newx = pguy.getx() + 1;
        newy = pguy.gety();
        shallmove = pguy.canMove(newx,newy);
    }

}
public void keyReleased(KeyEvent e) { 

    System.out.println("Hi");
    //update the grid if it can here
    //somewhere in here add this:
    //moveGrid.repaint(); //tell movegrid to repaint
    if(shallmove = true){
        //change a certain spot to the actor
        PGrid temp = new PGrid(2,newx,newy);
        moveGrid.pgrids.set(pguy.getplace(),temp);
        //need to also change to old space to be back to what it was....
        //*here*
        pguy.Move(pguy.newPos,newx, newy);
        panel.repaint();
    }

}
public void keyTyped(KeyEvent e) { }

}

moveGrid:

//a grid in which stuff can move
import java.awt.*; 
import java.awt.event.*; 
import java.util.ArrayList;
import javax.swing.*;
public class moveGrid extends JPanel {
private int height;
private int width;
private int newx, newy;
private static PActor pguy = new PActor("Bill", 2, 2);
private boolean shallmove = false;
public static ArrayList<PGrid> pgrids = new ArrayList<PGrid>(); //an array full of grid boxes with type PGrid
public moveGrid(int height, int width){
    this.height = height;
    this.width = width;
    setPreferredSize(new Dimension(800, 800));
    //make all the values in pgrids equal to "Water" and give them locations
    int i = 0;
    for(int y = 0; y < height; y++){
        for(int x = 0; x < width; x++){
            PGrid pnull = new PGrid(0, x, y);
            pgrids.add(i, pnull);
            i++;
        }
    }

    //drawGrid();
}
/*public void drawGrid(Graphics g){
    g.drawRect(x,y,20,20);
} */
 public void addNotify() {
        super.addNotify();
        requestFocus();
    }
public void paintComponent(Graphics g){
    //PGrid curLoc = new PGrid(height, height, height);
    //go through and draw out the grid
    int q = 0;
    int midx = 0; //need to make these somehow so the squares get drawn at the center
    int midy = 0;
    for(int qh = 0; qh < height; qh++){
        for(int qw = 0; qw < width; qw++){
            PGrid pcur = pgrids.get(q); //
            int p = pcur.getType();
            if(p == 0){
                //may want to import a water looking image
                g.setColor(Color.BLUE);
                g.fillRect((40*qw)+midx,(40*qh)+midy,40,40);
                g.setColor(Color.BLACK);
                g.drawRect((40*qw)+midx,(40*qh)+midy,40,40);
            }else if(p == 1){
                //may want to import a better image
                g.setColor(Color.GREEN);
                g.fillRect((40*qw)+midx,(40*qh)+midy,40,40);
            }else if(p == 2){
                //draws the "character"
                g.setColor(Color.ORANGE);
                g.fillRect((40*qw)+midx,(40*qh)+midy,40,40);
            }
            q++;
        }
    }
    //here draw the character in the proper position
    //so like multiply the x and y by 40 
}
}

我也可能在PActor类中有一个错误,它被认为是移动的Actor。

public class PActor {
private String name;
private int curx, cury;
int newPos;
public PActor(String name, int curx, int cury){
    this.name = name;
    this.curx = curx;
    this.cury = cury;
}
public boolean canMove(int x, int y){
    boolean abletomove = false;
    //test if the space that the user is trying to moveto can be moved to
    //use indexOf(a temp variable with values x and y also with type 1) to test
    PGrid togo = new PGrid(1,x,y);
    //now scan through pgrids in moveGrid to see the desired spot can be moved to
    for(int s = 0; s <= moveGrid.pgrids.size(); s++){
        PGrid temp = moveGrid.pgrids.get(s);
        //test if the temp space is equal
        if((togo.getType() == temp.getType()) && (togo.getx() == temp.getx()) && (togo.gety() == temp.gety())){
            abletomove = true;
            newPos = s;
            break; //stop scanning, as it is now unnecessary
        }
        else{ //do nothing
        }
    }
    //now test pgrids to see if there is a spot like such that is moveable

    return abletomove;
}
public int getplace(){
    return newPos;
}
public int getx(){
    return curx;
}
public int gety(){
    return cury;
}
public void Move(int pos, int x, int y){ 
    PGrid temp = new PGrid(2,x,y);
    moveGrid.pgrids.set(pos,temp);
}
public String toString(){
    return name + " ";
}
}

建议:

    同样,要使任何Java Swing侦听器工作,必须将侦听器添加到组件中。例如,要使KeyListener工作,首先需要通过调用该组件上的addKeyListener(myKeyListener)将其添加到希望侦听的组件中。
  • 要使KeyListener工作,被侦听的组件必须具有键盘焦点。这意味着,如果你正在监听JPanel,你首先必须通过调用JPanel上的setFocusable(true)使其可聚焦,然后你需要请求聚焦,例如通过调用requestFocusInWindow()
  • 如果稍后有东西窃取了焦点,例如JButton被按下或文本组件,那么KeyListener将不再工作。
  • 为了解决这个问题,我们通常建议您使用键绑定来代替键侦听器,但请注意,键绑定的设置与键侦听器的设置完全不同,您需要把所有的假设都放在一边,先学习使用键绑定的教程。
  • 如果仍然卡住,那么你将需要创建并发布一个最小的示例程序,一个比你发布的程序有很多代码的小程序,但它可以为我们编译,运行,并直接向我们显示你的问题。

链接:

    <
  • KeyListener教程/gh>
  • 键绑定教程

最新更新