117.info
人生若只如初见

Java怎么实现简单连连看游戏

以下是一个简单的Java实现连连看游戏的示例代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LianLianKan extends JFrame implements ActionListener {
private JButton[][] buttons;
private int[][] board;
private int size;
private int selectedRow;
private int selectedColumn;
public LianLianKan(int size) {
super("连连看");
this.size = size;
board = new int[size][size];
buttons = new JButton[size][size];
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(size, size));
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
JButton button = new JButton();
button.addActionListener(this);
buttons[i][j] = button;
add(button);
}
}
generateBoard();
refreshBoard();
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void generateBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
board[i][j] = (i * size + j) % (size * size / 2) + 1;
}
}
}
public void refreshBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
buttons[i][j].setText(String.valueOf(board[i][j]));
}
}
}
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
int row = -1;
int column = -1;
// Find the clicked button
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (buttons[i][j] == button) {
row = i;
column = j;
break;
}
}
}
if (selectedRow == -1 && selectedColumn == -1) {
// No previous selection
selectedRow = row;
selectedColumn = column;
button.setEnabled(false);
} else {
if (board[selectedRow][selectedColumn] == board[row][column]) {
// Match found, remove buttons
buttons[selectedRow][selectedColumn].setVisible(false);
buttons[row][column].setVisible(false);
board[selectedRow][selectedColumn] = 0;
board[row][column] = 0;
} else {
// No match, reset selection
buttons[selectedRow][selectedColumn].setEnabled(true);
}
selectedRow = -1;
selectedColumn = -1;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new LianLianKan(4); // Create a 4x4 game
}
});
}
}

在这个示例代码中,我们创建了一个JFrame窗口,其中包含了一个指定大小的网格,每个格子是一个JButton按钮。我们使用一个二维数组board来表示每个格子上的图案,初始时每个格子上的图案是随机生成的。

当玩家点击一个按钮时,我们通过ActionEvent来处理按钮点击事件。如果之前没有选中的格子,那么我们将当前格子设为选中,并禁用该按钮。如果之前已经选中了一个格子,我们将当前格子与之前选中的格子进行比较。如果两个格子上的图案相同,说明找到了一对匹配,我们将它们从界面上隐藏,并将board中对应位置的值设为0。如果两个格子上的图案不同,我们将之前选中的格子的按钮重新启用。

最后,在main方法中创建一个LianLianKan对象,传入指定的大小,即可运行连连看游戏。在这个示例中,我们创建了一个4x4的游戏,你可以根据需要调整大小。

未经允许不得转载 » 本文链接:https://www.117.info/ask/feab0AzsLBg9RA1E.html

推荐文章

  • 常见的Java异常有哪些

    常见的Java异常有以下几种: NullPointerException(空指针异常):当一个对象引用为null时,调用该对象的方法或访问其属性会抛出NullPointerException异常。 Ar...

  • java中service层有什么作用

    在Java中,Service层是应用程序的中间层,主要负责处理业务逻辑,与数据访问层(DAO层)和表示层(Controller层)进行交互。
    Service层的作用主要包括以下几...

  • Java中直接调用service业务类要注意哪些问题

    在Java中直接调用service业务类时,需要注意以下问题: 依赖注入:service业务类通常会依赖其他组件或服务,比如DAO层或者其他service层。在直接调用service时,...

  • java异常类型怎么判断

    在Java中,异常类型可以通过以下几种方式进行判断: 使用try-catch语句来捕获并处理异常。在catch块中,可以使用instanceof关键字来判断异常的类型。例如: try ...

  • jQuery中delegate()方法怎么使用

    在 jQuery 中,delegate()方法用于为一个元素或一组元素添加事件处理程序。它可以动态地将事件处理程序绑定到匹配选择器规定的子元素上,即使这些子元素在事件绑...

  • jquery模糊查询的方法有哪些

    在jQuery中,可以使用以下方法实现模糊查询: 使用.filter()方法:可以使用自定义的函数来过滤匹配的元素。例如,可以使用正则表达式来匹配包含特定字符串的元素...

  • 怎么隐藏自己vps的真实ip

    要隐藏VPS的真实IP地址,可以考虑以下几种方法: 使用VPN:通过使用虚拟专用网络(VPN),您可以将所有网络流量通过VPN服务器进行传输,从而隐藏VPS的真实IP地址...

  • 如何区分vps和实体机

    VPS(Virtual Private Server)和实体机之间的区别主要体现在以下几个方面: 资源分配:VPS是在一台物理服务器上通过虚拟化技术划分出的多个虚拟服务器,每个VPS...