基于JS实现简单滑块拼图游戏

2022-01-13,,,

本文通过实例代码给大家介绍了JS实现简单滑块拼图游戏,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧

成品效果

 

   /** * js配置 */ var config = { width: 300, height: 300, img: "./img/fj.jpg-600", gameDom: document.getElementById("game"), row: 3, //3行 col: 3 //3列 } //经过计算的一些数据 var computed = { num: config.col * config.row, //方块数量 w: config.width / config.col, //每个小方块的宽度 h: config.height / config.row //每个小方块的高度 } //方块对象的数组,每个对象中记录了方块的正确坐标、当前坐标、dom元素、以及一些实用方法 var blocks; /** * 为全局变量blocks赋值 */ function setBlocks() { blocks = []; var points = getPointsArray(); //该数组用于设置每个方块的正确坐标 var shuffledPoints = [...points]; //复制后的数组用于在洗牌后设置方块的当前坐标 shuffle(shuffledPoints);//洗牌 for (var i = 0; i <points.length; i++) { const point = points[i]; //创建方块对象 var b = { left: point.left, top: point.top, curLeft: shuffledPoints[i].left, curTop: shuffledPoints[i].top, dom: document.createElement("div"), update() { this.dom.style.transition = "all .5s"; this.dom.style.left = this.curLeft + "px"; this.dom.style.top = this.curTop + "px"; }, isCorrect() { return this.curTop === this.top && this.curLeft === this.left; }, isEmpty: i === points.length - 1 //是否应该是空白方块 } b.dom.style.width = computed.w + "px"; b.dom.style.height = computed.h + "px"; b.dom.style.position = "absolute"; b.dom.style.border = "1px solid #fff"; b.dom.style.boxSizing = "border-box"; b.dom.style.background = `url("${config.img}")`; b.dom.style.cursor = "pointer"; b.dom.style.backgroundPosition = `-${b.left}px -${b.top}px`; b.dom.block = b; b.dom.onclick = function () { switchBlock(this.block); } b.update(); blocks.push(b); } } /** * 生成游戏 */ function generateGame() { config.gameDom.style.width = config.width + "px"; config.gameDom.style.height = config.height + "px"; config.gameDom.style.border = "2px solid #8c8c8c"; config.gameDom.innerHTML = ""; //清空区域 for (const item of blocks) { if (!item.isEmpty) { config.gameDom.appendChild(item.dom); } } } /** * 获得所有方块的可取到的坐标数组 */ function getPointsArray() { var arr = []; for (var i = 0; i b.isEmpty); //判断是否相邻 if(Math.abs(block.curLeft - emptyBlock.curLeft) + Math.abs(block.curTop - emptyBlock.curTop) !== computed.w){ return; } //交换 var bLeft = block.curLeft; var bTop = block.curTop; block.curLeft = emptyBlock.curLeft; block.curTop = emptyBlock.curTop; emptyBlock.curLeft = bLeft; emptyBlock.curTop = bTop; block.update(); emptyBlock.update(); if(isWin()){ setTimeout(() => { alert("游戏胜利") }, 500); } } /** * 数组洗牌 * @param {*} arr */ function shuffle(arr) { for (var i = 0; i 

总结

以上所述是小编给大家介绍的基于JS实现简单滑块拼图游戏,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对本站网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

以上就是基于JS实现简单滑块拼图游戏的详细内容,更多请关注本站其它相关文章!

《基于JS实现简单滑块拼图游戏.doc》

下载本文的Word格式文档,以方便收藏与打印。