Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. var randButtonId;
  2. var currentPlayer = 1;
  3. var p1Score = 0;
  4. var p2Score = 0;
  5.  
  6. setBoard();
  7.  
  8. onEvent("button4", "click", function () {
  9. checkCorrect("button4");
  10. });
  11.  
  12. onEvent("button3", "click", function () {
  13. checkCorrect("button3");
  14. });
  15.  
  16. onEvent("button2", "click", function () {
  17. checkCorrect("button2");
  18. });
  19.  
  20. onEvent("button1", "click", function () {
  21. checkCorrect("button1");
  22. });
  23.  
  24. function checkGameOver() {
  25. if ((p1Score <= -9) || (p2Score > 5)) {
  26. setScreen("gameOver_screen");
  27. showElement("player2Win_label");
  28. } else if ((p2Score <= -9) || (p1Score > 5)) {
  29. setScreen("gameOver_screen");
  30. showElement("player1Win_label");
  31. }
  32. }
  33.  
  34. function switchPlayer() {
  35. if (currentPlayer == 1) {
  36. showElement("player2_highlight");
  37. hideElement("player1_highlight");
  38. currentPlayer = 2;
  39. } else {
  40. showElement("player1_highlight");
  41. hideElement("player2_highlight");
  42. currentPlayer = 1;
  43. }
  44. console.log("Current player is: " + currentPlayer);
  45. }
  46.  
  47. function updateScoreBy(amt) {
  48. if (currentPlayer == 1) {
  49. p1Score += amt;
  50. } else {
  51. p2Score += amt;
  52. }
  53. console.log("P1 score: " + p1Score);
  54. console.log("P2 score: " + p2Score);
  55. setText("score1_label", p1Score);
  56. setText("score2_label", p2Score);
  57. }
  58.  
  59. function checkCorrect(buttonId) {
  60. console.log("checking: " + buttonId);
  61. if (buttonId == randButtonId) {
  62. console.log("You got it right!");
  63. updateScoreBy(1);
  64. } else {
  65. console.log ("WRONG");
  66. updateScoreBy(-3);
  67. }
  68. checkGameOver();
  69. setBoard();
  70. switchPlayer();
  71. }
  72.  
  73. function setBoard() {
  74. var R = randomNumber(0,235);
  75. var G = randomNumber(0,235);
  76. var B = randomNumber(0,235);
  77. var color = rgb(R, G, B);
  78. for (var i = 1; i < 5; i++) {
  79. setProperty("button" + i, "background-color", color);
  80. }
  81. var diffColor = rgb(R + 20, G + 20, B + 20);
  82. randButtonId = "button" + randomNumber(1, 4);
  83. setProperty(randButtonId, "background-color", diffColor);
  84. console.log("correct button is: " + randButtonId);
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement