Advertisement
Guest User

Untitled

a guest
Jul 12th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //-----------------------------------------Variables
  2.             var gamePieceRed;
  3.             var gamePieceBorder;
  4.             var gameObstacle;
  5. //-----------------------------------------
  6.  
  7. //-----------------------------------------Main game function
  8.             function startGame()
  9.             {
  10.                 gamePieceRed = new component(22, 22, "rgb(255, 132, 156)", 10, 120);
  11.                 gamePieceBorder = new component(24, 24, "black", 9, 119);
  12.  
  13.                 obstacle = new component(10, 200, "rgb(64, 0 ,12)", 300, 120)
  14.  
  15.                 gameArea.start();
  16.             }
  17. //-----------------------------------------
  18.  
  19. //-----------------------------------------Creating game area and applying controls
  20.             var gameArea =
  21.             {
  22.                 canvas : document.createElement("canvas"), start : function()
  23.                 {
  24.                     this.canvas.width = 510;
  25.                     this.canvas.height = 280;
  26.                     this.context = this.canvas.getContext("2d");
  27.  
  28.                     document.body.insertBefore(this.canvas, document.body.childNodes[0]);
  29.  
  30.                     this.interval = setInterval(gameUpdate, 20);
  31.  
  32.                     window.addEventListener("keydown", function (e)
  33.                     {
  34.                         gameArea.keys = (gameArea.keys || []);
  35.                         gameArea.keys[e.keyCode] = true;
  36.                     }, true)
  37.  
  38.                     window.addEventListener("keyup", function (e)
  39.                     {
  40.                         gameArea.keys[e.keyCode] = false;
  41.                     }, true)
  42.                 },
  43.  
  44.                 clear : function()
  45.                 {
  46.                     this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  47.                 },
  48.  
  49.                 stop : function()
  50.                 {
  51.                     clearInterval(this.interval);
  52.                 },
  53.        
  54.           keyboard: function() {
  55.           if (this.keys) {
  56.                         if (this.keys[37]) {gamePieceBorder.speedX = gamePieceRed.speedX = -2;}
  57.                     else if (this.keys[39]) {gamePieceBorder.speedX = gamePieceRed.speedX = 2;}
  58.             else {gamePieceBorder.speedX = gamePieceRed.speedX = 0;}
  59.            
  60.                     if (this.keys[38]) {gamePieceBorder.speedY = gamePieceRed.speedY = -2;}
  61.                     else if (this.keys[40]) {gamePieceBorder.speedY = gamePieceRed.speedY = 2;}
  62.             else {gamePieceBorder.speedY = gamePieceRed.speedY = 0;}
  63.           }
  64.           }
  65.             }
  66. //-----------------------------------------
  67.  
  68. //-----------------------------------------Game component
  69.             function component(width, height, color, x, y)
  70.             {
  71.                 this.width = width;
  72.                 this.height = height;
  73.  
  74.                 this.x = x;
  75.                 this.y = y;
  76.  
  77.                 this.speedX = 0;
  78.                 this.speedY = 0;
  79.  
  80.                 this.update = function()
  81.                 {
  82.                     ctx = gameArea.context;
  83.                     ctx.fillStyle = color;
  84.                     ctx.fillRect(this.x, this.y, this.width, this.height)
  85.                 }
  86.  
  87.                 this.move = function()
  88.                 {
  89.                     this.x += this.speedX;
  90.                     this.y += this.speedY;
  91.                 }
  92.  
  93.                 this.crashGame = function(obj)
  94.                 {
  95.                     var left = this.x;
  96.                     var right = this.x + (this.width);
  97.                     var top = this.y;
  98.                     var bottom = this.y + (this.height);
  99.  
  100.                     var otherLeft = obj.x;
  101.                     var otherRight = obj.x + (obj.width);
  102.                     var otherTop = obj.y;
  103.                     var otherBottom = obj.y + (obj.height);
  104.  
  105.                     var crash = true;
  106.  
  107.                     if (bottom < otherTop || top > otherBottom || right < otherLeft || left > otherRight)
  108.                     {
  109.                         crash = false;
  110.                     }
  111.  
  112.                     return crash;
  113.                 }
  114.             }
  115. //-----------------------------------------
  116.  
  117. //-----------------------------------------Game area updater
  118.             function gameUpdate()
  119.             {
  120.                 if(gamePieceBorder.crashGame(obstacle) || gamePieceRed.crashGame(obstacle))
  121.                 {
  122.                     gameArea.stop();
  123.                 }
  124.  
  125.                 else
  126.                 {
  127.                     gameArea.clear();
  128.  
  129.           obstacle.update();
  130.          
  131.           gameArea.keyboard();
  132.  
  133.                     gamePieceBorder.move();
  134.                     gamePieceBorder.update();
  135.  
  136.                     gamePieceRed.move();
  137.                     gamePieceRed.update();
  138.  
  139.                 }
  140.             }
  141. //-----------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement