DIV CSS 佈局教程網

不依賴 HTML5,用 JavaScript 實作連連看遊戲
編輯:關於JavaScript     

連連看是一款經典的消除遊戲,規則簡單卻考驗路徑判斷演算法。本文分享一款不依賴 HTML5、純用傳統 DOM 與 JavaScript 實作的連連看,重點在於如何用程式判斷兩個相同圖案之間能否以「不超過兩個轉彎」的路徑連通。

HTML 佈局(index.html)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>連連看</title>
<link rel="stylesheet" type="text/css" href="css/index.css"/>
</head>
<body>
<center>
<div id="whole">
<div id="gamePanel" tabindex="0">
<div id="pieces">
</div>
</div>
<div id="gameLogo">
</div>
<div id="scorePanel">
<span>分 數</span>
</div>
<div id="score">
<span>0</span>
</div>
<div id="timePanel">
<span>時 間</span>
</div>
<div id="time">
<span>0</span>
</div>
<div id="button">
<input id="start" type="button" onclick="Start();" value="開始"></input>
<input id="reset" type="button" onclick="Reset();"value="重置"></input>
</div>
</div>
</center>
<script type="text/javascript" src="js/map.js"></script>
<script type="text/javascript" src="js/piece.js"></script>
<script type="text/javascript" src="js/game.js"></script>
</body>
</html>

樣式(index.css)

body {
font-size : 16px;
font-weight : bold;
color : grey;
}

#whole {
border : 1px double #999999;
border-width : 5px;
width : 800px;
height : 505px;
position : relative;
}

#gamePanel {
margin: 1px 1px 1px 1px;
width : 602px;
height : 502px;
background : url(../img/background.gif) repeat;
position : absolute;
}

#pieces {
margin-top : 35px;
border : 1px solid #999999;
width : 546px;
height : 434px;
position: relative;
}

#pieces .piece {
width : 32px;
height : 36px;
position : relative;
cursor : pointer;
float : left;
}

#pieces .track {
width : 32px;
height : 36px;
position : relative;
float : left;
}

#pieces .track2 {
width : 32px;
height : 36px;
position : relative;
float : left;
background : red;
}

#gameLogo {
margin-top : 60px;
border : 1px solid #999999;
left : 607px;
width : 187px;
height : 73px;
background : url(../img/logo.gif);
position: absolute;
}

#scorePanel {
border : 1px solid #999999;
left : 607px;
top : 200px;
width : 187px;
height : 30px;
position : absolute;
}

#score {
border : 1px solid #999999;
left : 607px;
top : 240px;
width : 187px;
height : 30px;
position : absolute;
}

#timePanel {
border : 1px solid #999999;
left : 607px;
top : 300px;
width : 187px;
height : 30px;
position : absolute;
}

#time {
border : 1px solid #999999;
left : 607px;
top : 340px;
width : 187px;
height : 30px;
position : absolute;
}

#button {
border : 1px solid #999999;
left : 607px;
top : 400px;
width : 187px;
height : 30px;
position : absolute;
}

核心 JS:遊戲控制類 game.js

遊戲分為三個源檔,各對應一個類別:game.js(遊戲控制)、map.js(自訂映射結構)、piece.js(圖片類)。遊戲透過 Game 類來操作 map 與 piece 物件。

// 游戲控制類
var Game = {
gamePanel : null,
score : 0,
time : 0,
pieceMap : null,
pieceList : [],
pieceImgList : [],
randomList : [],
trackList : [],
isGameBigin : false,
isGameOver : false,
isGameReset : false,
isFirstClick : true,
// 開始游戲
start : function() {
document.getElementById("start").disabled = true;
document.getElementById("reset").disabled = false;
if (this.isGameReset) {
this.isGameOver = false;
this.startTime();
return;
} else if (this.isGameBegin) {
return;
} else {
this.init();
return;
}
},
reset : function() {
document.getElementById("start").disabled = false;
document.getElementById("reset").disabled = true;
this.clear();
this.initPieces();
this.initImgPieces();
this.time = 0;
document.getElementById("time").innerHTML = 0;
this.score = 0;
document.getElementById("score").innerHTML = 0;
this.isGameReset = true;
this.isGameBegin = true;
},
// 初始化
init : function() {
if (this.isGameBegin) {
return;
}
this.pieceMap = new Map();
var _this = this;
this.time = 0;
this.startTime();
this.gamePanel = document.getElementById("pieces");
this.initPieces();
this.initImgPieces();
this.isGameBegin = true;
},
// 將隨機生成的圖片添加進畫布
initPieces : function() {
this.initRandomList();
this.messRandomList();
for (var i = 0; i < 204; i ++) {
var piece = new Piece(this);
this.pieceList.push(piece);
var x = (i%17);
var y = Math.floor(i/17);
this.pieceMap.put(x+","+y, piece);
piece.setPosition(x, y);
this.gamePanel.appendChild(piece.dom);
if (x == 0 || x == 16 || y == 0 || y == 11) {
piece.track = document.createElement("div");
piece.track.className = "track";
piece.dom.appendChild(piece.track);
piece.isTracked = true;
continue;
} else {
if (x == 1 || x == 15 || y == 1 || y == 10) {
piece.setAtEdge(true);
}
this.pieceImgList.push(piece);
}
}
},
initImgPieces : function() {
for (var i = 0; i < this.pieceImgList.length; i ++) {
this.pieceImgList[i].initImg();
this.pieceImgList[i].img.src = "img/pieces/"+this.randomList[i]+".gif"
this.pieceImgList[i].setImgSrc(this.pieceImgList[i].img.src);
this.pieceImgList[i].onClick();
}
},
initRandomList : function() {
// 獲取隨機數列,成雙出現
for (var i = 0; i < 75; i ++) {
var random = parseInt(Math.random()*22*10000, 10);
var number = random%23;
this.randomList.push(number);
this.randomList.push(number);
}
},
messRandomList : function() {
for (var i = 0; i < this.randomList.length; i ++) {
var random = parseInt(Math.random()*15*10000, 10);
var number = random%150;
var temp;
temp = this.randomList[i];
this.randomList[i] = this.randomList[number];
this.randomList[number] = temp;
}
},
startTime : function() {
var _this = this;
if (this.isGameOver) {
return;
} else {
this.time ++;
document.getElementById("time").innerHTML = this.time;
this.isGameBegin = true;
setTimeout(function() {_this.startTime();}, 1000);
}
},
clear : function() {
for (var i = 0; i < this.pieceList.length; i ++) {
this.gamePanel.removeChild(this.pieceList[i].dom);
}
this.pieceList = [];
this.randomList = [];
this.pieceImgList = [];
this.isGameOver = true;
this.isGameBegin = false;
}
}

window.onload = function() {
document.getElementById("start").disabled = false;
document.getElementById("reset").disabled = true;
}

function Start() { Game.start(); }
function Reset() { Game.reset(); }

自訂映射結構 map.js

var Map = function(){
this.data = [];
}
Map.prototype = {
put : function(key, value) { this.data[key] = value; },
get : function(key) { return this.data[key]; },
remove : function(key) { this.data[key] = null; },
isEmpty : function() { return this.data.length == 0; },
size : function() { return this.data.length; }
}

圖片類 piece.js(節錄路徑判斷核心)

連連看的靈魂在於路徑判斷。最簡單的是判斷兩張圖能否直線連通:

// 直線
isStraightReach : function(piece) {
if (this.isNear(piece)) {
return true;
}
var a = false;
var b = false;
// 沿 y 軸方向搜索
if (this.x == piece.x) {
for (var i = this.min(this.y, piece.y) + 1; i < this.max(this.y, piece.y); i ++) {
if (this.game.pieceMap.get(piece.x + "," + i).isPass()) {
a = true;
this.game.trackList.push(this.game.pieceMap.get(piece.x + "," + i));
continue;
} else {
a = false;
this.game.trackList = [];
return a;
}
}
}
// 沿 x 軸方向搜索
if (this.y == piece.y) {
for (var i = this.min(this.x, piece.x) + 1; i < this.max(this.x, piece.x); i ++) {
if (this.game.pieceMap.get(i + "," + piece.y).isPass()) {
b = true;
this.game.trackList.push(this.game.pieceMap.get(i + "," + piece.y));
continue;
} else {
b = false
this.game.trackList = [];
return b;
}
}
}
return a || b;
},

再來是拐一次彎的判斷,它會分別嘗試兩個可能的轉角點:

// 拐一次彎搜索
isReach1 : function(piece) {
var corner_1 = this.game.pieceMap.get(this.x + "," + piece.y);
var corner_2 = this.game.pieceMap.get(piece.x + "," + this.y);
var _this = this;
if ((_this.isStraightReach(corner_1))
&& (corner_1.isStraightReach(piece))
&& corner_1.isPass()) {
this.game.trackList.push(corner_1);
return true;
}
if ((_this.isStraightReach(corner_2))
&& (corner_2.isStraightReach(piece))
&& corner_2.isPass()) {
this.game.trackList.push(corner_2);
return true;
}
return false;
},

最複雜的拐兩次彎,則以被點擊的圖片為中心,分別沿 x 軸與 y 軸四個方向展開搜索,只要中途遇到某個可通行點能「拐一次彎」連到目標,就算連通:

// 拐兩次彎搜索
isReach2 : function(piece) {
// 沿 x 軸正向搜索
for (var i = this.x + 1; i < 17; i ++) {
if (!this.game.pieceMap.get(i + "," + this.y).isPass()) {
this.game.trackList = [];
break;
} else if (this.game.pieceMap.get(i + "," + this.y).isReach(piece)
&& this.game.pieceMap.get(i + "," + this.y).isPass()) {
this.game.trackList.push(this.game.pieceMap.get(i + "," + this.y));
return true;
}
}
// 沿 x 軸搜索
for (var i = this.x - 1; i >= 0; i --) {
if (!this.game.pieceMap.get(i + "," + this.y).isPass()) {
this.game.trackList = [];
break;
} else if (this.game.pieceMap.get(i + "," + this.y).isReach(piece)
&& this.game.pieceMap.get(i + "," + this.y).isPass()) {
this.game.trackList.push(this.game.pieceMap.get(i + "," + this.y));
return true;
}
}
// 沿 y 軸搜索
for (var i = this.y - 1; i >= 0; i --) {
if (!this.game.pieceMap.get(this.x + "," + i).isPass()) {
this.game.trackList = [];
break;
} else if (this.game.pieceMap.get(this.x + "," + i).isReach(piece)
&& this.game.pieceMap.get(this.x + "," + i).isPass()) {
this.game.trackList.push(this.game.pieceMap.get(this.x + "," + i));
return true;
}
}
// 沿 y 軸正向搜索
for (var i = this.y + 1; i < 12; i ++) {
if (!this.game.pieceMap.get(this.x + "," + i).isPass()) {
this.game.trackList = [];
break;
} else if (this.game.pieceMap.get(this.x + "," + i).isReach(piece)
&& this.game.pieceMap.get(this.x + "," + i).isPass()) {
this.game.trackList.push(this.game.pieceMap.get(this.x + "," + i));
return true;
}
}
return false;
},

整體設計相當巧妙:外圈(x 為 0、16 或 y 為 0、11)保留為可通行的「軌道」,讓連線能繞到棋盤外圍。三層判斷(直線 → 一彎 → 兩彎)逐層遞迴呼叫,完整實作了連連看的連通規則。完整源碼可參考作者於 CSDN(zhangjinpeng66)分享的資源。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved