Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. RE, alors voila la version Sans undertale :)
  2.  
  3. En gros, tu re-vas dans le dossier créé sur ton burau, et tu refais les étapes de ttalheure, sauf que tu copies ce code a la place de l'autre:
  4.  
  5. <canvas id="ctx" width="500" height="500" style="border:1px solid #000000;"></canvas>
  6.  
  7. <script>
  8. var ctx = document.getElementById("ctx").getContext("2d");
  9. ctx.font = '30px Arial';
  10.  
  11. var HEIGHT = 500;
  12. var WIDTH = 500;
  13. var timeWhenGameStarted = Date.now(); //return time in ms
  14.  
  15. var player = {
  16. x:50,
  17. spdX:30,
  18. y:40,
  19. spdY:5,
  20. name:'P',
  21. hp:10,
  22. width:20,
  23. height:20,
  24. color:'green',
  25. };
  26.  
  27. var enemyList = {};
  28.  
  29.  
  30. getDistanceBetweenEntity = function (entity1,entity2){ //return distance (number)
  31. var vx = entity1.x - entity2.x;
  32. var vy = entity1.y - entity2.y;
  33. return Math.sqrt(vx*vx+vy*vy);
  34. }
  35.  
  36. testCollisionEntity = function (entity1,entity2){ //return if colliding (true/false)
  37. var rect1 = {
  38. x:entity1.x-entity1.width/2,
  39. y:entity1.y-entity1.height/2,
  40. width:entity1.width,
  41. height:entity1.height,
  42. }
  43. var rect2 = {
  44. x:entity2.x-entity2.width/2,
  45. y:entity2.y-entity2.height/2,
  46. width:entity2.width,
  47. height:entity2.height,
  48. }
  49. return testCollisionRectRect(rect1,rect2);
  50.  
  51. }
  52.  
  53. Enemy = function (id,x,y,spdX,spdY,width,height){
  54. var enemy3 = {
  55. x:x,
  56. spdX:spdX,
  57. y:y,
  58. spdY:spdY,
  59. name:'E',
  60. id:id,
  61. width:500,
  62. height:50,
  63. color:'red',
  64. };
  65. enemyList[id] = enemy3;
  66.  
  67. }
  68.  
  69. document.onmousemove = function(mouse){
  70. var mouseX = mouse.clientX;
  71. var mouseY = mouse.clientY;
  72.  
  73. player.x = mouseX;
  74. player.y = mouseY;
  75. }
  76.  
  77.  
  78.  
  79.  
  80.  
  81. updateEntity = function (something){
  82. updateEntityPosition(something);
  83. drawEntity(something);
  84. }
  85. updateEntityPosition = function(something){
  86. something.x += something.spdX;
  87. something.y += something.spdY;
  88.  
  89. if(something.x < 0 || something.x > WIDTH){
  90. something.spdX = -something.spdX;
  91. }
  92. if(something.y < 0 || something.y > HEIGHT){
  93. something.spdY = -something.spdY;
  94. }
  95. }
  96.  
  97. testCollisionRectRect = function(rect1,rect2){
  98. return rect1.x <= rect2.x+rect2.width
  99. && rect2.x <= rect1.x+rect1.width
  100. && rect1.y <= rect2.y + rect2.height
  101. && rect2.y <= rect1.y + rect1.height;
  102. }
  103.  
  104.  
  105. drawEntity = function(something){
  106. ctx.save();
  107. ctx.fillStyle = something.color;
  108. ctx.fillRect(something.x-something.width/2,something.y-something.height/2,something.width,something.height);
  109. ctx.restore();
  110. }
  111.  
  112.  
  113.  
  114. update = function(){
  115. ctx.clearRect(0,0,WIDTH,HEIGHT);
  116.  
  117. for(var key in enemyList){
  118. updateEntity(enemyList[key]);
  119.  
  120. var isColliding = testCollisionEntity(player,enemyList[key]);
  121. if(isColliding){
  122. player.hp = player.hp - 1;
  123. if(player.hp <= 0){
  124. var timeSurvived = Date.now() - timeWhenGameStarted;
  125.  
  126. console.log("You lost! You survived for " + timeSurvived + " ms.");
  127. timeWhenGameStarted = Date.now();
  128. player.hp = 10;
  129. }
  130. }
  131.  
  132. }
  133.  
  134. drawEntity(player);
  135. ctx.fillText(player.hp + " Hp",0,30);
  136. }
  137.  
  138. Enemy('E1',150,350,10,15,30,30);
  139. Enemy('E2',250,350,10,-15,20,20);
  140. Enemy('E3',250,150,10,-8,40,10);
  141.  
  142.  
  143. setInterval(update,40);
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156. </script>
  157. VOILA
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement