Advertisement
Guest User

array problem

a guest
Nov 5th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Asteroid Game</title>
  5. <style>
  6. html, body {
  7. width: 100%;
  8. height: 100%;
  9. margin: 0px;
  10. overflow: hidden;
  11. display: block;
  12. }
  13. canvas {
  14. position:absolute;
  15. left:0px;
  16. top:0px;
  17. padding:0;
  18. margin:0;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <canvas id="main"></canvas>
  24. <script>
  25.  
  26.  
  27. function getMousePos(canvas, evt) {
  28. var rect = canvas.getBoundingClientRect();
  29. return {
  30. x: evt.clientX - rect.left,
  31. y: evt.clientY - rect.top
  32. };
  33. }
  34.  
  35. function Bullet(x,y,rot,vel) {
  36. this.x = x;
  37. this.y = y;
  38. this.rot = rot;
  39. this.vel = (vel+5);
  40.  
  41. this.move = function() {
  42. this.x += this.vel*Math.cos(this.rot-Math.PI/2);
  43. this.y += this.vel*Math.sin(this.rot-Math.PI/2);
  44. }
  45.  
  46. this.draw = function() {
  47. engine.circle(this.x, this.y, 4, "black");
  48.  
  49. var c = engine.canvas.getContext('2d');
  50. c.translate(this.x, this.y);
  51. c.rotate(this.rot);
  52. c.beginPath();
  53. c.strokeStyle="#00FF00";
  54. c.strokeRect(-5, -5, 10, 10);
  55. c.closePath();
  56. c.stroke();
  57. }
  58. }
  59.  
  60.  
  61. var engine = {
  62. canvas: document.getElementById('main'),
  63. mousePos: {x:0,y:0},
  64. mouseDown: false,
  65. keyDown: {
  66. w: false,
  67. s: false,
  68. a: false,
  69. d: false,
  70. sp: false
  71. },
  72. bullets: [],
  73.  
  74. circle: function(x,y,r,color){
  75. context = engine.canvas.getContext('2d');
  76. context.beginPath();
  77. context.arc(x, y, r, 0, 2*Math.PI);
  78. context.closePath();
  79. if(color === undefined) {
  80. context.stroke();
  81. } else {
  82. context.fillStyle=color;
  83. context.fill();
  84. }
  85. },
  86.  
  87. player: {
  88. rot: 0,
  89. x: 0,
  90. y: 0,
  91. velocity: 0,
  92. maxVelocity: 5,
  93. accel: 1,
  94. fire: 20,
  95. move: function() {
  96. // obsolete mouse controls
  97. /*if(engine.mouseDown == true && typeof engine.mousePos.x !== undefined) {
  98. dX = engine.mousePos.x-this.x;
  99. dY = engine.mousePos.y-this.y;
  100. dR = Math.sqrt((dX*dX)+(dY*dY));
  101. this.rot = Math.asin(dX/dR);
  102. if(dY >= 0) { this.rot-=Math.PI; this.rot=-this.rot }
  103. }*/
  104.  
  105. if(engine.keyDown.w == true && engine.keyDown.s == false) {
  106. if(this.velocity<this.maxVelocity){ this.velocity += this.accel; }
  107. } else if(engine.keyDown.s == true && engine.keyDown.w == false) {
  108. if(this.velocity>0) { this.velocity -= this.accel; }
  109. }
  110. if(engine.keyDown.a == true && engine.keyDown.d == false) {
  111. this.rot-=Math.PI/24;
  112. } else if(engine.keyDown.d == true && engine.keyDown.a == false) {
  113. this.rot+=Math.PI/24;
  114. }
  115. this.x += this.velocity*Math.cos(this.rot-Math.PI/2);
  116. this.y += this.velocity*Math.sin(this.rot-Math.PI/2);
  117. },
  118. draw: function() {
  119. context = engine.canvas.getContext('2d');
  120. context.save();
  121. context.translate(this.x,this.y);
  122. context.rotate(this.rot);
  123. context.beginPath();
  124. context.moveTo(-30, 50);
  125. context.lineTo(30, 50);
  126. context.lineTo(0, -50);
  127. context.lineTo(-30, 50);
  128. context.closePath();
  129. context.stroke();
  130. context.restore();
  131.  
  132. // obsolete mouse controls
  133. /*if(engine.mouseDown == true && typeof engine.mousePos.x !== undefined) {
  134. context.beginPath();
  135. context.moveTo(0,0);
  136. dX = engine.mousePos.x-this.x;
  137. dY = engine.mousePos.y-this.y;
  138. dR = Math.sqrt((dX*dX)+(dY*dY));
  139. context.lineTo(0,-dR);
  140. context.closePath();
  141. context.stroke();
  142. }*/
  143. },
  144. shoot: function() {
  145. if(engine.keyDown.sp == true) {
  146. if(this.fire > 20) {
  147. engine.bullets.unshift(new Bullet(this.x, this.y, this.rot, this.velocity));
  148. this.fire = 0;
  149. } else {
  150. this.fire++
  151. }
  152. }
  153. for(i = 0; i < engine.bullets.length; i++) {
  154. engine.bullets[i].move();
  155. engine.bullets[i].draw();
  156. if(engine.bullets[i].x > engine.canvas.width+5 || engine.bullets[i].x < -5
  157. || engine.bullets[i].y > engine.canvas.height+5 || engine.bullets[i].y < -5) {
  158. console.log('bullet gone, '+i);
  159. engine.bullets.splice(i, 1);
  160. }
  161. }
  162. },
  163. update: function() {
  164. this.move();
  165. this.draw();
  166. this.shoot();
  167. }
  168. },
  169.  
  170. draw: function() {
  171. engine.clear();
  172. engine.circle(engine.canvas.width/8, engine.canvas.height/2, 50, "black");
  173. engine.player.update();
  174. },
  175.  
  176. resizeCanvas: function() {
  177. engine.canvas.width = window.innerWidth;
  178. engine.canvas.height = window.innerHeight;
  179. },
  180.  
  181. clear: function() {
  182. engine.canvas.width = engine.canvas.width;
  183. engine.canvas.height = engine.canvas.height;
  184. },
  185.  
  186. init: function() {
  187. engine.resizeCanvas();
  188. var framerate = 1000/30;
  189. engine.player.x = engine.canvas.width/2;
  190. engine.player.y = engine.canvas.height/2;
  191.  
  192. window.setInterval(engine.draw,framerate);
  193. window.addEventListener('resize', engine.resizeCanvas, false);
  194. engine.canvas.addEventListener('mousemove', function(evt) {
  195. engine.mousePos = getMousePos(engine.canvas, evt);
  196. });
  197. engine.canvas.addEventListener('mousedown', function(evt) {
  198. engine.mouseDown = true;
  199. });
  200. engine.canvas.addEventListener('mouseup', function(evt) {
  201. engine.mouseDown = false;
  202. });
  203. window.addEventListener('keydown', function(evt) {
  204. if(evt.key == 'w' || evt.key == 'W') { engine.keyDown.w = true; }
  205. if(evt.key == 'a' || evt.key == 'A') { engine.keyDown.a = true; }
  206. if(evt.key == 's' || evt.key == 'S') { engine.keyDown.s = true; }
  207. if(evt.key == 'd' || evt.key == 'D') { engine.keyDown.d = true; }
  208. if(evt.key == " " || evt.key == " ") { engine.keyDown.sp = true;}
  209. });
  210. window.addEventListener('keyup', function(evt) {
  211. if(evt.key == 'w' || evt.key == 'W') { engine.keyDown.w = false; }
  212. if(evt.key == 'a' || evt.key == 'A') { engine.keyDown.a = false; }
  213. if(evt.key == 's' || evt.key == 'S') { engine.keyDown.s = false; }
  214. if(evt.key == 'd' || evt.key == 'D') { engine.keyDown.d = false; }
  215. if(evt.key == " " || evt.key == " ") { engine.keyDown.sp = false;}
  216. });
  217. }
  218. }
  219.  
  220. window.onload = engine.init();
  221.  
  222. </script>
  223. </body>
  224. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement