简单的Java小游戏 -- 数字连连消

本文介绍了一个简单的Java小游戏——数字连连消。游戏规则是点击选中并消除两个相同的数字,文章通过代码展示了数字生成、游戏逻辑、界面布局及游戏入口的实现。
摘要由CSDN通过智能技术生成

游戏规则很简单,点击选中两个相同的数字即可消除这两个数字,没有做复杂的判断。

效果图

在这里插入图片描述
在这里插入图片描述

下面开始代码

首先是MapTool.java,用于产生数字和判断选中的两个数字是否相同

package com.feonix;

import java.util.Random;

public class MapTool {
   

	public static int[][] createMap() {
   
		int[][] map = new int[10][10];
		Random rand = new Random();
		for (int i = 0; i < map.length; i++) {
   
			for (int j = 0; j < map[i].length; j++) {
   
				map[i][j] = rand.nextInt(9) + 1;
			}
		}
		return map;
	}

	public static int[][] removed(int[][] map, int pi, int pj, int ci, int cj) {
   
		if (map[pi][pj] == map[ci][cj] && (pj != cj || pi != ci)) {
   
			System.out.println("消除:map[" + ci + "][" + cj + "],map[" + pi + "][" + pj + "]");
			map[pi][pj] = 0;
			map[ci][cj] = 0;
		}
		return map;
	}

}

然后是GamePanel.java,游戏布局,游戏核心逻辑代码

package com.feonix;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Timer;

public class GamePanel extends JPanel {
   
	private static final long serialVersionUID = 2L;
	private static final int sx = 50;// 左边距
	private static final int sy = 50;// 上边距
	private static final int w = 40; // 小方格宽高
	private static final int rw = 400; // 网格总宽高

	private int pj = 0, pi = 0; // 记录两个点击选中的按钮,第一个被点击的按钮坐标
	private int cc = 0;// 被点击选中的按钮个数
	private int[][] map;// 存放游戏数据的二维数组
	private boolean isEnd = false; // 游戏结束标志
	private JButton[][] btnMap; // 存放按钮的二维数组,与map对应
	private int score; // 记录分数
	private JButton restart; // 重新开始按钮
	private Timer timer; // 定时器
	private int timestamp; // 时间戳

	public GamePanel() {
   
		// 设置布局为不使用预设的布局
		setLayout(null);
	}

	/**
	 * 开始游戏
	 */
	public void start() {
   
		
以下是一个简单Java 数字连连小游戏代码示例: ```java import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class NumberMatchingGame extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private static final int WIDTH = 400; private static final int HEIGHT = 400; private static final int ROWS = 4; private static final int COLUMNS = 4; private static final int DELAY = 1000; private static final int FONT_SIZE = 30; private static final int MAX_NUMBER = ROWS * COLUMNS / 2; private static final Random rand = new Random(); private Timer timer; private int[][] board; private JButton[][] buttons; private int firstRow, firstColumn, secondRow, secondColumn; private int count; private int score; private JLabel countLabel, scoreLabel; public NumberMatchingGame() { super("Number Matching Game"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(WIDTH, HEIGHT); setResizable(false); setLocationRelativeTo(null); setLayout(new BorderLayout()); JPanel boardPanel = new JPanel(new GridLayout(ROWS, COLUMNS)); buttons = new JButton[ROWS][COLUMNS]; board = new int[ROWS][COLUMNS]; for (int r = 0; r < ROWS; r++) { for (int c = 0; c < COLUMNS; c++) { buttons[r][c] = new JButton(); buttons[r][c].setFont(new Font("Arial", Font.BOLD, FONT_SIZE)); buttons[r][c].addActionListener(this); boardPanel.add(buttons[r][c]); } } add(boardPanel, BorderLayout.CENTER); JPanel infoPanel = new JPanel(new GridLayout(1, 2)); countLabel = new JLabel("Count: 0", JLabel.CENTER); countLabel.setFont(new Font("Arial", Font.PLAIN, FONT_SIZE)); infoPanel.add(countLabel); scoreLabel = new JLabel("Score: 0", JLabel.CENTER); scoreLabel.setFont(new Font("Arial", Font.PLAIN, FONT_SIZE)); infoPanel.add(scoreLabel); add(infoPanel, BorderLayout.SOUTH); timer = new Timer(DELAY, this); setVisible(true); initGame(); } private void initGame() { count = 0; score = 0; countLabel.setText("Count: " + count); scoreLabel.setText("Score: " + score); int[] numbers = new int[MAX_NUMBER]; for (int i = 0; i < MAX_NUMBER; i++) { numbers[i] = i + 1; } shuffle(numbers); int index = 0; for (int r = 0; r < ROWS; r++) { for (int c = 0; c < COLUMNS; c++) { board[r][c] = numbers[index]; buttons[r][c].setText(Integer.toString(board[r][c])); buttons[r][c].setEnabled(true); index++; } } timer.start(); } private void shuffle(int[] array) { for (int i = array.length - 1; i > 0; i--) { int j = rand.nextInt(i + 1); int temp = array[i]; array[i] = array[j]; array[j] = temp; } } public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == timer) { timer.stop(); hideNumbers(); } else { for (int r = 0; r < ROWS; r++) { for (int c = 0; c < COLUMNS; c++) { if (source == buttons[r][c]) { if (count == 0) { firstRow = r; firstColumn = c; buttons[r][c].setEnabled(false); count++; } else if (count == 1) { secondRow = r; secondColumn = c; buttons[r][c].setEnabled(false); count++; if (board[firstRow][firstColumn] == board[secondRow][secondColumn]) { score++; scoreLabel.setText("Score: " + score); if (score == MAX_NUMBER) { JOptionPane.showMessageDialog(this, "You win!"); initGame(); } } else { timer.start(); } } else { buttons[firstRow][firstColumn].setEnabled(true); buttons[secondRow][secondColumn].setEnabled(true); firstRow = r; firstColumn = c; buttons[r][c].setEnabled(false); count = 1; } } } } } } private void hideNumbers() { buttons[firstRow][firstColumn].setEnabled(true); buttons[secondRow][secondColumn].setEnabled(true); buttons[firstRow][firstColumn].setText(""); buttons[secondRow][secondColumn].setText(""); count = 0; } public static void main(String[] args) { new NumberMatchingGame(); } } ``` 这个小游戏的规则很简单:用户需要翻开数字牌,每次翻开两张数字牌,如果两张数字相同,则得分,继续翻下一对数字牌,直到翻完全部数字牌。如果翻开的两张数字不相同,则需要在一定时间内记住数字的位置,然后数字牌会再次翻转回去。用户需要根据记忆找到两张相同的数字牌,才能继续游戏。如果用户成功翻开所有数字牌,则游戏胜利。
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值