PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/asteroids.html

https://bitbucket.org/zombielifestyle/canvas
HTML | 336 lines | 306 code | 30 blank | 0 comment | 0 complexity | 8670191dcfad96a4a52917b342c434ae MD5 | raw file
  1. <html>
  2. <head>
  3. <script type="application/javascript" src="jgame.js?sa=fdsjkb"></script>
  4. <script type="application/javascript">
  5. onload = function() {
  6. function collision(p1, r1, p2, r2){
  7. var a = r1 + r2;
  8. var dx = p1.x - p2.x;
  9. var dy = p1.y - p2.y;
  10. return a * a > (dx * dx + dy * dy);
  11. }
  12. var Shot = new jgame.Class({
  13. $init: function(){
  14. this.x = 0;
  15. this.y = 0;
  16. this.width = 4;
  17. this.height = 4;
  18. this.show = true;
  19. this.dx = 0;
  20. this.dy = 0;
  21. this.speed = 6;
  22. },
  23. update: function(c, ts){
  24. if (!this.show) return;
  25. this.x += this.dx * this.speed;
  26. this.y += this.dy * this.speed;
  27. // replace at screens edge
  28. if (this.x + this.width > WIDTH) {
  29. this.x = -this.width;
  30. } else if (this.x + this.width < 0) {
  31. this.x = WIDTH - this.width;
  32. }
  33. if (this.y + this.height > HEIGHT) {
  34. this.y = -this.height;
  35. } else if (this.y + this.height < 0) {
  36. this.y = HEIGHT - this.height;
  37. }
  38. c.beginPath();
  39. c.strokeStyle = "#0000C8";
  40. c.arc(this.x, this.y, 5, 0, Math.PI*2, true);
  41. c.closePath();
  42. c.stroke();
  43. }
  44. });
  45. var Explosion = new jgame.Class({
  46. $init: function(){
  47. this.x = 0;
  48. this.y = 0;
  49. this.width = 80;
  50. this.height = 80;
  51. this.show = true;
  52. this.frameDelay = 2;
  53. this.frames = [];
  54. this.frameIndex = 0;
  55. },
  56. update: function(c, ts){
  57. if (!this.show) return;
  58. if (ts % this.frameDelay == 0) {
  59. ++this.frameIndex
  60. if (this.frameIndex >= this.frames.length) {
  61. this.show = false;
  62. return;
  63. }
  64. }
  65. var img = this.frames[this.frameIndex];
  66. c.drawImage(img, this.x, this.y, this.width, this.height);
  67. }
  68. });
  69. var Asteroid = new jgame.Class({
  70. $init: function(){
  71. this.x = Math.random() * WIDTH;
  72. this.y = Math.random() * HEIGHT;
  73. this.width = 64;
  74. this.height = 64;
  75. this.show = true;
  76. var rand = Math.random()*360;
  77. this.dx=Math.cos(2.0*Math.PI*(rand-90)/360.0);
  78. this.dy=Math.sin(2.0*Math.PI*(rand-90)/360.0);
  79. this.speed = Math.random()*1+1;
  80. this.frameDelay = 2;
  81. this.frames = [];
  82. this.frameIndex = Math.floor(Math.random()*16);
  83. },
  84. update: function(c, ts){
  85. this.x += this.dx + this.speed;
  86. this.y += this.dy + this.speed;
  87. // replace at screens edge
  88. if (this.x + this.width > WIDTH) {
  89. this.x = -this.width;
  90. } else if (this.x + this.width < 0) {
  91. this.x = WIDTH - this.width;
  92. }
  93. if (this.y + this.height > HEIGHT) {
  94. this.y = -this.height;
  95. } else if (this.y + this.height < 0) {
  96. this.y = HEIGHT - this.height;
  97. }
  98. if (ts % this.frameDelay == 0) {
  99. if (++this.frameIndex > this.frames.length - 1) {
  100. this.frameIndex = 0;
  101. }
  102. }
  103. var img = this.frames[this.frameIndex];
  104. c.drawImage(img, this.x, this.y, this.width, this.height);
  105. }
  106. });
  107. var Ship = new jgame.Class({
  108. $init: function(){
  109. this.x = 200;
  110. this.y = 200;
  111. this.width = 64;
  112. this.height = 64;
  113. this.show = true;
  114. this.angle = 0;
  115. this.tangle = 0;
  116. this.dx = 0;
  117. this.dy = 0;
  118. this.movex = 0;
  119. this.movey = 0;
  120. this.acceleration = .3;
  121. this.friction = .01;
  122. this.maxVelocity = 8;
  123. this.frames = [];
  124. this.force = 0;
  125. },
  126. update: function (c) {
  127. if (!this.show) return;
  128. //angle
  129. this.angle += this.tangle;
  130. if (this.angle >= 360) {
  131. this.angle -= 360;
  132. } else if (this.angle < 0) {
  133. this.angle += 360;
  134. }
  135. // friction
  136. if (this.movex > 0) {
  137. this.movex -= this.friction;
  138. } else if (this.movex < 0) {
  139. this.movex += this.friction;
  140. }
  141. if (this.movey > 0) {
  142. this.movey -= this.friction;
  143. } else if (this.movey < 0) {
  144. this.movey += this.friction;
  145. }
  146. if (this.force) {
  147. this.dx=Math.cos(2.0*Math.PI*(this.angle-90)/360.0);
  148. this.dy=Math.sin(2.0*Math.PI*(this.angle-90)/360.0);
  149. var mxn = this.movex + this.acceleration * this.dx;
  150. var myn = this.movey + this.acceleration * this.dy;
  151. var currentSpeed = Math.sqrt(mxn*mxn + myn*myn);
  152. if (currentSpeed < this.maxVelocity) {
  153. this.movex=mxn;
  154. this.movey=myn;
  155. }
  156. }
  157. // replace at screens edge
  158. if (this.x + this.width > WIDTH) {
  159. this.x = -this.width;
  160. } else if (this.x + this.width < 0) {
  161. this.x = WIDTH - this.width;
  162. }
  163. if (this.y + this.height > HEIGHT) {
  164. this.y = -this.height;
  165. } else if (this.y + this.height < 0) {
  166. this.y = HEIGHT - this.height;
  167. }
  168. this.x += this.movex;
  169. this.y += this.movey;
  170. var img = this.frames[this.angle/10];
  171. c.drawImage(img, this.x, this.y, this.width, this.height);
  172. }
  173. });
  174. var loop = new jgame.Loop(24, "canvas");
  175. var WIDTH = loop.width;
  176. var HEIGHT = loop.height;
  177. var c = loop.context;
  178. function loadImages(file, start, end){
  179. var fileParts = file.split('%d');
  180. var images = [];
  181. for (var i = 0; i <= end; i++) {
  182. var currentFile = fileParts[0] + i + fileParts[1];
  183. var img = new Image();
  184. img.src = currentFile;
  185. images[i] = img;
  186. }
  187. return images;
  188. }
  189. var bgImage = new Image(640, 480);
  190. bgImage.src = 'img/space.png';
  191. var explo = new Explosion();
  192. explo.show = false;
  193. explo.frames = loadImages("img/explosion/aexplosion_fr_%d.png", 0, 14);
  194. var ship = new Ship();
  195. ship.frames = loadImages("img/ship/aship_fr_%d.png", 0, 35);
  196. var shot = new Shot();
  197. shot.show = false;
  198. ship.frames = loadImages("img/ship/aship_fr_%d.png", 0, 35);
  199. var asteroidFrames = loadImages("img/asteroid/aasteroid_fr_%d.png", 0, 15);
  200. var asteroids = [];
  201. for (var i = 0; i < 8; i++) {
  202. asteroids[i] = new Asteroid();
  203. asteroids[i].frames = asteroidFrames;
  204. }
  205. // bg
  206. c.fillStyle = "#000000";
  207. //c.fillRect(0, 0, WIDTH, HEIGHT);
  208. var fps = 0;
  209. loop.loop = function(){
  210. fps = fps >= this.fps ? 0 : fps + 1
  211. c.drawImage(bgImage, 0, 0, WIDTH, HEIGHT);
  212. // c.fillRect(0, 0, WIDTH, HEIGHT);
  213. shot.update(c);
  214. ship.update(c);
  215. explo.update(c, fps);
  216. for (var i in asteroids) {
  217. if (ship.show) {
  218. if (collision(ship, ship.width/3, asteroids[i], asteroids[i].width/3)) {
  219. explo.x = ship.x
  220. explo.y = ship.y
  221. explo.frameIndex = 0;
  222. explo.show = true;
  223. ship.show = false;
  224. }
  225. }
  226. if (shot.show) {
  227. if (collision(shot, 5, asteroids[i], asteroids[i].width/3)) {
  228. delete asteroids[i];
  229. continue;
  230. }
  231. }
  232. asteroids[i].update(c, fps);
  233. }
  234. }
  235. loop.start();
  236. window.document.onkeypress = function(e){
  237. //log(e.keyCode);
  238. if (e.keyCode == '0' && !shot.show && ship.show) { // space
  239. shot.dx = Math.cos(2.0*Math.PI*(ship.angle-90)/360.0)
  240. shot.dy = Math.sin(2.0*Math.PI*(ship.angle-90)/360.0)
  241. shot.x = ship.x + ship.width / 2
  242. shot.y = ship.y + ship.height / 2
  243. shot.show = true;
  244. setTimeout(function(){
  245. shot.show = false;
  246. }, 2000);
  247. }
  248. }
  249. window.document.onkeydown = function(e){
  250. // turn left/right
  251. if (e.keyCode == '37') { // left
  252. ship.angle -= 10;
  253. } else if (e.keyCode == '39') { // right
  254. ship.angle += 10;
  255. }
  256. // angle
  257. if (e.keyCode == '37') { // left
  258. ship.tangle -= 10;
  259. } else if (e.keyCode == '39') { // right
  260. ship.tangle += 10;
  261. }
  262. // movement
  263. if (e.keyCode == '38') { // up
  264. ship.force = 1;
  265. }
  266. };
  267. window.document.onkeyup = function(e){
  268. //angle
  269. if (e.keyCode == '37' || e.keyCode == '39') { // left/right
  270. ship.tangle = 0;
  271. }
  272. // movement
  273. if (e.keyCode == '38') { // up
  274. ship.force = 0;
  275. }
  276. };
  277. if (showControls)
  278. showControls(loop);
  279. }
  280. </script>
  281. </head>
  282. <body >
  283. <canvas id="canvas" style="border: 1px solid black; padding 10px;"
  284. width="640" height="480"></canvas>
  285. <script>
  286. function showControls(loop){
  287. var e = document.getElementById('ctrl_pause');
  288. function pauseLoop(e){
  289. loop.pause();
  290. }
  291. e.onclick = pauseLoop;
  292. var e = document.getElementById('ctrl_fps');
  293. e.value = loop.fps;
  294. e.onchange = function(e){
  295. loop.fps = e.currentTarget.value;
  296. loop.restart();
  297. }
  298. }
  299. </script>
  300. <div id="controls">
  301. <input type="button" id="ctrl_pause" value="pause" />
  302. |
  303. FPS:<input type="text" id="ctrl_fps" size="2" />
  304. </div>
  305. </body>
  306. </html>