Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var randButtonId;
- var currentPlayer = 1;
- var p1Score = 0;
- var p2Score = 0;
- setBoard();
- onEvent("button4", "click", function () {
- checkCorrect("button4");
- });
- onEvent("button3", "click", function () {
- checkCorrect("button3");
- });
- onEvent("button2", "click", function () {
- checkCorrect("button2");
- });
- onEvent("button1", "click", function () {
- checkCorrect("button1");
- });
- function checkGameOver() {
- if ((p1Score <= -9) || (p2Score > 5)) {
- setScreen("gameOver_screen");
- showElement("player2Win_label");
- } else if ((p2Score <= -9) || (p1Score > 5)) {
- setScreen("gameOver_screen");
- showElement("player1Win_label");
- }
- }
- function switchPlayer() {
- if (currentPlayer == 1) {
- showElement("player2_highlight");
- hideElement("player1_highlight");
- currentPlayer = 2;
- } else {
- showElement("player1_highlight");
- hideElement("player2_highlight");
- currentPlayer = 1;
- }
- console.log("Current player is: " + currentPlayer);
- }
- function updateScoreBy(amt) {
- if (currentPlayer == 1) {
- p1Score += amt;
- } else {
- p2Score += amt;
- }
- console.log("P1 score: " + p1Score);
- console.log("P2 score: " + p2Score);
- setText("score1_label", p1Score);
- setText("score2_label", p2Score);
- }
- function checkCorrect(buttonId) {
- console.log("checking: " + buttonId);
- if (buttonId == randButtonId) {
- console.log("You got it right!");
- updateScoreBy(1);
- } else {
- console.log ("WRONG");
- updateScoreBy(-3);
- }
- checkGameOver();
- setBoard();
- switchPlayer();
- }
- function setBoard() {
- var R = randomNumber(0,235);
- var G = randomNumber(0,235);
- var B = randomNumber(0,235);
- var color = rgb(R, G, B);
- for (var i = 1; i < 5; i++) {
- setProperty("button" + i, "background-color", color);
- }
- var diffColor = rgb(R + 20, G + 20, B + 20);
- randButtonId = "button" + randomNumber(1, 4);
- setProperty(randButtonId, "background-color", diffColor);
- console.log("correct button is: " + randButtonId);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement