酷炫小程序

chan.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. import util from '../../utils/util'
  2. var app = getApp()
  3. Page({
  4. data: {
  5. headUrl: "",
  6. GAMETIME: 60,
  7. time: 0,
  8. mark: 0,
  9. ratio: 0,//比分
  10. gaming: false,
  11. rows: [],
  12. modal: {
  13. hidden: true
  14. },
  15. lastTouchIndex: -1,
  16. playVoide: true,
  17. audios: [],
  18. audiosLength: 0,
  19. currentAudio: 0,
  20. innerAudioContext: null,
  21. speed: 2,
  22. redMark: 0,
  23. overColIndex: -1,
  24. overRowIndex: -1,
  25. showContinue: false,
  26. showContinueTimes: 0,
  27. showContinueTimesFlag: 1,
  28. timeOver: false,
  29. overSharedFlag: false,
  30. overFlag: false,
  31. errorFlag: false,
  32. timerFlag: false,
  33. continueMsg: "求群好友帮你复活~",
  34. shareMsg: "我离音乐大师一步之遥,快来帮我挑战吧~",
  35. userId: wx.getStorageSync('userId'),
  36. },
  37. onLoad: function () {
  38. var that = this;
  39. wx.getStorage({
  40. key: 'userId',
  41. success: function(res) {
  42. that.setData({
  43. userId: res.data
  44. })
  45. },
  46. })
  47. wx.showShareMenu({
  48. withShareTicket: true //要求小程序返回分享目标信息
  49. })
  50. var userInfo = wx.getStorageSync("userInfo");
  51. this.setData({
  52. headUrl: userInfo.avatarUrl
  53. })
  54. this.data.innerAudioContext = wx.createInnerAudioContext();
  55. console.log('进入游戏页面')
  56. this.initData()
  57. },
  58. onShow: function () {
  59. // this.initData();
  60. },
  61. /**
  62. * 初始化数据
  63. */
  64. initData() {
  65. var random = Math.floor(Math.random() * 5);
  66. var audiosData = app.data.audios[random];
  67. var audiosDataLengh = audiosData.length;
  68. // 初始化方格数据
  69. var rows = []
  70. for (let i = 0; i < 4; i++) {
  71. var row = {
  72. blackIndex: util.random(1, 4, true)
  73. }
  74. rows.push(row)
  75. }
  76. // // 初始化时间
  77. // var time = this.data.GAMETIME
  78. // this.setData({
  79. // rows: rows,
  80. // time: time,
  81. // currentAudio: 0,
  82. // mark: 0,
  83. // audios: audiosData,
  84. // audiosLength: audiosDataLengh
  85. // })
  86. // 初始化时间
  87. var time = this.data.GAMETIME
  88. this.setData({
  89. rows: rows,
  90. redMark: 0
  91. });
  92. if (!this.data.showContinue) {
  93. this.setData({
  94. time: time,
  95. currentAudio: 0,
  96. mark: 0,
  97. audios: audiosData,
  98. audiosLength: audiosDataLengh,
  99. });
  100. } else {
  101. this.setData({
  102. showContinue: false,
  103. gaming: false
  104. });
  105. }
  106. },
  107. next(event) {
  108. var rowIndex = event.currentTarget.dataset.rowIndex - 0
  109. var colIndex = event.currentTarget.dataset.colIndex - 0
  110. if (rowIndex !== this.data.rows.length - 1) {
  111. return true;
  112. }
  113. // 如果当前游戏还没有开始 则开始游戏
  114. if (!this.data.gaming) {
  115. this.startGame()
  116. }
  117. if(this.data.timerFlag){
  118. this.startGame();
  119. }
  120. if (this.data.playVoide) {
  121. this.play()
  122. }
  123. // 加一分
  124. var mark = this.data.mark + 1;
  125. this.setData({
  126. lastTouchIndex: colIndex,
  127. mark: mark
  128. })
  129. var rows = this.data.rows
  130. let newRow = {
  131. blackIndex: util.random(1, 4, true)
  132. }
  133. rows.unshift(newRow)
  134. rows.pop()
  135. this.setData({
  136. rows: rows
  137. })
  138. // console.log(event)
  139. },
  140. play() {
  141. var context = wx.createInnerAudioContext();
  142. context.autoplay = true;
  143. context.src = this.data.audios[this.data.currentAudio];
  144. context.onPlay(() => {
  145. console.log('开始播放')
  146. })
  147. context.onError((res) => {
  148. console.log(res.errMsg)
  149. console.log(res.errCode)
  150. })
  151. context.onEnded(() => {
  152. context.destroy();
  153. })
  154. var currentAudioMark = this.data.currentAudio + 1;
  155. if (currentAudioMark >= this.data.audiosLength) {
  156. currentAudioMark = 0;
  157. }
  158. this.setData({
  159. currentAudio: currentAudioMark
  160. })
  161. },
  162. //播放音乐回调
  163. onplay() {
  164. console.log('音效已经播放')
  165. },
  166. // 开始游戏
  167. startGame() {
  168. this.setData({
  169. gaming: true,
  170. timerFlag: false
  171. });
  172. this.tId = setInterval(() => {
  173. console.log(this.data.time);
  174. var time = this.data.time - 1
  175. this.setData({
  176. time: time
  177. })
  178. if (this.data.time === 0) {
  179. if (this.data.overSharedFlag){
  180. this.setData({
  181. overFlag: true,
  182. })
  183. this.gameOver()
  184. } else {
  185. this.setData({
  186. overSharedFlag: true,
  187. overFlag: false,
  188. errorFlag: true,
  189. timerFlag: true
  190. })
  191. this.gameOver()
  192. var timePlus = this.data.time + 10;
  193. if (timePlus >= this.data.GAMETIME) {
  194. timePlus = this.data.GAMETIME;
  195. }
  196. this.setData({
  197. showContinue: true,
  198. time: timePlus
  199. })
  200. }
  201. }
  202. var ratio = (this.data.GAMETIME - this.data.time) / this.data.GAMETIME;
  203. this.setData({
  204. ratio: ratio
  205. })
  206. this.onReady();
  207. }, 500)
  208. },
  209. //点击错误时间
  210. over(event) {
  211. if (!this.data.gaming) {
  212. return
  213. }
  214. var rowIndex = event.currentTarget.dataset.rowIndex - 0
  215. var colIndex = event.currentTarget.dataset.colIndex - 0
  216. this.setData({
  217. redMark: 1,
  218. overColIndex: colIndex,
  219. overRowIndex: rowIndex
  220. })
  221. if (this.data.errorFlag) {
  222. this.setData({
  223. overFlag: true,
  224. })
  225. this.gameOver()
  226. } else {
  227. this.setData({
  228. overSharedFlag: false,
  229. overFlag: false,
  230. errorFlag: true,
  231. timerFlag: true
  232. })
  233. this.gameOver()
  234. var timePlus = this.data.time + 10;
  235. if(timePlus >= this.data.GAMETIME){
  236. timePlus = this.data.GAMETIME;
  237. }
  238. this.setData({
  239. showContinue: true,
  240. time: timePlus
  241. })
  242. }
  243. },
  244. /**
  245. * 结束游戏
  246. */
  247. gameOver() {
  248. this.setData({
  249. gaming: false,
  250. })
  251. clearInterval(this.tId)
  252. this.onShowModal()
  253. },
  254. reStartGame() {
  255. this.setData({
  256. 'modal.hidden': true,
  257. ratio: 0,
  258. currentAudio: 0
  259. })
  260. this.initData()
  261. },
  262. //答题错误
  263. onShowModal() {
  264. var that = this;
  265. this.setData({
  266. 'modal.hidden': false,
  267. })
  268. wx.setStorage({
  269. key: 'chanOneScore',
  270. data: that.data.mark
  271. })
  272. wx.getStorage({
  273. key: 'chanScore',
  274. success: function (res) {
  275. if (res.data <= that.data.mark) {
  276. //更新最高成绩
  277. wx.setStorage({
  278. key: 'chanScore',
  279. data: that.data.mark
  280. })
  281. app.reportGameScore('chan',that.data.mark);
  282. };
  283. console.log("showModal"+that.data.overFlag)
  284. if (that.shareOverAndRedirect()) {
  285. return;
  286. }
  287. if (that.data.overFlag) {
  288. that.setData({
  289. showContinue: false
  290. })
  291. wx.redirectTo({
  292. url: '/pages/layer/layer',
  293. })
  294. } else {
  295. if (that.data.showContinueTimesFlag >= -1){
  296. that.setData({
  297. showContinue: true
  298. })
  299. } else {
  300. wx.redirectTo({
  301. url: '/pages/layer/layer',
  302. })
  303. }
  304. }
  305. },
  306. fail: function (res) {
  307. //更新最高成绩
  308. wx.setStorage({
  309. key: 'chanScore',
  310. data: that.data.mark
  311. })
  312. app.reportGameScore('chan',that.data.mark);
  313. console.log("showModal" + that.data.overFlag)
  314. if (that.shareOverAndRedirect()) {
  315. return;
  316. }
  317. if (that.data.overFlag) {
  318. that.setData({
  319. showContinue: false
  320. })
  321. wx.redirectTo({
  322. url: '/pages/layer/layer',
  323. })
  324. } else {
  325. if (that.data.showContinueTimesFlag >= -1) {
  326. that.setData({
  327. showContinue: true
  328. })
  329. } else {
  330. wx.redirectTo({
  331. url: '/pages/layer/layer',
  332. })
  333. }
  334. }
  335. }
  336. })
  337. },
  338. onReady: function () {
  339. // 页面渲染完成
  340. var cxt_arc = wx.createCanvasContext('canvasArc');//创建并返回绘图上下文context对象。
  341. cxt_arc.setLineWidth(6);
  342. cxt_arc.setStrokeStyle('#3ea6ff');
  343. cxt_arc.setLineCap('round')
  344. cxt_arc.beginPath();//开始一个新的路径
  345. cxt_arc.arc(106, 53, 30, 0, 2 * Math.PI, false);//设置一个原点(106,106),半径为100的圆的路径到当前路径
  346. cxt_arc.stroke();//对当前路径进行描边
  347. cxt_arc.setLineWidth(6);
  348. cxt_arc.setStrokeStyle('#d2d2d2');
  349. cxt_arc.setLineCap('round')
  350. cxt_arc.beginPath();//开始一个新的路径
  351. cxt_arc.arc(106, 53, 30, -Math.PI * 1 / 2, -Math.PI * 1 / 2 + 2 * Math.PI * this.data.ratio, false);
  352. cxt_arc.stroke();//对当前路径进行描边
  353. cxt_arc.draw();
  354. },
  355. //放弃
  356. giveUp: function () {
  357. wx.redirectTo({
  358. url: '/pages/layer/layer',
  359. });
  360. },
  361. //右上角转发
  362. onShareAppMessage() {
  363. var that = this;
  364. return {
  365. title: this.data.shareMsg,
  366. path: '/pages/home/home',
  367. imageUrl: app.data.shareIcon[1],
  368. success: function (res) {
  369. console.log(res)
  370. console.log(res.shareTickets)
  371. // 转发成功
  372. if (!res.shareTickets || res.shareTickets.length == 0) {
  373. wx.showToast({
  374. title: '找不到群好友~',
  375. })
  376. } else {
  377. wx.showToast({
  378. title: '复活成功~',
  379. })
  380. var continueTimes = that.data.showContinueTimesFlag - 1;
  381. if (continueTimes <= 0) {
  382. that.initData();
  383. }
  384. that.setData({
  385. showContinueTimesFlag: continueTimes
  386. })
  387. }
  388. }
  389. }
  390. },
  391. //判断弹窗次数,如果已经分享过一次,则不再弹出,直接跳转到结果页面
  392. shareOverAndRedirect: function() {
  393. console.log(this.data.showContinueTimesFlag + " " + this.data.overFlag)
  394. if (this.data.showContinueTimesFlag < 0 && this.data.overFlag){
  395. wx.redirectTo({
  396. url: '/pages/layer/layer',
  397. })
  398. return true;
  399. }
  400. },
  401. //上报成绩事件
  402. // reportGameScore: function() {
  403. // var that = this;
  404. // app.func.req('/blackwhite/reportGameScore', 'get', "userId=" + this.data.userId, {
  405. // userId: that.data.userId,
  406. // channel: app.data.channel,
  407. // gameType: 'jingdian',
  408. // score: 99
  409. // },(res) => {
  410. // console.log(res)
  411. // })
  412. // }
  413. })