Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var canvas = document.getElementById("gameCanvas");
  2. var context = canvas.getContext("2d");
  3.  
  4.  
  5. // set up some event handlers to process keyboard input
  6. window.addEventListener('keydown', function(evt) { onKeyDown(evt); }, false);
  7. window.addEventListener('keyup', function(evt) { onKeyUp(evt); }, false);
  8.  
  9. var startFrameMillis = Date.now();
  10. var endFrameMillis = Date.now();
  11. function getDeltaTime() // Only call this function once per frame
  12. {
  13. endFrameMillis = startFrameMillis;
  14. startFrameMillis = Date.now();
  15. var deltaTime = (startFrameMillis - endFrameMillis) * 0.001;
  16. if (deltaTime > 1) // validate that the delta is within range
  17. {
  18. deltaTime = 1;
  19. }
  20. return deltaTime;
  21. }
  22.  
  23. var SCREEN_WIDTH = canvas.width;
  24. var SCREEN_HEIGHT = canvas.height;
  25. var ASTEROID_SPEED = 1.5;
  26. var PLAYER_SPEED = 2;
  27. var PLAYER_TURN_SPEED = 0.04;
  28. var BULLET_SPEED = 3;
  29.  
  30.  
  31. var backgroundImage = document.createElement("img");backgroundImage.src = "bg5.png";
  32.  
  33. // load the image to use for the tiled background
  34. var grass = document.createElement("img");
  35. grass.src = "grass.png";
  36.  
  37. // create the tiled background
  38. var background = [];
  39. for(var y=0;y<15;y++)
  40. {
  41. background[y] = [];
  42. for(var x=0; x<20; x++)
  43. background[y][x] = grass;
  44. }
  45.  
  46. // this creates the player object and assigns it some properties
  47. var player = {
  48. image: document.createElement("img"),
  49. x: SCREEN_WIDTH/2,
  50. y: SCREEN_HEIGHT/2,
  51. width: 93,
  52. height: 80,
  53. directionX: 0,
  54. directionY: 0,
  55. angularDirection: 0,
  56. rotation: 0
  57. };
  58. // set the image for the player to use
  59. player.image.src = "player2.png";
  60.  
  61. // Create an array to store our asteroids
  62. var asteroids = [];
  63.  
  64. // rand(floor, ceil)
  65. // Return a random number within the range of the two input variables
  66. function rand(floor, ceil)
  67. {
  68. return Math.floor( (Math.random()* (ceil-floor)) +floor );
  69. }
  70. // Create a new random asteroid and add it to our asteroids array.
  71. // We'll give the asteroid a random position (just off screen) and
  72. // set it moving towards the center of the screen
  73. function spawnAsteroid()
  74. {
  75. // make a random variable to specify which asteroid image to use
  76. // (small, mediam or large)
  77. var type = rand(0, 3);
  78. // create the new asteroid
  79. var asteroid = {};
  80. asteroid.image = document.createElement("img");
  81. asteroid.image.src = "rock_large.png";
  82. asteroid.width = 69;
  83. asteroid.height = 75;
  84. // to set a random position just off screen, we'll start at the centre of the
  85. // screen then move in a random direction by the width of the screen
  86. var x = SCREEN_WIDTH/2;
  87. var y = SCREEN_HEIGHT/2;
  88. var dirX = rand(-10,10);
  89. var dirY = rand(-10,10);
  90. // 'normalize' the direction (the hypotenuse of the triangle formed
  91. // by x,y will equal 1)
  92. var magnitude = (dirX * dirX) + (dirY * dirY);
  93. if(magnitude != 0)
  94. {
  95. var oneOverMag = 1 / Math.sqrt(magnitude);
  96. dirX *= oneOverMag;
  97. dirY *= oneOverMag;
  98. }
  99. // now we can multiply the dirX/Y by the screen width to move that amount from
  100. // the centre of the screen
  101. var movX = dirX * SCREEN_WIDTH;
  102. var movY = dirY * SCREEN_HEIGHT;
  103. // add the direction to the original position to get the starting position of the
  104. // asteroid
  105. asteroid.x = x + movX;
  106. asteroid.y = y + movY;
  107. // now, the easy way to set the velocity so that the asteroid moves towards the
  108. // centre of the screen is to just reverse the direction we found earlier
  109. asteroid.velocityX = -dirX * ASTEROID_SPEED;
  110. asteroid.velocityY = -dirY * ASTEROID_SPEED;
  111. // finally we can add our new asteroid to the end of our asteroids array
  112. asteroids.push(asteroid);
  113. }
  114.  
  115. // this creates the bullet object and assigns it some properties
  116. var bullet = {
  117. image: document.createElement("img"),
  118. x: player.x,
  119. y: player.y,
  120. width: 5,
  121. height: 5,
  122. velocityX: 0,
  123. velocityY: 0,
  124. isDead: true
  125. };
  126. // set the image for the bullet to use
  127. bullet.image.src = "bullet.png";
  128.  
  129. // tests if two rectangles are intersecting.
  130. // Pass in the x,y coordinates, width and height of each rectangle.
  131. // Returns 'true' if the rectangles are intersecting
  132. function intersects(x1, y1, w1, h1, x2, y2, w2, h2)
  133. {
  134.     if(y2 + h2 < y1 ||
  135.         x2 + w2 < x1 ||
  136.         x2 > x1 + w1 ||
  137.         y2 > y1 + h1)
  138.     {
  139.         return false;
  140.     }
  141. return true;
  142. }
  143.  
  144. function run()
  145. {
  146.  
  147. context.fillStyle = "#00FFFF";
  148. context.fillRect(0, 0, canvas.width, canvas.height);
  149.  
  150.  
  151. var deltaTime = getDeltaTime();
  152. // draw the tiled background (make sure you do this first, so everything
  153. // else in the scene will be drawn on top of the tiled background)
  154. for(var y=0; y<15; y++)
  155. {
  156. for(var x=0; x<20; x++)
  157. {
  158. context.drawImage(background[y][x], x*32, y*32);
  159. }
  160. }
  161. context.drawImage(backgroundImage, 0 ,0);
  162.  
  163. // update and draw the bullet
  164. // we want to do this first so that the plane is drawn on
  165. // top of the bullet
  166. if(bullet.isDead == false)
  167. {
  168. bullet.x += bullet.velocityX;
  169. bullet.y += bullet.velocityY;
  170. context.drawImage(bullet.image,
  171. bullet.x - bullet.width/2,
  172. bullet.y - bullet.height/2);
  173. // update all the asterioids in the asteroids array
  174. for(var i=0; i<asteroids.length; i++)
  175. {
  176. // update the asteroids position according to its current velocity.
  177. // TODO: Dont forget to multiply by deltaTime to get a constant speed
  178. asteroids[i].x = asteroids[i].x + asteroids[i].velocityX;
  179. asteroids[i].y = asteroids[i].y + asteroids[i].velocityY;
  180. // TODO: check if the asteroid has gone out of the screen boundaries
  181. // If so, wrap the astroid around the screen so it comes back from the
  182. // other side
  183. }
  184. // draw all the asteroids
  185. for(var i=0; i<asteroids.length; i++)
  186. {
  187. context.drawImage(asteroids[i].image, asteroids[i].x, asteroids[i].y);
  188. }
  189. spawnTimer -= deltaTime;
  190. if(spawnTimer <= 0)
  191. {
  192. spawnTimer = 1;
  193. spawnAsteroid();
  194. }
  195. }
  196.  
  197.  
  198. // calculate sin and cos for the player's current rotation
  199. var s = Math.sin(player.rotation);
  200. var c = Math.cos(player.rotation);
  201.    
  202.         // figure out what the player's velocity will be based on the current rotation
  203.         // (so that if the ship is moving forward, then it will move forward according to its
  204.         // rotation. Without this, the ship would just move up the screen when we pressed 'up',
  205.         // regardless of which way it was rotated)
  206.         // for an explanation of this formula, see http://en.wikipedia.org/wiki/Rotation_matrix
  207.     var xDir = (player.directionX * c) - (player.directionY * s);
  208.     var yDir = (player.directionX * s) + (player.directionY * c);
  209.     var xVel = xDir * PLAYER_SPEED;
  210.     var yVel = yDir * PLAYER_SPEED;
  211.  
  212.     player.x += xVel;
  213.     player.y += yVel;
  214.  
  215.     player.rotation += player.angularDirection * PLAYER_TURN_SPEED;
  216.  
  217.  
  218.    
  219.     // update and draw the asteroid (we don't need to
  220. // worry about rotation here)
  221. var velocityX = asteroid.directionX * ASTEROID_SPEED;
  222. var velocityY = asteroid.directionY * ASTEROID_SPEED;
  223. asteroid.x += velocityX;
  224. asteroid.y += velocityY;
  225. context.drawImage(asteroid.image,
  226. asteroid.x - asteroid.width/2,
  227. asteroid.y - asteroid.height/2);
  228.    
  229.     context.save();
  230.         context.translate(player.x, player.y);
  231.         context.rotate(player.rotation);
  232.         context.drawImage(
  233.             player.image, -player.width/2, -player.height/2);
  234.     context.restore();
  235.  
  236.     //Wrapping Player
  237. if ( player.x > SCREEN_WIDTH)
  238. {
  239. context.save();
  240. context.translate( player.x = 0, player.y)
  241. context.restore();
  242. }
  243. if ( player.x < 0)
  244. {
  245. context.save();
  246. context.translate( player.x = SCREEN_WIDTH, player.y)
  247. context.restore();
  248. }
  249. if ( player.y > SCREEN_HEIGHT)
  250. {
  251. context.save();
  252. context.translate( player.x, player.y = 0)
  253. context.restore();
  254. }
  255. if ( player.y < 0)
  256. {
  257. context.save();
  258. context.translate( player.x, player.y = SCREEN_HEIGHT)
  259. context.restore();
  260. }
  261.  
  262. //Wrapping Asteroid
  263. if ( asteroid.x > SCREEN_WIDTH)
  264. {
  265. context.save();
  266. context.translate( asteroid.x = 0, asteroid.y)
  267. context.restore();
  268. }
  269. if ( asteroid.x < 0)
  270. {
  271. context.save();
  272. context.translate( asteroid.x = SCREEN_WIDTH, asteroid.y)
  273. context.restore();
  274. }
  275. if ( asteroid.y > SCREEN_HEIGHT)
  276. {
  277. context.save();
  278. context.translate( asteroid.x, asteroid.y = 0)
  279. context.restore();
  280. }
  281. if ( asteroid.y < 0)
  282. {
  283. context.save();
  284. context.translate( asteroid.x, asteroid.y = SCREEN_HEIGHT)
  285. context.restore();
  286. }
  287.  
  288.  
  289. }
  290.  
  291.  
  292. var KEY_SPACE = 32;
  293. var KEY_LEFT = 37;
  294. var KEY_UP = 38;
  295. var KEY_RIGHT = 39;
  296. var KEY_DOWN = 40;
  297.  
  298. function onKeyDown(event)
  299. {
  300. if(event.keyCode == KEY_UP)
  301. {
  302. player.directionY = 1;
  303. }
  304. if(event.keyCode == KEY_DOWN)
  305. {
  306. player.directionY = -1;
  307. }
  308. if(event.keyCode == KEY_LEFT)
  309. {
  310. player.angularDirection = -1;
  311. }
  312. if(event.keyCode == KEY_RIGHT)
  313. {
  314. player.angularDirection = 1;
  315. }
  316. }
  317. function onKeyUp(event)
  318. {
  319. if(event.keyCode == KEY_UP)
  320. {
  321. player.directionY = 0;
  322. }
  323. if(event.keyCode == KEY_DOWN)
  324. {
  325. player.directionY = 0;
  326. }
  327. if(event.keyCode == KEY_LEFT)
  328. {
  329. player.angularDirection = 0;
  330. }
  331. if(event.keyCode == KEY_RIGHT)
  332. {
  333. player.angularDirection = 0;
  334. }
  335. if(event.keyCode == KEY_SPACE)
  336. {
  337. playerShoot();
  338. }
  339. }
  340.  
  341. function playerShoot()
  342. {
  343. // we can only have 1 bullet at a time
  344. if( bullet.isDead == false )
  345. return;
  346. // start off with a velocity that shoots the bullet straight up
  347. var velX = 0;
  348. var velY = 1;
  349. // now rotate this vector acording to the ship's current rotation
  350. var s = Math.sin(player.rotation);
  351. var c = Math.cos(player.rotation);
  352. // for an explanation of this formula,
  353. // see http://en.wikipedia.org/wiki/Rotation_matrix
  354. var xVel = (velX * c) - (velY * s);
  355. var yVel = (velX * s) + (velY * c);
  356. // dont bother storing a direction and calculating the
  357. // velocity every frame, because it won't change.
  358. // Just store the pre-calculated velocity
  359. bullet.velocityX = xVel * BULLET_SPEED;
  360. bullet.velocityY = yVel * BULLET_SPEED;
  361. // don't forget to set the bullet's position
  362. bullet.x = player.x;
  363. bullet.y = player.y;
  364. // make the bullet alive
  365. bullet.isDead = false;
  366. }
  367.  
  368. //60 FPS framework
  369.  
  370. {
  371. }
  372.  
  373. (function() {
  374. var onEachFrame;
  375. if (window.requestAnimationFrame)
  376. {
  377. onEachFrame = function(cb)
  378. {
  379. var _cb = function() { cb(); window.requestAnimationFrame(_cb); }
  380. _cb();
  381. };
  382. }
  383. else if (window.mozRequestAnimationFrame)
  384. {
  385. onEachFrame = function(cb)
  386. {
  387. var _cb = function() { cb(); window.mozRequestAnimationFrame(_cb); }
  388. _cb();
  389. };
  390. }
  391. else
  392. {
  393. onEachFrame = function(cb)
  394. {
  395. setInterval(cb, 1000 / 60);
  396. }
  397. }
  398.  
  399. window.onEachFrame = onEachFrame;
  400. })();
  401. window.onEachFrame(run);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement