原生JS實(shí)現(xiàn)記憶翻牌游戲
本文實(shí)例為大家分享了JS實(shí)現(xiàn)記憶翻牌游戲的具體代碼,供大家參考,具體內(nèi)容如下
html代碼
<div id="game"> <!-- div.block*16>div.pic --> </div>
css代碼
* { padding: 0; margin: 0; } #game { width: 600px; height: 600px; margin: 0 auto; } .block { float: left; box-sizing: border-box; width: 25%; height: 25%; border: 2px solid #ddd; background-color: #f0f0f0; } .block:hover { background-color: #2b84d0; } .pic { width: 100%; height: 100%; background-repeat: no-repeat; background-position: center center; transform: scaleX(0); transition: transform .2s; } .block.on .pic { transform: scaleX(1) }
js代碼
var game = { el: '', level: 0, blocks: 0, gameWidth: 600, gameHeight: 600, dataArr: [], judgeArr: [], turnNum: 0, picNum: 20, maxLevel: 3, // 最高游戲級(jí)別 init: function (options) { this.initData(options); this.render(); this.handle(); }, initData: function (options) { this.options = options; this.el = options.el; this.level = options.level > this.maxLevel ? this.maxLevel : options.level; this.blocks = (this.level * 2) * (this.level * 2); this.getdataArr(); }, getdataArr: function () { var randomArr = this.randomArr(); var halfBlocks = this.blocks / 2; var dataArr = []; for(var i = 0; i < halfBlocks; i ++) { var num = randomArr[i]; var info = { url: './images/' + num + '.png', id: num } dataArr.push(info, info); } console.log(dataArr); this.dataArr = this.shuffle(dataArr); }, randomArr: function () { var picNum = this.picNum; var arr = []; for(var i = 0; i < picNum; i ++) { arr.push(i + 1); } console.log(arr); return this.shuffle(arr); }, shuffle: function (arr) { return arr.sort(function () { return 0.5 - Math.random(); }) var length = arr.length - 1; for(var i = length ; i >= 0; i --) { var randomIndex = Math.floor(Math.random() * (i + 1)); var temp = arr[randomIndex]; arr[randomIndex] = arr[i]; arr[i] = temp; } return arr; }, render: function () { var blocks = this.blocks; var gameWidth = this.gameWidth; var gameHeight = this.gameHeight; var level = this.level; var blockWidth = gameWidth / ( level * 2 ); var blockHeight = gameHeight / ( level * 2 ); var dataArr = this.dataArr; for(var i = 0; i < blocks; i ++) { var info = dataArr[i]; var oBlock = document.createElement('div'); var oPic = document.createElement('div'); oPic.style.backgroundImage = 'url(' + info.url + ')'; oBlock.style.width = blockWidth + 'px'; oBlock.style.height = blockHeight + 'px'; oBlock.picid = info.id; oPic.setAttribute('class', 'pic'); oBlock.setAttribute('class', 'block'); oBlock.appendChild(oPic); this.el.appendChild(oBlock); handle: function () { var self = this; this.el.onclick = function (e) { var dom = e.target; var isBlock = dom.classList.contains('block'); if(isBlock) { self.handleBlock(dom); } } }, handleBlock: function (dom) { var picId = dom.picid; var judgeArr = this.judgeArr; var judgeLength = judgeArr.push({ id: picId, dom: dom }); dom.classList.add('on'); if(judgeLength === 2) { this.judgePic(); } this.judgeWin(); }, judgePic: function () { var judgeArr = this.judgeArr; var isSamePic = judgeArr[0].id === judgeArr[1].id; if(isSamePic) { this.turnNum += 2; } else { var picDom1 = judgeArr[0].dom; var picDom2 = judgeArr[1].dom; setTimeout(function () { picDom1.classList.remove('on'); picDom2.classList.remove('on'); }, 800) } judgeArr.length = 0; }, judgeWin: function () { if(this.turnNum === this.blocks) { setTimeout(function () { alert('勝利'); }, 300) } } } game.init({ el: document.getElementById('game'), level: 2 })
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
了解Javascript中函數(shù)作為對(duì)象的魅力
這篇文章主要介紹了了解Javascript中函數(shù)作為對(duì)象的魅力,javascript中函數(shù)可以有屬性,可以有方法, 可以享有所有對(duì)象所擁有的特性。并且最重要的,她還可以直接被調(diào)用,需要的朋友可以參考下2019-06-06escape、encodeURI 和 encodeURIComponent 的區(qū)別
escape(), encodeURI()和encodeURIComponent()是在Javascript中用于編碼字符串的三個(gè)常用的方法,而他們之間的異同卻困擾了很多的Javascript初學(xué)者,今天我就在這里對(duì)這三個(gè)方法詳細(xì)地分析與比較一下。2009-03-03JS動(dòng)態(tài)顯示倒計(jì)時(shí)效果
這篇文章主要介紹了JS實(shí)現(xiàn)倒計(jì)時(shí)效果動(dòng)態(tài)顯示倒計(jì)時(shí)功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12基于JavaScript將表單序列化類型的數(shù)據(jù)轉(zhuǎn)化成對(duì)象的處理(允許對(duì)象中包含對(duì)象)
這篇文章主要介紹了基于JavaScript將表單序列化類型的數(shù)據(jù)轉(zhuǎn)化成對(duì)象的處理(允許對(duì)象中包含對(duì)象) 的相關(guān)資料,需要的朋友可以參考下2015-12-12瀏覽器兼容console對(duì)象的簡(jiǎn)要解決方案分享
不同瀏覽器或者版本之間對(duì)于console對(duì)象的支持不盡相同,而console方法在開發(fā)調(diào)試過程中都是不錯(cuò)的工具。難道要在上線前把所有console.xxxx去掉以保證某些瀏覽器不報(bào)錯(cuò)么。其實(shí)可以變通解決2013-10-10JavaScript匿名函數(shù)之模仿塊級(jí)作用域
這篇文章主要介紹了JavaScript匿名函數(shù)之模仿塊級(jí)作用域的相關(guān)資料,需要的朋友可以參考下2015-12-12Javascript實(shí)現(xiàn)的簡(jiǎn)單右鍵菜單類
這篇文章主要介紹了Javascript實(shí)現(xiàn)的簡(jiǎn)單右鍵菜單類,通過JavaScript自定義類實(shí)現(xiàn)右鍵菜單功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09