app1.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. var done = 0;
  2. var picture = "https://sun9-51.userapi.com/c204524/v204524368/1974/GIIiot2uoiw.jpg";
  3. function getCoords(elem) {
  4. let box = elem.getBoundingClientRect();
  5. return {
  6. top: box.top + pageYOffset,
  7. left: box.left + pageXOffset
  8. };
  9. }
  10. function startgame() {
  11. let index = 0;
  12. let over = document.querySelector("#over");
  13. let board = document.querySelector("#board");
  14. board.style.width = 50 * 9;
  15. for (let r = 0; r < 6; r++) {
  16. let tr = '<tr class="row" data-row="' + r + '"></tr>';
  17. board.insertAdjacentHTML('beforeend', tr);
  18. for (let c = 0; c < 9; c++) {
  19. index = c + (r * 9);
  20. let td = '<td class="tile" data-id="' + index + '"></td>';
  21. let ar = document.querySelector('tr[data-row="' + r + '"]');
  22. ar.insertAdjacentHTML('beforeend', td);
  23. let ac = document.querySelector('td[data-id="' + index + '"]');
  24. let block = '<div class="block" style="background-position: ' + c * -50 + "px " + r * -50 + 'px" data-id="' + index + '"></div>';
  25. over.insertAdjacentHTML('beforeend', block);
  26. let ab = over.querySelector('div[data-id="' + index + '"]');
  27. ab.style.backgroundImage = 'url("' + picture + '")';
  28. ab.addEventListener("mousedown", down);
  29. ab.style.left = Math.random() * (document.documentElement.clientWidth / 2 - 50) + "px";
  30. ab.style.top = Math.random() * (document.documentElement.clientHeight - 50) + "px";
  31. ab.dataset.left = getCoords(ab).left;
  32. ab.dataset.top = getCoords(ab).top;
  33. }
  34. }
  35. }
  36. function isCollide(orig, tile) {
  37. let b = getCoords(tile);
  38. let a = getCoords(orig);
  39. return !(
  40. ((a.top + 50) < (b.top)) ||
  41. (a.top > (b.top + 50)) ||
  42. ((a.left + 50) < b.left) ||
  43. (a.left > (b.left + 50))
  44. );
  45. }
  46. function down(e) {
  47. cell = e.target;
  48. if (cell.classList.contains("complete")) {
  49. return;
  50. }
  51. let coords = getCoords(cell);
  52. let shiftX = e.pageX - coords.left;
  53. let shiftY = e.pageY - coords.top;
  54. cell.style.position = 'absolute';
  55. moveAt(e);
  56. cell.style.zIndex = 1000;
  57. function moveAt(e) {
  58. cell.style.left = (e.pageX - shiftX) + 'px';
  59. cell.style.top = (e.pageY - shiftY) + 'px';
  60. }
  61. document.onmousemove = function(e) {
  62. moveAt(e);
  63. };
  64. cell.onmouseup = function() {
  65. document.onmousemove = null;
  66. cell.onmouseup = null;
  67. cell.style.zIndex = 500;
  68. field = document.querySelector("td[data-id='" + cell.dataset.id + "']");
  69. if (isCollide(cell, field)) {
  70. let temp = getCoords(field);
  71. cell.style.left = temp.left + "px";
  72. cell.style.top = temp.top + "px";
  73. cell.classList.add("complete");
  74. done++;
  75. if (done===54) alert("You win!");
  76. } else {
  77. if (!isCollide(cell, document.querySelector("#pieces"))) {
  78. cell.style.left = cell.dataset.left + "px";
  79. cell.style.top = cell.dataset.top + "px";
  80. }
  81. }
  82. };
  83. cell.ondragstart = function() {
  84. return false;
  85. };
  86. }
  87. startgame();