Advertisement
Guest User

Pong By Mattia

a guest
Oct 27th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Pong</title>
  6.  
  7.  
  8. <style>
  9. canvas {
  10. display: block;
  11. position: absolute;
  12. margin: auto;
  13. top: 0;
  14. bottom: 0;
  15. left: 0;
  16. right: 0;
  17. }
  18. body {
  19. background-image: url("PinoSpace.jpg");
  20. background-color: #cccccc;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <script>
  26. var
  27.  
  28. WIDTH = 700,
  29. HEIGHT = 600,
  30. pi = Math.PI,
  31. UpArrow = 38,
  32. DownArrow = 40,
  33.  
  34. canvas,
  35. ctx,
  36. keystate,
  37.  
  38. player = {
  39. x: null,
  40. y: null,
  41. width: 50,
  42. height: 70,
  43.  
  44. update: function() {
  45. if (keystate[UpArrow]) this.y -= 7;
  46. if (keystate[DownArrow]) this.y += 7;
  47.  
  48. this.y = Math.max(Math.min(this.y, HEIGHT - this.height), 0);
  49. },
  50.  
  51.  
  52. draw: function() {
  53. ctx.fillRect(this.x, this.y, this.width, this.height);
  54. }
  55. },
  56.  
  57. ai = {
  58. x: null,
  59. y: null,
  60. width: 20,
  61. height: 100,
  62.  
  63. update: function() {
  64.  
  65. var desty = ball.y - (this.height - ball.side)*0.5;
  66.  
  67. this.y += (desty - this.y) * 0.1;
  68.  
  69. this.y = Math.max(Math.min(this.y, HEIGHT - this.height), 0);
  70. },
  71.  
  72. draw: function() {
  73. ctx.fillRect(this.x, this.y, this.width, this.height);
  74. }
  75. },
  76.  
  77. ball = {
  78. x: null,
  79. y: null,
  80. vel: null,
  81. side: 20,
  82. speed: 23,
  83.  
  84. serve: function(side) {
  85.  
  86. var r = Math.random();
  87. this.x = side===1 ? player.x+player.width : ai.x - this.side;
  88. this.y = (HEIGHT - this.side)*r;
  89.  
  90.  
  91. var phi = 0.1*pi*(1 - 2*r);
  92.  
  93. this.vel = {
  94. x: side*this.speed*Math.cos(phi),
  95. y: this.speed*Math.sin(phi)
  96. }
  97. },
  98.  
  99. update: function() {
  100.  
  101. this.x += this.vel.x;
  102. this.y += this.vel.y;
  103.  
  104. if (0 > this.y || this.y+this.side > HEIGHT) {
  105.  
  106. var offset = this.vel.y < 0 ? 0 - this.y : HEIGHT - (this.y+this.side);
  107. this.y += 2*offset;
  108.  
  109. this.vel.y *= -1;
  110. }
  111.  
  112. var AABBIntersect = function(ax, ay, aw, ah, bx, by, bw, bh) {
  113. return ax < bx+bw && ay < by+bh && bx < ax+aw && by < ay+ah;
  114. };
  115.  
  116. var pdle = this.vel.x < 0 ? player : ai;
  117. if (AABBIntersect(pdle.x, pdle.y, pdle.width, pdle.height,
  118. this.x, this.y, this.side, this.side)
  119. ) {
  120.  
  121. this.x = pdle===player ? player.x+player.width : ai.x - this.side;
  122. var n = (this.y+this.side - pdle.y)/(pdle.height+this.side);
  123. var phi = 0.25*pi*(2*n - 1);
  124.  
  125. var smash = Math.abs(phi) > 0.2*pi ? 1.5 : 1;
  126. this.vel.x = smash*(pdle===player ? 1 : -1)*this.speed*Math.cos(phi);
  127. this.vel.y = smash*this.speed*Math.sin(phi);
  128. }
  129.  
  130. if (0 > this.x+this.side || this.x > WIDTH) {
  131. this.serve(pdle===player ? 1 : -1);
  132. }
  133. },
  134.  
  135. draw: function() {
  136. ctx.fillRect(this.x, this.y, this.side, this.side);
  137. }
  138. };
  139.  
  140. function main() {
  141.  
  142. canvas = document.createElement("canvas");
  143. canvas.width = WIDTH;
  144. canvas.height = HEIGHT;
  145. ctx = canvas.getContext("2d");
  146. document.body.appendChild(canvas);
  147. keystate = {};
  148.  
  149. document.addEventListener("keydown", function(evt) {
  150. keystate[evt.keyCode] = true;
  151. });
  152. document.addEventListener("keyup", function(evt) {
  153. delete keystate[evt.keyCode];
  154. });
  155. init();
  156. var loop = function() {
  157. update();
  158. draw();
  159. window.requestAnimationFrame(loop, canvas);
  160. };
  161. window.requestAnimationFrame(loop, canvas);
  162. }
  163.  
  164. function init() {
  165. player.x = player.width;
  166. player.y = (HEIGHT - player.height)/2;
  167. ai.x = WIDTH - (player.width + ai.width);
  168. ai.y = (HEIGHT - ai.height)/2;
  169. ball.serve(1);
  170. }
  171.  
  172. function update() {
  173. ball.update();
  174. player.update();
  175. ai.update();
  176. }
  177.  
  178. function draw() {
  179. ctx.fillRect(0, 0, WIDTH, HEIGHT);
  180. ctx.save();
  181. ctx.fillStyle = "#fff";
  182. ball.draw();
  183. player.draw();
  184. ai.draw();
  185.  
  186. var w = 4;
  187. var x = (WIDTH - w)*0.5;
  188. var y = 0;
  189. var step = HEIGHT/20;
  190. while (y < HEIGHT) {
  191. ctx.fillRect(x, y+step*0.25, w, step*0.5);
  192. y += step;
  193. }
  194. ctx.restore();
  195. }
  196.  
  197. main();
  198. </script>
  199. </body>
  200. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement