Guest User

Untitled

a guest
Sep 24th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.  
  5. <!----
  6. Explanations
  7. HTML code: this time we've used an oninput in each input field, and an onchange attribute on the <select> HTML drop down menu:
  8.  
  9. <div id="controls">
  10. <label for="nbBalls">Number of balls: </label>
  11. <input type="number" min=1 max=30
  12. value=10 id="nbBalls"
  13. oninput="changeNbBalls(this.value);">
  14. <p></p>
  15. <label for="nbBalls">Player color: </label>
  16. <input type="color" value='#FF0000'
  17. oninput="changePlayerColor(this.value);">
  18. <p></p>
  19. <label for="nbBalls">Color of ball to eat: </label>
  20. <select onchange="changeColorToEat(this.value);">
  21. <option value='red'>red</option>
  22. <option value='blue'>blue</option>
  23. <option value='green'>green</option>
  24. </select>
  25. <p></p>
  26.  
  27. <label for="nbBalls">Change ball speed: </label>
  28. - <input type="range" value='1'
  29. min=0.1 max=3 step=0.1
  30. oninput="changeBallSpeed(this.value);"> +
  31. <p></p>
  32. </div>
  33. JavaScript code: we've added some new variables in order to get closer to a real game with a goal, levels, game over menu and so on.
  34.  
  35. var initialNumberOfBalls; // number of balls at the beginning
  36. var globalSpeedMutiplier = 1; // will change when we move the speed
  37. // slider
  38. var colorToEat = 'red'; // color of the "good" balls to eat
  39. var wrongBallsEaten = goodBallsEaten = 0; //number of good/bad balls
  40. // eaten
  41. var numberOfGoodBalls; // number of good balls in the set
  42. And here are the callback functions called when you use the input fields:
  43.  
  44. function changeNbBalls(nb) {
  45. startGame(nb);
  46. }
  47.  
  48. function changeColorToEat(color) {
  49. colorToEat = color;
  50. startGame(initialNumberOfBalls);
  51. }
  52.  
  53. function changePlayerColor(color) {
  54. player.color = color;
  55. }
  56.  
  57. function changeBallSpeed(coef) {
  58. globalSpeedMutiplier = coef;
  59. }
  60. Each time we change the number of balls in the game, or the color of the balls you need to eat, we need to restart the game.
  61.  
  62. Here is the startGame(nb_balls) function:
  63.  
  64. function startGame(nb) {
  65. do {
  66. balls = createBalls(nb);
  67. initialNumberOfBalls = nb;
  68. numberOfGoodBalls = countNumberOfGoodBalls(balls, colorToEat);
  69. } while(numberOfGoodBalls === 0); // in case no good ball in the set
  70. wrongBallsEaten = goodBallsEaten = 0;
  71. }
  72. ... and here is the function that counts the number of good balls in the newly created set of balls:
  73.  
  74. function countNumberOfGoodBalls(balls, colorToEat) {
  75. var nb = 0;
  76. balls.forEach(function(b) {
  77. if(b.color === colorToEat) // we count the number of balls
  78. nb++; // of this color in the balls array
  79. });
  80. return nb; // return this number to the caller
  81. }
  82.  
  83. -->
  84.  
  85. <meta charset="utf-8">
  86. <title>Draw a monster in a canvas</title>
  87. </head>
  88. <body>
  89. <div id="controls">
  90. <label for="nbBalls">Number of balls: </label>
  91. <input type="number" min=1 max=30
  92. value=10 id="nbBalls"
  93. oninput="changeNbBalls(this.value);">
  94. <p></p>
  95. <label for="colorChooser">Player color: </label>
  96. <input type="color" value='#FF0000'
  97. oninput="changePlayerColor(this.value);" id="colorChooser">
  98. <p></p>
  99. <label for="selectColorOfBallToEat">Color of ball to eat: </label>
  100. <select onchange="changeColorToEat(this.value);" id="selectColorOfBallToEat">
  101. <option value='red'>red</option>
  102. <option value='blue'>blue</option>
  103. <option value='green'>green</option>
  104. </select>
  105. <p></p>
  106.  
  107. <label for="ballSpeed">Change ball speed: </label>
  108. - <input type="range" value='1'
  109. min=0.1 max=3 step=0.1
  110. oninput="changeBallSpeed(this.value);"
  111. id="ballSpeed"> +
  112. <p></p>
  113.  
  114. </div>
  115. <canvas id="myCanvas" width="400" height="400"></canvas>
  116. </body>
  117. </html>
Add Comment
Please, Sign In to add comment