Advertisement
Raset24

Untitled

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