canvas实现冠状病毒射击游戏

た 入场券 2022-08-28 13:57 290阅读 0赞

在线演示

20210918181427738.gif

结构

  1. <canvas id='canvas' width='1280' height='720'></canvas>

样式

  1. body {
  2. background:#111;
  3. margin:0;
  4. padding:0
  5. }
  6. canvas {
  7. background:#000;
  8. display:block;
  9. cursor:none
  10. }

方法

  1. "use strict"
  2. var stage = {
  3. w:1280,
  4. h:720
  5. }
  6. var _pexcanvas = document.getElementById("canvas");
  7. _pexcanvas.width = stage.w;
  8. _pexcanvas.height = stage.h;
  9. var ctx = _pexcanvas.getContext("2d");
  10. var pointer = {
  11. x:stage.w/2,
  12. y:stage.h/4
  13. }
  14. var scale = 1;
  15. var portrait = true;
  16. var loffset = 0;
  17. var toffset = 0;
  18. var mxpos = 0;
  19. var mypos = 0;
  20. // ------------------------------------------------------------------------------- Gamy
  21. var againprog = 0;
  22. var healthprog = 0;
  23. function newGame() {
  24. score = 0;
  25. health = 100;
  26. enemies=[];
  27. enemies.push(new Enemy());
  28. enemies.push(new Enemy());
  29. enemies.push(new Enemy());
  30. againprog = 0;
  31. }
  32. function drawHeart(x,y,w) {
  33. ctx.beginPath();
  34. ctx.arc(x-w/4, y, w/4, 0.75*Math.PI,0);
  35. ctx.arc(x+w/4, y, w/4, 1*Math.PI, 2.25*Math.PI);
  36. ctx.lineTo(x,y+w/1.5);
  37. ctx.closePath();
  38. ctx.fill();
  39. }
  40. var Cannon = function(x,y,tx,ty) {
  41. this.x = x;
  42. this.y = y;
  43. this.tx = tx;
  44. this.ty = ty;
  45. this.r = 10;
  46. }
  47. var cannons = [];
  48. var gameover = false;
  49. cannons.push(new Cannon(stage.w,stage.h,stage.w/2,stage.h/2));
  50. var firetm = 0;
  51. var fireact = true;
  52. var health = 100;
  53. var score = 0;
  54. var arm = {x:stage.w,y:stage.h};
  55. var arm2 = {x:0,y:stage.h};
  56. var danger = false;
  57. var dangera = 0;
  58. var Enemy = function() {
  59. this.x = stage.w/2;
  60. this.y = stage.h/2;
  61. this.r = 10;
  62. this.tx = Math.floor(Math.random()*stage.w);
  63. this.ty = Math.floor(Math.random()*stage.h);
  64. this.des = false;
  65. this.eyeX = 0.4;
  66. this.eyeY = 0.25;
  67. this.eyeR = 0.25;
  68. this.sp = 50;
  69. this.spl = 1.4;
  70. this.op=1;
  71. this.danger = false;
  72. this.nuked = false;
  73. }
  74. var enemies = [];
  75. // for (var i = 0; i < 10; i++) {
  76. // enemies[i] = new Enemy();
  77. // }
  78. enemies.push(new Enemy());
  79. enemies.push(new Enemy());
  80. enemies.push(new Enemy());
  81. var entm = 0;
  82. var ga =0;
  83. var steptime = 0;
  84. var Star = function() {
  85. this.a = Math.random()*Math.PI*2;
  86. this.v = 3+Math.random()*5;
  87. this.x = stage.w/2;
  88. this.y = stage.h/2;
  89. this.r = 0.2;
  90. }
  91. var Power = function() {
  92. this.type = Math.floor(Math.random()*2)+1;
  93. this.a = Math.random()*Math.PI*2;
  94. this.v = 3+Math.random()*5;
  95. this.x = stage.w/2;
  96. this.y = stage.h/2;
  97. this.r = 0.2;
  98. this.dis = false;
  99. this.op = 1;
  100. }
  101. var powers = [];
  102. var powertm = 0;
  103. var powermax = Math.random()*800+300;
  104. // powermax = 10;
  105. var stars = [];
  106. for (var i =0;i<200;i++) {
  107. stars[i] = new Star();
  108. var st = stars[i];
  109. var move = Math.random()*400;
  110. st.x += Math.sin(st.a)*move;
  111. st.y += Math.cos(st.a)*move;
  112. }
  113. // powers.push(new Power());
  114. function enginestep() {
  115. steptime = Date.now();
  116. ctx.clearRect(0,0,stage.w,stage.h);
  117. ctx.fillStyle = "#ffffff";
  118. for (var i = 0; i < stars.length; i++) {
  119. var st = stars[i];
  120. st.x += Math.sin(st.a)*st.v;
  121. st.y += Math.cos(st.a)*st.v;
  122. st.r += st.v/200;
  123. ctx.beginPath();
  124. ctx.arc(st.x,st.y, st.r, 2*Math.PI, 0);
  125. ctx.fill();
  126. if (st.x>stage.w||st.x<0||st.y<0||st.y>stage.h) {
  127. stars[i] = new Star();
  128. }
  129. }
  130. if (!gameover) {
  131. danger = false;
  132. powertm++;
  133. if (powertm>powermax) {
  134. powers.push(new Power());
  135. powertm = 0;
  136. powermax = Math.random()*1200+600;
  137. // powermax = 10;
  138. }
  139. for (var i = 0; i < powers.length; i++) {
  140. var st = powers[i];
  141. if (!st.des) {
  142. st.x += Math.sin(st.a)*st.v/1.5;
  143. st.y += Math.cos(st.a)*st.v/1.5;
  144. st.r += st.v/15;
  145. } else {
  146. st.r *=1.1;
  147. if (st.type==1) {
  148. st.op += (0-st.op)/10;
  149. } else {
  150. st.op += (0-st.op)/20;
  151. }
  152. st.x += (stage.w/2-st.x)/10;
  153. st.y += (stage.h/2-st.y)/10;
  154. }
  155. if (st.type ==1) {
  156. ctx.fillStyle = "rgba(255,0,0,"+st.op+")";
  157. drawHeart(st.x,st.y-st.r/4, st.r*2);
  158. } else {
  159. ctx.fillStyle = "rgba(255,255,0,"+st.op+")";
  160. ctx.strokeStyle = "rgba(255,255,0,"+st.op+")";
  161. ctx.lineWidth = st.r/10;
  162. ctx.beginPath();
  163. ctx.arc(st.x,st.y, st.r, 2*Math.PI, 0);
  164. ctx.stroke();
  165. ctx.beginPath();
  166. ctx.arc(st.x,st.y, st.r*0.15, 2*Math.PI, 0);
  167. ctx.fill();
  168. ctx.beginPath();
  169. ctx.arc(st.x,st.y, st.r*0.85, 1.67*Math.PI, 2*Math.PI);
  170. ctx.arc(st.x,st.y, st.r*0.25, 2*Math.PI, 1.67*Math.PI,true);
  171. ctx.closePath();
  172. ctx.fill();
  173. ctx.beginPath();
  174. ctx.arc(st.x,st.y, st.r*0.85, 3*Math.PI, 3.33*Math.PI);
  175. ctx.arc(st.x,st.y, st.r*0.25, 3.33*Math.PI,3*Math.PI, true);
  176. ctx.closePath();
  177. ctx.fill();
  178. ctx.beginPath();
  179. ctx.arc(st.x,st.y, st.r*0.85, 2.33*Math.PI, 2.67*Math.PI);
  180. ctx.arc(st.x,st.y, st.r*0.25, 2.67*Math.PI,2.33*Math.PI, true);
  181. ctx.lineTo(st.x,st.y);
  182. ctx.closePath();
  183. ctx.fill();
  184. }
  185. if (st.x>stage.w||st.x<0||st.y<0||st.y>stage.h||st.r>stage.w/2) {
  186. powers.splice(i,1);
  187. if (st.type == 2&&st.r>stage.w/2) {
  188. for (var e = 0; e < enemies.length; e++) {
  189. enemies[e].des = true;
  190. enemies[e].nuked = true;
  191. }
  192. }
  193. i--;
  194. }
  195. }
  196. entm++;
  197. if (enemies.length<10&&entm>300) {
  198. entm=0;
  199. enemies.push(new Enemy());
  200. }
  201. ctx.lineWidth = 2;
  202. for (var i = 0; i < enemies.length; i++) {
  203. var en = enemies[i];
  204. if (!en.danger) {
  205. ctx.strokeStyle = "rgba(0,255,255,"+en.op*2+")";
  206. } else {
  207. health -= 0.01;
  208. ctx.strokeStyle = "rgba(255,0,0,"+en.op*2+")";
  209. danger = true;
  210. }
  211. if (!en.des) {
  212. if (en.danger) {
  213. var randx = Math.floor(Math.random()*4)-2;
  214. var randy = Math.floor(Math.random()*4)-2;
  215. en.x = en.tx+randx;
  216. en.y = en.ty+randy;
  217. } else {
  218. en.x += (en.tx-en.x)/100;
  219. en.y += (en.ty-en.y)/100;
  220. var randx = 0;
  221. var randy = 0;
  222. }
  223. en.r += (50-en.r)/100;
  224. if (Math.abs(50-en.r)<2&&!en.danger) {
  225. en.tx=en.x;
  226. en.ty=en.y;
  227. en.danger=true;
  228. }
  229. ctx.beginPath();
  230. ctx.arc(en.x-en.r*en.eyeX,en.y-en.r*en.eyeY, en.r*en.eyeR, 0, 2*Math.PI);
  231. ctx.stroke();
  232. ctx.beginPath();
  233. ctx.arc(en.x+en.r*en.eyeX,en.y-en.r*en.eyeY, en.r*en.eyeR, 0, 2*Math.PI);
  234. ctx.stroke();
  235. ctx.beginPath();
  236. ctx.arc(en.x,en.y+en.r/4, en.r/3, 2*Math.PI, Math.PI);
  237. ctx.stroke();
  238. ctx.beginPath();
  239. ctx.arc(en.x,en.y, en.r, 0, 2*Math.PI);
  240. ctx.stroke();
  241. } else {
  242. en.eyeR += (0.5-en.eyeR)/5;
  243. en.op += (0-en.op)/5;
  244. // en.sp += (5-en.sp)/20;
  245. en.r += (100-en.r)/20;
  246. en.spl += (2.5-en.spl)/5;
  247. ctx.beginPath();
  248. ctx.arc(en.x-en.r*en.eyeX,en.y-en.r*en.eyeY, en.r*en.eyeR, 0, 2*Math.PI);
  249. ctx.stroke();
  250. ctx.beginPath();
  251. ctx.arc(en.x+en.r*en.eyeX,en.y-en.r*en.eyeY, en.r*en.eyeR, 0, 2*Math.PI);
  252. ctx.stroke();
  253. ctx.beginPath();
  254. ctx.arc(en.x,en.y+en.r/2, en.r*en.eyeR, Math.PI,2*Math.PI);
  255. ctx.stroke();
  256. ctx.beginPath();
  257. ctx.arc(en.x,en.y, en.r, 0, 2*Math.PI);
  258. ctx.stroke();
  259. }
  260. //spikes
  261. for (var s = 0; s < 12; s++) {
  262. var a = (Math.PI*2/12)*s+ga;
  263. ctx.beginPath();
  264. ctx.moveTo(en.x+Math.sin(a)*en.r,en.y+Math.cos(a)*en.r);
  265. ctx.lineTo(en.x+Math.sin(a)*en.r*1.2,en.y+Math.cos(a)*en.r*1.2);
  266. ctx.lineTo(en.x+Math.sin(a+Math.PI/en.sp)*en.r*en.spl,en.y+Math.cos(a+Math.PI/en.sp)*en.r*en.spl);
  267. ctx.lineTo(en.x+Math.sin(a-Math.PI/en.sp)*en.r*en.spl,en.y+Math.cos(a-Math.PI/en.sp)*en.r*en.spl);
  268. ctx.lineTo(en.x+Math.sin(a)*en.r*1.2,en.y+Math.cos(a)*en.r*1.2);
  269. ctx.stroke();
  270. // ctx.fill();
  271. }
  272. if (Math.abs(0.5-en.eyeR)<0.01) {
  273. var rand = Math.floor(Math.random()*2);
  274. if (enemies[i].nuked&&rand==1) {
  275. enemies.splice(i,1);
  276. } else {
  277. enemies[i] = new Enemy();
  278. }
  279. }
  280. }
  281. if (danger) {
  282. dangera += 0.05+(100-health)/1000;
  283. if (dangera>=Math.PI) {
  284. dangera=0;
  285. }
  286. ctx.fillStyle='rgba(255,0,0,'+(1-Math.sin(dangera))/4+')';
  287. ctx.fillRect(0,0,stage.w,stage.h);
  288. if (health<10) {
  289. ctx.fillStyle='rgba(255,255,0,'+(Math.sin(dangera))+')';
  290. ctx.strokeStyle='rgba(255,255,0,'+(Math.sin(dangera))+')';
  291. ctx.lineWidth = 10;
  292. ctx.beginPath();
  293. ctx.lineJoin = 'round';
  294. ctx.moveTo(stage.w/2,stage.h/4);
  295. ctx.lineTo(stage.w/2+stage.h/7,stage.h/2);
  296. ctx.lineTo(stage.w/2-stage.h/7,stage.h/2);
  297. ctx.closePath();
  298. ctx.stroke();
  299. ctx.font = "bold 130px arial";
  300. ctx.textAlign = "center";
  301. ctx.textBaseline = "middle";
  302. ctx.fillText("!",stage.w/2,stage.h/2.5);
  303. ctx.font = "bold 50px arial";
  304. ctx.fillText("LOW HEALTH",stage.w/2,stage.h*0.6);
  305. }
  306. } else {
  307. dangera = 0;
  308. }
  309. healthprog += (health-healthprog)/5;
  310. ctx.fillStyle='#00ffff';
  311. ctx.font = "30px arial";
  312. ctx.textAlign = "left";
  313. ctx.textBaseline = "middle";
  314. ctx.fillText("Health: ",20,40);
  315. ctx.fillText("Score: "+score,stage.w-200,40);
  316. // ctx.fillText("Step: "+(Date.now()-steptime),20,120);
  317. if (health>30) {
  318. ctx.fillStyle='rgba(0,255,255,0.8)';
  319. } else {
  320. ctx.fillStyle='rgba(255,0,0,0.8)';
  321. }
  322. ctx.lineWidth = 2;
  323. ctx.fillRect(130,25,healthprog*3,30);
  324. ctx.strokeStyle = "#00ffff";
  325. ctx.strokeRect(130,25,300,30);
  326. if (health<0) {
  327. gameover = true;
  328. }
  329. } else {
  330. ctx.fillStyle='rgba(0,255,255,0.3)';
  331. ctx.fillRect((stage.w-220)/2,stage.h*0.65-25,againprog,50);
  332. ctx.fillStyle='#00ffff';
  333. ctx.font = "bold 130px arial";
  334. ctx.textAlign = "center";
  335. ctx.textBaseline = "middle";
  336. ctx.fillText("GAME OVER",stage.w/2,stage.h/3);
  337. ctx.font = "bold 50px arial";
  338. ctx.fillText("SCORE: "+score,stage.w/2,stage.h/2);
  339. ctx.font = "bold 30px arial";
  340. ctx.fillText("PLAY AGAIN",stage.w/2,stage.h*0.65);
  341. ctx.strokeRect((stage.w-220)/2,stage.h*0.65-25,220,50);
  342. againprog += (0-againprog)/50;
  343. }
  344. ctx.strokeStyle = "#00ffff";
  345. ctx.fillStyle = "#00ffff";
  346. ctx.lineWidth = 2;
  347. if (fireact) {
  348. firetm++;
  349. if(firetm>5) {
  350. cannons.push(new Cannon(pointer.x+(stage.w-pointer.x)/2.5,pointer.y+(stage.h-pointer.y)/2.5,pointer.x,pointer.y));
  351. cannons.push(new Cannon(pointer.x-(pointer.x)/2.5,pointer.y+(stage.h-pointer.y)/2.5,pointer.x,pointer.y));
  352. firetm=0;
  353. }
  354. arm.x=Math.floor(Math.random()*50)-25+stage.w;
  355. arm.y=Math.floor(Math.random()*50)-25+stage.h;
  356. arm2.x=Math.floor(Math.random()*30)-15;
  357. arm2.y=Math.floor(Math.random()*30)-15+stage.h;
  358. } else {
  359. arm.x=stage.w;
  360. arm.y=stage.h;
  361. arm2.x=0;
  362. arm2.y=stage.h;
  363. }
  364. for (var i = 0; i < cannons.length; i++) {
  365. var can = cannons[i];
  366. can.x += (can.tx-can.x)/5;
  367. can.y += (can.ty-can.y)/5;
  368. can.r += (0-can.r)/5;
  369. ctx.beginPath();
  370. ctx.arc(can.x,can.y, can.r, 0, 2*Math.PI);
  371. ctx.fill();
  372. if (can.r<2&&!gameover) {
  373. for (var a = 0; a < enemies.length; a++) {
  374. var en = enemies[a];
  375. var dx = can.x-en.x;
  376. var dy = can.y-en.y;
  377. var dis = dx*dx+dy*dy;
  378. if (dis<en.r*en.r) {
  379. // enemies.splice(a,1);
  380. if (!enemies[a].des) {
  381. enemies[a].des = true;
  382. score+=10;
  383. }
  384. }
  385. }
  386. }
  387. if (can.r<1&&!gameover) {
  388. for (var a = 0; a < powers.length; a++) {
  389. var en = powers[a];
  390. var dx = can.x-en.x;
  391. var dy = can.y-en.y;
  392. var dis = dx*dx+dy*dy;
  393. if (dis<en.r*en.r) {
  394. if (!en.des) {
  395. powers[a].des = true;
  396. if (en.type==1) {
  397. health = 100;
  398. }
  399. }
  400. }
  401. }
  402. }
  403. if (can.r<1&&gameover) {
  404. if (can.x>(stage.w-220)/2&&can.y>stage.h*0.65-25&&can.x<(stage.w-220)/2+220&&can.y<stage.h*0.65-25+50) {
  405. againprog +=1;
  406. if (againprog>220) {
  407. newGame();
  408. gameover = false;
  409. }
  410. }
  411. }
  412. if (Math.abs(can.tx-can.x)<1) {
  413. cannons.splice(i,1);
  414. }
  415. }
  416. ctx.beginPath();
  417. ctx.moveTo(pointer.x-20,pointer.y);
  418. ctx.lineTo(pointer.x+20,pointer.y);
  419. ctx.stroke();
  420. ctx.beginPath();
  421. ctx.moveTo(pointer.x,pointer.y-20);
  422. ctx.lineTo(pointer.x,pointer.y+20);
  423. ctx.stroke();
  424. ctx.beginPath();
  425. ctx.arc(pointer.x,pointer.y, 8, 0, 2*Math.PI);
  426. ctx.stroke();
  427. ctx.beginPath();
  428. ctx.moveTo(pointer.x+(arm.x-pointer.x)/3,pointer.y+(arm.y-pointer.y)/3+10);
  429. ctx.lineTo(pointer.x+(arm.x-pointer.x)/2.5,pointer.y+(arm.y-pointer.y)/2.5+10);
  430. ctx.lineTo(pointer.x+(arm.x-pointer.x)/2,pointer.y+(arm.y-pointer.y)/2+10);
  431. ctx.lineTo(pointer.x+(arm.x-pointer.x)/1.5,pointer.y+(arm.y-pointer.y)/1.5+50);
  432. ctx.lineTo(pointer.x+(arm.x-pointer.x)/1.2,pointer.y+(arm.y-pointer.y)/1.2+80);
  433. ctx.lineTo(pointer.x+(arm.x-pointer.x)/1.1,pointer.y+(arm.y-pointer.y)/1.1+100);
  434. ctx.stroke();
  435. ctx.beginPath();
  436. ctx.moveTo(pointer.x+(arm.x-pointer.x)/3-10,pointer.y+(arm.y-pointer.y)/3);
  437. ctx.lineTo(pointer.x+(arm.x-pointer.x)/2.5-10,pointer.y+(arm.y-pointer.y)/2.5);
  438. ctx.lineTo(pointer.x+(arm.x-pointer.x)/2-10,pointer.y+(arm.y-pointer.y)/2);
  439. ctx.lineTo(pointer.x+(arm.x-pointer.x)/1.5-50,pointer.y+(arm.y-pointer.y)/1.5);
  440. ctx.lineTo(pointer.x+(arm.x-pointer.x)/1.2-80,pointer.y+(arm.y-pointer.y)/1.2);
  441. ctx.lineTo(pointer.x+(arm.x-pointer.x)/1.1-100,pointer.y+(arm.y-pointer.y)/1.1);
  442. ctx.stroke();
  443. ctx.beginPath();
  444. ctx.moveTo(pointer.x+(arm.x-pointer.x)/3,pointer.y+(arm.y-pointer.y)/3-10);
  445. ctx.lineTo(pointer.x+(arm.x-pointer.x)/2.5,pointer.y+(arm.y-pointer.y)/2.5-10);
  446. ctx.lineTo(pointer.x+(arm.x-pointer.x)/2,pointer.y+(arm.y-pointer.y)/2-10);
  447. ctx.lineTo(pointer.x+(arm.x-pointer.x)/1.5,pointer.y+(arm.y-pointer.y)/1.5-50);
  448. ctx.lineTo(pointer.x+(arm.x-pointer.x)/1.2,pointer.y+(arm.y-pointer.y)/1.2-80);
  449. ctx.lineTo(pointer.x+(arm.x-pointer.x)/1.1,pointer.y+(arm.y-pointer.y)/1.1-100);
  450. ctx.stroke();
  451. ctx.beginPath();
  452. ctx.moveTo(arm2.x+pointer.x-(pointer.x)/3,pointer.y+(arm2.y-pointer.y)/3+10);
  453. ctx.lineTo(arm2.x+pointer.x-(pointer.x)/2.5,pointer.y+(arm2.y-pointer.y)/2.5+10);
  454. ctx.lineTo(arm2.x+pointer.x-(pointer.x)/2,pointer.y+(arm2.y-pointer.y)/2+10);
  455. ctx.lineTo(arm2.x+pointer.x-(pointer.x)/1.5,pointer.y+(arm2.y-pointer.y)/1.5+50);
  456. ctx.lineTo(arm2.x+pointer.x-(pointer.x)/1.2,pointer.y+(arm2.y-pointer.y)/1.2+80);
  457. ctx.lineTo(arm2.x+pointer.x-(pointer.x)/1.1,pointer.y+(arm2.y-pointer.y)/1.1+100);
  458. ctx.stroke();
  459. ctx.beginPath();
  460. ctx.moveTo(arm2.x+pointer.x-(pointer.x)/3-10,pointer.y+(arm2.y-pointer.y)/3);
  461. ctx.lineTo(arm2.x+pointer.x-(pointer.x)/2.5-10,pointer.y+(arm2.y-pointer.y)/2.5);
  462. ctx.lineTo(arm2.x+pointer.x-(pointer.x)/2-10,pointer.y+(arm2.y-pointer.y)/2);
  463. ctx.lineTo(arm2.x+pointer.x-(pointer.x)/1.5-50,pointer.y+(arm2.y-pointer.y)/1.5);
  464. ctx.lineTo(arm2.x+pointer.x-(pointer.x)/1.2-80,pointer.y+(arm2.y-pointer.y)/1.2);
  465. ctx.lineTo(arm2.x+pointer.x-(pointer.x)/1.1-100,pointer.y+(arm2.y-pointer.y)/1.1);
  466. ctx.stroke();
  467. ctx.beginPath();
  468. ctx.moveTo(arm2.x+pointer.x-(pointer.x)/3,pointer.y+(arm2.y-pointer.y)/3-10);
  469. ctx.lineTo(arm2.x+pointer.x-(pointer.x)/2.5,pointer.y+(arm2.y-pointer.y)/2.5-10);
  470. ctx.lineTo(arm2.x+pointer.x-(pointer.x)/2,pointer.y+(arm2.y-pointer.y)/2-10);
  471. ctx.lineTo(arm2.x+pointer.x-(pointer.x)/1.5,pointer.y+(arm2.y-pointer.y)/1.5-50);
  472. ctx.lineTo(arm2.x+pointer.x-(pointer.x)/1.2,pointer.y+(arm2.y-pointer.y)/1.2-80);
  473. ctx.lineTo(arm2.x+pointer.x-(pointer.x)/1.1,pointer.y+(arm2.y-pointer.y)/1.1-100);
  474. ctx.stroke();
  475. ctx.beginPath();
  476. ctx.arc(pointer.x+(arm.x-pointer.x)/3,pointer.y+(arm.y-pointer.y)/3, 10, 0, 2*Math.PI);
  477. ctx.arc(pointer.x+(arm.x-pointer.x)/2.5,pointer.y+(arm.y-pointer.y)/2.5, 10, 0, 2*Math.PI);
  478. ctx.arc(pointer.x+(arm.x-pointer.x)/2,pointer.y+(arm.y-pointer.y)/2, 10, 0, 2*Math.PI);
  479. ctx.arc(pointer.x+(arm.x-pointer.x)/1.5,pointer.y+(arm.y-pointer.y)/1.5, 50, 0, 2*Math.PI);
  480. ctx.arc(pointer.x+(arm.x-pointer.x)/1.2,pointer.y+(arm.y-pointer.y)/1.2, 80, 0, 2*Math.PI);
  481. ctx.arc(pointer.x+(arm.x-pointer.x)/1.1,pointer.y+(arm.y-pointer.y)/1.1, 100, 0, 2*Math.PI);
  482. ctx.stroke();
  483. ctx.beginPath();
  484. ctx.arc(arm2.x+pointer.x-(pointer.x/3),pointer.y+(arm2.y-pointer.y)/3, 10, 0, 2*Math.PI);
  485. ctx.arc(arm2.x+pointer.x-(pointer.x/2.5),pointer.y+(arm2.y-pointer.y)/2.5, 10, 0, 2*Math.PI);
  486. ctx.arc(arm2.x+pointer.x-(pointer.x)/2,pointer.y+(arm2.y-pointer.y)/2, 10, 0, 2*Math.PI);
  487. ctx.arc(arm2.x+pointer.x-(pointer.x)/1.5,pointer.y+(arm2.y-pointer.y)/1.5, 50, 0, 2*Math.PI);
  488. ctx.arc(arm2.x+pointer.x-(pointer.x)/1.2,pointer.y+(arm2.y-pointer.y)/1.2, 80, 0, 2*Math.PI);
  489. ctx.arc(arm2.x+pointer.x-(pointer.x)/1.1,pointer.y+(arm2.y-pointer.y)/1.1, 100, 0, 2*Math.PI);
  490. ctx.stroke();
  491. ctx.fillStyle='#004444';
  492. ctx.font = "14px arial";
  493. ctx.textAlign = "center";
  494. ctx.textBaseline = "middle";
  495. ctx.fillText("Coronavirus Shooting Game, Designed & Developed by Faisal Jawed",stage.w/2,stage.h-20);
  496. }
  497. // ------------------------------------------------------------------------------- events
  498. // ------------------------------------------------------------------------------- events
  499. // ------------------------------------------------------------------------------- events
  500. // ------------------------------------------------------------------------------- events
  501. function toggleFullScreen() {
  502. var doc = window.document;
  503. var docEl = doc.documentElement;
  504. var requestFullScreen = docEl.requestFullscreen || docEl.mozRequestFullScreen || docEl.webkitRequestFullScreen || docEl.msRequestFullscreen;
  505. var cancelFullScreen = doc.exitFullscreen || doc.mozCancelFullScreen || doc.webkitExitFullscreen || doc.msExitFullscreen;
  506. if(!doc.fullscreenElement && !doc.mozFullScreenElement && !doc.webkitFullscreenElement && !doc.msFullscreenElement) {
  507. requestFullScreen.call(docEl);
  508. }
  509. else {
  510. cancelFullScreen.call(doc);
  511. }
  512. }
  513. var ox = 0;
  514. var oy = 0;
  515. function mousestart(e) {
  516. mxpos = (e.pageX-loffset)*scale;
  517. mypos = (e.pageY-toffset)*scale;
  518. pointer.x = mxpos;
  519. pointer.y = mypos;
  520. // fireact = true;
  521. }
  522. function mousemove(e) {
  523. mxpos = (e.pageX-loffset)*scale;
  524. mypos = (e.pageY-toffset)*scale;
  525. pointer.x = mxpos;
  526. pointer.y = mypos;
  527. // ball.vY += (mxpos-ox)/15*line.d;
  528. ox = mxpos;
  529. }
  530. function mouseend(e) {
  531. // fireact = false;
  532. }
  533. var moveX = 0;
  534. var moveY = 0;
  535. var moveZ = 0;
  536. function keydowned(e) {
  537. // if (e.keyCode==65) {
  538. // moveX = 10;
  539. // } else if (e.keyCode==68) {
  540. // moveX = -10;
  541. // }
  542. // if (e.keyCode==83) {
  543. // moveY = -10;
  544. // } else if (e.keyCode==87) {
  545. // moveY = 10;
  546. // }
  547. // if (e.keyCode==69) {
  548. // moveZ = 10;
  549. // } else if (e.keyCode==81) {
  550. // moveZ = -10;
  551. // }
  552. // console.log(e.keyCode);
  553. }
  554. function keyuped(e) {
  555. // if (e.keyCode==65) {
  556. // moveX = 0;
  557. // } else if (e.keyCode==68) {
  558. // moveX = 0;
  559. // }
  560. // if (e.keyCode==87) {
  561. // moveY = 0;
  562. // } else if (e.keyCode==83) {
  563. // moveY = 0;
  564. // }
  565. // if (e.keyCode==81) {
  566. // moveZ = 0;
  567. // } else if (e.keyCode==69) {
  568. // moveZ = 0;
  569. // }
  570. // console.log("u"+e.keyCode);
  571. }
  572. window.addEventListener('mousedown', function(e) {
  573. mousestart(e);
  574. }, false);
  575. window.addEventListener('mousemove', function(e) {
  576. mousemove(e);
  577. }, false);
  578. window.addEventListener('mouseup', function(e) {
  579. mouseend(e);
  580. }, false);
  581. window.addEventListener('touchstart', function(e) {
  582. e.preventDefault();
  583. mousestart(e.touches[0]);
  584. }, false);
  585. window.addEventListener('touchmove', function(e) {
  586. e.preventDefault();
  587. mousemove(e.touches[0]);
  588. }, false);
  589. window.addEventListener('touchend', function(e) {
  590. e.preventDefault();
  591. mouseend(e.touches[0]);
  592. }, false);
  593. window.addEventListener('keydown', function(e) {
  594. keydowned(e);
  595. }, false);
  596. window.addEventListener('keyup', function(e) {
  597. keyuped(e);
  598. }, false);
  599. // ------------------------------------------------------------------------ stager
  600. // ------------------------------------------------------------------------ stager
  601. // ------------------------------------------------------------------------ stager
  602. // ------------------------------------------------------------------------ stager
  603. function _pexresize() {
  604. var cw = window.innerWidth;
  605. var ch = window.innerHeight;
  606. if (cw<=ch*stage.w/stage.h) {
  607. portrait = true;
  608. scale = stage.w/cw;
  609. loffset = 0;
  610. toffset = Math.floor(ch-(cw*stage.h/stage.w))/2;
  611. _pexcanvas.style.width = cw + "px";
  612. _pexcanvas.style.height = Math.floor(cw*stage.h/stage.w) + "px";
  613. } else {
  614. scale = stage.h/ch;
  615. portrait = false;
  616. loffset = Math.floor(cw-(ch*stage.w/stage.h))/2;
  617. toffset = 0;
  618. _pexcanvas.style.height = ch + "px";
  619. _pexcanvas.style.width = Math.floor(ch*stage.w/stage.h) + "px";
  620. }
  621. _pexcanvas.style.marginLeft = loffset +"px";
  622. _pexcanvas.style.marginTop = toffset +"px";
  623. }
  624. window.requestAnimFrame = (function(){
  625. return window.requestAnimationFrame ||
  626. window.webkitRequestAnimationFrame ||
  627. window.mozRequestAnimationFrame ||
  628. window.oRequestAnimationFrame ||
  629. window.msRequestAnimationFrame ||
  630. function( callback ){
  631. window.setTimeout(callback, 1000 / 60);
  632. };})();
  633. var fps = 60;
  634. var nfcount = 0;
  635. function animated() {
  636. requestAnimFrame(animated);
  637. enginestep();
  638. nfcount++;
  639. ctx.fillStyle='#00ffff';
  640. ctx.font = "12px arial";
  641. ctx.textAlign = "left";
  642. ctx.fillText("FPS: "+Math.floor(fps),10,stage.h-20);
  643. }
  644. _pexresize();
  645. animated();
  646. function countfps() {
  647. fps = nfcount;
  648. nfcount = 0;
  649. }
  650. setInterval(countfps,1000);

发表评论

表情:
评论列表 (有 0 条评论,290人围观)

还没有评论,来说两句吧...

相关阅读