java代码完成一个游戏,生成一个点击的页面,页面有36个矩形的格子。在游戏开始时,随机生成18对不同的动物在格子中,生成的动物在格子中显示对应的名称。当点击名称相同的动物时,需要删除对应的动物名称,并进行标识。如果点击的是不同的动物时,将被点击的动物还原。当所有的动物全部消除时,结束游戏并提示重新开始新的游戏。
时间: 2023-11-22 16:54:29 浏览: 38
以下是一个简单的 Java 代码实现,生成一个游戏页面,包含36个矩形的格子,并随机生成18对不同的动物在格子中,生成的动物在格子中显示对应的名称。点击名称相同的动物时,对应的动物名称将会被删除并进行标识,若点击不同的动物,则被点击的动物还原。当所有的动物全部消除时,结束游戏并提示重新开始新的游戏。
```java
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class AnimalMatchingGame extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private final String[] animals = {"Cat", "Dog", "Panda", "Lion", "Monkey", "Horse", "Cow", "Tiger", "Elephant", "Giraffe", "Bear", "Fox", "Rabbit", "Penguin", "Sheep", "Kangaroo", "Deer", "Wolf"};
private final List<String> animalList = new ArrayList<String>();
private final List<JButton> buttonList = new ArrayList<JButton>();
private final JPanel panel = new JPanel(new GridLayout(6, 6));
private final JLabel label = new JLabel("Welcome to Animal Matching Game!");
private int firstClick = -1;
private int secondClick = -1;
private int totalMatches = 0;
private boolean[] matched = new boolean[36];
public AnimalMatchingGame() {
setTitle("Animal Matching Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 600);
setLocationRelativeTo(null);
// Generate animal pairs
for (String animal : animals) {
animalList.add(animal);
animalList.add(animal);
}
Collections.shuffle(animalList);
// Create buttons
for (int i = 0; i < 36; i++) {
JButton button = new JButton();
button.setPreferredSize(new Dimension(80, 80));
button.addActionListener(this);
buttonList.add(button);
panel.add(button);
}
// Add components to frame
Container contentPane = getContentPane();
contentPane.add(label, "North");
contentPane.add(panel, "Center");
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
JButton clickedButton = (JButton) e.getSource();
int index = buttonList.indexOf(clickedButton);
if (matched[index]) {
return;
}
if (firstClick == -1) {
firstClick = index;
clickedButton.setText(animalList.get(index));
} else if (secondClick == -1 && index != firstClick) {
secondClick = index;
clickedButton.setText(animalList.get(index));
if (animalList.get(firstClick).equals(animalList.get(secondClick))) {
matched[firstClick] = true;
matched[secondClick] = true;
totalMatches++;
if (totalMatches == 18) {
JOptionPane.showMessageDialog(this, "Congratulations! You won the game!");
int option = JOptionPane.showConfirmDialog(this, "Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION);
if (option == JOptionPane.YES_OPTION) {
resetGame();
} else {
System.exit(0);
}
}
} else {
label.setText("Sorry, try again!");
}
firstClick = -1;
secondClick = -1;
}
}
private void resetGame() {
for (int i = 0; i < 36; i++) {
matched[i] = false;
buttonList.get(i).setText("");
}
totalMatches = 0;
label.setText("Welcome to Animal Matching Game!");
// Generate animal pairs
Collections.shuffle(animalList);
}
public static void main(String[] args) {
new AnimalMatchingGame();
}
}
```
上述代码使用了 Java 的 Swing 库来创建游戏界面,实现了一个简单的动物配对游戏。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)