Java代码实现 ,需要生成点击游戏的页面,游戏的页面有16个矩形格子。游戏开始,随机生成动物并显示对应的名称。红色的8张牌有8个动物,大象,狮子,老虎,豹子,狼,狗,猫,老鼠。蓝色的8张牌也有8个动物,大象,狮子,老虎,豹子,狼,狗,猫,老鼠。其中大象大于狮子,狮子大于老虎,老虎大于豹子,豹子大于狼,狼大于狗,狗大于猫,猫大于老鼠,老鼠大于大象。一回合一个玩家行动一次,然后有另一方进行行动。每个玩家可以通过点击格子来移动一张牌来吃掉对方的牌,移动牌需要检查该牌的动物类型是否可以吃掉对方区域中的牌,并将被吃掉的牌从界面中删除并移动到目标位置。最后任意一方的所有牌全部被吃结束游戏。
时间: 2024-03-20 15:39:31 浏览: 145
JAVA代码实现打飞机小游戏
以下是Java代码实现,实现了点击游戏的页面,可以随机生成动物并显示对应的名称,可以移动牌并吃掉对方的牌。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AnimalGame extends JFrame implements ActionListener {
private JPanel gamePanel, infoPanel;
private JLabel[][] board;
private JButton[][] buttons;
private JLabel playerLabel, animalLabel;
private JButton moveButton;
private int player;
private int[] animalOrder = {7, 6, 5, 4, 3, 2, 1, 0};
private String[] animalNames = {"大象", "狮子", "老虎", "豹子", "狼", "狗", "猫", "老鼠"};
private int[][] animalMap = {{0, 1, 1, 1, 1, 1, 1, 1},
{0, 0, 1, 1, 1, 1, 1, 1},
{0, 0, 0, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0}};
public AnimalGame() {
setTitle("动物棋游戏");
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gamePanel = new JPanel(new GridLayout(4, 4));
buttons = new JButton[4][4];
board = new JLabel[4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
buttons[i][j] = new JButton();
buttons[i][j].addActionListener(this);
board[i][j] = new JLabel();
board[i][j].setHorizontalAlignment(JLabel.CENTER);
board[i][j].setVerticalAlignment(JLabel.CENTER);
gamePanel.add(buttons[i][j]);
gamePanel.add(board[i][j]);
}
}
infoPanel = new JPanel(new GridLayout(2, 2));
playerLabel = new JLabel("玩家1");
infoPanel.add(playerLabel);
animalLabel = new JLabel("动物:");
infoPanel.add(animalLabel);
moveButton = new JButton("移动");
moveButton.addActionListener(this);
infoPanel.add(moveButton);
add(gamePanel, BorderLayout.CENTER);
add(infoPanel, BorderLayout.SOUTH);
player = 1;
updateAnimal();
updateBoard();
}
private void updateAnimal() {
int index = (int) (Math.random() * 8);
animalLabel.setText("动物:" + animalNames[animalOrder[index]]);
}
private void updateBoard() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (animalMap[i][j] == 0) {
buttons[i][j].setEnabled(false);
} else {
buttons[i][j].setEnabled(true);
if (player == 1) {
board[i][j].setText(animalNames[animalOrder[animalMap[i][j] - 1]]);
} else {
board[i][j].setText("");
}
}
}
}
}
private void moveAnimal(int x1, int y1, int x2, int y2) {
if (animalMap[x1][y1] == 0 || animalMap[x2][y2] != 0) {
return;
}
int animal1 = animalMap[x1][y1] - 1;
int animal2 = animalMap[x2][y2] - 1;
if (animalMap[x1][y1] == 8 && animalMap[x2][y2] == 1) {
animalMap[x1][y1] = 0;
animalMap[x2][y2] = 8;
} else if (animalMap[x1][y1] == 1 && animalMap[x2][y2] == 8) {
animalMap[x1][y1] = 0;
animalMap[x2][y2] = 1;
} else if (animalOrder[animal1] > animalOrder[animal2] && animalMap[x1][y1] != animalMap[x2][y2]) {
animalMap[x1][y1] = 0;
animalMap[x2][y2] = animalMap[x1][y1];
} else {
return;
}
player = 3 - player;
playerLabel.setText("玩家" + player);
updateBoard();
if (checkWin()) {
JOptionPane.showMessageDialog(this, "玩家" + player + "获胜!");
}
}
private boolean checkWin() {
boolean win = true;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (animalMap[i][j] != 0 && animalMap[i][j] != 8) {
win = false;
}
}
}
return win;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == moveButton) {
player = 3 - player;
playerLabel.setText("玩家" + player);
updateBoard();
return;
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (e.getSource() == buttons[i][j]) {
moveAnimal(i, j, i-1, j);
moveAnimal(i, j, i+1, j);
moveAnimal(i, j, i, j-1);
moveAnimal(i, j, i, j+1);
return;
}
}
}
}
public static void main(String[] args) {
AnimalGame game = new AnimalGame();
game.setVisible(true);
}
}
```
阅读全文