SHARE
TWEET

Untitled

a guest Jan 14th, 2015 1,490 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <canvas id="ctx" width="500" height="500" style="border:1px solid #000000;"></canvas>
  2.  
  3. <script>
  4. var ctx = document.getElementById("ctx").getContext("2d");
  5. ctx.font = '30px Arial';
  6.  
  7. var HEIGHT = 500;
  8. var WIDTH = 500;
  9. var timeWhenGameStarted = Date.now();   //return time in ms
  10.  
  11. var player = {
  12.         x:50,
  13.         spdX:30,
  14.         y:40,
  15.         spdY:5,
  16.         name:'P',
  17.         hp:10, 
  18. };
  19.  
  20. var enemyList = {};
  21.  
  22.  
  23. getDistanceBetweenEntity = function (entity1,entity2){  //return distance (number)
  24.         var vx = entity1.x - entity2.x;
  25.         var vy = entity1.y - entity2.y;
  26.         return Math.sqrt(vx*vx+vy*vy);
  27. }
  28.  
  29. testCollisionEntity = function (entity1,entity2){       //return if colliding (true/false)
  30.         var distance = getDistanceBetweenEntity(entity1,entity2);
  31.         return distance < 30;
  32. }
  33.  
  34. Enemy = function (id,x,y,spdX,spdY){
  35.         var enemy3 = {
  36.                 x:x,
  37.                 spdX:spdX,
  38.                 y:y,
  39.                 spdY:spdY,
  40.                 name:'E',
  41.                 id:id,
  42.         };
  43.         enemyList[id] = enemy3;
  44.        
  45. }
  46.  
  47. document.onmousemove = function(mouse){
  48.         var mouseX = mouse.clientX;
  49.         var mouseY = mouse.clientY;
  50.        
  51.         player.x = mouseX;
  52.         player.y = mouseY;
  53. }
  54.  
  55.  
  56.  
  57.  
  58.  
  59. updateEntity = function (something){
  60.         updateEntityPosition(something);
  61.         drawEntity(something);
  62. }
  63. updateEntityPosition = function(something){
  64.         something.x += something.spdX;
  65.         something.y += something.spdY;
  66.                        
  67.         if(something.x < 0 || something.x > WIDTH){
  68.                 something.spdX = -something.spdX;
  69.         }
  70.         if(something.y < 0 || something.y > HEIGHT){
  71.                 something.spdY = -something.spdY;
  72.         }
  73. }
  74.  
  75.  
  76. drawEntity = function(something){
  77.         ctx.fillText(something.name,something.x,something.y);
  78. }
  79.  
  80.  
  81.  
  82. update = function(){
  83.         ctx.clearRect(0,0,WIDTH,HEIGHT);
  84.        
  85.         for(var key in enemyList){
  86.                 updateEntity(enemyList[key]);
  87.                
  88.                 var isColliding = testCollisionEntity(player,enemyList[key]);
  89.                 if(isColliding){
  90.                         player.hp = player.hp - 1;
  91.                         if(player.hp <= 0){
  92.                                 var timeSurvived = Date.now() - timeWhenGameStarted;
  93.                                
  94.                                 console.log("You lost! You survived for " + timeSurvived + " ms.");
  95.                                 timeWhenGameStarted = Date.now();
  96.                                 player.hp = 10;
  97.                         }
  98.                 }
  99.                
  100.         }
  101.        
  102.         drawEntity(player);
  103.         ctx.fillText(player.hp + " Hp",0,30);
  104. }
  105.  
  106. Enemy('E1',150,350,10,15);
  107. Enemy('E2',250,350,10,-15);
  108. Enemy('E3',250,150,10,-8);
  109.  
  110.  
  111. setInterval(update,40);
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124. </script>
RAW Paste Data
Pastebin PRO Summer Special!
Get 60% OFF on Pastebin PRO accounts!
Top