script.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // The player
  2. const ducko = document.querySelector("#ducko-div");
  3. // The score
  4. const score = document.querySelector("#score-div");
  5. // The text
  6. const text_div = document.querySelector("#text-div");
  7. // The game div
  8. const game = document.querySelector("#game-div");
  9. window.spawn_interval;
  10. window.move_interval;
  11. let started = false;
  12. let first_start = true;
  13. // Jump
  14. function jump() {
  15. if (! ducko.classList.contains("jumping")) {
  16. ducko.classList.add("jumping");
  17. setTimeout(function() {
  18. ducko.classList.remove("jumping");
  19. }, 900);
  20. // To check if the player is high enough to dodge the cactus
  21. setTimeout(function () {
  22. ducko.classList.add("safe");
  23. setTimeout( function() {
  24. ducko.classList.remove("safe");
  25. }, 580);
  26. }, 140);
  27. }
  28. }
  29. document.body.addEventListener("keypress", key_pressed);
  30. document.body.addEventListener("click", key_pressed);
  31. document.body.addEventListener("touchstart", key_pressed);
  32. text_div.addEventListener("click", reset);
  33. text_div.addEventListener("touchstart", reset);
  34. // Spawn a cactus
  35. function cactusSpawn() {
  36. let cactus = document.createElement("div");
  37. cactus.classList.add("cactus");
  38. cactus.style.right = "0vw";
  39. game.appendChild(cactus);
  40. }
  41. // Move all cactus
  42. function cactusMove() {
  43. var all_cactus = document.querySelectorAll(".cactus");
  44. all_cactus.forEach(cactus => {
  45. let right = parseInt(cactus.style.right.slice(0, -2));
  46. if (right <= 99) {
  47. cactus.style.right = right + 1 + "vw";
  48. if ((! ducko.classList.contains("safe")) && 85 <= right && right <= 94) {
  49. clearInterval(window.move_interval);
  50. clearInterval(window.spawn_interval);
  51. text_div.innerHTML = "Your score : " + (parseInt(score.textContent) + 1) + "<br>Click here to restart";
  52. started = false;
  53. };
  54. } else {
  55. cactus.classList.remove("cactus");
  56. }
  57. });
  58. score.textContent = parseInt(score.textContent) + 1;
  59. }
  60. function cactusRandom() {
  61. let x = Math.random();
  62. if (x < 0.5) {
  63. cactusSpawn();
  64. }
  65. }
  66. function start() {
  67. window.spawn_interval = setInterval(cactusRandom, 1200);
  68. window.move_interval = setInterval(cactusMove, 40);
  69. started = true;
  70. first_start = false;
  71. text_div.innerHTML = "";
  72. }
  73. function reset() {
  74. if (!started && !first_start) {
  75. started = true;
  76. var all_cactus = document.querySelectorAll(".cactus");
  77. all_cactus.forEach(cactus => {
  78. cactus.classList.remove("cactus");
  79. });
  80. text_div.innerHTML = "";
  81. score.textContent = 0;
  82. window.spawn_interval = setInterval(cactusRandom, 1200);
  83. window.move_interval = setInterval(cactusMove, 40);
  84. }
  85. }
  86. function key_pressed() {
  87. if (started) {
  88. jump();
  89. } else if (first_start) {
  90. start();
  91. }
  92. }