app.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. var screenWidth = window.innerWidth;
  2. var screenHeight = window.innerHeight-15;
  3. var s02 = Math.round(screenWidth * .02);
  4. var s03 = Math.round(screenWidth * .03);
  5. var s05 = Math.round(screenWidth * .05);
  6. var s10 = Math.round(screenWidth * .08);
  7. var s15 = Math.round(screenWidth * .15);
  8. var s50 = Math.round(screenWidth * .5);
  9. var s60 = Math.round(screenWidth * .6);
  10. var s70 = Math.round(screenWidth * .8);
  11. var s90 = Math.round(screenWidth * .9);
  12. var h60 = Math.round(screenHeight * .7);
  13. var h70 = Math.round(screenHeight * .9);
  14. var h61 = Math.round(screenHeight * .8);
  15. var h30 = Math.round(screenHeight * .35);
  16. var canvas, stage, btn, txt, help, btnStartGame;
  17. var round, scoreAll, roundMsg, scoreMsg, lineWarning, ball, em, bigX, bigY, tTime, shot, maxScore, warning, gameRunning;
  18. var oneShot, tenLine, sixLine, threeLine, zeroLine, temp, oneShotScore, warningMsg, miss;
  19. var resultScore, resultMax, hisScore, hisMax, share;
  20. var startPx = s50;
  21. var startPy = screenHeight - s02;
  22. var scores = [];
  23. var clickcount=0;
  24. var checkcount=1;
  25. if(navigator.userAgent.indexOf('Android')>1){
  26. checkcount=2;
  27. }
  28. var cancount=5;
  29. function init() {
  30. canvas = document.getElementById("canvas");
  31. stage = new createjs.Stage(canvas);
  32. canvas.width = screenWidth;
  33. canvas.height = screenHeight;
  34. optimizeForTouchAndScreens();
  35. homeScreen()
  36. };
  37. function optimizeForTouchAndScreens() {
  38. if (createjs.Touch.isSupported()) {
  39. createjs.Touch.enable(stage)
  40. }
  41. };
  42. function homeScreen() {
  43. help = new createjs.Bitmap('img/home.png');
  44. help.regX = 320;
  45. help.regY = 372;
  46. help.x = s50;
  47. help.y = h70 / 2.2;
  48. help.scaleX = s90 / 640;
  49. help.scaleY = s90 / 640;
  50. stage.addChild(help);
  51. btnStartGame = new createjs.Container();
  52. stage.addChild(btnStartGame);
  53. btn = new createjs.Shape();
  54. btn.graphics.beginStroke('#fff').setStrokeStyle(3).beginFill('#66f').drawRect(0, 0, s70, s15);
  55. btn.regX = s70 / 2;
  56. btn.regY = s15 / 2;
  57. btn.x = s50;
  58. btn.y = h60+20;
  59. btnStartGame.addChild(btn);
  60. txt = new createjs.Text("开始游戏", s10 + "px Arial", "#fff");
  61. txt.textAlign = 'center';
  62. txt.textBaseline = "middle";
  63. txt.x = s50;
  64. txt.y = h60+20;
  65. btnStartGame.addChild(txt);
  66. btnStartGame.addEventListener("click", initGame);
  67. btnStartGame = new createjs.Container();
  68. stage.addChild(btnStartGame);
  69. btn = new createjs.Shape();
  70. btn.graphics.beginStroke('#fff').setStrokeStyle(3).beginFill('#66f').drawRect(0, 0, s70, s15);
  71. btn.regX = s70 / 2;
  72. btn.regY = s15 / 2;
  73. btn.x = s50;
  74. btn.y = h70;
  75. btnStartGame.addChild(btn);
  76. txt = new createjs.Text("更多游戏", s10 + "px Arial", "#fff");
  77. txt.textAlign = 'center';
  78. txt.textBaseline = "middle";
  79. txt.x = s50;
  80. txt.y = h70;
  81. btnStartGame.addChild(txt);
  82. btnStartGame.addEventListener("click", function(){clickMore();});
  83. gameRunning = false;
  84. startGame();
  85. clickcount=0;
  86. };
  87. function initGame(e) {
  88. e.stopPropagation();
  89. btnStartGame.removeEventListener("click", initGame);
  90. stage.removeAllChildren();
  91. round = 0;
  92. scoreAll = 0;
  93. maxScore = 0;
  94. scores = [];
  95. createjs.Ticker.setPaused(false);
  96. newGame();
  97. clickcount=0;
  98. return true;
  99. };
  100. function startGame() {
  101. createjs.Ticker.setFPS(30);
  102. createjs.Ticker.addEventListener("tick",
  103. function(e) {
  104. if (!e.paused) {
  105. stage.update();
  106. if (gameRunning && warning && em.y > 0.7 * screenHeight) {
  107. warning = false;
  108. lineWarning.graphics.beginStroke("#ff0000").drawRect(0, 0, screenWidth, 1);
  109. lineWarning.alpha = 1
  110. }
  111. }
  112. })
  113. };
  114. function newGame() {
  115. roundMsg = new createjs.Text("第 " + round + "/"+cancount+" 弹", s05 + "px Arial", "#ffffff");
  116. roundMsg.textAlign = 'left';
  117. roundMsg.x = s03;
  118. roundMsg.y = s03;
  119. stage.addChild(roundMsg);
  120. scoreMsg = new createjs.Text("总分: " + scoreAll + ".0", s05 + "px Arial", "#ffffff");
  121. scoreMsg.textAlign = 'right';
  122. scoreMsg.x = screenWidth - s03;
  123. scoreMsg.y = s03;
  124. stage.addChild(scoreMsg);
  125. newShot()
  126. };
  127. function newShot() {
  128. round++;
  129. roundMsg.text = "第 " + round + "/"+cancount+" 弹";
  130. shot = false;
  131. warning = true;
  132. miss = false;
  133. lineWarning = new createjs.Shape();
  134. lineWarning.graphics.beginStroke("#ffff00").drawRect(0, 0, screenWidth, 1);
  135. lineWarning.alpha = .5;
  136. lineWarning.x = 0;
  137. lineWarning.y = 0.7 * screenHeight;
  138. stage.addChild(lineWarning);
  139. buildBall();
  140. buildem();
  141. stage.addEventListener("stagemousedown", moveBall)
  142. };
  143. function buildBall() {
  144. ball = new createjs.Shape();
  145. ball.graphics.beginFill('#FFFFFF').drawCircle(0, 0, s02);
  146. ball.graphics.endFill();
  147. ball.x = startPx;
  148. ball.y = startPy;
  149. stage.addChild(ball)
  150. };
  151. function buildem() {
  152. var emX = s02 + Math.random() * (screenWidth - s02 * 2);
  153. var emY = -s10 - Math.random() * s15;
  154. var emDx = s02 + Math.random() * (screenWidth - s02 * 2);
  155. var tt = 3000 + Math.random() * 2000;
  156. em = new createjs.Shape();
  157. em.graphics.beginFill('#f99').drawCircle(0, 0, s02);
  158. em.x = emX;
  159. em.y = emY;
  160. stage.addChild(em);
  161. gameRunning = true;
  162. createjs.Tween.get(em).wait(500).to({
  163. x: emDx,
  164. y: screenHeight + s10
  165. },
  166. tt).call(missShot, [round])
  167. };
  168. function missShot(rd) {
  169. if (rd == round && !shot) {
  170. stage.removeEventListener("stagemousedown", moveBall);
  171. miss = true;
  172. shot = true;
  173. bigX = startPx;
  174. bigY = startPy;
  175. bigBall()
  176. }
  177. };
  178. function moveBall(e) {
  179. clickcount++;
  180. if(clickcount<checkcount){
  181. return;
  182. }
  183. stage.removeEventListener("stagemousedown", moveBall);
  184. shot = true;
  185. bigX = e.stageX;
  186. bigY = e.stageY;
  187. tTime = 2 * Math.round(Math.sqrt((bigX - startPx) * (bigX - startPx) + (bigY - startPy) * (bigY - startPy)));
  188. createjs.Tween.get(ball).to({
  189. x: bigX,
  190. y: bigY
  191. },
  192. tTime).call(bigBall)
  193. };
  194. function bigBall() {
  195. gameRunning = false;
  196. var xx = bigX - em.x;
  197. var yy = bigY - em.y;
  198. var score = Math.floor(110 - cancount * (Math.sqrt(xx * xx + yy * yy)) / s02);
  199. if (em.y > 0.7 * screenHeight) {
  200. score = Math.round(score / 2)
  201. };
  202. if (score <= 0 || miss) {
  203. score = 0
  204. };
  205. if (score > maxScore) {
  206. maxScore = score
  207. };
  208. scoreAll += score;
  209. scoreMsg.text = "总分: " + Math.floor(scoreAll / cancount) + "." + scoreAll % cancount;
  210. stage.removeChild(ball);
  211. oneShot = new createjs.Container();
  212. stage.addChild(oneShot);
  213. ball = new createjs.Shape();
  214. ball.graphics.beginFill('#6666ff').drawCircle(0, 0, s02);
  215. ball.graphics.endFill();
  216. ball.x = bigX;
  217. ball.y = bigY;
  218. ball.alpha = 0;
  219. oneShot.addChildAt(ball, 0);
  220. em.alpha = 0;
  221. tenLine = new createjs.Shape();
  222. tenLine.graphics.beginStroke("#ffffff").setStrokeStyle(2).drawCircle(0, 0, s02);
  223. tenLine.x = bigX;
  224. tenLine.y = bigY;
  225. oneShot.addChild(tenLine);
  226. sixLine = new createjs.Shape();
  227. sixLine.graphics.beginStroke("#ffffff").setStrokeStyle(2).drawCircle(0, 0, s02 * 4);
  228. sixLine.x = bigX;
  229. sixLine.y = bigY;
  230. oneShot.addChild(sixLine);
  231. threeLine = new createjs.Shape();
  232. threeLine.graphics.beginStroke("#ffffff").setStrokeStyle(2).drawCircle(0, 0, s02 * 7);
  233. threeLine.x = bigX;
  234. threeLine.y = bigY;
  235. oneShot.addChild(threeLine);
  236. temp = new createjs.Shape();
  237. temp.graphics.beginFill("#ffff00").drawCircle(0, 0, s02);
  238. temp.x = em.x;
  239. temp.y = em.y;
  240. stage.removeChild(em);
  241. temp.alpha = 0;
  242. stage.addChild(temp);
  243. oneShotScore = new createjs.Text(Math.floor(score / cancount) + "." + score % cancount, s10 + "px Arial", "#ff6");
  244. oneShotScore.textAlign = 'center';
  245. oneShotScore.textBaseline = 'middle';
  246. oneShotScore.x = startPx;
  247. oneShotScore.y = s15;
  248. if (warning == false) {
  249. warningMsg = new createjs.Text("(越线环数减半)", s05 + "px Arial", "#fff");
  250. warningMsg.textAlign = 'center';
  251. warningMsg.textBaseline = 'middle';
  252. warningMsg.x = startPx;
  253. warningMsg.y = s15 + s10;
  254. oneShot.addChild(warningMsg);
  255. if (miss) {
  256. oneShotScore.text = "miss";
  257. warningMsg.text = ""
  258. }
  259. };
  260. if (score > 0) {
  261. scores.push([xx, yy, warning, score])
  262. };
  263. createjs.Tween.get(ball).to({
  264. alpha: 1,
  265. scaleX: 10,
  266. scaleY: 10
  267. },
  268. 50).call(function() {
  269. oneShot.addChild(oneShotScore);
  270. temp.alpha = 1;
  271. createjs.Tween.get(temp).wait(1000).to({
  272. alpha: 0
  273. },
  274. 600);
  275. createjs.Tween.get(oneShot).wait(1000).to({
  276. alpha: 0
  277. },
  278. 600).call(restartBall)
  279. })
  280. };
  281. function restartBall() {
  282. stage.removeChild(oneShot);
  283. stage.removeChild(temp);
  284. stage.removeChild(lineWarning);
  285. checkGame()
  286. };
  287. function checkGame() {
  288. if (round < cancount) {
  289. newShot();
  290. return
  291. };
  292. stage.removeAllChildren();
  293. hisScore = 0;
  294. hisMax = 0;
  295. if ($.cookie('score')) {
  296. hisScore = $.cookie('score')
  297. };
  298. if ($.cookie('max')) {
  299. hisMax = $.cookie('max')
  300. };
  301. if (scoreAll > hisScore) {
  302. hisScore = scoreAll;
  303. $.cookie('score', scoreAll, {
  304. expires: 30
  305. })
  306. };
  307. if (maxScore > hisMax) {
  308. hisMax = maxScore;
  309. $.cookie('max', maxScore, {
  310. expires: 30
  311. })
  312. };
  313. resultScore = new createjs.Text("总分: " + Math.floor(scoreAll / cancount) + "." + scoreAll % cancount + " , 最准: " + Math.floor(maxScore / cancount) + "." + maxScore % cancount, s10 + "px Arial", "#ff0");
  314. resultScore.textAlign = 'center';
  315. resultScore.x = screenWidth / 2;
  316. resultScore.y = 0.5 * screenHeight;
  317. stage.addChild(resultScore);
  318. resultMax = new createjs.Text("纪录: " + Math.floor(hisScore / cancount) + "." + hisScore % cancount + " , " + Math.floor(hisMax / cancount) + "." + hisMax % cancount, s10 * .8 + "px Arial", "#fff");
  319. resultMax.textAlign = 'center';
  320. resultMax.x = screenWidth / 2;
  321. resultMax.y = 0.6 * screenHeight;
  322. stage.addChild(resultMax);
  323. share = new createjs.Text("点击上方按钮分享 ↑↑", s05 * 1.2 + "px Arial", "#f00");
  324. share.textAlign = 'right';
  325. share.x = screenWidth - s05;
  326. share.y = s03;
  327. stage.addChild(share);
  328. showAllScores();
  329. btnStartGame = new createjs.Container();
  330. stage.addChild(btnStartGame);
  331. btn = new createjs.Shape();
  332. btn.graphics.beginStroke('#fff').setStrokeStyle(3).beginFill('#66f').drawRect(0, 0, s70, s15);
  333. btn.regX = s70 / 2;
  334. btn.regY = s15 / 2;
  335. btn.x = s50;
  336. btn.y = h61;
  337. btnStartGame.addChild(btn);
  338. txt = new createjs.Text("重新开始", s10 + "px Arial", "#fff");
  339. txt.textAlign = 'center';
  340. txt.textBaseline = "middle";
  341. txt.x = s50;
  342. txt.y = h61;
  343. btnStartGame.addChild(txt);
  344. btnStartGame.addEventListener("click", initGame);
  345. btnStartGame = new createjs.Container();
  346. stage.addChild(btnStartGame);
  347. btn = new createjs.Shape();
  348. btn.graphics.beginStroke('#fff').setStrokeStyle(3).beginFill('#66f').drawRect(0, 0, s70, s15);
  349. btn.regX = s70 / 2;
  350. btn.regY = s15 / 2;
  351. btn.x = s50;
  352. btn.y = h70+30;
  353. btnStartGame.addChild(btn);
  354. txt = new createjs.Text("更多游戏", s10 + "px Arial", "#fff");
  355. txt.textAlign = 'center';
  356. txt.textBaseline = "middle";
  357. txt.x = s50;
  358. txt.y = h70+30;
  359. btnStartGame.addChild(txt);
  360. btnStartGame.addEventListener("click", function(){clickMore();});
  361. stage.update();
  362. createjs.Ticker.setPaused(true);
  363. updateSql();
  364. var score1=Math.floor(scoreAll / cancount)+(Math.floor(scoreAll % cancount)/10);
  365. var score2=Math.floor(maxScore / cancount)+(Math.floor(maxScore % cancount)/10);
  366. dp_submitScore(score1,score2);
  367. //window.wxData.desc = "我打出了" + Math.floor(scoreAll / cancount) + "." + scoreAll % cancount + "环,并销魂命中一发" + Math.floor(maxScore / cancount) + "." + maxScore % cancount + "环!快来挑战我吧!";
  368. //window.wxFriend.desc = "我打出了" + Math.floor(scoreAll / cancount) + "." + scoreAll % cancount + "环,并销魂命中一发" + Math.floor(maxScore / cancount) + "." + maxScore % cancount + "环!快来挑战我吧!"
  369. };
  370. function updateSql() {
  371. };
  372. function showAllScores() {
  373. var i, dd, nindex;
  374. oneShot = new createjs.Container();
  375. stage.addChild(oneShot);
  376. ball = new createjs.Shape();
  377. ball.graphics.beginFill('#6666ff').drawCircle(0, 0, s02 * 10);
  378. ball.graphics.endFill();
  379. ball.x = s50;
  380. ball.y = h30;
  381. oneShot.addChildAt(ball, 0);
  382. tenLine = new createjs.Shape();
  383. tenLine.graphics.beginStroke("#ffffff").setStrokeStyle(2).drawCircle(0, 0, s02);
  384. tenLine.x = s50;
  385. tenLine.y = h30;
  386. oneShot.addChild(tenLine);
  387. sixLine = new createjs.Shape();
  388. sixLine.graphics.beginStroke("#ffffff").setStrokeStyle(2).drawCircle(0, 0, s02 * 4);
  389. sixLine.x = s50;
  390. sixLine.y = h30;
  391. oneShot.addChild(sixLine);
  392. threeLine = new createjs.Shape();
  393. threeLine.graphics.beginStroke("#ffffff").setStrokeStyle(2).drawCircle(0, 0, s02 * 7);
  394. threeLine.x = s50;
  395. threeLine.y = h30;
  396. oneShot.addChild(threeLine);
  397. zeroLine = new createjs.Shape();
  398. zeroLine.graphics.beginStroke("#ffffff").setStrokeStyle(2).drawCircle(0, 0, s02 * 10);
  399. zeroLine.x = s50;
  400. zeroLine.y = h30;
  401. oneShot.addChild(zeroLine);
  402. for (i = 0; i < scores.length; i++) {
  403. dd = new createjs.Shape();
  404. if (scores[i][2] == true) {
  405. dd.graphics.beginStroke('#000').beginFill("#ffff00").drawCircle(0, 0, s02);
  406. dd.alpha = .7
  407. } else {
  408. dd.graphics.beginStroke('#000').beginFill("#ffffff").drawCircle(0, 0, s02);
  409. dd.alpha = .7
  410. };
  411. dd.x = s50 - scores[i][0];
  412. dd.y = h30 - scores[i][1];
  413. oneShot.addChild(dd);
  414. if (scores[i][3] == maxScore) {
  415. nindex = i;
  416. oneShot.removeChild(dd)
  417. }
  418. };
  419. if (scores.length > 0) {
  420. dd = new createjs.Shape();
  421. dd.graphics.beginStroke('#000').beginFill("#ff6600").drawCircle(0, 0, s02);
  422. dd.x = s50 - scores[nindex][0];
  423. dd.y = h30 - scores[nindex][1];
  424. oneShot.addChild(dd)
  425. }
  426. }