Advertisement
xOliverr

Untitled

Mar 20th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.80 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. beep1.play();
  175. }
  176. }
  177.  
  178. // Handle paddle-ball collision
  179. if (this.ball.x - this.ball.width <= this.paddle.x && this.ball.x >= this.paddle.x - this.paddle.width) {
  180. if (this.ball.y <= this.paddle.y + this.paddle.height && this.ball.y + this.ball.height >= this.paddle.y) {
  181. this.ball.x = (this.paddle.x - this.ball.width);
  182. this.ball.moveX = DIRECTION.LEFT;
  183.  
  184. beep1.play();
  185. }
  186. }
  187. }
  188.  
  189. // Handle the end of round transition
  190. // Check to see if the player won the round.
  191. if (this.player.score === rounds[this.round]) {
  192. // Check to see if there are any more rounds/levels left and display the victory screen if
  193. // there are not.
  194. if (!rounds[this.round + 1]) {
  195. this.over = true;
  196. setTimeout(function () { Pong.endGameMenu('Winner!'); }, 1000);
  197. } else {
  198. // If there is another round, reset all the values and increment the round number.
  199. this.color = this._generateRoundColor();
  200. this.player.score = this.paddle.score = 0;
  201. this.player.speed += 0.5;
  202. this.paddle.speed += 1;
  203. this.ball.speed += 1;
  204. this.round += 1;
  205.  
  206. beep3.play();
  207. }
  208. }
  209. // Check to see if the paddle/AI has won the round.
  210. else if (this.paddle.score === rounds[this.round]) {
  211. this.over = true;
  212. setTimeout(function () { Pong.endGameMenu('Game Over!'); }, 1000);
  213. }
  214. },
  215.  
  216. // Draw the objects to the canvas element
  217. draw: function () {
  218. // Clear the Canvas
  219. this.context.clearRect(
  220. 0,
  221. 0,
  222. this.canvas.width,
  223. this.canvas.height
  224. );
  225.  
  226. // Set the fill style to black
  227. this.context.fillStyle = this.color;
  228.  
  229. // Draw the background
  230. this.context.fillRect(
  231. 0,
  232. 0,
  233. this.canvas.width,
  234. this.canvas.height
  235. );
  236.  
  237. // Set the fill style to white (For the paddles and the ball)
  238. this.context.fillStyle = '#ffffff';
  239.  
  240. // Draw the Player
  241. this.context.fillRect(
  242. this.player.x,
  243. this.player.y,
  244. this.player.width,
  245. this.player.height
  246. );
  247.  
  248. // Draw the Paddle
  249. this.context.fillRect(
  250. this.paddle.x,
  251. this.paddle.y,
  252. this.paddle.width,
  253. this.paddle.height
  254. );
  255.  
  256. // Draw the Ball
  257. if (Pong._turnDelayIsOver.call(this)) {
  258. this.context.fillRect(
  259. this.ball.x,
  260. this.ball.y,
  261. this.ball.width,
  262. this.ball.height
  263. );
  264. }
  265.  
  266. // Draw the net (Line in the middle)
  267. this.context.beginPath();
  268. this.context.setLineDash([7, 15]);
  269. this.context.moveTo((this.canvas.width / 2), this.canvas.height - 140);
  270. this.context.lineTo((this.canvas.width / 2), 140);
  271. this.context.lineWidth = 10;
  272. this.context.strokeStyle = '#ffffff';
  273. this.context.stroke();
  274.  
  275. // Set the default canvas font and align it to the center
  276. this.context.font = '100px Courier New';
  277. this.context.textAlign = 'center';
  278.  
  279. // Draw the players score (left)
  280. this.context.fillText(
  281. this.player.score.toString(),
  282. (this.canvas.width / 2) - 300,
  283. 200
  284. );
  285.  
  286. // Draw the paddles score (right)
  287. this.context.fillText(
  288. this.paddle.score.toString(),
  289. (this.canvas.width / 2) + 300,
  290. 200
  291. );
  292.  
  293. // Change the font size for the center score text
  294. this.context.font = '30px Courier New';
  295.  
  296. // Draw the winning score (center)
  297. this.context.fillText(
  298. 'Round ' + (Pong.round + 1),
  299. (this.canvas.width / 2),
  300. 35
  301. );
  302.  
  303. // Change the font size for the center score value
  304. this.context.font = '40px Courier';
  305.  
  306. // Draw the current round number
  307. this.context.fillText(
  308. rounds[Pong.round] ? rounds[Pong.round] : rounds[Pong.round - 1],
  309. (this.canvas.width / 2),
  310. 100
  311. );
  312. },
  313.  
  314. loop: function () {
  315. Pong.update();
  316. Pong.draw();
  317.  
  318. // If the game is not over, draw the next frame.
  319. if (!Pong.over) requestAnimationFrame(Pong.loop);
  320. },
  321.  
  322. listen: function () {
  323. document.addEventListener('keydown', function (key) {
  324. // Handle the 'Press any key to begin' function and start the game.
  325. if (Pong.running === false) {
  326. Pong.running = true;
  327. window.requestAnimationFrame(Pong.loop);
  328. }
  329.  
  330. // Handle up arrow and w key events
  331. if (key.keyCode === 38 || key.keyCode === 87) Pong.player.move = DIRECTION.UP;
  332.  
  333. // Handle down arrow and s key events
  334. if (key.keyCode === 40 || key.keyCode === 83) Pong.player.move = DIRECTION.DOWN;
  335. });
  336.  
  337. // Stop the player from moving when there are no keys being pressed.
  338. document.addEventListener('keyup', function (key) { Pong.player.move = DIRECTION.IDLE; });
  339. },
  340.  
  341. // Reset the ball location, the player turns and set a delay before the next round begins.
  342. _resetTurn: function(victor, loser) {
  343. this.ball = Ball.new.call(this, this.ball.speed);
  344. this.turn = loser;
  345. this.timer = (new Date()).getTime();
  346.  
  347. victor.score++;
  348. beep2.play();
  349. },
  350.  
  351. // Wait for a delay to have passed after each turn.
  352. _turnDelayIsOver: function() {
  353. return ((new Date()).getTime() - this.timer >= 1000);
  354. },
  355.  
  356. // Select a random color as the background of each level/round.
  357. _generateRoundColor: function () {
  358. var newColor = colors[Math.floor(Math.random() * colors.length)];
  359. if (newColor === this.color) return Pong._generateRoundColor();
  360. return newColor;
  361. }
  362. };
  363.  
  364. var Pong = Object.assign({}, Game);
  365. Pong.initialize();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement