Abdulg

Simple dodging game

Sep 21st, 2016
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. var canvas = document.getElementById("canvas");
  4. var context = canvas.getContext("2d");
  5.  
  6. var screenRect;
  7.  
  8. var colours = ["#000000", "#FF0000", "#FFA500" , "#00FF00"];
  9.  
  10. var ldown, udown, rdown, ddown, odown;
  11. var player, enemies, score;
  12.  
  13. var intervalID;
  14.  
  15. function resize()
  16. {
  17.     canvas.width = window.innerWidth;
  18.     canvas.height = window.innerHeight;
  19.    
  20.     screenRect.w = canvas.width - 20;
  21.     screenRect.h = canvas.height - 20;
  22.  
  23.     player.rect.w = player.rect.h = canvas.height / 20;
  24.     if (player.rect.x + player.rect.w > screenRect.x + screenRect.w)
  25.     {
  26.         player.rect.x = screenRect.x + screenRect.w - player.rect.w;
  27.     }
  28.     if (player.rect.y + player.rect.h > screenRect.y + screenRect.h)
  29.     {
  30.         player.rect.y = screenRect.y + screenRect.h - player.rect.h;
  31.     }
  32. }
  33.  
  34. $(document).keydown(function(e)
  35. {
  36.     odown = true;
  37.     switch(e.keyCode)
  38.     {
  39.         case 37:
  40.             ldown = true;
  41.             break;
  42.         case 38:
  43.             udown = true;
  44.             break;
  45.         case 39:
  46.             rdown = true;
  47.             break;
  48.         case 40:
  49.             ddown = true;
  50.             break;
  51.     }
  52. });
  53.  
  54. $(document).keyup(function(e)
  55. {
  56.     switch(e.keyCode)
  57.     {
  58.         case 37:
  59.             ldown = false;
  60.             break;
  61.         case 38:
  62.             udown = false;
  63.             break;
  64.         case 39:
  65.             rdown = false;
  66.             break;
  67.         case 40:
  68.             ddown = false;
  69.             break;
  70.         default:
  71.             odown = false;
  72.     }
  73. });
  74.  
  75. function rect(x,y,width,height)
  76. {
  77.     this.x = x;
  78.     this.y = y;
  79.     this.w = width;
  80.     this.h = height;
  81.  
  82.     this.fill = function()
  83.     {
  84.         context.fillRect(this.x + rect.xoffset, this.y + rect.yoffset, this.w, this.h);
  85.     }
  86. }
  87.  
  88. function checkColliding(rect1, rect2)
  89. {
  90.     return !(rect2.x >= rect1.x + rect1.w ||
  91.              rect2.x + rect2.w <= rect1.x ||
  92.              rect2.y >= rect1.y + rect1.h ||
  93.              rect2.y + rect2.h <= rect1.y);
  94. }
  95.  
  96. function drawBackground()
  97. {
  98.     context.fillStyle = colours[player.lives];
  99.     context.fillRect(0,0, canvas.width, canvas.height);
  100.  
  101.     context.fillStyle = "#FFFFFF";
  102.     screenRect.fill();
  103. }
  104.  
  105. function Character()
  106. {
  107.     this.lives = 3;
  108.     this.rect = new rect(canvas.width / 2, canvas.height / 2 ,canvas.height / 20, canvas.height / 20);
  109.  
  110.     this.xvel = 0;
  111.     this.yvel = 0;
  112.  
  113.     this.invtimer = 0;
  114.  
  115.     this.draw = function()
  116.     {
  117.         if (this.invtimer % 10 < 5) this.rect.fill();
  118.     }
  119.  
  120.     this.move = function()
  121.     {
  122.         if (this.invtimer > 0) (this.invtimer)--;
  123.  
  124.         if (udown && this.yvel > -8)
  125.         {
  126.             if (this.yvel > 2) this.yvel /= 2;
  127.             else this.yvel -= 0.3;
  128.         }
  129.         else if (ddown && this.yvel < 8)
  130.         {
  131.             if (this.yvel < -2) this.yvel /= 2;
  132.             else this.yvel += 0.3;
  133.         }
  134.         else this.yvel *= 0.8;
  135.  
  136.         if (ldown && this.xvel > -8)
  137.         {
  138.             if (this.xvel > 2) this.xvel /= 2;
  139.             else this.xvel -= 0.3;
  140.         }
  141.         else if (rdown && this.xvel < 8)
  142.         {
  143.             if (this.xvel < -2) this.xvel /= 2;
  144.             else this.xvel += 0.3;
  145.         }
  146.         else this.xvel *= 0.8;
  147.  
  148.         if (Math.abs(this.xvel) < 0.1) this.xvel = 0;
  149.         if (Math.abs(this.yvel) < 0.1) this.yvel = 0;
  150.  
  151.         this.rect.x += this.xvel;
  152.         this.rect.y += this.yvel;
  153.  
  154.         if (this.rect.x < screenRect.x || this.rect.x + this.rect.w > screenRect.x + screenRect.w)
  155.         {
  156.             this.xvel *= -1;
  157.             this.rect.x += this.xvel;
  158.         }
  159.  
  160.         if (this.rect.y < screenRect.y || this.rect.y + this.rect.h > screenRect.y + screenRect.h)
  161.         {
  162.             this.yvel *= -1;
  163.             this.rect.y += this.yvel;
  164.         }
  165.     }
  166. }
  167.  
  168. var enemies = [];
  169.  
  170. //returns a randomly generated obstacle
  171. function Enemy()
  172. {
  173.     var direction = Math.random();
  174.     if (direction > 0.5) //horizontal
  175.     {
  176.         this.yvel = 0;
  177.  
  178.         var w = (Math.random() * canvas.width / 3) + 16;
  179.         var h = (Math.random() * canvas.height / 4) + 16;
  180.         var y = Math.random() * (screenRect.h - h) + screenRect.y;
  181.         var x;
  182.  
  183.         if (direction > 0.75) //left to right
  184.         {
  185.             x = -w + screenRect.x;
  186.             this.xvel = Math.random() * 4 + 5;
  187.         }
  188.  
  189.         else //right to left
  190.         {
  191.             x = screenRect.x + screenRect.w;
  192.             this.xvel = -Math.random() * 4 - 5;
  193.         }
  194.  
  195.         this.rect = new rect(x,y,w,h);
  196.     }
  197.     else //vertical
  198.     {
  199.         this.xvel = 0;
  200.         var w = (Math.random() * canvas.width / 4) + 16;
  201.         var h = (Math.random() * canvas.height / 4) + 16;
  202.         var x = Math.random() * (screenRect.w - w) + screenRect.x;
  203.  
  204.         if (direction > 0.25) //top to bottom
  205.         {
  206.             y = -h + screenRect.y;
  207.             this.yvel = Math.random() * 2 + 6;
  208.         }
  209.  
  210.         else //going up
  211.         {
  212.             y = screenRect.y + screenRect.h;
  213.             this.yvel = -Math.random() * 2 - 6;
  214.         }
  215.  
  216.         this.rect = new rect(x,y,w,h);
  217.     }
  218.  
  219.     this.update = function()
  220.     {
  221.         this.rect.x += this.xvel;
  222.         this.rect.y += this.yvel;
  223.  
  224.         if (!checkColliding(this.rect, screenRect))
  225.         {
  226.             enemies.splice(enemies.indexOf(this), 1);
  227.             score++;
  228.         }
  229.     }
  230.  
  231.     this.draw = function()
  232.     {
  233.         this.rect.fill();
  234.     }
  235. }
  236.  
  237. function drawEnemies()
  238. {
  239.     for (var i = 0; i < enemies.length; i++)
  240.     {
  241.         enemies[i].draw();
  242.     }
  243. }
  244.  
  245. function updateEnemies()
  246. {
  247.     for (var i = 0; i < enemies.length; i++)
  248.     {
  249.         enemies[i].update();
  250.     }
  251. }
  252.  
  253. function render()
  254. {
  255.     context.clearRect(0, 0, canvas.width, canvas.height);
  256.  
  257.     if (render.shaketime > 0)
  258.     {
  259.         render.shaketime--;
  260.         rect.xoffset = Math.random() * 8 - 4;
  261.         rect.yoffset = Math.random() * 8 - 4;
  262.         if (render.shaketime == 0) rect.xoffset = rect.yoffset = 0;
  263.     }
  264.  
  265.     drawBackground();
  266.  
  267.     context.fillStyle = colours[player.lives];
  268.     context.fillText(score, screenRect.x + 20, screenRect.y + 60);
  269.     player.draw();
  270.     drawEnemies();
  271. }
  272.  
  273. function update()
  274. {
  275.     render();
  276.     player.move();
  277.  
  278.     if (++(update.spawncounter) >= update.spawntime)
  279.     {
  280.         enemies.push(new Enemy());
  281.         update.spawncounter = 0;
  282.         if (update.spawntime > 30) update.spawntime -= 10;
  283.     }
  284.     updateEnemies();
  285.  
  286.     checkCollisions();
  287. }
  288.  
  289. function checkCollisions()
  290. {
  291.     if (player.invtimer == 0)
  292.     {
  293.         for (var i = 0; i < enemies.length; i++)
  294.         {
  295.             if (checkColliding(enemies[i].rect, player.rect))
  296.             {
  297.                 if (player.lives == 1)
  298.                 {
  299.                     clearInterval(intervalID);
  300.                     setTimeout(gameOver, 1000/60 + 1);
  301.                 }
  302.  
  303.                 player.invtimer = 200;
  304.                 render.shaketime = 100;
  305.                 (player.lives)--;
  306.             }
  307.         }
  308.     }
  309. }
  310.  
  311. function gameLoop()
  312. {
  313.     update();
  314.     render();
  315. }
  316.  
  317. function gameOver()
  318. {
  319.     context.clearRect(0, 0, canvas.width, canvas.height);
  320.  
  321.     context.fillStyle = "#FF0000";
  322.     context.fillRect(0,0, canvas.width, canvas.height);
  323.  
  324.     context.fillStyle = "#FFFFFF";
  325.     screenRect.fill();
  326.  
  327.     context.fillStyle = "#FF0000";
  328.     context.textAlign = "center";
  329.     context.font = "64px Comic Sans MS";
  330.     context.fillText("You scored " + score, canvas.width / 2, canvas.height / 2);
  331.  
  332.     odown = false;
  333.     startGameOnPress.intervalid = window.setInterval(startGameOnPress, 1000 / 60);
  334. }
  335.  
  336. function initGame()
  337. {
  338.     player = new Character();
  339.  
  340.     screenRect = new rect(10,10, 0, 0);
  341.  
  342.     window.addEventListener('resize', resize, false);
  343.     resize();
  344.  
  345.     ldown = rdown = udown = ddown = odown = false;
  346.     score = 0;
  347.  
  348.     rect.xoffset = rect.yoffset = 0;
  349.     render.shaketime = 0;
  350.  
  351.     update.spawntime = 200;
  352.     update.spawncounter = update.spawntime - 40;
  353.     enemies = [];
  354.  
  355.     context.font = "64px Comic Sans MS";
  356.     intervalID = window.setInterval(gameLoop, 1000 / 60);
  357. }
  358.  
  359. function startGameOnPress()
  360. {
  361.     if (odown)
  362.     {
  363.         clearInterval(startGameOnPress.intervalid);
  364.         initGame();
  365.     }
  366. }
  367.  
  368. function start()
  369. {
  370.     canvas.width = window.innerWidth;
  371.     canvas.height = window.innerHeight;
  372.  
  373.     context.fillStyle = "#00FF00";
  374.     context.fillRect(0,0, window.innerWidth, window.innerHeight);
  375.  
  376.     context.fillStyle = "#FFFFFF";
  377.     context.fillRect(10,10, window.innerWidth - 20, window.innerHeight - 20);
  378.  
  379.     context.font = "64px Comic Sans MS";
  380.     context.fillStyle = "#00FF00";
  381.     context.textAlign = "center";
  382.     context.fillText("Dodge", window.innerWidth/2, window.innerHeight/2);
  383.     odown = false;
  384.  
  385.     startGameOnPress.intervalid = window.setInterval(startGameOnPress, 1000 / 60);
  386. }
  387.  
  388. start();
Advertisement
Add Comment
Please, Sign In to add comment