Advertisement
Guest User

Untitled

a guest
Jan 18th, 2015
3,403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  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. width:20,
  19. height:20,
  20. color:'green',
  21. };
  22.  
  23. var enemyList = {};
  24.  
  25.  
  26. getDistanceBetweenEntity = function (entity1,entity2){ //return distance (number)
  27. var vx = entity1.x - entity2.x;
  28. var vy = entity1.y - entity2.y;
  29. return Math.sqrt(vx*vx+vy*vy);
  30. }
  31.  
  32. testCollisionEntity = function (entity1,entity2){ //return if colliding (true/false)
  33. var rect1 = {
  34. x:entity1.x-entity1.width/2,
  35. y:entity1.y-entity1.height/2,
  36. width:entity1.width,
  37. height:entity1.height,
  38. }
  39. var rect2 = {
  40. x:entity2.x-entity2.width/2,
  41. y:entity2.y-entity2.height/2,
  42. width:entity2.width,
  43. height:entity2.height,
  44. }
  45. return testCollisionRectRect(rect1,rect2);
  46.  
  47. }
  48.  
  49. Enemy = function (id,x,y,spdX,spdY,width,height){
  50. var enemy3 = {
  51. x:x,
  52. spdX:spdX,
  53. y:y,
  54. spdY:spdY,
  55. name:'E',
  56. id:id,
  57. width:width,
  58. height:height,
  59. color:'red',
  60. };
  61. enemyList[id] = enemy3;
  62.  
  63. }
  64.  
  65. document.onmousemove = function(mouse){
  66. var mouseX = mouse.clientX;
  67. var mouseY = mouse.clientY;
  68.  
  69. player.x = mouseX;
  70. player.y = mouseY;
  71. }
  72.  
  73.  
  74.  
  75.  
  76.  
  77. updateEntity = function (something){
  78. updateEntityPosition(something);
  79. drawEntity(something);
  80. }
  81. updateEntityPosition = function(something){
  82. something.x += something.spdX;
  83. something.y += something.spdY;
  84.  
  85. if(something.x < 0 || something.x > WIDTH){
  86. something.spdX = -something.spdX;
  87. }
  88. if(something.y < 0 || something.y > HEIGHT){
  89. something.spdY = -something.spdY;
  90. }
  91. }
  92.  
  93. testCollisionRectRect = function(rect1,rect2){
  94. return rect1.x <= rect2.x+rect2.width
  95. && rect2.x <= rect1.x+rect1.width
  96. && rect1.y <= rect2.y + rect2.height
  97. && rect2.y <= rect1.y + rect1.height;
  98. }
  99.  
  100.  
  101. drawEntity = function(something){
  102. ctx.save();
  103. ctx.fillStyle = something.color;
  104. ctx.fillRect(something.x-something.width/2,something.y-something.height/2,something.width,something.height);
  105. ctx.restore();
  106. }
  107.  
  108.  
  109.  
  110. update = function(){
  111. ctx.clearRect(0,0,WIDTH,HEIGHT);
  112.  
  113. for(var key in enemyList){
  114. updateEntity(enemyList[key]);
  115.  
  116. var isColliding = testCollisionEntity(player,enemyList[key]);
  117. if(isColliding){
  118. player.hp = player.hp - 1;
  119. if(player.hp <= 0){
  120. var timeSurvived = Date.now() - timeWhenGameStarted;
  121.  
  122. console.log("You lost! You survived for " + timeSurvived + " ms.");
  123. timeWhenGameStarted = Date.now();
  124. player.hp = 10;
  125. }
  126. }
  127.  
  128. }
  129.  
  130. drawEntity(player);
  131. ctx.fillText(player.hp + " Hp",0,30);
  132. }
  133.  
  134. Enemy('E1',150,350,10,15,30,30);
  135. Enemy('E2',250,350,10,-15,20,20);
  136. Enemy('E3',250,150,10,-8,40,10);
  137.  
  138.  
  139. setInterval(update,40);
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement