簡單的Java小遊戲 -- 數字連連消

遊戲規則很簡單,點擊選中兩個相同的數字即可消除這兩個數字,沒有做複雜的判斷。

效果圖

在這裏插入圖片描述
在這裏插入圖片描述

下面開始代碼

首先是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() {
		// 創建遊戲數據地圖
		map = MapTool.createMap();
		btnMap = new JButton[10][10];
		score = 0;
		timestamp = 0;
		isEnd = false;

		// 創建按鈕,設置按鈕屬性,監聽事件,並添加到按鈕數組和窗體中
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[i].length; j++) {
				JButton btn = new JButton(map[i][j] + "");
				btn.setBounds(sx + (j * w) + 2, sy + (i * w) + 2, w - 2, w - 2);
				btn.setForeground(Color.RED);
				btn.setFont(new Font("Arial", 0, 30));
				btn.setBackground(Color.WHITE);
				btn.setBorder(BorderFactory.createRaisedBevelBorder());
				btn.setFocusPainted(false);
				btn.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						// 如果遊戲結束,返回,不執行後面的代碼
						if (isEnd) {
							return;
						}
						for (int i = 0; i < btnMap.length; i++) {
							for (int j = 0; j < btnMap[i].length; j++) {
								if (e.getSource().equals(btnMap[i][j])) {
									// 被選中的方格個數增加一個
									cc++;
									compare(j, i);
								}
							}
						}

					}
				});
				btnMap[i][j] = btn;
				this.add(btn);
			}
		}
		if (restart != null) {
			restart.setVisible(false);
			this.remove(restart);
			restart = null;
		}
		repaint();

		// 定時器,用來刷新時間
		timer = new Timer(1000, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				timestamp++;
				repaint();
			}
		});
		timer.start();
	}

	/**
	 * 判斷是否遊戲結束
	 * 1、判斷二維數組map中的所有元素是否均爲0, 全部爲0返回true表示遊戲結束
	 * 2、有不爲0的,判斷二維數組map中是否還有重複值,沒有重複值返回true表示遊戲結束
	 * 否則返回false遊戲繼續
	 * 
	 * @param map 二維數組,元素爲int類型
	 * @return
	 */
	public boolean isEnd(int[][] map) {
		int count_0 = 0;
		int count = 0;
		HashSet<Integer> hashSet = new HashSet<Integer>();
		for (int[] ms : map) {
			for (int m : ms) {
				count++;
				if (m != 0) {
					hashSet.add(m);
				} else {
					count_0++;
				}
			}
		}

		for (int[] ms : map) {
			for (int m : ms) {
				if (m != 0) {
					if (hashSet.size() + count_0 == count) {
						return true;
					}
					return false;
				}
			}
		}
		
		return true;
	}

	/**
	 * 重載JPanel父類的paintComponent方法,用來繪製網格,以及Game Over等
	 */
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		try {
			// 獲取分鐘
			int min = timestamp / 60;
			// 獲取秒數
			int sec = timestamp % 60;
			// 判斷是否結束遊戲
			if (isEnd) {
				// 設置畫筆顏色爲紅色
				g.setColor(Color.RED);
				// 設置字體 微軟雅黑 加粗 62號
				g.setFont(new Font("微軟雅黑", 0, 62));
				// 繪製GAME OVER字樣
				g.drawString("GAME OVER", 60, 150);
				// 設置字體 微軟雅黑 加粗 40號
				g.setFont(new Font("微軟雅黑", 0, 40));
				// 繪製得分
				g.drawString("得分:" + score, 80, 230);
				// 繪製用時
				g.drawString("用時:" + String.format("%02d", min) + ":" + String.format("%02d", sec), 80, 280);
			} else {
				// 設置字體 微軟雅黑 加粗 20號
				g.setFont(new Font("微軟雅黑", Font.BOLD, 20));
				// 設置畫筆顏色爲黑色
				g.setColor(Color.BLACK);
				// 繪製時間顯示框
				g.fillRect(100, 8, 80, 30);
				// 繪製分數顯示框
				g.fillRect(400, 8, 50, 30);
				// 設置畫筆顏色爲紅色
				g.setColor(Color.RED);
				// 繪製時間提示標籤
				g.drawString("時間:", 50, 30);
				// 繪製時間
				g.drawString(String.format("%02d", min) + ":" + String.format("%02d", sec), 110, 30);
				// 繪製分數提示標籤
				g.drawString("分數:", 350, 30);
				// 繪製分數
				g.drawString(String.format("%03d", score) + "", 405, 30);
				
				// 繪製外層矩形框
				g.drawRect(sx, sy, rw, rw);
				// 繪製水平10個,垂直10個方格。 即水平方向9條線,垂直方向9條線, 外圍四周4條線已經畫過了,不需要再畫。 同時內部64個方格填寫數字。
				for (int i = 1; i < 10; i++) {
					// 繪製第i條豎直線
					g.drawLine(sx + (i * w), sy, sx + (i * w), sy + rw);

					// 繪製第i條水平線
					g.drawLine(sx, sy + (i * w), sx + rw, sy + (i * w));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 繪製按鈕顯示和隱藏
	 * 
	 * @param i
	 * @param j
	 */
	private void drawButton(int i, int j) {
		if (map[i][j] != 0) {
			btnMap[i][j].setVisible(true);
		} else {
			btnMap[i][j].setVisible(false);
		}
	}

	/**
	 * 比較兩次點擊的按鈕對應的數字
	 * 
	 * @param cj
	 * @param ci
	 */
	private void compare(int cj, int ci) {
		/**
		 * 如果cc是1,表示當前一共選中了一個方格,用px,py來記住這個方格的位置; 否則,表示現在選中的這個方格要與之前選中的方案比較,決定是否要刪除
		 */
		if (cc == 1) {
			pj = cj;
			pi = ci;
			printMap(ci, cj);
			// 將所點擊的方格背景設置爲灰色
			btnMap[ci][cj].setBackground(Color.LIGHT_GRAY);
			drawButton(ci, cj);
		} else {// 此時,cc肯定是大於1的,表示要比較兩個方格的值是否相同
			printMap(ci, cj);
			map = MapTool.removed(map, pi, pj, ci, cj);// 讓MapTool類的remove方法去判斷上一次所選的(px,py)處的方格值與本次選擇的(cx,cy)處的方格值是否可以消掉
			// 處理第一個方格
			btnMap[ci][cj].setBackground(Color.WHITE);
			drawButton(ci, cj);
			// 處理第二個方格
			btnMap[pi][pj].setBackground(Color.WHITE);
			drawButton(pi, pj);
			cc = 0;// 將cc的值復位

			if (map[pi][pj] == map[ci][cj]) {
				score += 10;
			}
			isEnd = isEnd(map);
			// 遊戲結束
			if (isEnd) {
				// 關閉定時器
				timer.stop();
				// 隱藏剩餘的按鈕
				for (int i = 0; i < map.length; i++) {
					for (int j = 0; j < map[i].length; j++) {
						if (map[i][j] != 0) {
							btnMap[i][j].setVisible(false);
						}
					}
				}
				// 創建添加重新開始按鈕
				restart = new JButton("重新開始");
				restart.setBackground(Color.WHITE);
				restart.setBounds(180, 350, 120, 40);
				restart.setBorder(BorderFactory.createRaisedBevelBorder());
				restart.setFocusPainted(false);
				restart.setForeground(Color.RED);
				restart.setFont(new Font("微軟雅黑", 0, 20));
				restart.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						start();
					}
				});
				this.add(restart);
				repaint();
			}
		}

		repaint();
	}

	/**
	 * 打印網格數據
	 * 
	 * @param ci
	 * @param cj
	 */
	private void printMap(int ci, int cj) {
		if (ci == pi && cj == pj) {
			System.out.println("ci:" + ci + ", cj:" + cj);
		} else {
			System.out.println("ci:" + ci + ", cj:" + cj + ", pi:" + pi + ", pj:" + pj);
		}
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[i].length; j++) {
				if (ci == pi && cj == pj) {
					System.out.print(((ci == i && cj == j) ? "[" + map[i][j] + "]" : " " + map[i][j] + " ") + "  ");
				} else {
					System.out.print(
							((ci == i && cj == j || pi == i && pj == j) ? "[" + map[i][j] + "]" : " " + map[i][j] + " ")
									+ "  ");
				}
			}
			System.out.println();
		}
	}
}

下面是GameFrame.java,定義遊戲窗體

package com.feonix;

import javax.swing.JFrame;

public class GameFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	GamePanel panel;
	
	/**
	 * GameFrame構造方法
	 */
	public GameFrame() {
		// 設置窗體標題
		setTitle("數字連連消");
		// 設置窗體位置和大小
		setBounds(100, 100, 515, 520);
		// 設置窗體不能改變大小
		setResizable(false);
		// 設置窗體居中顯示
		setLocationRelativeTo(null);
		// 設置窗體關閉即退出
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		panel = new GamePanel();
		add(panel);
		// 最後顯示窗體
		setVisible(true);
	}

	/**
	 * 啓動遊戲
	 */
	public void start() {
		panel.start();
	}
}

最後是Main.java,遊戲程序的入口

package com.feonix;

public class Main {
	public static void main(String[] args) {
		new GameFrame().start();
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章