使用网格的简单Java游戏

时间:2014-03-22 20:30:18

标签: java arrays swing jbutton

使用photoshop编辑此图片以显示我想要做的事情:

enter image description here

基本上,我想做的是通过点击它来移动黑棒人并移动到可用位置(灰色框)。

这是我到目前为止所做的: Arena.java

public class Arena extends JFrame{
JPanel a = new JPanel();
Player players[][] = new Player[10][10];
private ImageIcon player = new ImageIcon("player.jpg");
private ImageIcon player2 = new ImageIcon("player2.jpg");
private ImageIcon poop = new ImageIcon("poop.jpg");
private ImageIcon moves = new ImageIcon("moves.jpg");

public static void main(String args[]){
    new Arena();
}

public Arena(){
    setTitle("The Arena");
    setSize(500,500);
    setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    a.setLayout(new GridLayout(10,10));

    for(int i=0;i<10;i++){
        for(int j=0;j<10;j++){
            players[i][j] = new Player();
            a.add(players[i][j]);
        }
    }
    //player position
    players[9][4].setIcon(player);
    //player 2 position
    players[9][7].setIcon(player2);
    //poop
    players[7][2].setIcon(poop);
    players[5][7].setIcon(poop);

    add(a);
    setVisible(true);
}
}

Player.java

public class Player extends JButton{
private int xPos;
private int yPos;

public Player(){

}

public int getXPos(){
    return xPos;
}
public int getYPos(){
    return yPos;
}
public void setXPos(){
    this.xPos = x;
}
public void setYPos(){
    this.yPos = y;
}
}

谢谢!

0 个答案:

没有答案