Advertisement
xOliverr

Untitled

Mar 14th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.74 KB | None | 0 0
  1. // Global Variables
  2. var DIRECTION = {
  3. IDLE: 0,
  4. UP: 1,
  5. DOWN: 2,
  6. LEFT: 3,
  7. RIGHT: 4
  8. };
  9.  
  10. var rounds = [5, 5, 3, 3, 2];
  11. var colors = ['#1abc9c', '#2ecc71', '#3498db', '#e74c3c', '#9b59b6'];
  12.  
  13. // The ball object (The cube that bounces back and forth)
  14. var Ball = {
  15. new: function (incrementedSpeed) {
  16. return {
  17. width: 18,
  18. height: 18,
  19. x: (this.canvas.width / 2) - 9,
  20. y: (this.canvas.height / 2) - 9,
  21. moveX: DIRECTION.IDLE,
  22. moveY: DIRECTION.IDLE,
  23. speed: incrementedSpeed || 9
  24. };
  25. }
  26. };
  27.  
  28. // The paddle object (The two lines that move up and down)
  29. var Paddle = {
  30. new: function (side) {
  31. return {
  32. width: 18,
  33. height: 70,
  34. x: side === 'left' ? 150 : this.canvas.width - 150,
  35. y: (this.canvas.height / 2) - 35,
  36. score: 0,
  37. move: DIRECTION.IDLE,
  38. speed: 10
  39. };
  40. }
  41. };
  42.  
  43. var Game = {
  44. initialize: function () {
  45. this.canvas = document.querySelector('canvas');
  46. this.context = this.canvas.getContext('2d');
  47.  
  48. this.canvas.width = 1400;
  49. this.canvas.height = 1000;
  50.  
  51. this.canvas.style.width = (this.canvas.width / 2) + 'px';
  52. this.canvas.style.height = (this.canvas.height / 2) + 'px';
  53.  
  54. this.player = Paddle.new.call(this, 'left');
  55. this.paddle = Paddle.new.call(this, 'right');
  56. this.ball = Ball.new.call(this);
  57.  
  58. this.paddle.speed = 8;
  59. this.running = this.over = false;
  60. this.turn = this.paddle;
  61. this.timer = this.round = 0;
  62. this.color = '#2c3e50';
  63.  
  64. Pong.menu();
  65. Pong.listen();
  66. },
  67.  
  68. endGameMenu: function (text) {
  69. // Change the canvas font size and color
  70. Pong.context.font = '50px Courier New';
  71. Pong.context.fillStyle = this.color;
  72.  
  73. // Draw the rectangle behind the 'Press any key to begin' text.
  74. Pong.context.fillRect(
  75. Pong.canvas.width / 2 - 350,
  76. Pong.canvas.height / 2 - 48,
  77. 700,
  78. 100
  79. );
  80.  
  81. // Change the canvas color;
  82. Pong.context.fillStyle = '#ffffff';
  83.  
  84. // Draw the end game menu text ('Game Over' and 'Winner')
  85. Pong.context.fillText(text,
  86. Pong.canvas.width / 2,
  87. Pong.canvas.height / 2 + 15
  88. );
  89.  
  90. setTimeout(function () {
  91. Pong = Object.assign({}, Game);
  92. Pong.initialize();
  93. }, 3000);
  94. },
  95.  
  96. menu: function () {
  97. // Draw all the Pong objects in their current state
  98. Pong.draw();
  99.  
  100. // Change the canvas font size and color
  101. this.context.font = '50px Courier New';
  102. this.context.fillStyle = this.color;
  103.  
  104. // Draw the rectangle behind the 'Press any key to begin' text.
  105. this.context.fillRect(
  106. this.canvas.width / 2 - 350,
  107. this.canvas.height / 2 - 48,
  108. 700,
  109. 100
  110. );
  111.  
  112. // Change the canvas color;
  113. this.context.fillStyle = '#ffffff';
  114.  
  115. // Draw the 'press any key to begin' text
  116. this.context.fillText('Press any key to begin',
  117. this.canvas.width / 2,
  118. this.canvas.height / 2 + 15
  119. );
  120. },
  121.  
  122. // Update all objects (move the player, paddle, ball, increment the score, etc.)
  123. update: function () {
  124. if (!this.over) {
  125. // If the ball collides with the bound limits - correct the x and y coords.
  126. if (this.ball.x <= 0) Pong._resetTurn.call(this, this.paddle, this.player);
  127. if (this.ball.x >= this.canvas.width - this.ball.width) Pong._resetTurn.call(this, this.player, this.paddle);
  128. if (this.ball.y <= 0) this.ball.moveY = DIRECTION.DOWN;
  129. if (this.ball.y >= this.canvas.height - this.ball.height) this.ball.moveY = DIRECTION.UP;
  130.  
  131. // Move player if they player.move value was updated by a keyboard event
  132. if (this.player.move === DIRECTION.UP) this.player.y -= this.player.speed;
  133. else if (this.player.move === DIRECTION.DOWN) this.player.y += this.player.speed;
  134.  
  135. // On new serve (start of each turn) move the ball to the correct side
  136. // and randomize the direction to add some challenge.
  137. if (Pong._turnDelayIsOver.call(this) && this.turn) {
  138. this.ball.moveX = this.turn === this.player ? DIRECTION.LEFT : DIRECTION.RIGHT;
  139. this.ball.moveY = [DIRECTION.UP, DIRECTION.DOWN][Math.round(Math.random())];
  140. this.ball.y = Math.floor(Math.random() * this.canvas.height - 200) + 200;
  141. this.turn = null;
  142. }
  143.  
  144. // If the player collides with the bound limits, update the x and y coords.
  145. if (this.player.y <= 0) this.player.y = 0;
  146. else if (this.player.y >= (this.canvas.height - this.player.height)) this.player.y = (this.canvas.height - this.player.height);
  147.  
  148. // Move ball in intended direction based on moveY and moveX values
  149. if (this.ball.moveY === DIRECTION.UP) this.ball.y -= (this.ball.speed / 1.5);
  150. else if (this.ball.moveY === DIRECTION.DOWN) this.ball.y += (this.ball.speed / 1.5);
  151. if (this.ball.moveX === DIRECTION.LEFT) this.ball.x -= this.ball.speed;
  152. else if (this.ball.moveX === DIRECTION.RIGHT) this.ball.x += this.ball.speed;
  153.  
  154. // Handle paddle (AI) UP and DOWN movement
  155. if (this.paddle.y > this.ball.y - (this.paddle.height / 2)) {
  156. if (this.ball.moveX === DIRECTION.RIGHT) this.paddle.y -= this.paddle.speed / 1.5;
  157. else this.paddle.y -= this.paddle.speed / 4;
  158. }
  159. if (this.paddle.y < this.ball.y - (this.paddle.height / 2)) {
  160. if (this.ball.moveX === DIRECTION.RIGHT) this.paddle.y += this.paddle.speed / 1.5;
  161. else this.paddle.y += this.paddle.speed / 4;
  162. }
  163.  
  164. // Handle paddle (AI) wall collision
  165. if (this.paddle.y >= this.canvas.height - this.paddle.height) this.paddle.y = this.canvas.height - this.paddle.height;
  166. else if (this.paddle.y <= 0) this.paddle.y = 0;
  167.  
  168. // Handle Player-Ball collisions
  169. if (this.ball.x - this.ball.width <= this.player.x && this.ball.x >= this.player.x - this.player.width) {
  170. if (this.ball.y <= this.player.y + this.player.height && this.ball.y + this.ball.height >= this.player.y) {
  171. this.ball.x = (this.player.x + this.ball.width);
  172. this.ball.moveX = DIRECTION.RIGHT;
  173.  
  174. }
  175. }
  176.  
  177. // Handle paddle-ball collision
  178. if (this.ball.x - this.ball.width <= this.paddle.x && this.ball.x >= this.paddle.x - this.paddle.width) {
  179. if (this.ball.y <= this.paddle.y + this.paddle.height && this.ball.y + this.ball.height >= this.paddle.y) {
  180. this.ball.x = (this.paddle.x - this.ball.width);
  181. this.ball.moveX = DIRECTION.LEFT;
  182. }
  183. }
  184. }
  185.  
  186. // Handle the end of round transition
  187. // Check to see if the player won the round.
  188. if (this.player.score === rounds[this.round]) {
  189. // Check to see if there are any more rounds/levels left and display the victory screen if
  190. // there are not.
  191. if (!rounds[this.round + 1]) {
  192. this.over = true;
  193. setTimeout(function () { Pong.endGameMenu('Winner!'); }, 1000);
  194. } else {
  195. // If there is another round, reset all the values and increment the round number.
  196. this.color = this._generateRoundColor();
  197. this.player.score = this.paddle.score = 0;
  198. this.player.speed += 0.5;
  199. this.paddle.speed += 1;
  200. this.ball.speed += 1;
  201. this.round += 1;
  202.  
  203. }
  204. }
  205. // Check to see if the paddle/AI has won the round.
  206. else if (this.paddle.score === rounds[this.round]) {
  207. this.over = true;
  208. setTimeout(function () { Pong.endGameMenu('Game Over!'); }, 1000);
  209. }
  210. },
  211.  
  212. // Draw the objects to the canvas element
  213. draw: function () {
  214. // Clear the Canvas
  215. this.context.clearRect(
  216. 0,
  217. 0,
  218. this.canvas.width,
  219. this.canvas.height
  220. );
  221.  
  222. // Set the fill style to black
  223. this.context.fillStyle = this.color;
  224.  
  225. // Draw the background
  226. this.context.fillRect(
  227. 0,
  228. 0,
  229. this.canvas.width,
  230. this.canvas.height
  231. );
  232.  
  233. // Set the fill style to white (For the paddles and the ball)
  234. this.context.fillStyle = '#ffffff';
  235.  
  236. // Draw the Player
  237. this.context.fillRect(
  238. this.player.x,
  239. this.player.y,
  240. this.player.width,
  241. this.player.height
  242. );
  243.  
  244. // Draw the Paddle
  245. this.context.fillRect(
  246. this.paddle.x,
  247. this.paddle.y,
  248. this.paddle.width,
  249. this.paddle.height
  250. );
  251.  
  252. // Draw the Ball
  253. if (Pong._turnDelayIsOver.call(this)) {
  254. this.context.fillRect(
  255. this.ball.x,
  256. this.ball.y,
  257. this.ball.width,
  258. this.ball.height
  259. );
  260. }
  261.  
  262. // Draw the net (Line in the middle)
  263. this.context.beginPath();
  264. this.context.setLineDash([7, 15]);
  265. this.context.moveTo((this.canvas.width / 2), this.canvas.height - 140);
  266. this.context.lineTo((this.canvas.width / 2), 140);
  267. this.context.lineWidth = 10;
  268. this.context.strokeStyle = '#ffffff';
  269. this.context.stroke();
  270.  
  271. // Set the default canvas font and align it to the center
  272. this.context.font = '100px Courier New';
  273. this.context.textAlign = 'center';
  274.  
  275. // Draw the players score (left)
  276. this.context.fillText(
  277. this.player.score.toString(),
  278. (this.canvas.width / 2) - 300,
  279. 200
  280. );
  281.  
  282. // Draw the paddles score (right)
  283. this.context.fillText(
  284. this.paddle.score.toString(),
  285. (this.canvas.width / 2) + 300,
  286. 200
  287. );
  288.  
  289. // Change the font size for the center score text
  290. this.context.font = '30px Courier New';
  291.  
  292. // Draw the winning score (center)
  293. this.context.fillText(
  294. 'Round ' + (Pong.round + 1),
  295. (this.canvas.width / 2),
  296. 35
  297. );
  298.  
  299. // Change the font size for the center score value
  300. this.context.font = '40px Courier';
  301.  
  302. // Draw the current round number
  303. this.context.fillText(
  304. rounds[Pong.round] ? rounds[Pong.round] : rounds[Pong.round - 1],
  305. (this.canvas.width / 2),
  306. 100
  307. );
  308. },
  309.  
  310. loop: function () {
  311. Pong.update();
  312. Pong.draw();
  313.  
  314. // If the game is not over, draw the next frame.
  315. if (!Pong.over) requestAnimationFrame(Pong.loop);
  316. },
  317.  
  318. listen: function () {
  319. document.addEventListener('keydown', function (key) {
  320. // Handle the 'Press any key to begin' function and start the game.
  321. if (Pong.running === false) {
  322. Pong.running = true;
  323. window.requestAnimationFrame(Pong.loop);
  324. }
  325.  
  326. // Handle up arrow and w key events
  327. if (key.keyCode === 38 || key.keyCode === 87) Pong.player.move = DIRECTION.UP;
  328.  
  329. // Handle down arrow and s key events
  330. if (key.keyCode === 40 || key.keyCode === 83) Pong.player.move = DIRECTION.DOWN;
  331. });
  332.  
  333. // Stop the player from moving when there are no keys being pressed.
  334. document.addEventListener('keyup', function (key) { Pong.player.move = DIRECTION.IDLE; });
  335. },
  336.  
  337. // Reset the ball location, the player turns and set a delay before the next round begins.
  338. _resetTurn: function(victor, loser) {
  339. this.ball = Ball.new.call(this, this.ball.speed);
  340. this.turn = loser;
  341. this.timer = (new Date()).getTime();
  342.  
  343. victor.score++;
  344. beep2.play();
  345. },
  346.  
  347. // Wait for a delay to have passed after each turn.
  348. _turnDelayIsOver: function() {
  349. return ((new Date()).getTime() - this.timer >= 1000);
  350. },
  351.  
  352. // Select a random color as the background of each level/round.
  353. _generateRoundColor: function () {
  354. var newColor = colors[Math.floor(Math.random() * colors.length)];
  355. if (newColor === this.color) return Pong._generateRoundColor();
  356. return newColor;
  357. }
  358. };
  359.  
  360. var Pong = Object.assign({}, Game);
  361. Pong.initialize();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement