Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.26 KB | None | 0 0
  1. var canvas = document.getElementById("gameCanvas");
  2. var context = canvas.getContext("2d");
  3.  
  4. var grass = document.createElement("img");
  5. grass.src = "assets/grass.png";
  6.  
  7. var background = [];
  8.  
  9. for(var y=0;y<15;y++)
  10. {
  11. background[y] = [];
  12. for(var x=0; x<20; x++)
  13. background[y][x] = grass;
  14. }
  15.  
  16. // set up some event handlers to process keyboard input
  17. window.addEventListener('keydown', function(evt) { onKeyDown(evt); }, false);
  18. window.addEventListener('keyup', function(evt) { onKeyUp(evt); }, false);
  19.  
  20. var SCREEN_WIDTH = canvas.width;
  21. var SCREEN_HEIGHT = canvas.height;
  22.  
  23. var ASTEROID_SPEED = 0.8;
  24. var PLAYER_SPEED = 1;
  25. var PLAYER_TURN_SPEED = 0.04;
  26. var BULLET_SPEED = 1.5;
  27.  
  28. var player = {
  29. image: document.createElement("img"),
  30. x: SCREEN_WIDTH/2,
  31. y: SCREEN_HEIGHT/2,
  32. width: 93,
  33. height: 80,
  34. directionX: 0,
  35. directionY: 0,
  36. angularDirection: 0,
  37. rotation: 0
  38. };
  39. player.image.src = "assets/ship.png";
  40.  
  41. var asteroids = [];
  42.  
  43. // key constants
  44. // Go here for a list of key codes:
  45. // https://developer.mozilla.org/en-US/docs/DOM/KeyboardEvent
  46. var KEY_SPACE = 32;
  47. var KEY_LEFT = 37;
  48. var KEY_UP = 38;
  49. var KEY_RIGHT = 39;
  50. var KEY_DOWN = 40;
  51.  
  52.  
  53. //create all the bullets in our game
  54. var bullets = [];
  55.  
  56.  
  57. // rand(floor, ceil)
  58. // Return a random number within the range of the two input variables
  59. function rand(floor, ceil)
  60. {
  61. return Math.floor( (Math.random()* (ceil-floor)) +floor );
  62. }
  63.  
  64. // Create a new random asteroid and add it to our asteroids array.
  65. // We'll give the asteroid a random position (just off screen) and
  66. // set it moving towards the center of the screen
  67. function spawnAsteroid()
  68. {
  69. // make a random variable to specify which asteroid image to use
  70. // (small, mediam or large)
  71. var type = rand(0, 3);
  72.  
  73. // create the new asteroid
  74. var asteroid = {};
  75. asteroid.image = document.createElement("img");
  76. asteroid.image.src = "assets/rock_large.png";
  77. asteroid.width = 69;
  78. asteroid.height = 75;
  79.  
  80. // to set a random position just off screen, we'll start at the centre of the
  81. // screen then move in a random direction by the width of the screen
  82. var x = SCREEN_WIDTH/2;
  83. var y = SCREEN_HEIGHT/2;
  84.  
  85. var dirX = rand(-10,10);
  86. var dirY = rand(-10,10);
  87.  
  88. // 'normalize' the direction (the hypotenuse of the triangle formed
  89. // by x,y will equal 1)
  90. var magnitude = (dirX * dirX) + (dirY * dirY);
  91. if(magnitude != 0)
  92. {
  93. var oneOverMag = 1 / Math.sqrt(magnitude);
  94. dirX *= oneOverMag;
  95. dirY *= oneOverMag;
  96. }
  97. // now we can multiply the dirX/Y by the screen width to move that amount from
  98. // the centre of the screen
  99. var movX = dirX * SCREEN_WIDTH;
  100. var movY = dirY * SCREEN_HEIGHT;
  101.  
  102. // add the direction to the original position to get the starting position of the
  103. // asteroid
  104. asteroid.x = x + movX;
  105. asteroid.y = y + movY;
  106.  
  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.  
  112. // finally we can add our new asteroid to the end of our asteroids array
  113. asteroids.push(asteroid);
  114. }
  115.  
  116. function playerShoot()
  117. {
  118. var bullet = {
  119. image: document.createElement("img"),
  120. x: player.x,
  121. y: player.y,
  122. width: 5,
  123. height: 5,
  124. velocityX: 0,
  125. velocityY: 0
  126. };
  127.  
  128. bullet.image.src = "assets/bullet.png";
  129.  
  130. // start off with a velocity that shoots the bullet straight up
  131. var velX = 0;
  132. var velY = 1;
  133.  
  134. // now rotate this vector acording to the ship's current rotation
  135. var s = Math.sin(player.rotation);
  136. var c = Math.cos(player.rotation);
  137.  
  138. // for an explanation of this formula,
  139. // see http://en.wikipedia.org/wiki/Rotation_matrix
  140. var xVel = (velX * c) - (velY * s);
  141. var yVel = (velX * s) + (velY * c);
  142.  
  143. bullet.velocityX = xVel * BULLET_SPEED;
  144. bullet.velocityY = yVel * BULLET_SPEED;
  145.  
  146. // finally, add the bullet to the bullets array
  147. bullets.push(bullet);
  148. }
  149.  
  150. var shootTimer = 0;
  151. function onKeyDown(event)
  152. {
  153. if(event.keyCode == KEY_UP)
  154. {
  155. player.directionY = 1;
  156. }
  157. if(event.keyCode == KEY_DOWN)
  158. {
  159. player.directionY = -1;
  160. }
  161. if(event.keyCode == KEY_LEFT)
  162. {
  163. player.angularDirection = -1;
  164. }
  165. if(event.keyCode == KEY_RIGHT)
  166. {
  167. player.angularDirection = 1;
  168. }
  169. if(event.keyCode == KEY_SPACE && shootTimer <= 0)
  170. {
  171. shootTimer += 0.3;
  172. playerShoot();
  173. }
  174. }
  175.  
  176. function onKeyUp(event)
  177. {
  178. if(event.keyCode == KEY_UP)
  179. {
  180. player.directionY = 0;
  181. }
  182. if(event.keyCode == KEY_DOWN)
  183. {
  184. player.directionY = 0;
  185. }
  186. if(event.keyCode == KEY_LEFT)
  187. {
  188. player.angularDirection = 0;
  189. }
  190. if(event.keyCode == KEY_RIGHT)
  191. {
  192. player.angularDirection = 0;
  193. }
  194. }
  195.  
  196. function intersects(x1, y1, w1, h1, x2, y2, w2, h2)
  197. {
  198. if(y2 + h2 < y1 ||
  199. x2 + w2 < x1 ||
  200. x2 > x1 + w1 ||
  201. y2 > y1 + h1)
  202. {
  203. return false;
  204. }
  205. return true;
  206. }
  207.  
  208. function run()
  209. {
  210. context.fillStyle = "#ccc";
  211. context.fillRect(0, 0, canvas.width, canvas.height);
  212.  
  213. var deltaTime = getDeltaTime();
  214.  
  215. for(var y=0; y<15; y++)
  216. {
  217. for(var x=0; x<20; x++)
  218. {
  219. context.drawImage(background[y][x], x*32, y*32);
  220. }
  221. }
  222.  
  223. // check if any bullet intersects any asteroid. If so, kill them both
  224. for(var i=0; i<asteroids.length; i++)
  225. {
  226. for(var j=0; j<bullets.length; j++)
  227. {
  228. if(intersects(
  229. bullets[j].x, bullets[j].y,
  230. bullets[j].width, bullets[j].height,
  231. asteroids[i].x, asteroids[i].y,
  232. asteroids[i].width, asteroids[i].height) == true)
  233. {
  234. asteroids.splice(i, 1);
  235. bullets.splice(j, 1);
  236. break;
  237. }
  238. }
  239. }
  240.  
  241.  
  242. // update all the asterioids in the asteroids array
  243. for(var i=0; i<asteroids.length; i++)
  244. {
  245. // update the asteroids position according to its current velocity.
  246. // TODO: Dont forget to multiply by deltaTime to get a constant speed
  247. asteroids[i].x = asteroids[i].x + asteroids[i].velocityX;
  248. asteroids[i].y = asteroids[i].y + asteroids[i].velocityY;
  249.  
  250. // TODO: check if the asteroid has gone out of the screen boundaries
  251. // If so, wrap the astroid around the screen so it comes back from the
  252. // other side
  253. }
  254.  
  255. //draw all the asteroids
  256. for(var i=0; i<asteroids.length; i++)
  257. {
  258. context.drawImage(asteroids[i].image, asteroids[i].x, asteroids[i].y);
  259. }
  260.  
  261. spawnTimer -= deltaTime;
  262. if(spawnTimer <= 0)
  263. {
  264. spawnTimer = 1;
  265. spawnAsteroid();
  266. }
  267. }
  268.  
  269. var s = Math.sin(player.rotation);
  270. var c = Math.cos(player.rotation);
  271. var xDir = (player.directionX * c) - (player.directionY * s);
  272. var yDir = (player.directionX * s) + (player.directionY * c);
  273. var xVel = xDir * PLAYER_SPEED;
  274. var yVel = yDir * PLAYER_SPEED;
  275.  
  276. player.x += xVel;
  277. player.y += yVel;
  278.  
  279. player.rotation += player.angularDirection * PLAYER_TURN_SPEED;
  280.  
  281. context.save();
  282. context.translate(player.x, player.y);
  283. context.rotate(player.rotation);
  284. context.drawImage(
  285. player.image, -player.width/2, -player.height/2);
  286. context.restore();
  287.  
  288. var velocityX = asteroid.directionX * ASTEROID_SPEED;
  289. var velocityY = asteroid.directionY * ASTEROID_SPEED;
  290. asteroid.x += velocityX;
  291. asteroid.Y += velocityY;
  292.  
  293. // update the shoot timer
  294. if(shootTimer > 0)
  295. shootTimer -= deltaTime;
  296.  
  297. // update all the bullets
  298. for(var i=0; i<bullets.length; i++)
  299. {
  300. bullets[i].x += bullets[i].velocityX;
  301. bullets[i].y += bullets[i].velocityY;
  302. }
  303.  
  304. for(var i=0; i<bullets.length; i++)
  305. {
  306. // check if the bullet has gone out of the screen boundaries
  307. // and if so kill it
  308. if(bullets[i].x < -bullets[i].width ||
  309. bullets[i].x > SCREEN_WIDTH ||
  310. bullets[i].y < -bullets[i].height ||
  311. bullets[i].y > SCREEN_HEIGHT)
  312. {
  313. // remove 1 element at position i
  314. bullets.splice(i, 1);
  315. // because we are deleting elements from the middle of the
  316. // array, we can only remove 1 at a time. So, as soon as we
  317. // remove 1 bullet stop.
  318. break;
  319. }
  320. }
  321.  
  322. // draw all the bullets
  323. for(var i=0; i<bullets.length; i++)
  324. {
  325. context.drawImage(bullets[i].image,
  326. bullets[i].x - bullets[i].width/2,
  327. bullets[i].y - bullets[i].height/2);
  328. }
  329. {
  330. context.drawImage(asteroid.image,
  331. asteroid.x - asteroid.width/2,
  332. asteroid.y - asteroid.height/2);
  333. }
  334.  
  335.  
  336. //60FPS framework
  337. (function()
  338. {
  339. var onEachFrame;
  340.  
  341. if (window.requestAnimationFrame)
  342. {
  343. onEachFrame = function(cb)
  344. {
  345. var _cb = function() { cb(); window.requestAnimationFrame(_cb); }
  346. _cb();
  347. };
  348.  
  349. }
  350. else if (window.mozRequestAnimationFrame)
  351. {
  352. onEachFrame = function(cb) {
  353. var _cb = function() { cb(); window.mozRequestAnimationFrame(_cb); }
  354. _cb();
  355. };
  356.  
  357. }
  358. else
  359. {
  360. onEachFrame = function(cb)
  361. {
  362. setInterval(cb, 1000 / 60);
  363. }
  364. }
  365.  
  366. window.onEachFrame = onEachFrame;
  367. })();
  368.  
  369. window.onEachFrame(run);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement