资讯专栏INFORMATION COLUMN

展示js简单拼图游戏

3403771864 / 242人阅读

  我们看看js实现简单拼图游戏的详细代码,HTML仅有一个id为game的div,也不错编写CSS,仅要img文件夹中放置一个图片文件就行,此处我放置的是LOL皇子的图片,图片名为'lol.png'

  <div id="game">
  </div>

  下面展示具体效果

  多的不说,直接上js代码

  /**
  * 游戏配置
  */
  var gameConfig = {
  width: 500,
  height: 500,
  rows: 3, //行数
  cols: 3, //列数
  isOver: false, //游戏是否结束
  imgurl: "img/lol.png", //图片路径,注意:相对的是页面路径
  dom: document.getElementById("game") //游戏的dom对象
  };
  //每一个小块的宽高
  gameConfig.pieceWidth = gameConfig.width / gameConfig.cols;
  gameConfig.pieceHeight = gameConfig.height / gameConfig.rows;
  //小块的数量
  gameConfig.pieceNumber = gameConfig.rows * gameConfig.cols;
  var blocks = []; //包含小方块信息的数组
  function isEqual(n1, n2) {
  return parseInt(n1) === parseInt(n2);
  }
  /**
  * 小方块的构造函数
  * @param {*} left
  * @param {*} top
  * @param {*} isVisible 是否可见
  */
  function Block(left, top, isVisible) {
  this.left = left; //当前的横坐标
  this.top = top; //当前的纵坐标
  this.correctLeft = this.left; //正确的横坐标
  this.correctTop = this.top; //正确的纵坐标
  this.isVisible = isVisible; //是否可见
  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属性变化的时候,在0.5秒中内完成
  if (!isVisible) {
  this.dom.style.display = "none";
  }
  gameConfig.dom.appendChild(this.dom);
  this.show = function () {
  //根据当前的left、top,重新设置div的位置
  this.dom.style.left = this.left + "px";
  this.dom.style.top = this.top + "px";
  }
  //判断当前方块是否在正确的位置上
  this.isCorrect = function () {
  return isEqual(this.left, this.correctLeft) && isEqual(this.top, this.correctTop);
  }
  this.show();
  }
  /**
  * 初始化游戏
  */
  function init() {
  //1. 初始化游戏的容器
  initGameDom();
  //2. 初始化小方块
  //2.1 准备好一个数组,数组的每一项是一个对象,记录了每一个小方块的信息
  initBlocksArray();
  //2.2 数组洗牌
  shuffle();
  //3. 注册点击事件
  regEvent();
  /**
  * 处理点击事件
  */
  function regEvent() {
  //找到看不见的方块
  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)) {
  //交换当前方块和看不见的方块的坐标位置
  exchange(b, inVisibleBlock);
  //游戏结束判定
  isWin();
  }
  //交换当前方块和看不见的方块的坐标位置
  // exchange(b, inVisibleBlock);
  // //游戏结束判定
  // isWin();
  }
  })
  }
  /**
  * 游戏结束判定
  */
  function isWin() {
  var wrongs = blocks.filter(function (item) {
  return !item.isCorrect();
  });
  if (wrongs.length === 0) {
  gameConfig.isOver = true;
  //游戏结束,去掉所有边框
  blocks.forEach(function (b) {
  b.dom.style.border = "none";
  b.dom.style.display = "block";
  });
  }
  }
  /**
  * 随机数,能取到最大值
  * @param {*} min
  * @param {*} max
  */
  function getRandom(min, max) {
  return Math.floor(Math.random() * (max + 1 - min) + min);
  }
  /**
  * 交换两个方块的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数组洗牌
  */
  function shuffle() {
  for (var i = 0; i < blocks.length - 1; i++) {
  //随机产生一个下标
  var index = getRandom(0, blocks.length - 2);
  //将数组的当前项与随机项交换left和top值
  exchange(blocks[i], blocks[index]);
  }
  }
  /**
  * 初始化一个小方块的数组
  */
  function initBlocksArray() {
  for (var i = 0; i < gameConfig.rows; i++) {
  for (var j = 0; j < gameConfig.cols; j++) {
  //i 行号,j 列号
  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();


文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/128176.html

相关文章

  • 利用Vue.js实现拼图游戏

    摘要:之前写过一篇基于的表格分页组件的文章,主要介绍了组件的编写方法,有兴趣的可以访问这里进行阅读前言为了进一步让大家了解的神奇魅力,了解的一种以数据为驱动的理念,本文主要利用实现了一个数字拼图游戏,其原理并不是很复杂,效果图如下展示地址为有能力 之前写过一篇《基于Vue.js的表格分页组件》的文章,主要介绍了Vue组件的编写方法,有兴趣的可以访问这里进行阅读:https://segment...

    wh469012917 评论0 收藏0
  • 拼图游戏

    摘要:学习小游戏开发中最常用的碰撞检测状态监控刷新保持状态的处理方法。保存缩略图的信息是当游戏结束后显示源缩略图时,根据中的内容展示图片。 如果您想要综合使用javascript中canvas、原生拖拽、本地存储等多种技术完成一个有趣的项目,那么这篇博文将非常适合您,水平有限,还望感兴趣的开发人员给予更多代码优化建议。 1 简介和源码 该项目中的拼图小游戏使用javascript原创,相比于...

    svtter 评论0 收藏0
  • js实现数字拼图代码展示

      话不多说,直接开干。  重点:  下图我们可以看到,游戏区分为8个div,进行游戏时需要判断点击的div是否可移动,移动后判断游戏是否结束。  解决思路:将游戏界面看作一个div大盒子,将大盒子分为9个区域进行编号,这9个区域的位置始终不变;8个div以定位top和left控制其位置,设置9个区域的div分别可以往哪个区域移动,点击时判断可移动的编号区域内是否有div,若有,则无法向该方向移动...

    3403771864 评论0 收藏0

发表评论

0条评论

3403771864

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<