Advertisement
Guest User

Untitled

a guest
Aug 5th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. // Get the 2D context from the canvas in
  2. //our HTML page
  3. var canvas = document.getElementById("gameCanvas");
  4. var context = canvas.getContext("2d");
  5.  
  6. // set up some event handlers to process keyboard input
  7. window.addEventListener('keydown', function(evt) { onKeyDown(evt); }, false);
  8. window.addEventListener('keyup', function(evt) { onKeyUp(evt); }, false);
  9.  
  10. var SCREEN_WIDTH = canvas.width;
  11. var SCREEN_HEIGHT = canvas.height;
  12.  
  13. var ASTEROID_SPEED = 0.8;
  14. var PLAYER_SPEED = 1;
  15. var PLAYER_TURN_SPEED = 0.04;
  16. var BULLET_SPEED = 1.5;
  17.  
  18. //this will create the player object and assigns it some properties
  19. var player = {
  20. image: document.createElement("img"),
  21. x: SCREEN_WIDTH/2,
  22. y: SCREEN_HEIGHT/2,
  23. width: 93,
  24. height: 80,
  25. directionX: 0,
  26. directionY: 0,
  27. angularDirection: 0,
  28. rotation: 0
  29. };
  30.  
  31. //players image here
  32. player.image.src = "ship.png";
  33.  
  34.  
  35. // key constants
  36. // Go here for a list of key codes:
  37. // https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
  38. var KEY_SPACE = 32;
  39. var KEY_A = 65;
  40. var KEY_W = 87;
  41. var KEY_D = 68;
  42. var KEY_S = 83;
  43.  
  44.  
  45. //this creates the asteroid object and assigns it some properties
  46. var asteroid = {
  47. image: document.createElement("img"),
  48. x: 100,
  49. y: 100,
  50. width: 69,
  51. height: 75,
  52. directionX: 0,
  53. directionY: 0,
  54. isDead: false
  55. };
  56.  
  57. //set the image
  58. asteroid.image.src = "rock_large.png";
  59. asteroid.directionX = 10;
  60. asteroid.directionY = 8;
  61.  
  62. //normalize the velocity (make it equal to 1)
  63.  
  64. var magnitude = Math.sqrt( (asteroid.directionX * asteroid.directionX) + (asteroid.directionY * asteroid.directionY) );
  65.  
  66. if (magnitude != 0)
  67. {
  68. asteroid.directionx /= magnitude;
  69. asteroid.directionY /= magnitude;
  70. }
  71.  
  72. // this creates the bullet object and assigns it some properties
  73. var bullet = {
  74. image: document.createElement("img"),
  75. x: player.x,
  76. y: player.y,
  77. width: 5,
  78. height: 5,
  79. velocityX: 0,
  80. velocityY: 0,
  81. isDead: true
  82. };
  83. // set the image for the asteroid to use
  84. bullet.image.src = "bullet.png";
  85.  
  86. function playerShoot()
  87. {
  88. // we can only have 1 bullet at a time
  89. if( bullet.isDead == false )
  90. return;
  91. // start off with a velocity that shoots the bullet straight up
  92. var velX = 0;
  93. var velY = 1;
  94. // now rotate this vector acording to the ship's current rotation
  95. var s = Math.sin(player.rotation);
  96. var c = Math.cos(player.rotation);
  97. // for an explanation of this formula,
  98. // see http://en.wikipedia.org/wiki/Rotation_matrix
  99. var xVel = (velX * c) - (velY * s);
  100. var yVel = (velX * s) + (velY * c);
  101. // dont bother storing a direction and calculating the
  102. // velocity every frame, because it won't change.
  103. // Just store the pre-calculated velocity
  104. bullet.velocityX = xVel * BULLET_SPEED;
  105. bullet.velocityY = yVel * BULLET_SPEED;
  106. // don't forget to set the bullet's position
  107. bullet.x = player.x;
  108. bullet.y = player.y;
  109. // make the bullet alive
  110. bullet.isDead = false;
  111.  
  112.  
  113. function onKeyDown(event)
  114. {
  115. if(event.keyCode == KEY_W)
  116. {
  117. player.directionY = 1;
  118. }
  119.  
  120. if(event.keyCode == KEY_S)
  121. {
  122. player.directionY = -1;
  123. }
  124.  
  125. if(event.keyCode == KEY_A)
  126. {
  127. player.angularDirection = -1;
  128. }
  129.  
  130. if(event.keyCode == KEY_D)
  131. {
  132. player.angularDirection = 1;
  133. }
  134. }
  135.  
  136. function onKeyUp(event)
  137. {
  138. if(event.keyCode == KEY_W)
  139. {
  140. player.directionY = 0;
  141. }
  142. if(event.keyCode == KEY_S)
  143. {
  144. player.directionY = 0;
  145. }
  146. if(event.keyCode == KEY_A)
  147. {
  148. player.angularDirection = 0;
  149. }
  150. if(event.keyCode == KEY_D)
  151. {
  152. player.angularDirection = 0;
  153. }
  154. if(event.keyCode == KEY_SPACE)
  155. {
  156. playerShoot();
  157. }
  158.  
  159. }
  160.  
  161.  
  162. // tests if two rectangles are intersecting.
  163. // Pass in the x,y coordinates, width and height of each rectangle.
  164. // Returns 'true' if the rectangles are intersecting
  165. function intersects(x1, y1, w1, h1, x2, y2, w2, h2)
  166. {
  167. if(y2 + h2 < y1 ||
  168. x2 + w2 < x1 ||
  169. x2 > x1 + w1 ||
  170. y2 > y1 + h1)
  171. {
  172. return false;
  173. }
  174. return true;
  175. }
  176.  
  177. function run()
  178. {
  179. context.fillStyle = "#8a2be2";
  180. context.fillRect(0, 0, canvas.width, canvas.height);
  181.  
  182. // update and draw the bullet
  183. // we want to do this first so that the plane is drawn on
  184. // top of the bullet
  185. if(bullet.isDead == false)
  186. {
  187. bullet.x += bullet.velocityX;
  188. bullet.y += bullet.velocityY;
  189. context.drawImage(bullet.image,
  190. bullet.x - bullet.width/2,
  191. bullet.y - bullet.height/2);
  192.  
  193. if(asteroid.isDead == false)
  194. {
  195. var hit = intersects(
  196. bullet.x, bullet.y,
  197. bullet.width, bullet.height,
  198. asteroid.x, asteroid.y,
  199. asteroid.width, asteroid.height);
  200. if(hit == true)
  201. {
  202. bullet.isDead = true;
  203. asteroid.isDead = true;
  204. }
  205. }
  206. if(bullet.x < 0 || bullet.x > SCREEN_WIDTH ||
  207. bullet.y < 0 || bullet.y > SCREEN_HEIGHT)
  208. {
  209. bullet.isDead = true;
  210. }
  211. }
  212.  
  213. //calculate sin and cos for the players current rotation
  214. var s = Math.sin(player.rotation);
  215. var c = Math.cos(player.rotation);
  216.  
  217. // figure out what the player's velocity will be based on the current rotation
  218. // (so that if the ship is moving forward, then it will move forward according to its
  219. // rotation. Without this, the ship would just move up the screen when we pressed 'up',
  220. // regardless of which way it was rotated)
  221. // for an explanation of this formula, see http://en.wikipedia.org/wiki/Rotation_matrix
  222.  
  223. var xDir = (player.directionX * c) - (player.directionY * s);
  224. var yDir = (player.directionX * s) + (player.directionY * c);
  225. var xVel = xDir * PLAYER_SPEED;
  226. var yVel = yDir * PLAYER_SPEED;
  227.  
  228. player.x += xVel;
  229. player.y += yVel;
  230.  
  231. player.rotation += player.angularDirection * PLAYER_TURN_SPEED;
  232.  
  233. context.save();
  234. context.translate(player.X, player.Y);
  235. context.rotate(player.rotation);
  236. context.drawImage(player.image, -player.width/2, -player.height/2);
  237. context.restore();
  238.  
  239. //update and draw asteroid
  240. var velocityX = asteroid.directionX * ASTEROID_SPEED;
  241. var velocityY = asteroid.directionY * ASTEROID_SPEED;
  242. asteroid.x += velocityX;
  243. asteroid.y += velocityY;
  244. context.drawImage(asteroid.image,
  245. asteroid.X - asteroid.width/2,
  246. asteroid.y - asteroid.height/2);
  247. }
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266. //-------------------- Don't modify anything below here
  267. // This code will set up the framework so that the 'run' function is
  268. // called 60 times per second. We have some options to fall back on
  269. // in case the browser doesn't support our preferred method.
  270. (function() {
  271. var onEachFrame;
  272. if (window.requestAnimationFrame) {
  273. onEachFrame = function(cb) {
  274. var _cb = function() { cb(); window.requestAnimationFrame(_cb); }
  275. _cb();
  276. };
  277. } else if (window.mozRequestAnimationFrame) {
  278. onEachFrame = function(cb) {
  279. var _cb = function() { cb();
  280. window.mozRequestAnimationFrame(_cb); }
  281. _cb();
  282. };
  283. } else {
  284. onEachFrame = function(cb) {
  285. setInterval(cb, 1000 / 60);
  286. }
  287. }
  288.  
  289. window.onEachFrame = onEachFrame;
  290. })();
  291. window.onEachFrame(run);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement