Advertisement
Guest User

Test

a guest
Nov 29th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var animate = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function (callback) {
  2.         window.setTimeout(callback, 1000 / 60)
  3.     };
  4. var canvas = document.createElement("canvas");
  5. var width = 400;
  6. var height = 600;
  7. canvas.width = width;
  8. canvas.height = height;
  9. var context = canvas.getContext('2d');
  10. var player = new Player();
  11. var computer = new Computer();
  12. var ball = new Ball(200, 300);
  13.  
  14. var keysDown = {};
  15.  
  16. var render = function () {
  17.     context.fillStyle = "#FF00FF";
  18.     context.fillRect(0, 0, width, height);
  19.     player.render();
  20.     computer.render();
  21.     ball.render();
  22. };
  23.  
  24. var update = function () {
  25.     player.update();
  26.     computer.update(ball);
  27.     ball.update(player.paddle, computer.paddle);
  28. };
  29.  
  30. var step = function () {
  31.     update();
  32.     render();
  33.     animate(step);
  34. };
  35.  
  36. function Paddle(x, y, width, height) {
  37.     this.x = x;
  38.     this.y = y;
  39.     this.width = width;
  40.     this.height = height;
  41.     this.x_speed = 0;
  42.     this.y_speed = 0;
  43. }
  44.  
  45. Paddle.prototype.render = function () {
  46.     context.fillStyle = "#0000FF";
  47.     context.fillRect(this.x, this.y, this.width, this.height);
  48. };
  49.  
  50. Paddle.prototype.move = function (x, y) {
  51.     this.x += x;
  52.     this.y += y;
  53.     this.x_speed = x;
  54.     this.y_speed = y;
  55.     if (this.x < 0) {
  56.         this.x = 0;
  57.         this.x_speed = 0;
  58.     } else if (this.x + this.width > 400) {
  59.         this.x = 400 - this.width;
  60.         this.x_speed = 0;
  61.     }
  62. };
  63.  
  64. function Computer() {
  65.     this.paddle = new Paddle(175, 10, 50, 10);
  66. }
  67.  
  68. Computer.prototype.render = function () {
  69.     this.paddle.render();
  70. };
  71.  
  72. Computer.prototype.update = function (ball) {
  73.     var x_pos = ball.x;
  74.     var diff = -((this.paddle.x + (this.paddle.width / 2)) - x_pos);
  75.     if (diff < 0 && diff < -4) {
  76.         diff = -5;
  77.     } else if (diff > 0 && diff > 4) {
  78.         diff = 5;
  79.     }
  80.     this.paddle.move(diff, 0);
  81.     if (this.paddle.x < 0) {
  82.         this.paddle.x = 0;
  83.     } else if (this.paddle.x + this.paddle.width > 400) {
  84.         this.paddle.x = 400 - this.paddle.width;
  85.     }
  86. };
  87.  
  88. function Player() {
  89.     this.paddle = new Paddle(175, 580, 50, 10);
  90. }
  91.  
  92. Player.prototype.render = function () {
  93.     this.paddle.render();
  94. };
  95.  
  96. Player.prototype.update = function () {
  97.     for (var key in keysDown) {
  98.         var value = Number(key);
  99.         if (value == 37) {
  100.             this.paddle.move(-4, 0);
  101.         } else if (value == 39) {
  102.             this.paddle.move(4, 0);
  103.         } else {
  104.             this.paddle.move(0, 0);
  105.         }
  106.     }
  107. };
  108.  
  109. function Ball(x, y) {
  110.     this.x = x;
  111.     this.y = y;
  112.     this.x_speed = 0;
  113.     this.y_speed = 3;
  114. }
  115.  
  116. Ball.prototype.render = function () {
  117.     context.beginPath();
  118.     context.arc(this.x, this.y, 5, 2 * Math.PI, false);
  119.     context.fillStyle = "#000000";
  120.     context.fill();
  121. };
  122.  
  123. Ball.prototype.update = function (paddle1, paddle2) {
  124.     this.x += this.x_speed;
  125.     this.y += this.y_speed;
  126.     var top_x = this.x - 5;
  127.     var top_y = this.y - 5;
  128.     var bottom_x = this.x + 5;
  129.     var bottom_y = this.y + 5;
  130.  
  131.     if (this.x - 5 < 0) {
  132.         this.x = 5;
  133.         this.x_speed = -this.x_speed;
  134.     } else if (this.x + 5 > 400) {
  135.         this.x = 395;
  136.         this.x_speed = -this.x_speed;
  137.     }
  138.  
  139.     if (this.y < 0 || this.y > 600) {
  140.         this.x_speed = 0;
  141.         this.y_speed = 3;
  142.         this.x = 200;
  143.         this.y = 300;
  144.     }
  145.  
  146.     if (top_y > 300) {
  147.         if (top_y < (paddle1.y + paddle1.height) && bottom_y > paddle1.y && top_x < (paddle1.x + paddle1.width) && bottom_x > paddle1.x) {
  148.             this.y_speed = -3;
  149.             this.x_speed += (paddle1.x_speed / 2);
  150.             this.y += this.y_speed;
  151.         }
  152.     } else {
  153.         if (top_y < (paddle2.y + paddle2.height) && bottom_y > paddle2.y && top_x < (paddle2.x + paddle2.width) && bottom_x > paddle2.x) {
  154.             this.y_speed = 3;
  155.             this.x_speed += (paddle2.x_speed / 2);
  156.             this.y += this.y_speed;
  157.         }
  158.     }
  159. };
  160.  
  161. document.body.appendChild(canvas);
  162. animate(step);
  163.  
  164. window.addEventListener("keydown", function (event) {
  165.     keysDown[event.keyCode] = true;
  166. });
  167.  
  168. window.addEventListener("keyup", function (event) {
  169.     delete keysDown[event.keyCode];
  170. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement