Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let order = [];
- let playerOrder = [];
- let flash;
- let turn;
- let good;
- let compTurn;
- let intervalId;
- let strict = false;
- let noise = true;
- let on = false;
- let win;
- let nivel_maximo = 0;
- let tempo_resposta = 800;
- let acrescimo_tempo = 150;
- const turnCounter = document.querySelector("#turn");
- const topLeft = document.querySelector("#topleft");
- const topRight = document.querySelector("#topright");
- const bottomLeft = document.querySelector("#bottomleft");
- const bottomRight = document.querySelector("#bottomright");
- const strictButton = document.querySelector("#strict");
- const onButton = document.querySelector("#on");
- const startButton = document.querySelector("#start");
- strictButton.addEventListener('click', (event) => {
- if (strictButton.checked == true) {
- strict = true;
- } else {
- strict = false;
- }
- });
- onButton.addEventListener('click', (event) => {
- if (onButton.checked == true) {
- on = true;
- turnCounter.innerHTML = "-";
- } else {
- on = false;
- turnCounter.innerHTML = "";
- clearColor();
- clearInterval(intervalId);
- }
- });
- startButton.addEventListener('click', (event) => {
- if (on || win) {
- nivel_maximo = 10;
- play();
- }
- });
- function play() {
- win = false;
- order = [];
- playerOrder = [];
- flash = 0;
- intervalId = 0;
- turn = 1;
- turnCounter.innerHTML = 1;
- good = true;
- for (var i = 0; i < 20; i++) {
- order.push(Math.floor(Math.random() * 4) + 1);
- }
- compTurn = true;
- intervalId = setInterval(gameTurn, tempo_resposta);
- }
- function gameTurn() {
- on = false;
- if (flash == turn) {
- clearInterval(intervalId);
- compTurn = false;
- clearColor();
- on = true;
- }
- if (compTurn) {
- clearColor();
- setTimeout(() => {
- if (order[flash] == 1) one();
- if (order[flash] == 2) two();
- if (order[flash] == 3) three();
- if (order[flash] == 4) four();
- flash++;
- }, 100);
- }
- }
- function one() {
- if (noise) {
- let audio = document.getElementById("clip1");
- audio.play();
- }
- noise = true;
- topLeft.style.backgroundColor = "lightgreen";
- }
- function two() {
- if (noise) {
- let audio = document.getElementById("clip2");
- audio.play();
- }
- noise = true;
- topRight.style.backgroundColor = "tomato";
- }
- function three() {
- if (noise) {
- let audio = document.getElementById("clip3");
- audio.play();
- }
- noise = true;
- bottomLeft.style.backgroundColor = "yellow";
- }
- function four() {
- if (noise) {
- let audio = document.getElementById("clip4");
- audio.play();
- }
- noise = true;
- bottomRight.style.backgroundColor = "lightskyblue";
- }
- function clearColor() {
- topLeft.style.backgroundColor = "darkgreen";
- topRight.style.backgroundColor = "darkred";
- bottomLeft.style.backgroundColor = "goldenrod";
- bottomRight.style.backgroundColor = "darkblue";
- }
- function flashColor() {
- topLeft.style.backgroundColor = "lightgreen";
- topRight.style.backgroundColor = "tomato";
- bottomLeft.style.backgroundColor = "yellow";
- bottomRight.style.backgroundColor = "lightskyblue";
- }
- document.addEventListener('keydown', (e) => {
- if (e.keyCode == 103) { //topleft '7'
- if (on) {
- playerOrder.push(1);
- check();
- one();
- if (!win) {
- setTimeout(() => {
- clearColor();
- }, 300);
- }
- }
- }
- if (e.keyCode == 105) { //topright
- if (on) {
- playerOrder.push(2);
- check();
- two();
- if (!win) {
- setTimeout(() => {
- clearColor();
- }, 300);
- }
- }
- }
- if (e.keyCode == 97) { //bottomleft
- if (on) {
- playerOrder.push(3);
- check();
- three();
- if (!win) {
- setTimeout(() => {
- clearColor();
- }, 300);
- }
- }
- }
- if (e.keyCode == 99) { //bottomright
- if (on) {
- playerOrder.push(4);
- check();
- four();
- if (!win) {
- setTimeout(() => {
- clearColor();
- }, 300);
- }
- }
- }
- })
- topLeft.addEventListener('click', (event) => {
- if (on) {
- playerOrder.push(1);
- check();
- one();
- if (!win) {
- setTimeout(() => {
- clearColor();
- }, 300);
- }
- }
- })
- topRight.addEventListener('click', (event) => {
- if (on) {
- playerOrder.push(2);
- check();
- two();
- if (!win) {
- setTimeout(() => {
- clearColor();
- }, 300);
- }
- }
- })
- bottomLeft.addEventListener('click', (event) => {
- if (on) {
- playerOrder.push(3);
- check();
- three();
- if (!win) {
- setTimeout(() => {
- clearColor();
- }, 300);
- }
- }
- })
- bottomRight.addEventListener('click', (event) => {
- if (on) {
- playerOrder.push(4);
- check();
- four();
- if (!win) {
- setTimeout(() => {
- clearColor();
- }, 300);
- }
- }
- })
- function check() {
- if (playerOrder[playerOrder.length - 1] !== order[playerOrder.length - 1])
- good = false;
- if (playerOrder.length == nivel_maximo && good) {
- winGame();
- }
- if (good == false) {
- flashColor();
- turnCounter.innerHTML = "NO!";
- gameOver();
- setTimeout(() => {
- turnCounter.innerHTML = turn;
- clearColor();
- if (strict) {
- play();
- } else {
- compTurn = true;
- flash = 0;
- playerOrder = [];
- good = true;
- intervalId = setInterval(gameTurn, tempo_resposta);
- }
- }, tempo_resposta);
- noise = false;
- }
- if (turn == playerOrder.length && good && !win) {
- turn++;
- if (tempo_resposta >= 400) {
- tempo_resposta -= acrescimo_tempo;
- }
- playerOrder = [];
- compTurn = true;
- flash = 0;
- turnCounter.innerHTML = turn;
- intervalId = setInterval(gameTurn, tempo_resposta);
- }
- }
- function winGame() {
- flashColor();
- turnCounter.innerHTML = "WIN!";
- on = false;
- win = true;
- }
- function gameOver() {
- alert("Game over")
- window.location.reload()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement