Advertisement
Guest User

Untitled

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