Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. "use strict"; // for future-proof error-fixing
  2.  
  3. // define global variable here
  4. var canvas = document.getElementById('canvas');
  5. var context = canvas.getContext('2d');
  6. var width = canvas.width;
  7. var height = canvas.height;
  8. var ball = new Ball();
  9. var player_1 = new Paddle(10, 0);
  10.  
  11. // paddle constructor function
  12. function Paddle(x_position, y_position){
  13. this.width = 5; // width in pixels
  14. this.height = 50; // height in pixels
  15. this.x_position = x_position; // position in pixels
  16. this.y_position = y_position; // position in pixels
  17. this.player_score = 0;
  18. //this.left = x_position - width / 2;
  19. //this.right = x_position + width / 2;
  20. //this.top = y_position - height / 2;
  21. //this.bottom = y_position + height / 2;
  22. }
  23.  
  24. // method to draw paddle
  25. Paddle.prototype.render = function(){
  26. context.fillRect(this.x_position, this.y_position, this.width, this.height); // draw paddle
  27. };
  28.  
  29. // ball constructor function
  30. function Ball(){
  31. this.x_speed = 10; //pixels per second (change to desired speed)
  32. this.y_speed = 10; //pixels per second (change to desired speed)
  33. this.ball_radius = 5; // pixels
  34. this.x_position = width * 0.5; // position in pixels
  35. this.y_position = height * 0.5; // position in pixels
  36. //this.left = x_position - width / 2;
  37. //this.right = x_position + width / 2;
  38. //this.top = y_position - height / 2;
  39. //this.bottom = y_position + height / 2;
  40. }
  41.  
  42. // method to draw ball
  43. Ball.prototype.render = function(){
  44. context.beginPath();
  45. context.arc(this.x_position, this.y_position, this.ball_radius, 0, Math.PI * 2); // draw ball
  46. context.fill();
  47. }
  48.  
  49. // TODO: Add data for top and bottom rectangles (i.e boundaries)
  50.  
  51. function render(){
  52. context.fillStyle = 'tomato'; // set colour of components within the canvas
  53. context.clearRect(0, 0, width, height); // clear the canvas
  54. //context.fillRect(width/2, 0, 2, height);
  55. ball.render();
  56. // draw player_1 paddle
  57. player_1.y_position = (height * 0.5) - (player_1.height/2);
  58. player_1.render();
  59. // TODO: draw the paddle (player_2 / cpu)
  60. // TODO: Make sure to render the top and bottom rectangle (i.e boundaries)
  61. }
  62.  
  63. Ball.prototype.update = function(t_elapsed) {
  64. this.x_position += this.x_speed;
  65. //this.y_position += this.y_speed;
  66. }
  67.  
  68. function update(t_elapsed) {
  69. ball.update(t_elapsed);
  70. if(ball.x_position < 0 || ball.x_position > width) {
  71. ball.x_speed = -ball.x_speed;
  72. }
  73. if(ball.y_position < 0 || ball.y_position > height) {
  74. ball.y_speed = -ball.y_speed;
  75. }
  76.  
  77. // TODO: Update ball position based on time elapsed
  78. // TODO: Bounce the ball of top and bottom rectangles
  79. // TODO: Record score and reset if ball goes passed the left or right paddle
  80. // TODO: Bounce the ball off the paddle
  81. }
  82.  
  83. // have keyboard inputs (controls for the paddle)
  84. function keyboard_input(event){
  85. // TODO: Modify code so the paddle goes up and down but not outside the area of play.
  86. //console.log(event.keyCode); // use this to view key codes
  87. }
  88.  
  89. window.addEventListener("keydown", keyboard_input); // listen to keyboard button press
  90.  
  91. // main game loop
  92. var previous;
  93. function main(timestamp){
  94. if (!previous) previous = timestamp; //start with no elapsed time
  95. var t_elapsed = (timestamp - previous) / 1000; //work out the elapsed time
  96. update(t_elapsed); //update the game based on elapsed time
  97. render();
  98. // TODO: render scene here (e.g draw ball, draw paddle, top and bottom rectangle detection), this function already exist;
  99. previous = timestamp; //set the previous timestamp ready for next time
  100. window.requestAnimationFrame(main); //ask browser to call this function again, when it's ready
  101. }
  102.  
  103.  
  104. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement