Advertisement
RRusev77

ClickyGame

Jul 8th, 2020
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let clicksCount, imagesValue;
  2.  
  3. // start the game;
  4. appInit();
  5.  
  6.  
  7. function changeBackground() {
  8.     // array of colors
  9.     const colors = ['#FFA421', '#0078ff', '#bd00ff', '#01ff1f', '#e3ff00'];
  10.     // random number assigned to a var
  11.     let num = Math.floor(Math.random() * colors.length);
  12.     // get the body element
  13.     const bodyEl = document.querySelector('body');
  14.     // style it
  15.     bodyEl.style.backgroundColor = colors[num];
  16. }
  17.  
  18. function appInit() {
  19.    
  20.     if(localStorage.getItem('clicksCount') == null) {
  21.         clicksCount = 0;
  22.         localStorage.setItem('clicksCount', clicksCount);
  23.     } else {
  24.         clicksCount = Number(localStorage.getItem('clicksCount'));
  25.     }
  26.  
  27.     if(localStorage.getItem('imagesValue') == null) {
  28.         imagesValue = 0;
  29.         localStorage.setItem('imagesValue', imagesValue);
  30.     } else {
  31.         imagesValue = Number(localStorage.getItem('imagesValue'));
  32.     }
  33.  
  34.     const span = document.getElementById('clicks');
  35.     span.innerText = clicksCount;
  36.  
  37.     setImgSrc(imagesValue, false);
  38.  
  39.     const img = document.querySelector('.image');
  40.  
  41.     img.addEventListener('click', () => {
  42.         changeBackground();
  43.         clicksPresentUI(clicksCounter());
  44.         checkNumberOfClicks(clicksCount);
  45.     });
  46. }
  47.  
  48. function setImgSrc(value, animationBool) {
  49.     const img = document.querySelector('.image');
  50.     img.src = `img/img-${value}.png`;
  51.  
  52.     if(animationBool) {
  53.         img.classList.add('shake');
  54.     } else {
  55.         img.classList.remove('shake');
  56.     }
  57. }
  58.  
  59. function clicksCounter() {
  60.     clicksCount++;
  61.     localStorage.setItem('clicksCount', clicksCount);
  62.     return clicksCount;
  63. }
  64.  
  65. function clicksPresentUI(clicks) {
  66.     const span = document.getElementById('clicks');
  67.     span.innerText = clicks;
  68. }
  69.  
  70. function checkNumberOfClicks(clicks) {
  71.     if(clicks / 100 == imagesValue + 1) {
  72.         imagesValue++;
  73.         localStorage.setItem('imagesValue', imagesValue);
  74.         setImgSrc(imagesValue, true);
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement