[前端] js实现简单拼图游戏

2926 0
黑夜隐士 2022-10-21 15:44:32 | 显示全部楼层 |阅读模式
本文实例为大家分享了js实现简单拼图游戏的具体代码,供大家参考,具体内容如下
HTML仅有一个id为game的div,并且没有编写css样式,只需要在img文件夹中放置一个图片文件就行,此处我放置的是LOL皇子的图片,图片名为'lol.png'
  1. <div id="game">
  2. </div>
复制代码
以下为实现后的效果图


多的不说,直接上js代码
  1. /**
  2. * 游戏配置
  3. */
  4. var gameConfig = {
  5.   width: 500,
  6.   height: 500,
  7.   rows: 3, //行数
  8.   cols: 3, //列数
  9.   isOver: false, //游戏是否结束
  10.   imgurl: "img/lol.png", //图片路径,注意:相对的是页面路径
  11.   dom: document.getElementById("game") //游戏的dom对象
  12. };
  13. //每一个小块的宽高
  14. gameConfig.pieceWidth = gameConfig.width / gameConfig.cols;
  15. gameConfig.pieceHeight = gameConfig.height / gameConfig.rows;
  16. //小块的数量
  17. gameConfig.pieceNumber = gameConfig.rows * gameConfig.cols;
  18. var blocks = []; //包含小方块信息的数组
  19. function isEqual(n1, n2) {
  20.   return parseInt(n1) === parseInt(n2);
  21. }
  22. /**
  23. * 小方块的构造函数
  24. * @param {*} left
  25. * @param {*} top
  26. * @param {*} isVisible 是否可见
  27. */
  28. function Block(left, top, isVisible) {
  29.   this.left = left; //当前的横坐标
  30.   this.top = top; //当前的纵坐标
  31.   this.correctLeft = this.left; //正确的横坐标
  32.   this.correctTop = this.top; //正确的纵坐标
  33.   this.isVisible = isVisible; //是否可见
  34.   this.dom = document.createElement("div");
  35.   this.dom.style.width = gameConfig.pieceWidth + "px";
  36.   this.dom.style.height = gameConfig.pieceHeight + "px";
  37.   this.dom.style.background = `url("${gameConfig.imgurl}") -${this.correctLeft}px -${this.correctTop}px`;
  38.   this.dom.style.position = "absolute";
  39.   this.dom.style.border = "1px solid #fff";
  40.   this.dom.style.boxSizing = "border-box";
  41.   this.dom.style.cursor = "pointer";
  42.   // this.dom.style.transition = ".5s"; //css属性变化的时候,在0.5秒中内完成
  43.   if (!isVisible) {
  44.     this.dom.style.display = "none";
  45.   }
  46.   gameConfig.dom.appendChild(this.dom);
  47.   this.show = function () {
  48.     //根据当前的left、top,重新设置div的位置
  49.     this.dom.style.left = this.left + "px";
  50.     this.dom.style.top = this.top + "px";
  51.   }
  52.   //判断当前方块是否在正确的位置上
  53.   this.isCorrect = function () {
  54.     return isEqual(this.left, this.correctLeft) && isEqual(this.top, this.correctTop);
  55.   }
  56.   this.show();
  57. }
  58. /**
  59. * 初始化游戏
  60. */
  61. function init() {
  62.   //1. 初始化游戏的容器
  63.   initGameDom();
  64.   //2. 初始化小方块
  65.   //2.1 准备好一个数组,数组的每一项是一个对象,记录了每一个小方块的信息
  66.   initBlocksArray();
  67.   //2.2 数组洗牌
  68.   shuffle();
  69.   //3. 注册点击事件
  70.   regEvent();
  71.   /**
  72.   * 处理点击事件
  73.   */
  74.   function regEvent() {
  75.     //找到看不见的方块
  76.     var inVisibleBlock = blocks.find(function (b) {
  77.       return !b.isVisible;
  78.     });
  79.     blocks.forEach(function (b) {
  80.       b.dom.onclick = function () {
  81.         if (gameConfig.isOver) {
  82.           return;
  83.         }
  84.         //判断是可以交换
  85.         if (b.top === inVisibleBlock.top &&
  86.           isEqual(Math.abs(b.left - inVisibleBlock.left), gameConfig.pieceWidth)
  87.           ||
  88.           b.left === inVisibleBlock.left &&
  89.           isEqual(Math.abs(b.top - inVisibleBlock.top), gameConfig.pieceHeight)) {
  90.           //交换当前方块和看不见的方块的坐标位置
  91.           exchange(b, inVisibleBlock);
  92.           //游戏结束判定
  93.           isWin();
  94.         }
  95.         //交换当前方块和看不见的方块的坐标位置
  96.         // exchange(b, inVisibleBlock);
  97.         // //游戏结束判定
  98.         // isWin();
  99.       }
  100.     })
  101.   }
  102.   /**
  103.   * 游戏结束判定
  104.   */
  105.   function isWin() {
  106.     var wrongs = blocks.filter(function (item) {
  107.       return !item.isCorrect();
  108.     });
  109.     if (wrongs.length === 0) {
  110.       gameConfig.isOver = true;
  111.       //游戏结束,去掉所有边框
  112.       blocks.forEach(function (b) {
  113.         b.dom.style.border = "none";
  114.         b.dom.style.display = "block";
  115.       });
  116.     }
  117.   }
  118.   /**
  119.   * 随机数,能取到最大值
  120.   * @param {*} min
  121.   * @param {*} max
  122.   */
  123.   function getRandom(min, max) {
  124.     return Math.floor(Math.random() * (max + 1 - min) + min);
  125.   }
  126.   /**
  127.   * 交换两个方块的left和top
  128.   * @param {*} b1
  129.   * @param {*} b2
  130.   */
  131.   function exchange(b1, b2) {
  132.     var temp = b1.left;
  133.     b1.left = b2.left;
  134.     b2.left = temp;
  135.     temp = b1.top;
  136.     b1.top = b2.top;
  137.     b2.top = temp;
  138.     b1.show();
  139.     b2.show();
  140.   }
  141.   /**
  142.   * 给blocks数组洗牌
  143.   */
  144.   function shuffle() {
  145.     for (var i = 0; i < blocks.length - 1; i++) {
  146.       //随机产生一个下标
  147.       var index = getRandom(0, blocks.length - 2);
  148.       //将数组的当前项与随机项交换left和top值
  149.       exchange(blocks[i], blocks[index]);
  150.     }
  151.   }
  152.   /**
  153.   * 初始化一个小方块的数组
  154.   */
  155.   function initBlocksArray() {
  156.     for (var i = 0; i < gameConfig.rows; i++) {
  157.       for (var j = 0; j < gameConfig.cols; j++) {
  158.         //i 行号,j 列号
  159.         var isVisible = true;
  160.         if (i === gameConfig.rows - 1 && j === gameConfig.cols - 1) {
  161.           isVisible = false;
  162.         }
  163.         var b = new Block(j * gameConfig.pieceWidth, i * gameConfig.pieceHeight, isVisible);
  164.         blocks.push(b);
  165.       }
  166.     }
  167.   }
  168.   /**
  169.   * 初始化游戏容器
  170.   */
  171.   function initGameDom() {
  172.     gameConfig.dom.style.width = gameConfig.width + "px";
  173.     gameConfig.dom.style.height = gameConfig.height + "px";
  174.     gameConfig.dom.style.border = "2px solid #ccc";
  175.     gameConfig.dom.style.position = "relative";
  176.   }
  177. }
  178. init();
复制代码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持中国红客联盟。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行