Advertisement
RazanQaoud

Razan's BrickGame1

Oct 1st, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5. <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  6. <meta content="utf-8" http-equiv="encoding">
  7. </head>
  8.  
  9. <body>
  10. <canvas id="gameCanvas" width="800" height="600"></canvas>
  11.  
  12. <script>
  13. var canvas, canvasContext;
  14.  
  15. //ball shit
  16. var ballX = 75;
  17. var ballY = 75;
  18. var ballSpeedX = 5;
  19. var ballSpeedY = 7;
  20.  
  21. //brick shit
  22. const BRICK_H = 100;
  23. const BRICK_W = 50;
  24. const BRICK_COUNT = 4;
  25. var brickGrid = new Array(BRICK_COUNT);
  26.  
  27.  
  28.  
  29.  
  30. //paddles shit
  31. const PADDLE_THICKNESS = 15;
  32. const PADDLE_HEIGHT = 150;
  33. const PADDLE_DIST_FROM_EDGE = 60;
  34.  
  35.  
  36. var paddleY = 250;
  37.  
  38. //mouse shit
  39. var mouseX = 0;
  40. var mouseY = 0;
  41.  
  42. function calculateMousePos(evt) {
  43. var rect = canvas.getBoundingClientRect();
  44. var root = document.documentElement;
  45. mouseX = evt.clientX - rect.left - root.scrollLeft;
  46. mouseY = evt.clientY - rect.top - root.scrollTop;
  47. return {
  48. x: mouseX,
  49. y: mouseY
  50. };
  51. }
  52.  
  53.  
  54.  
  55. function brickReset() {
  56. for(var i=0; i<BRICK_COUNT; i++) {
  57. brickGrid[i] = true;
  58. }
  59. }
  60.  
  61. window.onload = function() {
  62. canvas = document.getElementById('gameCanvas');
  63. canvasContext = canvas.getContext('2d');
  64.  
  65. var framesPerSecond = 30;
  66. setInterval(updateAll, 1000 / framesPerSecond);
  67.  
  68. canvas.addEventListener('mousemove', function(evt) {
  69. var mousePos = calculateMousePos(evt);
  70. paddleY = mousePos.y - (PADDLE_HEIGHT / 2);
  71. });
  72.  
  73. ballReset();
  74. }
  75.  
  76. function updateAll() {
  77. moveAll();
  78. drawAll();
  79. }
  80.  
  81.  
  82. function ballReset() {
  83. ballSpeedX = -ballSpeedX;
  84. ballX = canvas.width / 2;
  85. ballY = canvas.height / 2;
  86. }
  87.  
  88. function moveAll() {
  89. ballX += ballSpeedX;
  90. ballY += ballSpeedY;
  91.  
  92. if (ballY < 0) {
  93. ballSpeedY *= -1;
  94.  
  95. }
  96. if (ballY > canvas.height) {
  97. ballSpeedY *= -1;
  98. }
  99.  
  100. if (ballX > canvas.width) {
  101. ballSpeedX *= -1;
  102.  
  103. }
  104. if (ballX < 0) {
  105. ballReset();
  106.  
  107. }
  108.  
  109. ///varibales for the paddles so we can do the math and make the ball bounce off of it
  110.  
  111. var paddleTopEdgeY = paddleY; //
  112.  
  113. var paddleBottomEdgeY = paddleTopEdgeY + PADDLE_HEIGHT;
  114.  
  115.  
  116. var paddleLeftEdgeX = PADDLE_DIST_FROM_EDGE;
  117.  
  118. var paddleRightEdgeX = paddleLeftEdgeX + PADDLE_THICKNESS;
  119.  
  120.  
  121.  
  122. if (ballY > paddleTopEdgeY && // below the top of paddle
  123. ballY < paddleBottomEdgeY && // above bottom of paddle
  124. ballX > paddleLeftEdgeX && // right of the left side of paddle
  125. ballX < paddleRightEdgeX) { // left of the left side of paddle
  126. ballSpeedX *= -1;
  127.  
  128. ///this maks the ball bouse on an angle off the paddle
  129.  
  130. var centerOfPaddleY = paddleY + PADDLE_HEIGHT/2;
  131.  
  132. var ballDistFromPaddleCenterY = ballY - centerOfPaddleY;
  133.  
  134. ballSpeedX = ballDistFromPaddleCenterX * 0.25;
  135. }
  136.  
  137. }
  138.  
  139.  
  140. function drawBricks() {
  141. for(var i=0;i<BRICK_COUNT;i++) {
  142.  
  143. if(brickGrid[i]) {
  144. colorRect(canvas.width-BRICK_W,0+(BRICK_H*i), BRICK_W,BRICK_H-2, 'blue');
  145. }
  146. }
  147. }
  148.  
  149. function drawAll() {
  150. //canvas
  151. colorRect(0, 0, canvas.width, canvas.height, 'black');
  152.  
  153. ///ball
  154. colorCircle(ballX, ballY, 10, 'white');
  155.  
  156. ///this is the paddle
  157. colorRect(PADDLE_DIST_FROM_EDGE, paddleY, PADDLE_THICKNESS, PADDLE_HEIGHT, 'teal');
  158.  
  159. //drawing bricks
  160. drawBricks();
  161.  
  162.  
  163. //this shows us where the mouse is on the canvas
  164. colorText(mouseX+","+mouseY,mouseX, mouseY, 'yellow');
  165.  
  166. }
  167.  
  168. //helps draw squars/rects
  169.  
  170. function colorRect(topLeftX, topLeftY, boxWidth, boxHeight, fillColor) {
  171. canvasContext.fillStyle = fillColor;
  172. canvasContext.fillRect(topLeftX, topLeftY, boxWidth, boxHeight);
  173. }
  174. ///helps draw circles
  175.  
  176. function colorCircle(centerX, centerY, radius, fillColor) {
  177. canvasContext.fillStyle = fillColor;
  178. canvasContext.beginPath();
  179. canvasContext.arc(centerX, centerY, 10, 0, Math.PI * 2, true);
  180. canvasContext.fill();
  181. }
  182.  
  183. ///shows me where the mouse is
  184.  
  185. function colorText(showWords, textX, textY, fillColor){
  186. canvasContext. fillStyle = fillColor;
  187. canvasContext. fillText(showWords, textX, textY);
  188.  
  189.  
  190. }
  191.  
  192. </script>
  193.  
  194. </body>
  195.  
  196. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement