game.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. (function() {
  2. function pointToPoint(a, b) {
  3. return Math.sqrt(Math.pow(a[0] - b[0], 2) + Math.pow(a[1] - b[1], 2));
  4. }
  5. function pointToAngle(origin, point) {
  6. return Math.atan2((point[1] - origin[1]), point[0] - origin[0]);
  7. }
  8. function ptInRect(p, rect) {
  9. return (p[0] >= rect[0] && p[0] <= rect[0] + rect[2]) &&
  10. (p[1] >= rect[1] && p[1] <= rect[1] + rect[3]);
  11. }
  12. var canvas = document.getElementById('canvas');
  13. var content = canvas.parentNode;
  14. var center = [canvas.width / 2, canvas.height / 2]; // 中心坐标
  15. var zoom = 1; // 缩放比
  16. var score = -1;
  17. var updateShape;
  18. var maxScore = localStorage['xxoo.maxScore'] || 0;
  19. var colors = ['#FF0000', '#FFFFFF', '#0000FF', '#FFFF00', '#FF00FF'];
  20. var maxTime = 30;
  21. var start; // 开始时间
  22. var lifetime = maxTime;
  23. var subscorecount=1;
  24. function resize() {
  25. canvas.style.height = window.innerHeight + 'px';
  26. canvas.style.width = window.innerHeight *
  27. (canvas.width / canvas.height) + 'px';
  28. content.style.width = canvas.style.width;
  29. zoom = canvas.height / window.innerHeight;
  30. }
  31. window.addEventListener('resize', resize);
  32. window.addEventListener('load', resize);
  33. var path = [];
  34. var pathTemp;
  35. var lastCode;
  36. canvas.addEventListener('touchmove', function(e) {
  37. if (status === 'gameover') {
  38. return;
  39. }
  40. if (path.length > 200) {
  41. return;
  42. }
  43. path.push([
  44. (e.targetTouches[0].pageX - canvas.offsetLeft) * zoom,
  45. (e.targetTouches[0].pageY - canvas.offsetTop) * zoom
  46. ]);
  47. });
  48. canvas.addEventListener('touchstart', function(e) {
  49. var pos = [
  50. (e.targetTouches[0].pageX - canvas.offsetLeft) * zoom,
  51. (e.targetTouches[0].pageY - canvas.offsetTop) * zoom
  52. ];
  53. if (status === 'gameover') {
  54. if (ptInRect(pos, [
  55. center[0] - textWidth / 2, canvas.height / 2 + 2 - 45,
  56. textWidth, 90
  57. ])) {
  58. status = 'waiting';
  59. score = 0;
  60. subscorecount=1;
  61. } else if (ptInRect(pos, [
  62. center[0] - textWidth / 2, canvas.height / 2 + 2 + 200 - 45,
  63. textWidth, 90
  64. ])) {
  65. dp_share();
  66. } else if (ptInRect(pos, [
  67. center[0] - textWidth / 2, canvas.height / 2 + 2 + 400 - 45,
  68. textWidth, 90
  69. ])) {
  70. clickMore();
  71. }
  72. return;
  73. } else if (status === 'waiting') {
  74. status = 'playing';
  75. start = new Date;
  76. return;
  77. }
  78. path = [pos];
  79. });
  80. function gesture(path) {
  81. // return 1:o 2:\ 3:/
  82. if (path.length <= 2) {
  83. return;
  84. }
  85. var distance = pointToPoint(path[0], path[path.length - 1]);
  86. var angle = pointToAngle(path[0], path[path.length - 1]);
  87. var bottomPos = path[0]; // 最底部的点
  88. var bottomIndex = 0;
  89. var circleCenter = [0, 0];
  90. path.forEach(function(item, index) {
  91. circleCenter[0] += item[0];
  92. circleCenter[1] += item[1];
  93. if (bottomPos[1] <= item[1]) {
  94. bottomPos = item;
  95. bottomIndex = index;
  96. }
  97. });
  98. circleCenter[0] /= path.length;
  99. circleCenter[1] /= path.length;
  100. if (bottomIndex === 0 || bottomIndex === path.length - 1) { // 可能是斜线
  101. if (Math.abs(angle - 1 / 4 * Math.PI) < 1 / 8 * Math.PI ||
  102. Math.abs(angle + 3 / 4 * Math.PI) < 1 / 8 * Math.PI) { // \
  103. if (Math.abs(angle - pointToAngle(path[0], circleCenter)) < 1 / 8 * Math.PI) {
  104. return 2;
  105. }
  106. }
  107. if (Math.abs(angle - 3 / 4 * Math.PI) < 1 / 8 * Math.PI ||
  108. Math.abs(angle + 1 / 4 * Math.PI) < 1 / 8 * Math.PI) { // /
  109. if (Math.abs(angle - pointToAngle(path[0], circleCenter)) < 1 / 8 * Math.PI) {
  110. return 3;
  111. }
  112. }
  113. }
  114. var avgDistance = 0;
  115. path.forEach(function(item) {
  116. avgDistance += pointToPoint(circleCenter, item);
  117. });
  118. avgDistance /= path.length;
  119. var flag = false;
  120. path.forEach(function(item) {
  121. if (Math.abs(avgDistance - pointToPoint(circleCenter, item)) > avgDistance * 2) {
  122. flag = true;
  123. return false;
  124. }
  125. });
  126. if (!flag) {
  127. return 1;
  128. }
  129. }
  130. canvas.addEventListener('touchmove', function(e) {
  131. e.preventDefault();
  132. });
  133. canvas.addEventListener('touchend', function(e) {
  134. if (status === 'gameover') {
  135. return;
  136. }
  137. var code = gesture(path);
  138. var flag = false;
  139. if ((code === 3 || code === 2) && !pathTemp) {
  140. pathTemp = path;
  141. } else {
  142. pathTemp = null;
  143. }
  144. switch (currentShape) {
  145. case 1: // x
  146. if (code === 1) { // o
  147. flag = true;
  148. }
  149. break;
  150. case 0: // o
  151. if (code === 3 && lastCode === 2) { // x
  152. flag = true;
  153. } else if (code === 2 && lastCode === 3) {
  154. flag = true;
  155. }
  156. break;
  157. }
  158. path = [];
  159. lastCode = code;
  160. if (flag) {
  161. pathTemp = null;
  162. lastCode = null;
  163. updateShape();
  164. }
  165. });
  166. var context = canvas.getContext('2d');
  167. context.textBaseline = 'middle';
  168. context.font = '40px Arial,sans-serif';
  169. var textWidth = context.measureText('四个汉字').width;
  170. var status = 'waiting';
  171. var currentShape;
  172. var nextShape = parseInt(Math.random() * 2);
  173. function updateShape() {
  174. currentShape = nextShape;
  175. nextShape = parseInt(Math.random() * 2);
  176. score++;
  177. if (score > maxScore || 0) {
  178. maxScore = score;
  179. localStorage['xxoo.maxScore'] = maxScore;
  180. }
  181. render();
  182. }
  183. updateShape();
  184. function onPolyline(item, index) {
  185. if (index === 0) {
  186. context.moveTo(item[0], item[1]);
  187. } else {
  188. context.lineTo(item[0], item[1]);
  189. }
  190. }
  191. function render() {
  192. var angle;
  193. context.fillStyle = '#000000';
  194. context.fillRect(0, 0, canvas.width, canvas.height);
  195. context.fillStyle = '#FFFFFF';
  196. if (status === 'share') {
  197. dp_share();
  198. return;
  199. }
  200. if (score >= 0) {
  201. context.textAlign = 'left';
  202. if (status === 'gameover') {
  203. context.fillText('积分:' + score + '(记录:' + maxScore + ')', 0, 70);
  204. } else {
  205. context.fillText('积分:' + score, 0, 70);
  206. }
  207. }
  208. context.textAlign = 'right';
  209. context.beginPath();
  210. switch (status) {
  211. case 'waiting':
  212. context.save();
  213. context.textAlign = 'right';
  214. context.fillText('见x画o、见o画x', canvas.width, canvas.height - 40);
  215. context.restore();
  216. context.fillText('时间:' + maxTime.toFixed(2) + ' 秒', canvas.width, 70);
  217. break;
  218. case 'playing':
  219. context.save();
  220. context.textAlign = 'right';
  221. context.fillText('见x画o、见o画x', canvas.width, canvas.height - 40);
  222. context.restore();
  223. context.fillText('时间:' + (lifetime / 1000).toFixed(2) + ' 秒', canvas.width, 70);
  224. break;
  225. case 'gameover':
  226. context.save();
  227. context.fillText('Game Over', canvas.width, 70);
  228. context.textAlign = 'center';
  229. context.textBaseline = 'bottom';
  230. context.strokeStyle = '#FFFFFF';
  231. context.fillText('再来一局', center[0], canvas.height / 2);
  232. context.fillText('炫耀一下', center[0], canvas.height / 2 + 200);
  233. context.fillText('更多游戏', center[0], canvas.height / 2 + 400);
  234. context.beginPath();
  235. context.lineWidth = 3;
  236. context.moveTo(center[0] - textWidth / 2, canvas.height / 2 + 2);
  237. context.lineTo(center[0] + textWidth / 2, canvas.height / 2 + 2);
  238. context.moveTo(center[0] - textWidth / 2, canvas.height / 2 + 2 + 200);
  239. context.lineTo(center[0] + textWidth / 2, canvas.height / 2 + 2 + 200);
  240. context.moveTo(center[0] - textWidth / 2, canvas.height / 2 + 2 + 400);
  241. context.lineTo(center[0] + textWidth / 2, canvas.height / 2 + 2 + 400);
  242. context.stroke();
  243. context.restore();
  244. if(subscorecount==1){
  245. subscorecount=0;
  246. dp_submitScore(score);
  247. }
  248. return;
  249. }
  250. context.strokeStyle = colors[score % colors.length];
  251. context.lineWidth = canvas.width * 0.06;
  252. context.beginPath();
  253. switch (currentShape) {
  254. case 0: // o
  255. context.arc(center[0], center[1], canvas.width * 0.3, 0, 2 * Math.PI);
  256. break;
  257. case 1: // x
  258. angle = 1 / 8 * 2 * Math.PI;
  259. context.moveTo(
  260. center[0] + Math.cos(angle) * canvas.width * 0.35,
  261. center[1] + Math.sin(angle) * canvas.width * 0.35
  262. );
  263. context.lineTo(
  264. center[0] - Math.cos(angle) * canvas.width * 0.35,
  265. center[1] - Math.sin(angle) * canvas.width * 0.35
  266. );
  267. angle = 3 / 8 * 2 * Math.PI;
  268. context.moveTo(
  269. center[0] + Math.cos(angle) * canvas.width * 0.35,
  270. center[1] + Math.sin(angle) * canvas.width * 0.35
  271. );
  272. context.lineTo(
  273. center[0] - Math.cos(angle) * canvas.width * 0.35,
  274. center[1] - Math.sin(angle) * canvas.width * 0.35
  275. );
  276. break;
  277. }
  278. context.closePath();
  279. context.stroke();
  280. context.strokeStyle = colors[(score + 1) % colors.length];
  281. context.lineWidth = canvas.width * 0.03;
  282. context.beginPath();
  283. switch (nextShape) {
  284. case 0: // o
  285. context.arc(canvas.width * 0.25, canvas.height * 0.85, canvas.width * 0.3 * 0.25, 0, 2 * Math.PI);
  286. break;
  287. case 1: // x
  288. angle = 1 / 8 * 2 * Math.PI;
  289. context.moveTo(
  290. canvas.width * 0.25 + Math.cos(angle) * canvas.width * 0.35 * 0.25,
  291. canvas.height * 0.85 + Math.sin(angle) * canvas.width * 0.35 * 0.25
  292. );
  293. context.lineTo(
  294. canvas.width * 0.25 - Math.cos(angle) * canvas.width * 0.35 * 0.25,
  295. canvas.height * 0.85 - Math.sin(angle) * canvas.width * 0.35 * 0.25
  296. );
  297. angle = 3 / 8 * 2 * Math.PI;
  298. context.moveTo(
  299. canvas.width * 0.25 + Math.cos(angle) * canvas.width * 0.35 * 0.25,
  300. canvas.height * 0.85 + Math.sin(angle) * canvas.width * 0.35 * 0.25
  301. );
  302. context.lineTo(
  303. canvas.width * 0.25 - Math.cos(angle) * canvas.width * 0.35 * 0.25,
  304. canvas.height * 0.85 - Math.sin(angle) * canvas.width * 0.35 * 0.25
  305. );
  306. break;
  307. }
  308. context.closePath();
  309. context.stroke();
  310. context.save();
  311. context.lineWidth = canvas.width * 0.06;
  312. context.strokeStyle = 'green';
  313. context.globalAlpha = 0.5;
  314. context.lineCap = 'round';
  315. context.lineJoin = 'round';
  316. context.beginPath();
  317. path.forEach(onPolyline);
  318. if (pathTemp) {
  319. pathTemp.forEach(onPolyline);
  320. }
  321. context.stroke();
  322. context.restore();
  323. }
  324. setInterval(function() {
  325. if (status === 'playing') {
  326. lifetime = maxTime * 1000 - (new Date - start);
  327. if (lifetime < 0) {
  328. status = 'gameover';
  329. lifetime = 0;
  330. path = [];
  331. pathTemp = null;
  332. }
  333. }
  334. render();
  335. }, 100);
  336. })()