Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. let order = [];
  2. let playerOrder = [];
  3. let flash;
  4. let turn;
  5. let good;
  6. let compTurn;
  7. let intervalId;
  8. let strict = false;
  9. let noise = true;
  10. let on = false;
  11. let win;
  12.  
  13. let nivel_maximo = 0;
  14. let tempo_resposta = 800;
  15. let acrescimo_tempo = 150;
  16.  
  17. const turnCounter = document.querySelector("#turn");
  18. const topLeft = document.querySelector("#topleft");
  19. const topRight = document.querySelector("#topright");
  20. const bottomLeft = document.querySelector("#bottomleft");
  21. const bottomRight = document.querySelector("#bottomright");
  22. const strictButton = document.querySelector("#strict");
  23. const onButton = document.querySelector("#on");
  24. const startButton = document.querySelector("#start");
  25.  
  26. strictButton.addEventListener('click', (event) => {
  27. if (strictButton.checked == true) {
  28. strict = true;
  29. } else {
  30. strict = false;
  31. }
  32. });
  33.  
  34. onButton.addEventListener('click', (event) => {
  35. if (onButton.checked == true) {
  36. on = true;
  37. turnCounter.innerHTML = "-";
  38. } else {
  39. on = false;
  40. turnCounter.innerHTML = "";
  41. clearColor();
  42. clearInterval(intervalId);
  43. }
  44. });
  45.  
  46. startButton.addEventListener('click', (event) => {
  47. if (on || win) {
  48. nivel_maximo = 10;
  49. play();
  50. }
  51. });
  52.  
  53. function play() {
  54. win = false;
  55. order = [];
  56. playerOrder = [];
  57. flash = 0;
  58. intervalId = 0;
  59. turn = 1;
  60. turnCounter.innerHTML = 1;
  61. good = true;
  62. for (var i = 0; i < 20; i++) {
  63. order.push(Math.floor(Math.random() * 4) + 1);
  64. }
  65. compTurn = true;
  66.  
  67. intervalId = setInterval(gameTurn, tempo_resposta);
  68. }
  69.  
  70. function gameTurn() {
  71. on = false;
  72.  
  73. if (flash == turn) {
  74. clearInterval(intervalId);
  75. compTurn = false;
  76. clearColor();
  77. on = true;
  78. }
  79.  
  80. if (compTurn) {
  81. clearColor();
  82. setTimeout(() => {
  83. if (order[flash] == 1) one();
  84. if (order[flash] == 2) two();
  85. if (order[flash] == 3) three();
  86. if (order[flash] == 4) four();
  87. flash++;
  88. }, 100);
  89. }
  90. }
  91.  
  92. function one() {
  93. if (noise) {
  94. let audio = document.getElementById("clip1");
  95. audio.play();
  96. }
  97. noise = true;
  98. topLeft.style.backgroundColor = "lightgreen";
  99. }
  100.  
  101. function two() {
  102. if (noise) {
  103. let audio = document.getElementById("clip2");
  104. audio.play();
  105. }
  106. noise = true;
  107. topRight.style.backgroundColor = "tomato";
  108. }
  109.  
  110. function three() {
  111. if (noise) {
  112. let audio = document.getElementById("clip3");
  113. audio.play();
  114. }
  115. noise = true;
  116. bottomLeft.style.backgroundColor = "yellow";
  117. }
  118.  
  119. function four() {
  120. if (noise) {
  121. let audio = document.getElementById("clip4");
  122. audio.play();
  123. }
  124. noise = true;
  125. bottomRight.style.backgroundColor = "lightskyblue";
  126. }
  127.  
  128. function clearColor() {
  129. topLeft.style.backgroundColor = "darkgreen";
  130. topRight.style.backgroundColor = "darkred";
  131. bottomLeft.style.backgroundColor = "goldenrod";
  132. bottomRight.style.backgroundColor = "darkblue";
  133. }
  134.  
  135. function flashColor() {
  136. topLeft.style.backgroundColor = "lightgreen";
  137. topRight.style.backgroundColor = "tomato";
  138. bottomLeft.style.backgroundColor = "yellow";
  139. bottomRight.style.backgroundColor = "lightskyblue";
  140. }
  141.  
  142. document.addEventListener('keydown', (e) => {
  143. if (e.keyCode == 103) { //topleft '7'
  144. if (on) {
  145. playerOrder.push(1);
  146. check();
  147. one();
  148. if (!win) {
  149. setTimeout(() => {
  150. clearColor();
  151. }, 300);
  152. }
  153. }
  154. }
  155.  
  156. if (e.keyCode == 105) { //topright
  157. if (on) {
  158. playerOrder.push(2);
  159. check();
  160. two();
  161. if (!win) {
  162. setTimeout(() => {
  163. clearColor();
  164. }, 300);
  165. }
  166. }
  167. }
  168.  
  169. if (e.keyCode == 97) { //bottomleft
  170. if (on) {
  171. playerOrder.push(3);
  172. check();
  173. three();
  174. if (!win) {
  175. setTimeout(() => {
  176. clearColor();
  177. }, 300);
  178. }
  179. }
  180. }
  181.  
  182. if (e.keyCode == 99) { //bottomright
  183. if (on) {
  184. playerOrder.push(4);
  185. check();
  186. four();
  187. if (!win) {
  188. setTimeout(() => {
  189. clearColor();
  190. }, 300);
  191. }
  192. }
  193. }
  194.  
  195. })
  196.  
  197. topLeft.addEventListener('click', (event) => {
  198. if (on) {
  199. playerOrder.push(1);
  200. check();
  201. one();
  202. if (!win) {
  203. setTimeout(() => {
  204. clearColor();
  205. }, 300);
  206. }
  207. }
  208. })
  209.  
  210. topRight.addEventListener('click', (event) => {
  211. if (on) {
  212. playerOrder.push(2);
  213. check();
  214. two();
  215. if (!win) {
  216. setTimeout(() => {
  217. clearColor();
  218. }, 300);
  219. }
  220. }
  221. })
  222.  
  223. bottomLeft.addEventListener('click', (event) => {
  224. if (on) {
  225. playerOrder.push(3);
  226. check();
  227. three();
  228. if (!win) {
  229. setTimeout(() => {
  230. clearColor();
  231. }, 300);
  232. }
  233. }
  234. })
  235.  
  236. bottomRight.addEventListener('click', (event) => {
  237. if (on) {
  238. playerOrder.push(4);
  239. check();
  240. four();
  241. if (!win) {
  242. setTimeout(() => {
  243. clearColor();
  244. }, 300);
  245. }
  246. }
  247. })
  248.  
  249. function check() {
  250. if (playerOrder[playerOrder.length - 1] !== order[playerOrder.length - 1])
  251. good = false;
  252.  
  253.  
  254. if (playerOrder.length == nivel_maximo && good) {
  255. winGame();
  256. }
  257.  
  258. if (good == false) {
  259. flashColor();
  260. turnCounter.innerHTML = "NO!";
  261. gameOver();
  262. setTimeout(() => {
  263. turnCounter.innerHTML = turn;
  264. clearColor();
  265.  
  266. if (strict) {
  267. play();
  268. } else {
  269. compTurn = true;
  270. flash = 0;
  271. playerOrder = [];
  272. good = true;
  273. intervalId = setInterval(gameTurn, tempo_resposta);
  274. }
  275. }, tempo_resposta);
  276.  
  277. noise = false;
  278. }
  279.  
  280. if (turn == playerOrder.length && good && !win) {
  281. turn++;
  282. if (tempo_resposta >= 400) {
  283. tempo_resposta -= acrescimo_tempo;
  284. }
  285. playerOrder = [];
  286. compTurn = true;
  287. flash = 0;
  288. turnCounter.innerHTML = turn;
  289. intervalId = setInterval(gameTurn, tempo_resposta);
  290. }
  291.  
  292. }
  293.  
  294. function winGame() {
  295. flashColor();
  296. turnCounter.innerHTML = "WIN!";
  297. on = false;
  298. win = true;
  299. }
  300.  
  301. function gameOver() {
  302. alert("Game over")
  303. window.location.reload()
  304.  
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement