Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. const memoryGame = {
  2. tileCount: 20, //liczba klocków
  3. tileOnRow: 5, //liczba klocków na rząd
  4. divBoard: null, //div z planszą gry
  5. divScore: null, //div z wynikiem gry
  6. tiles: [], //tutaj trafi wymieszana tablica klocków
  7. tilesChecked: [], //zaznaczone klocki
  8. moveCount: 0, //liczba ruchów
  9. tilesImg: [
  10. //grafiki dla klocków
  11. 'images/tile_1.png',
  12. 'images/tile_2.png',
  13. 'images/tile_3.png',
  14. 'images/tile_4.png',
  15. 'images/tile_5.png',
  16. 'images/tile_6.png',
  17. 'images/tile_7.png',
  18. 'images/tile_8.png',
  19. 'images/tile_9.png',
  20. 'images/tile_10.png'
  21. ],
  22. canGet: true, //czy można klikać na kafelki
  23. tilePairs: 0, //liczba dopasowanych kafelkow
  24.  
  25. tileClick: function(e) {
  26. if (this.canGet) {
  27. //jeżeli jeszcze nie pobraliśmy 1 elementu
  28. //lub jeżeli index tego elementu nie istnieje w pobranych...
  29. if (
  30. !this.tilesChecked[0] ||
  31. this.tilesChecked[0].dataset.index !== e.target.dataset.index
  32. ) {
  33. this.tilesChecked.push(e.target);
  34. e.target.style.backgroundImage =
  35. 'url(' + this.tilesImg[e.target.dataset.cardType] + ')';
  36. console.log(e.target.style);
  37. }
  38.  
  39. if (this.tilesChecked.length === 2) {
  40. this.canGet = false;
  41.  
  42. if (
  43. this.tilesChecked[0].dataset.cardType ===
  44. this.tilesChecked[1].dataset.cardType
  45. ) {
  46. setTimeout(this.deleteTiles.bind(this), 500);
  47. } else {
  48. setTimeout(this.resetTiles.bind(this), 500);
  49. }
  50.  
  51. this.moveCount++;
  52. this.divScore.innerText = this.moveCount;
  53. }
  54. }
  55. },
  56.  
  57. deleteTiles: function() {
  58. this.tilesChecked[0].remove();
  59. this.tilesChecked[1].remove();
  60.  
  61. this.canGet = true;
  62. this.tilesChecked = [];
  63.  
  64. this.tilePairs++;
  65. if (this.tilePairs >= this.tileCount / 2) {
  66. alert('gameOver!');
  67. }
  68. },
  69.  
  70. resetTiles: function() {
  71. this.tilesChecked[0].style.backgroundImage = 'url(images/tile.png)';
  72. this.tilesChecked[1].style.backgroundImage = 'url(images/tile.png)';
  73.  
  74. this.tilesChecked = [];
  75. this.canGet = true;
  76. },
  77.  
  78. startGame: function() {
  79. //czyścimy planszę
  80. this.divBoard = document.querySelector('.game-board');
  81. this.divBoard.innerHTML = '';
  82.  
  83. //czyścimy planszę z ruchami
  84. this.divScore = document.querySelector('.game-score');
  85. this.divScore.innerHTML = '';
  86.  
  87. //czyścimy zmienne (bo gra może się zacząć ponownie)
  88. this.tiles = [];
  89. this.tilesChecked = [];
  90. this.moveCount = 0;
  91. this.canGet = true;
  92. this.tilePairs = 0;
  93.  
  94. //generujemy tablicę numerów kocków (parami)
  95. for (let i = 0; i < this.tileCount; i++) {
  96. this.tiles.push(Math.floor(i / 2));
  97. }
  98.  
  99. //i ją mieszamy
  100. for (let i = this.tileCount - 1; i > 0; i--) {
  101. const swap = Math.floor(Math.random() * i);
  102. const tmp = this.tiles[i];
  103. this.tiles[i] = this.tiles[swap];
  104. this.tiles[swap] = tmp;
  105. }
  106.  
  107. for (let i = 0; i < this.tileCount; i++) {
  108. const tile = document.createElement('div');
  109. tile.classList.add('game-tile');
  110. this.divBoard.appendChild(tile);
  111.  
  112. tile.dataset.cardType = this.tiles[i];
  113. tile.dataset.index = i;
  114.  
  115. tile.style.left =
  116. 5 + (tile.offsetWidth + 10) * (i % this.tileOnRow) + 'px';
  117. tile.style.top =
  118. 5 + (tile.offsetHeight + 10) * Math.floor(i / this.tileOnRow) + 'px';
  119.  
  120. tile.addEventListener('click', this.tileClick.bind(this));
  121. }
  122. }
  123. };
  124.  
  125. document.addEventListener('DOMContentLoaded', function() {
  126. document.querySelector('.game-start').addEventListener('click', function() {
  127. memoryGame.startGame();
  128. });
  129. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement