Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. canvas = document.getElementById('gameCanvas');
  2. ctx = canvas.getContext('2d');
  3.  
  4.  
  5. var canvas;
  6. var ctx;
  7. var paddleX = canvas.width/2;
  8. const PADDLE_WIDTH = 100;
  9. var ballX = canvas.width/2;
  10. var ballY = canvas.height/2;
  11. var ballSpeedX = 4;
  12. var ballSpeedY = 4;
  13. const BALL_RADIUS = 10;
  14. const BRICK_WIDTH = 100;
  15. const BRICK_HEIGHT = 15;
  16. var gameRunning = 0;
  17.  
  18. window.onload=function(){
  19.  
  20.  
  21. blocksLogic();
  22. setInterval(update,1000/60);
  23. gameRunning = 1;
  24. }
  25. function update(){
  26. draw();
  27. move();
  28. blocksLogic();
  29.  
  30. }
  31. function draw(){
  32. //clear canvas
  33. colorRect(0,0,canvas.width,canvas.height,'black');
  34. //draw paddle
  35. colorRect(paddleX - (PADDLE_WIDTH/2), 400, PADDLE_WIDTH, 10, 'white' );
  36. //draw ball
  37. colorCircle(ballX, ballY, BALL_RADIUS, 'white');
  38. //draw blocks
  39. drawBlocks();
  40. }
  41. function move(){
  42. ballX += ballSpeedX;
  43. ballY += ballSpeedY;
  44. if(ballY > canvas.height + BALL_RADIUS){
  45. reset();
  46. }
  47. if(ballX > canvas.width - BALL_RADIUS){
  48. ballSpeedX = -ballSpeedX;
  49. }
  50. if(ballX < 0 + BALL_RADIUS){
  51. ballSpeedX = -ballSpeedX;
  52. }
  53. if(ballY < 0 + BALL_RADIUS){
  54. ballSpeedY = -ballSpeedY;
  55. }
  56. if (ballX <= paddleX + PADDLE_WIDTH && ballY == 400 - BALL_RADIUS && ballX >= paddleX -PADDLE_WIDTH && ballX < paddleX + PADDLE_WIDTH - 40){
  57.  
  58. ballSpeedY = -ballSpeedY;
  59. }
  60. ai1();
  61.  
  62. }
  63. function colorRect(leftX, topY, width, height, drawColor){
  64. ctx.fillStyle = drawColor;
  65. ctx.fillRect(leftX, topY, width, height);
  66. }
  67. function colorCircle(centerX, centerY, radius, drawColor){
  68. ctx.fillStyle = drawColor;
  69. ctx.beginPath();
  70. ctx.arc(centerX, centerY, radius ,0,Math.PI*2, true);
  71. ctx.fill();
  72. }
  73. function reset(){
  74. ballSpeedX = -ballSpeedX;
  75. ballSpeedY = -ballSpeedY;
  76. ballX = canvas.width/2;
  77. ballY = canvas.height/2;
  78. paddleX = canvas.width/2;
  79. }
  80. function ai1(){
  81. if(paddleX != 100 || paddleX - PADDLE_WIDTH != canvas.width){
  82. if(ballX > paddleX + PADDLE_WIDTH/2){
  83. paddleX +=4;
  84. } else if(ballX < paddleX){
  85. paddleX -=4;
  86. }
  87.  
  88. }
  89. }
  90. function drawBlocks(){
  91. var storedX;
  92. var storedY;
  93. for(var x= 0; x < 7; x++){
  94. for(var y = 0; y < 7; y++){
  95. colorRect(x*105 + 34, y*25 + 10, BRICK_WIDTH, BRICK_HEIGHT, 'blue');
  96. storedX = x;
  97. storedY = y;
  98. }
  99.  
  100. }
  101.  
  102. }
  103. function blocksLogic(){
  104.  
  105. if(gameRunning == 0){
  106. var blocks = new Array(7);
  107. for(var i=0; i <7; i++){
  108. blocks[i] = new Array(7);
  109. }
  110. for(var x=0; x < 7; x++){
  111. for(var y=0; y < 7; y++){
  112. blocks[x][y] = false;
  113. console.log(blocks[x][y]);
  114. }
  115. }
  116. }
  117. console.log("gamerunning function ran");
  118. // COLLISION!!!!!!!
  119. for(var brickX = 0; x < 7; x++){
  120. console.log("for x has been run!");
  121. for( var brickY = 0; y < 7; y++){
  122. console.log("for y has been run!");
  123. var tempBrickX = brickX * 105 + 34;
  124. var tempBrickY = brickY * 25 - 10;
  125. //top collision
  126. if(ballY >= tempBrickX && ballX >= tempBrickX && ballX <= tempBrickX + BRICK_WIDTH){
  127. console.log("The top of this block has been hit!");
  128. ballSpeedX = -ballSpeedX;
  129. ballSpeedY = -ballSpeedY;
  130. }
  131. //bottom collision
  132. if(ballY <= tempBrickY + BRICK_HEIGHT && ballX >= tempBrickX && ballX >= tempBrickX){
  133. console.log("The bottom of this brick has been hit!");
  134. ballSpeedX = -ballSpeedX;
  135. ballSpeedY = -ballSpeedY;
  136. }
  137. }
  138. }
  139.  
  140.  
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement