Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var randButtonId = "button" + randomNumber(1,4);
- var currentPlayer = 1;
- var p1Score = 0;
- var p2Score = 0;
- onEvent("start_btn", "click", function() {
- setScreen("gamePlay_screen");
- });
- setBoard();
- onEvent("playagainbtn", "click", function() {
- setScreen("gamePlay_screen");
- setBoard();
- p1Score = 0;
- p2Score = 0;
- currentPlayer = 1;
- showElement("player1_highlight");
- hideElement("player2_highlight");
- hideElement("player2Win_label");
- hideElement("player1Win_label");
- setText("score1_label", p1Score);
- setText("score2_label", p2Score);
- });
- function checkGameover() {
- if (p1Score >= 5) {
- setScreen("gameOver_screen");
- showElement("player1Win_label");
- } else if ((p2Score >= 5)) {
- setScreen("gameOver_screen");
- showElement("player2Win_label");
- }
- }
- function updateScoreBy(amt){
- if (currentPlayer == 1){
- p1Score = p1Score+ amt;
- } else {
- p2Score = p2Score + amt;
- }
- }
- function switchPlayer() {
- if (currentPlayer == 1) {
- hideElement("player1_highlight");
- currentPlayer = 2;
- showElement("player2_highlight");
- }else{
- hideElement("player2_highlight");
- currentPlayer = 1;
- showElement("player1_highlight");
- }
- console.log("current Player is : "+currentPlayer);
- }
- function setBoard() {
- var R = randomNumber(0,235);
- var G = randomNumber(0,235);
- var B = randomNumber(0,235);
- var color = "rgb("+R+","+G+","+B+")";
- setProperty("button1", "background-color", color);
- setProperty("button2", "background-color", color);
- setProperty("button3", "background-color", color);
- setProperty("button4", "background-color", color);
- R = R +15;
- G = G +15;
- B = B+15;
- var diffcolor =("rgb("+R)+","+G+","+B+")";
- randButtonId = "button"+randomNumber(1,4);
- setProperty(randButtonId, "background-color", diffcolor);
- console.log("Correct one is: "+randButtonId);
- }
- function checkCorrect(buttonId) {
- console.log("Checking: "+buttonId);
- if (buttonId == randButtonId) {
- console.log("CORRECT");
- updateScoreBy(1);
- } else {
- updateScoreBy(-3);
- console.log("WRONG");
- }
- console.log("P1 Score : "+p1Score);
- console.log("P2 Score : "+p2Score);
- setText("score1_label",p1Score);
- setText("score2_label",p2Score);
- setBoard();
- switchPlayer();
- checkGameover();
- }
- onEvent("button1", "click", function() {
- checkCorrect("button1");
- });
- onEvent("button2", "click", function() {
- checkCorrect("button2");
- });
- onEvent("button3", "click", function() {
- checkCorrect("button3");
- });
- onEvent("button4", "click", function() {
- checkCorrect("button4");
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement