js實(shí)現(xiàn)簡(jiǎn)單拼圖游戲
本文實(shí)例為大家分享了js實(shí)現(xiàn)簡(jiǎn)單拼圖游戲的具體代碼,供大家參考,具體內(nèi)容如下
HTML僅有一個(gè)id為game的div,并且沒(méi)有編寫(xiě)css樣式,只需要在img文件夾中放置一個(gè)圖片文件就行,此處我放置的是LOL皇子的圖片,圖片名為'lol.png'
<div id="game">? </div>
以下為實(shí)現(xiàn)后的效果圖
多的不說(shuō),直接上js代碼
/** ?* 游戲配置 ?*/ var gameConfig = { ? ? width: 500, ? ? height: 500, ? ? rows: 3, //行數(shù) ? ? cols: 3, //列數(shù) ? ? isOver: false, //游戲是否結(jié)束 ? ? imgurl: "img/lol.png", //圖片路徑,注意:相對(duì)的是頁(yè)面路徑 ? ? dom: document.getElementById("game") //游戲的dom對(duì)象 }; //每一個(gè)小塊的寬高 gameConfig.pieceWidth = gameConfig.width / gameConfig.cols; gameConfig.pieceHeight = gameConfig.height / gameConfig.rows; //小塊的數(shù)量 gameConfig.pieceNumber = gameConfig.rows * gameConfig.cols; ? var blocks = []; //包含小方塊信息的數(shù)組 ? function isEqual(n1, n2) { ? ? return parseInt(n1) === parseInt(n2); } ? /** ?* 小方塊的構(gòu)造函數(shù) ?* @param {*} left? ?* @param {*} top? ?* @param {*} isVisible 是否可見(jiàn) ?*/ function Block(left, top, isVisible) { ? ? this.left = left; //當(dāng)前的橫坐標(biāo) ? ? this.top = top; //當(dāng)前的縱坐標(biāo) ? ? this.correctLeft = this.left; //正確的橫坐標(biāo) ? ? this.correctTop = this.top; //正確的縱坐標(biāo) ? ? this.isVisible = isVisible; //是否可見(jiàn) ? ? this.dom = document.createElement("div"); ? ? this.dom.style.width = gameConfig.pieceWidth + "px"; ? ? this.dom.style.height = gameConfig.pieceHeight + "px"; ? ? this.dom.style.background = `url("${gameConfig.imgurl}") -${this.correctLeft}px -${this.correctTop}px`; ? ? this.dom.style.position = "absolute"; ? ? this.dom.style.border = "1px solid #fff"; ? ? this.dom.style.boxSizing = "border-box"; ? ? this.dom.style.cursor = "pointer"; ? ? // this.dom.style.transition = ".5s"; //css屬性變化的時(shí)候,在0.5秒中內(nèi)完成 ? ? if (!isVisible) { ? ? ? ? this.dom.style.display = "none"; ? ? } ? ? gameConfig.dom.appendChild(this.dom); ? ? ? this.show = function () { ? ? ? ? //根據(jù)當(dāng)前的left、top,重新設(shè)置div的位置 ? ? ? ? this.dom.style.left = this.left + "px"; ? ? ? ? this.dom.style.top = this.top + "px"; ? ? } ? ? //判斷當(dāng)前方塊是否在正確的位置上 ? ? this.isCorrect = function () { ? ? ? ? return isEqual(this.left, this.correctLeft) && isEqual(this.top, this.correctTop); ? ? } ? ? ? this.show(); } ? /** ?* 初始化游戲 ?*/ function init() { ? ? //1. 初始化游戲的容器 ? ? initGameDom(); ? ? //2. 初始化小方塊 ? ? //2.1 準(zhǔn)備好一個(gè)數(shù)組,數(shù)組的每一項(xiàng)是一個(gè)對(duì)象,記錄了每一個(gè)小方塊的信息 ? ? initBlocksArray(); ? ? //2.2 數(shù)組洗牌 ? ? shuffle(); ? ? //3. 注冊(cè)點(diǎn)擊事件 ? ? regEvent(); ? ? ? /** ? ? ?* 處理點(diǎn)擊事件 ? ? ?*/ ? ? function regEvent() { ? ? ? ? //找到看不見(jiàn)的方塊 ? ? ? ? var inVisibleBlock = blocks.find(function (b) { ? ? ? ? ? ? return !b.isVisible; ? ? ? ? }); ? ? ? ? blocks.forEach(function (b) { ? ? ? ? ? ? b.dom.onclick = function () { ? ? ? ? ? ? ? ? if (gameConfig.isOver) { ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? //判斷是可以交換 ? ? ? ? ? ? ? ? if (b.top === inVisibleBlock.top && ? ? ? ? ? ? ? ? ? ? isEqual(Math.abs(b.left - inVisibleBlock.left), gameConfig.pieceWidth) ? ? ? ? ? ? ? ? ? ? || ? ? ? ? ? ? ? ? ? ? b.left === inVisibleBlock.left && ? ? ? ? ? ? ? ? ? ? isEqual(Math.abs(b.top - inVisibleBlock.top), gameConfig.pieceHeight)) { ? ? ? ? ? ? ? ? ? ? //交換當(dāng)前方塊和看不見(jiàn)的方塊的坐標(biāo)位置 ? ? ? ? ? ? ? ? ? ? exchange(b, inVisibleBlock); ? ? ? ? ? ? ? ? ? ? //游戲結(jié)束判定 ? ? ? ? ? ? ? ? ? ? isWin(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? //交換當(dāng)前方塊和看不見(jiàn)的方塊的坐標(biāo)位置 ? ? ? ? ? ? ? ? // exchange(b, inVisibleBlock); ? ? ? ? ? ? ? ? // //游戲結(jié)束判定 ? ? ? ? ? ? ? ? // isWin(); ? ? ? ? ? ? } ? ? ? ? }) ? ? } ? ? ? /** ? ? ?* 游戲結(jié)束判定 ? ? ?*/ ? ? function isWin() { ? ? ? ? var wrongs = blocks.filter(function (item) { ? ? ? ? ? ? return !item.isCorrect(); ? ? ? ? }); ? ? ? ? if (wrongs.length === 0) { ? ? ? ? ? ? gameConfig.isOver = true; ? ? ? ? ? ? //游戲結(jié)束,去掉所有邊框 ? ? ? ? ? ? blocks.forEach(function (b) { ? ? ? ? ? ? ? ? b.dom.style.border = "none"; ? ? ? ? ? ? ? ? b.dom.style.display = "block"; ? ? ? ? ? ? }); ? ? ? ? } ? ? } ? ? ? /** ? ? ?* 隨機(jī)數(shù),能取到最大值 ? ? ?* @param {*} min? ? ? ?* @param {*} max? ? ? ?*/ ? ? function getRandom(min, max) { ? ? ? ? return Math.floor(Math.random() * (max + 1 - min) + min); ? ? } ? ? ? /** ? ? ?* 交換兩個(gè)方塊的left和top ? ? ?* @param {*} b1? ? ? ?* @param {*} b2? ? ? ?*/ ? ? function exchange(b1, b2) { ? ? ? ? var temp = b1.left; ? ? ? ? b1.left = b2.left; ? ? ? ? b2.left = temp; ? ? ? ? ? temp = b1.top; ? ? ? ? b1.top = b2.top; ? ? ? ? b2.top = temp; ? ? ? ? ? b1.show(); ? ? ? ? b2.show(); ? ? } ? ? ? /** ? ? ?* 給blocks數(shù)組洗牌 ? ? ?*/ ? ? function shuffle() { ? ? ? ? for (var i = 0; i < blocks.length - 1; i++) { ? ? ? ? ? ? //隨機(jī)產(chǎn)生一個(gè)下標(biāo) ? ? ? ? ? ? var index = getRandom(0, blocks.length - 2); ? ? ? ? ? ? //將數(shù)組的當(dāng)前項(xiàng)與隨機(jī)項(xiàng)交換left和top值 ? ? ? ? ? ? exchange(blocks[i], blocks[index]); ? ? ? ? } ? ? } ? ? ? /** ? ? ?* 初始化一個(gè)小方塊的數(shù)組 ? ? ?*/ ? ? function initBlocksArray() { ? ? ? ? for (var i = 0; i < gameConfig.rows; i++) { ? ? ? ? ? ? for (var j = 0; j < gameConfig.cols; j++) { ? ? ? ? ? ? ? ? //i 行號(hào),j 列號(hào) ? ? ? ? ? ? ? ? var isVisible = true; ? ? ? ? ? ? ? ? if (i === gameConfig.rows - 1 && j === gameConfig.cols - 1) { ? ? ? ? ? ? ? ? ? ? isVisible = false; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? var b = new Block(j * gameConfig.pieceWidth, i * gameConfig.pieceHeight, isVisible); ? ? ? ? ? ? ? ? blocks.push(b); ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? ? /** ? ? ?* 初始化游戲容器 ? ? ?*/ ? ? function initGameDom() { ? ? ? ? gameConfig.dom.style.width = gameConfig.width + "px"; ? ? ? ? gameConfig.dom.style.height = gameConfig.height + "px"; ? ? ? ? gameConfig.dom.style.border = "2px solid #ccc"; ? ? ? ? gameConfig.dom.style.position = "relative"; ? ? } } ? init();?
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
input標(biāo)簽內(nèi)容改變的觸發(fā)事件介紹
onchange事件在內(nèi)容改變(兩次內(nèi)容有可能相等)且失去焦點(diǎn)時(shí)觸發(fā);onpropertychange事件是實(shí)時(shí)觸發(fā),每增加或刪除一個(gè)字符就會(huì)觸發(fā)2014-06-06關(guān)于Promise基本方法的簡(jiǎn)單實(shí)現(xiàn)
Promise大家一定都不陌生了,JavaScript異步流程從最初的Callback,到Promise,到Generator,再到目前使用最多的Async/Await,下面這篇文章主要給大家介紹了關(guān)于Promise基本方法的簡(jiǎn)單實(shí)現(xiàn),需要的朋友可以參考下2022-02-02JavaScript實(shí)現(xiàn)事件的中斷傳播和行為阻止方法示例
這篇文章主要給大家介紹了利用JavaScript實(shí)現(xiàn)事件的中斷傳播和行為阻止的方法示例,文中給出了詳細(xì)的介紹和示例代碼,相信對(duì)大家的理解和學(xué)習(xí)具有一定的參考借鑒價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-01-01BootStrap3學(xué)習(xí)筆記(一)之網(wǎng)格系統(tǒng)
本文給大家介紹BootStrap3網(wǎng)格系統(tǒng)的相關(guān)知識(shí),本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-05-05JS實(shí)現(xiàn)簡(jiǎn)單隨機(jī)3D骰子
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)簡(jiǎn)單隨機(jī)3D骰子,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10讓低版本瀏覽器支持input的placeholder屬性(js方法)
低版本瀏覽器一般都不會(huì)支持input的placeholder屬性,接下來(lái)使用js實(shí)現(xiàn)下,感興趣的朋友可以參考下哈2013-04-04D3.js 從P元素的創(chuàng)建開(kāi)始(顯示可加載數(shù)據(jù))
D3是一個(gè)基于數(shù)據(jù)操作的可視化js庫(kù),認(rèn)識(shí)d3,就從最基礎(chǔ)的顯示可加載數(shù)據(jù)談起,需要的朋友可以參考下2014-10-10