Advertisement
Skizerzz

Basic Breakout Example HTML5

May 31st, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var canvas = document.createElement('canvas');
  2. var wid = 400;
  3. var hei = 400;
  4. canvas.width = wid;
  5. canvas.height = hei;
  6. var ctx = canvas.getContext("2d");
  7. document.body.appendChild(canvas);
  8.  
  9. var pressing_left = false;
  10. var pressing_right = false;
  11.  
  12. var objects = [];
  13. var blocks = [];
  14. var paddle = new Paddle(canvas.width/2 - 35, canvas.height - canvas.height/8);
  15. var ball = new Ball(canvas.width/2, canvas.height/2);
  16. objects.push(paddle);
  17. objects.push(ball);
  18.  
  19. var blocks_x = 8;
  20. var blocks_y = 4;
  21. generate_blocks(blocks_x, blocks_y);
  22.  
  23. window.addEventListener('keydown', function(event) {
  24.     switch(event.keyCode) {
  25.         case 37:
  26.             pressing_left = true;
  27.         break;
  28.        
  29.         case 39:
  30.             pressing_right = true;
  31.         break;
  32.     }
  33. });
  34.  
  35. window.addEventListener('keyup', function(event) {
  36.     switch(event.keyCode) {
  37.         case 37:
  38.             pressing_left = false;
  39.         break;
  40.        
  41.         case 39:
  42.             pressing_right = false;
  43.         break;
  44.     }
  45. });
  46.  
  47. function generate_blocks(x_count, y_count) {
  48.     var cur_ind = 2;
  49.     var block_offset = 3;
  50.     var block_x_count = x_count;
  51.     var block_y_count = y_count;
  52.     var total_width = (40+block_offset)*block_x_count;
  53.     var block_x = canvas.width/2 - total_width/2;
  54.     var block_y = 35;
  55.     for(var i = 0; i < block_y_count; i++) {
  56.         for(var k = 0; k < block_x_count; k++) {
  57.             var block = new Block(block_x, block_y, cur_ind);
  58.             blocks.push(block);
  59.             block_x += block.width + block_offset;
  60.            
  61.             cur_ind++;
  62.         }
  63.         block_x = canvas.width/2 - total_width/2;
  64.         block_y += 20 + block_offset;
  65.     }
  66. }
  67.  
  68. function Block(xx, yy, index) {
  69.     this.x = xx;
  70.     this.y = yy;
  71.     this.width = 40;
  72.     this.height = 20;
  73.     this.ind = index;
  74.    
  75.     this.update = function() {
  76.        
  77.     }
  78.    
  79.     this.draw = function() {
  80.         ctx.fillStyle = "#FFFFFF";
  81.         ctx.fillRect(this.x, this.y, this.width, this.height);
  82.     }
  83. }
  84.  
  85. function Ball(xx, yy) {
  86.     this.x = xx;
  87.     this.y = yy;
  88.     this.x_spd = Math.floor(Math.random() * (4 - (-4))) + (-4);
  89.     this.y_spd = 3;
  90.     this.width = 8;
  91.     this.height = 8;
  92.    
  93.     this.update = function() {
  94.        
  95.         this.y += this.y_spd;
  96.        
  97.         var cpx_left = this.x > paddle.x && this.x < paddle.x + paddle.width;
  98.         var cpx_center = this.x+this.width/2 > paddle.x && this.x+this.width/2 < paddle.x + paddle.width;
  99.         var cpx_right = this.x+this.width > paddle.x && this.x+this.width < paddle.x+paddle.width;
  100.        
  101.         var cpy_up = this.y > paddle.y && this.y < paddle.y + paddle.height;
  102.         var cpy_center = this.y+this.height/2 > paddle.y && this.y+this.height/2 < paddle.y + paddle.height;
  103.         var cpy_down = this.y+this.height > paddle.y && this.y+this.height < paddle.y + paddle.height;
  104.        
  105.         var collision_x = cpx_left || cpx_center || cpx_right;
  106.         var collision_y = cpy_up || cpy_center || cpy_down;
  107.        
  108.         if(collision_x && collision_y) {
  109.                 this.y_spd += 0.5;
  110.                 this.y_spd = -this.y_spd;
  111.                 this.x_spd = ((this.x+this.width/2) - (paddle.x+paddle.width/2)) / 16;
  112.                 this.y += this.y_spd;
  113.             }
  114.         else
  115.         {
  116.             this.x += this.x_spd;  
  117.        
  118.             cpx_left = this.x > paddle.x && this.x < paddle.x + paddle.width;
  119.             cpx_center = this.x+this.width/2 > paddle.x && this.x+this.width/2 < paddle.x + paddle.width;
  120.             cpx_right = this.x+this.width > paddle.x && this.x+this.width < paddle.x+paddle.width;
  121.            
  122.             cpy_up = this.y > paddle.y && this.y < paddle.y + paddle.height;
  123.             cpy_center = this.y+this.height/2 > paddle.y && this.y+this.height/2 < paddle.y + paddle.height;
  124.             cpy_down = this.y+this.height > paddle.y && this.y+this.height < paddle.y + paddle.height;
  125.            
  126.             collision_x = cpx_left || cpx_center || cpx_right;
  127.             collision_y = cpy_up || cpy_center || cpy_down;
  128.            
  129.             if(collision_x && collision_y) {
  130.                 console.log("x collision with paddle");
  131.                 this.x_spd = -this.x_spd;
  132.                 this.x += this.x_spd;
  133.             }
  134.         }
  135.        
  136.         var index_destroy = [];
  137.         for(var i = 0; i < blocks.length; i++)
  138.         {
  139.             var block = blocks[i];
  140.             var cpx_left = this.x > block.x && this.x < block.x + block.width;
  141.             var cpx_center = this.x+this.width/2 > block.x && this.x+this.width/2 < block.x + block.width;
  142.             var cpx_right = this.x+this.width > block.x && this.x+this.width < block.x+block.width;
  143.            
  144.             var cpy_up = this.y > block.y && this.y < block.y + block.height;
  145.             var cpy_center = this.y+this.height/2 > block.y && this.y+this.height/2 < block.y + block.height;
  146.             var cpy_down = this.y+this.height > block.y && this.y+this.height < block.y + block.height;
  147.            
  148.             collision_x = cpx_left || cpx_center || cpx_right;
  149.             collision_y = cpy_up || cpy_center || cpy_down;
  150.            
  151.             if(collision_x && collision_y) {
  152.                 this.y_spd = -this.y_spd;
  153.                 //this.x_spd = -this.x_spd;
  154.                 index_destroy.push(i);
  155.             }
  156.         }
  157.         for(var i = 0; i < index_destroy.length; i++) {
  158.             blocks.splice(index_destroy[i], 1);
  159.         }
  160.            
  161.            
  162.         if(this.x < 0)
  163.         {
  164.             this.x_spd -= 0.35;
  165.             this.x_spd = -this.x_spd;
  166.         }
  167.         else if(this.x > canvas.width - this.width)
  168.         {
  169.             this.x_spd += 0.35;
  170.             this.x_spd = -this.x_spd;
  171.         }
  172.        
  173.         if(this.y < 0)
  174.             this.y_spd = -this.y_spd;
  175.         else if(this.y > canvas.height) {
  176.             this.x = canvas.width/2;
  177.             this.y = canvas.height/2;
  178.             this.x_spd = Math.floor(Math.random() * (4 - (-4))) + (-4);
  179.             this.y_spd = 3;
  180.         }
  181.     }
  182.    
  183.     this.draw = function() {
  184.         ctx.fillStyle = "#FFFFFF";
  185.         ctx.fillRect(this.x, this.y, this.width, this.height);
  186.     }
  187. }
  188.  
  189. function Paddle(xx, yy) {
  190.     this.x = xx;
  191.     this.y = yy;
  192.     this.spd = 8;
  193.     this.width = 70;
  194.     this.height = 8;
  195.    
  196.     this.update = function() {
  197.         var dir = -pressing_left + pressing_right;
  198.         var move_x = dir * this.spd;
  199.         this.x += move_x;
  200.        
  201.         if(this.x < 0)
  202.             this.x = 0;
  203.         else if(this.x > canvas.width - this.width)
  204.             this.x = canvas.width - this.width;
  205.     }
  206.    
  207.     this.draw = function() {
  208.         ctx.fillStyle = "#FFFFFF";
  209.         ctx.fillRect(this.x, this.y, this.width, this.height);
  210.     }
  211. }
  212.  
  213. setInterval(gameloop, (1/60) * 1000);
  214.  
  215. function gameloop() {
  216.     ctx.fillStyle = "black";
  217.     ctx.fillRect(0, 0, canvas.width, canvas.height);
  218.     for(var i = 0; i < objects.length; i++) {
  219.         objects[i].update();
  220.     }
  221.    
  222.     for(var i = 0; i < objects.length; i++) {
  223.         objects[i].draw();
  224.     }
  225.     for(var i = 0; i < blocks.length; i++) {
  226.         blocks[i].draw();
  227.     }
  228.     if(blocks.length <= 0) {
  229.         generate_blocks(blocks_x, blocks_y);
  230.     }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement