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("button1", "click", function() {
- checkCorrect("button1");
- });
- onEvent("button2", "click", function() {
- checkCorrect("button2");
- });
- onEvent("button3", "click", function() {
- checkCorrect("button3");
- });
- onEvent("button4", "click", function() {
- checkCorrect("button4");
- });
- function setBoard() {
- var R = randomNumber(0, 235);
- var G = randomNumber(0, 235);
- var B = randomNumber(0, 235);
- var color = rgb(R, G, B);
- var colorDiff = rgb(R + 20, G + 20, B + 20);
- randButtonID = "button" + randomNumber(1, 4);
- setProperty("button1", "background-color", color);
- setProperty("button2", "background-color", color);
- setProperty("button3", "background-color", color);
- setProperty("button4", "background-color", color);
- setProperty(randButtonID, "background-color", colorDiff);
- }
- function checkCorrect(buttonID) {
- console.log("Checking: " + buttonID);
- if (buttonID == randButtonID) {
- updateScoreBy(1);
- console.log("You Got It Right!");
- } else {
- updateScoreBy(-3);
- console.log("You Got It Wrong!");
- }
- switchPlayer();
- checkGameOver();
- setBoard();
- }
- function switchPlayer() {
- if (currentPlayer == 1) {
- currentPlayer = 2;
- showElement("player2_highlight");
- hideElement("player1_highlight");
- } else {
- currentPlayer = 1;
- showElement("player1_highlight");
- hideElement("player2_highlight");
- }
- console.log("Current Player: " + currentPlayer);
- }
- function updateScoreBy(amt) {
- if (currentPlayer == 1) {
- p1Score = p1Score + amt;
- } else {
- p2Score = p2Score + amt;
- }
- setText("score1_label", p1Score);
- setText("score2_label", p2Score);
- }
- function checkGameOver() {
- if (p1Score > 20) {
- setScreen("gameOver_screen");
- showElement("player1Win_label");
- hideElement("player2Win_label");
- } else if (p2Score > 20) {
- setScreen("gameOver_screen");
- showElement("player2Win_label");
- hideElement("player1Win_label");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement