经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » jQuery » 查看文章
jq + 面向对象实现拼图游戏
来源:cnblogs  作者:smile_or  时间:2019/5/5 8:57:33  对本文有异议

jq + 面向对象实现拼图游戏

知识点

  • 拖拽事件
  • es6面向对象
  • jquery事件
  • 效果图

html:

  1. <div class="wraper">
  2. <div class="btn">
  3. <button class="start">开始</button>
  4. </div>
  5. <div class="box"></div>
  6. </div>

css:

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. list-style: none;
  5. }
  6. html,
  7. body {
  8. width: 100%;
  9. height: 100%;
  10. background: url('../img/bg_pic.jpg') no-repeat;
  11. background-size: 100% 100%;
  12. display: flex;
  13. justify-content: center;
  14. align-items: center;
  15. flex-direction: column;
  16. }
  17. .wraper {
  18. width: 500px;
  19. height: 600px;
  20. position: relative;
  21. }
  22. .wraper .btn {
  23. text-align: center;
  24. line-height: 80px;
  25. }
  26. .wraper .btn button {
  27. width: 100px;
  28. height: 40px;
  29. background: yellow;
  30. border: none;
  31. outline: none;
  32. font-size: 14px;
  33. color: red;
  34. border-radius: 20px;
  35. cursor: pointer;
  36. }
  37. .wraper .box {
  38. width: 100%;
  39. height: 500px;
  40. position: relative;
  41. border: 10px solid red;
  42. border-radius: 10px;
  43. }
  44. .wraper .box .pic {
  45. position: absolute;
  46. background: url('../img/zy.jpg') no-repeat;
  47. box-shadow: 0 0 5px #fff;
  48. background-size: 500px 500px;
  49. cursor: pointer;
  50. }

js:

  1. class Game {
  2. constructor() {
  3. this.boxW = parseInt($('.box').css('width'));
  4. this.boxH = parseInt($('.box').css('height'));
  5. this.imgW = this.boxW / 5;
  6. this.imgH = this.boxH / 5;
  7. this.flag = true; //true为开始 false为重排
  8. this.orArr = []; //标准数组
  9. this.randArr = []; //乱序数组
  10. this.init();
  11. }
  12. init() {
  13. this.createDom();
  14. this.getState();
  15. }
  16. createDom() {
  17. //行
  18. for (var i = 0; i < 5; i++) {
  19. //列
  20. for (var j = 0; j < 5; j++) {
  21. this.orArr.push(i * 5 + j);
  22. let imgs = $("<div class='pic'></div>").css({
  23. width: this.imgW + 'px',
  24. height: this.imgH + 'px',
  25. left: j * this.imgW + 'px',
  26. top: i * this.imgH + 'px',
  27. backgroundPosition: -j * this.imgW + 'px ' + -i * this.imgH + 'px'
  28. });
  29. $('.box').append(imgs);
  30. }
  31. }
  32. }
  33. getState() {
  34. let btn = $('.btn .start');
  35. let imgs = $('.pic');
  36. let _this = this;
  37. btn.on('click', function() {
  38. if (_this.flag) {
  39. _this.flag = false;
  40. btn.text('重排');
  41. _this.getRandom();
  42. _this.getOrder(_this.randArr);
  43. imgs.on('mousedown', function(e) {
  44. let index = $(this).index();
  45. let left = e.pageX - imgs.eq(index).offset().left;
  46. let top = e.pageY - imgs.eq(index).offset().top;
  47. $(document).on('mousemove', function(e1) {
  48. let left1 = e1.pageX - left - $('.box').offset().left - 10;
  49. let top1 = e1.pageY - top - $('.box').offset().top - 10;
  50. imgs.eq(index).css({
  51. 'z-index': '40',
  52. 'left': left1,
  53. 'top': top1
  54. })
  55. }).on('mouseup', function(e2) {
  56. let left2 = e2.pageX - left - $('.box').offset().left - 10;
  57. let top2 = e2.pageY - top - $('.box').offset().top - 10;
  58. let index2 = _this.changeIndex(left2, top2, index);
  59. if (index === index2) {
  60. _this.picReturn(index);
  61. } else {
  62. _this.picChange(index, index2);
  63. }
  64. $(document).off('mousemove').off('mouseup').off('mousedown');
  65. })
  66. return false;
  67. })
  68. } else {
  69. _this.flag = true;
  70. btn.text('开始');
  71. _this.getOrder(_this.orArr);
  72. imgs.off('mousemove').off('mouseup').off('mousedown');
  73. }
  74. })
  75. }
  76. changeIndex(left, top, index) {
  77. if (left < 0 || left > this.boxW || top < 0 || top > this.boxH) {
  78. return index;
  79. } else {
  80. let col = Math.floor(left / this.imgW);
  81. let row = Math.floor(top / this.imgH);
  82. let moveIndex = 5 * row + col;
  83. let i = 0;
  84. let len = this.randArr.length;
  85. while ((i < len) && this.randArr[i] !== moveIndex) {
  86. i++;
  87. }
  88. return i;
  89. }
  90. }
  91. picReturn(index) {
  92. let j = this.randArr[index] % 5;
  93. let i = Math.floor(this.randArr[index] / 5);
  94. $('.pic').eq(index).css('z-index', '40').animate({
  95. 'left': j * this.imgW,
  96. 'top': i * this.imgH
  97. }, 300, function() {
  98. $(this).css('z-index', '10');
  99. })
  100. }
  101. picChange(index, index2) {
  102. let _this = this;
  103. let fromJ = _this.randArr[index] % 5;
  104. let fromI = Math.floor(_this.randArr[index] / 5);
  105. let toJ = _this.randArr[index2] % 5;
  106. let toI = Math.floor(_this.randArr[index2] / 5);
  107. let temp = _this.randArr[index];
  108. $('.pic').eq(index).css('z-index', '40').animate({
  109. 'left': toJ * _this.imgW + 'px',
  110. 'top': toI * _this.imgH + 'px'
  111. }, 300, function() {
  112. $(this).css('z-index', '10');
  113. })
  114. $('.pic').eq(index2).css('z-index', '40').animate({
  115. 'left': fromJ * _this.imgW + 'px',
  116. 'top': fromI * _this.imgH + 'px'
  117. }, 300, function() {
  118. $(this).css('z-index', '10');
  119. _this.randArr[index] = _this.randArr[index2];
  120. _this.randArr[index2] = temp;
  121. _this.check();
  122. })
  123. }
  124. getRandom() {
  125. this.randArr = [...this.orArr];
  126. this.randArr.sort(function() {
  127. return Math.random() - 0.5;
  128. })
  129. }
  130. getOrder(arr) {
  131. let len = arr.length;
  132. for (var i = 0; i < len; i++) {
  133. $('.box .pic').eq(i).animate({
  134. left: arr[i] % 5 * this.imgW,
  135. top: Math.floor(arr[i] / 5) * this.imgH
  136. }, 400)
  137. }
  138. }
  139. check() { //判断是否成功
  140. if (this.randArr.toString() == this.orArr.toString()) {
  141. alert('拼图成功');
  142. this.flag = true;
  143. $('.btn .start').text('开始');
  144. $('.pic').off('mousemove').off('mouseup').off('mousedown');
  145. }
  146. }
  147. }
  148. new Game();

参考至腾讯课堂渡一教育

原文链接:http://www.cnblogs.com/sgs123/p/10808246.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号