Advertisement
Guest User

Untitled

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