Advertisement
Guest User

Untitled

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