Advertisement
Guest User

Untitled

a guest
Dec 11th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. (() => {
  2.  
  3. let canvas = document.getElementById("game");
  4. let game = canvas.getContext("2d");
  5. let lastTimestamp = 0;
  6.  
  7. const FRAME_RATE = 60;
  8. const FRAME_DURATION = 1000 / FRAME_RATE;
  9.  
  10.  
  11. let fallers = [];
  12. let score = 0;
  13.  
  14.  
  15. let colourValues = ["red", "blue"]
  16. colourValues = {
  17. red: "#ff0000",
  18. blue: "#0000ff"
  19. };
  20.  
  21. let colour = colourValues[Math.floor(Math.random()*colourValues.length)];
  22.  
  23. //ignore
  24. //let scoreCount = document.getElementById("scoreCount”);
  25.  
  26. let showScore = function(){
  27. scoreCount.innerHTML = "Your score is " + score;
  28. };
  29.  
  30.  
  31. let addScore = function(pointValue){
  32. score += pointValue;
  33. showScore();
  34. };
  35.  
  36. let fallerIn = function(inside){
  37. inside.captured = true;
  38. addScore(inside.pointValue);
  39. };
  40.  
  41. const DEFAULT_DESCENT = 0.0001; // This is per millisecond.
  42. let Faller = function (x, y, width, height, dx = 0, dy = 0, ax = 0, ay = DEFAULT_DESCENT) {
  43. this.x = x;
  44. this.y = y;
  45. this.width = width;
  46. this.height = height;
  47.  
  48. this.captured = false;
  49. this.pointValue = 5;
  50. this.colour;
  51.  
  52. // Velocity.
  53. this.dx = dx;
  54. this.dy = dy;
  55.  
  56. // Acceleration.
  57. this.ax = ax;
  58. this.ay = ay;
  59. };
  60.  
  61. Faller.prototype.draw = function () {
  62. game.fillStyle = colour;
  63. game.fillRect(this.x, this.y, this.width, this.height);
  64. };
  65.  
  66. Faller.prototype.move = function (millisecondsElapsed) {
  67. this.x += this.dx * millisecondsElapsed;
  68. this.y += this.dy * millisecondsElapsed;
  69.  
  70. this.dx += this.ax * millisecondsElapsed;
  71. this.dy += this.ay * millisecondsElapsed;
  72. };
  73.  
  74.  
  75. const DEFAULT_PLAYER_WIDTH = 65;
  76. const DEFAULT_PLAYER_HEIGHT = 45;
  77. const DEFAULT_PLAYER_Y = canvas.height - DEFAULT_PLAYER_HEIGHT;
  78. let Player = function (x, y = DEFAULT_PLAYER_Y, width = DEFAULT_PLAYER_WIDTH, height = DEFAULT_PLAYER_HEIGHT) {
  79. this.x = x;
  80. this.y = y;
  81. this.width = width;
  82. this.height = height;
  83. };
  84.  
  85. Player.prototype.draw = function () {
  86. let grd = game.createLinearGradient(0, 200, 200, 0);
  87. grd.addColorStop(0, "black");
  88. grd.addColorStop(0.5, "red");
  89. grd.addColorStop(1, "white");
  90. game.fillStyle = grd;
  91. game.fillRect(this.x, this.y, this.width, this.height);
  92. game.fill();
  93. };
  94.  
  95. let player = new Player(canvas.width / 2);
  96.  
  97. let draw = (millisecondsElapsed) => {
  98. game.clearRect(0, 0, canvas.width, canvas.height);
  99.  
  100. fallers.forEach((faller) => {
  101. faller.draw();
  102. faller.move(millisecondsElapsed);
  103. if (!(faller.captured)&&
  104. faller.y + faller.height > canvas.height &&
  105. faller.x + faller.width < player.x + player.width &&
  106. faller.x > player.x){
  107. fallerIn(faller);
  108. }
  109. });
  110.  
  111. player.draw();
  112.  
  113. fallers = fallers.filter((faller) => {
  114. return faller.y < canvas.height;
  115. });
  116.  
  117. };
  118.  
  119. const MIN_WIDTH = 10;
  120. const WIDTH_RANGE = 20;
  121. const MIN_HEIGHT = 10;
  122. const HEIGHT_RANGE = 20;
  123. const MILLISECONDS_BETWEEN_FALLERS = 750;
  124.  
  125. let fallerGenerator;
  126. let startFallerGenerator = () => {
  127. fallerGenerator = setInterval(() => {
  128.  
  129. let fallerWidth = Math.floor(Math.random() * WIDTH_RANGE) + MIN_WIDTH;
  130. fallers.push(new Faller(
  131. Math.floor(Math.random() * (canvas.width - fallerWidth)), 0,
  132. fallerWidth, Math.floor(Math.random() * HEIGHT_RANGE) + MIN_HEIGHT
  133. ));
  134. }, MILLISECONDS_BETWEEN_FALLERS);
  135. };
  136.  
  137. let stopFallerGenerator = () => clearInterval(fallerGenerator);
  138.  
  139. let setPlayerPositionBasedOnMouse = (event) => {
  140. player.x = event.clientX / document.body.clientWidth * canvas.width;
  141. };
  142.  
  143. document.body.addEventListener("mouseenter", setPlayerPositionBasedOnMouse);
  144. document.body.addEventListener("mousemove", setPlayerPositionBasedOnMouse);
  145.  
  146. let running = false;
  147. let nextFrame = (timestamp) => {
  148. if (!lastTimestamp) {
  149. lastTimestamp = timestamp;
  150. }
  151.  
  152. if (timestamp - lastTimestamp < FRAME_DURATION) {
  153. if (running) {
  154. window.requestAnimationFrame(nextFrame);
  155. }
  156.  
  157. return;
  158. }
  159.  
  160. draw(timestamp - lastTimestamp);
  161.  
  162. lastTimestamp = timestamp;
  163. if (running) {
  164. window.requestAnimationFrame(nextFrame);
  165. }
  166. };
  167.  
  168. document.getElementById("start-button").addEventListener("click", () => {
  169. running = true;
  170. lastTimestamp = 0;
  171. startFallerGenerator();
  172. window.requestAnimationFrame(nextFrame);
  173. });
  174.  
  175. document.getElementById("stop-button").addEventListener("click", () => {
  176. stopFallerGenerator();
  177. running = false;
  178. });
  179. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement