• 【小程序】实现经典2048小游戏


    概述

    经典小游戏2048,2048小游戏对于逻辑要求还是很有技术含量的,有兴趣的可以看看

    详细

    以前学习时写的小游戏2048,技术含量还是不错的,有兴趣的可以看看

    2048已经封装好了,在主页面直接引入文件可以直接调用

    演示图:

    1625056573387.gif

    调用wxml文件

    1. <view class="container">
    2. <view class="game-body">
    3. <loading hidden="{{hidden}}">
    4. 加载中...
    5. </loading>
    6. <view class="heading">
    7. <text class="title">2048</text>
    8. <view class="scores-container">
    9. <view class="score-container">{{score}}</view>
    10. <view class="best-container">{{highscore}}</view>
    11. </view>
    12. </view>
    13. <view class="game-container">
    14. <view class="game-message game-{{over ? (win ? 'won' : 'over') : ''}}">
    15. <text class="over-msg">{{overMsg}}</text>
    16. <view class="lower">
    17. <text class="retry-button" bindtap="restart">再试一次</text>
    18. </view>
    19. </view>
    20. <view class="grid-container" bindtouchstart="touchStart" bindtouchmove="touchMove" bindtouchend="touchEnd">
    21. <view wx:for="{{grids}}" wx:for-index="rowIdx" wx:for-item="row" class="grid-row">
    22. <view wx:for="{{row}}" wx:for-index="colIdx" wx:for-item="cell" class="grid-cell">
    23. <view class="tile tile-{{cell.value}}">
    24. <view wx:if="{{cell}}" class="tile-inner">
    25. {{cell.value}}
    26. </view>
    27. </view>
    28. </view>
    29. </view>
    30. </view>
    31. </view>
    32. <view class="above-game" style="margin-top:40rpx;display:flex;flex-flow: column;align-items: center;">
    33. <text class="restart-button" bindtap="restart" style="margin-top:40rpx">重新开始</text>
    34. </view>
    35. </view>
    36. </view>

    调用css

    1. .container {
    2. margin: 0;
    3. padding: 20px 0;
    4. background: #000;
    5. color: #FFF;
    6. font-family: "Helvetica Neue", Arial, sans-serif;
    7. font-size: 18px;
    8. height: 100vh;
    9. }
    10. .heading:after {
    11. content: "";
    12. display: block;
    13. clear: both;
    14. }
    15. .title {
    16. font-size: 80px;
    17. font-weight: bold;
    18. margin: 0;
    19. display: block;
    20. float: left;
    21. }
    22. .scores-container {
    23. float: right;
    24. text-align: right;
    25. }
    26. .score-container, .best-container {
    27. position: relative;
    28. display: inline-block;
    29. background: #bbada0;
    30. padding: 15px 25px;
    31. font-size: 25px;
    32. height: 25px;
    33. line-height: 47px;
    34. font-weight: bold;
    35. border-radius: 3px;
    36. color: white;
    37. text-align: center;
    38. margin: 8px 0 0 8px;
    39. }
    40. .score-container:after, .best-container:after {
    41. position: absolute;
    42. width: 100%;
    43. top: 10px;
    44. left: 0;
    45. text-transform: uppercase;
    46. font-size: 13px;
    47. line-height: 13px;
    48. text-align: center;
    49. color: #eee4da;
    50. }
    51. .score-container .score-addition, .best-container .score-addition {
    52. position: absolute;
    53. right: 30px;
    54. color: red;
    55. font-size: 25px;
    56. line-height: 25px;
    57. font-weight: bold;
    58. color: rgba(119, 110, 101, 0.9);
    59. z-index: 100;
    60. }
    61. .score-container:after {
    62. content: "Score";
    63. }
    64. .best-container:after {
    65. content: "Best";
    66. }
    67. p {
    68. margin-top: 0;
    69. margin-bottom: 10px;
    70. line-height: 1.65;
    71. }
    72. a {
    73. color: #776e65;
    74. font-weight: bold;
    75. text-decoration: underline;
    76. cursor: pointer;
    77. }
    78. strong.important {
    79. text-transform: uppercase;
    80. }
    81. hr {
    82. border: none;
    83. border-bottom: 1px solid #d8d4d0;
    84. margin-top: 20px;
    85. margin-bottom: 30px;
    86. }
    87. .game-container {
    88. margin-top: 40px;
    89. position: relative;
    90. padding: 15px;
    91. cursor: default;
    92. -webkit-touch-callout: none;
    93. -ms-touch-callout: none;
    94. -webkit-user-select: none;
    95. -moz-user-select: none;
    96. -ms-user-select: none;
    97. -ms-touch-action: none;
    98. touch-action: none;
    99. background: #bbada0;
    100. border-radius: 6px;
    101. width: 500px;
    102. height: 500px;
    103. -webkit-box-sizing: border-box;
    104. -moz-box-sizing: border-box;
    105. box-sizing: border-box;
    106. }
    107. .game-container .game-message {
    108. /*display: none;*/
    109. position: absolute;
    110. top: 0;
    111. right: 0;
    112. bottom: 0;
    113. left: 0;
    114. background: rgba(238, 228, 218, 0.5);
    115. z-index: 100;
    116. text-align: center;
    117. }
    118. .game-container .game-message p {
    119. font-size: 60px;
    120. font-weight: bold;
    121. height: 60px;
    122. line-height: 60px;
    123. margin-top: 222px;
    124. }
    125. .game-container .game-message .lower {
    126. display: block;
    127. margin-top: 59px;
    128. }
    129. .game-container .game-message a {
    130. display: inline-block;
    131. background: #8f7a66;
    132. border-radius: 3px;
    133. padding: 0 20px;
    134. text-decoration: none;
    135. color: #f9f6f2;
    136. height: 40px;
    137. line-height: 42px;
    138. margin-left: 9px;
    139. }
    140. .game-container .game-message .keep-playing-button {
    141. display: none;
    142. }
    143. .game-container .game-message.game-won {
    144. background: rgba(237, 194, 46, 0.5);
    145. color: #f9f6f2;
    146. }
    147. .game-container .game-message.game-won .keep-playing-button {
    148. display: inline-block;
    149. }
    150. .game-container .game-message.game-won, .game-container .game-message.game-over {
    151. display: block;
    152. }
    153. .grid-container {
    154. position: absolute;
    155. z-index: 1;
    156. }
    157. .grid-row {
    158. margin-bottom: 15px;
    159. }
    160. .grid-row:last-child {
    161. margin-bottom: 0;
    162. }
    163. .grid-row:after {
    164. content: "";
    165. display: block;
    166. clear: both;
    167. }
    168. .grid-cell {
    169. width: 106.25px;
    170. height: 106.25px;
    171. margin-right: 15px;
    172. float: left;
    173. border-radius: 3px;
    174. background: rgba(238, 228, 218, 0.35);
    175. }
    176. .grid-cell:last-child {
    177. margin-right: 0;
    178. }
    179. .tile-container {
    180. position: absolute;
    181. z-index: 2;
    182. }
    183. .tile, .tile .tile-inner {
    184. width: 107px;
    185. height: 107px;
    186. line-height: 107px;
    187. }
    188. .tile.tile-position-1-1 {
    189. -webkit-transform: translate(0px, 0px);
    190. -moz-transform: translate(0px, 0px);
    191. -ms-transform: translate(0px, 0px);
    192. transform: translate(0px, 0px);
    193. }
    194. .tile.tile-position-1-2 {
    195. -webkit-transform: translate(0px, 121px);
    196. -moz-transform: translate(0px, 121px);
    197. -ms-transform: translate(0px, 121px);
    198. transform: translate(0px, 121px);
    199. }
    200. .tile.tile-position-1-3 {
    201. -webkit-transform: translate(0px, 242px);
    202. -moz-transform: translate(0px, 242px);
    203. -ms-transform: translate(0px, 242px);
    204. transform: translate(0px, 242px);
    205. }
    206. .tile.tile-position-1-4 {
    207. -webkit-transform: translate(0px, 363px);
    208. -moz-transform: translate(0px, 363px);
    209. -ms-transform: translate(0px, 363px);
    210. transform: translate(0px, 363px);
    211. }
    212. .tile.tile-position-2-1 {
    213. -webkit-transform: translate(121px, 0px);
    214. -moz-transform: translate(121px, 0px);
    215. -ms-transform: translate(121px, 0px);
    216. transform: translate(121px, 0px);
    217. }
    218. .tile.tile-position-2-2 {
    219. -webkit-transform: translate(121px, 121px);
    220. -moz-transform: translate(121px, 121px);
    221. -ms-transform: translate(121px, 121px);
    222. transform: translate(121px, 121px);
    223. }
    224. .tile.tile-position-2-3 {
    225. -webkit-transform: translate(121px, 242px);
    226. -moz-transform: translate(121px, 242px);
    227. -ms-transform: translate(121px, 242px);
    228. transform: translate(121px, 242px);
    229. }
    230. .tile.tile-position-2-4 {
    231. -webkit-transform: translate(121px, 363px);
    232. -moz-transform: translate(121px, 363px);
    233. -ms-transform: translate(121px, 363px);
    234. transform: translate(121px, 363px);
    235. }
    236. .tile.tile-position-3-1 {
    237. -webkit-transform: translate(242px, 0px);
    238. -moz-transform: translate(242px, 0px);
    239. -ms-transform: translate(242px, 0px);
    240. transform: translate(242px, 0px);
    241. }
    242. .tile.tile-position-3-2 {
    243. -webkit-transform: translate(242px, 121px);
    244. -moz-transform: translate(242px, 121px);
    245. -ms-transform: translate(242px, 121px);
    246. transform: translate(242px, 121px);
    247. }
    248. .tile.tile-position-3-3 {
    249. -webkit-transform: translate(242px, 242px);
    250. -moz-transform: translate(242px, 242px);
    251. -ms-transform: translate(242px, 242px);
    252. transform: translate(242px, 242px);
    253. }
    254. .tile.tile-position-3-4 {
    255. -webkit-transform: translate(242px, 363px);
    256. -moz-transform: translate(242px, 363px);
    257. -ms-transform: translate(242px, 363px);
    258. transform: translate(242px, 363px);
    259. }
    260. .tile.tile-position-4-1 {
    261. -webkit-transform: translate(363px, 0px);
    262. -moz-transform: translate(363px, 0px);
    263. -ms-transform: translate(363px, 0px);
    264. transform: translate(363px, 0px);
    265. }
    266. .tile.tile-position-4-2 {
    267. -webkit-transform: translate(363px, 121px);
    268. -moz-transform: translate(363px, 121px);
    269. -ms-transform: translate(363px, 121px);
    270. transform: translate(363px, 121px);
    271. }
    272. .tile.tile-position-4-3 {
    273. -webkit-transform: translate(363px, 242px);
    274. -moz-transform: translate(363px, 242px);
    275. -ms-transform: translate(363px, 242px);
    276. transform: translate(363px, 242px);
    277. }
    278. .tile.tile-position-4-4 {
    279. -webkit-transform: translate(363px, 363px);
    280. -moz-transform: translate(363px, 363px);
    281. -ms-transform: translate(363px, 363px);
    282. transform: translate(363px, 363px);
    283. }
    284. .tile {
    285. position: absolute;
    286. -webkit-transition: 100ms ease-in-out;
    287. -moz-transition: 100ms ease-in-out;
    288. transition: 100ms ease-in-out;
    289. -webkit-transition-property: -webkit-transform;
    290. -moz-transition-property: -moz-transform;
    291. transition-property: transform;
    292. }
    293. .tile .tile-inner {
    294. border-radius: 3px;
    295. background: #eee4da;
    296. text-align: center;
    297. font-weight: bold;
    298. z-index: 10;
    299. font-size: 55px;
    300. }
    301. .tile.tile-2 .tile-inner {
    302. background: #eee4da;
    303. box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0), inset 0 0 0 1px rgba(255, 255, 255, 0);
    304. }
    305. .tile.tile-4 .tile-inner {
    306. background: #ede0c8;
    307. box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0), inset 0 0 0 1px rgba(255, 255, 255, 0);
    308. }
    309. .tile.tile-8 .tile-inner {
    310. color: #f9f6f2;
    311. background: #f2b179;
    312. }
    313. .tile.tile-16 .tile-inner {
    314. color: #f9f6f2;
    315. background: #f59563;
    316. }
    317. .tile.tile-32 .tile-inner {
    318. color: #f9f6f2;
    319. background: #f67c5f;
    320. }
    321. .tile.tile-64 .tile-inner {
    322. color: #f9f6f2;
    323. background: #f65e3b;
    324. }
    325. .tile.tile-128 .tile-inner {
    326. color: #f9f6f2;
    327. background: #edcf72;
    328. box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.2381), inset 0 0 0 1px rgba(255, 255, 255, 0.14286);
    329. font-size: 45px;
    330. }
    331. @media screen and (max-width:520px) {
    332. .tile.tile-128 .tile-inner {
    333. font-size: 25px;
    334. }
    335. }
    336. .tile.tile-256 .tile-inner {
    337. color: #f9f6f2;
    338. background: #edcc61;
    339. box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.31746), inset 0 0 0 1px rgba(255, 255, 255, 0.19048);
    340. font-size: 45px;
    341. }
    342. @media screen and (max-width:520px) {
    343. .tile.tile-256 .tile-inner {
    344. font-size: 25px;
    345. }
    346. }
    347. .tile.tile-512 .tile-inner {
    348. color: #f9f6f2;
    349. background: #edc850;
    350. box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.39683), inset 0 0 0 1px rgba(255, 255, 255, 0.2381);
    351. font-size: 45px;
    352. }
    353. @media screen and (max-width:520px) {
    354. .tile.tile-512 .tile-inner {
    355. font-size: 25px;
    356. }
    357. }
    358. .tile.tile-1024 .tile-inner {
    359. color: #f9f6f2;
    360. background: #edc53f;
    361. box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.47619), inset 0 0 0 1px rgba(255, 255, 255, 0.28571);
    362. font-size: 35px;
    363. }
    364. @media screen and (max-width:520px) {
    365. .tile.tile-1024 .tile-inner {
    366. font-size: 15px;
    367. }
    368. }
    369. .tile.tile-2048 .tile-inner {
    370. color: #f9f6f2;
    371. background: #edc22e;
    372. box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.55556), inset 0 0 0 1px rgba(255, 255, 255, 0.33333);
    373. font-size: 35px;
    374. }
    375. @media screen and (max-width:520px) {
    376. .tile.tile-2048 .tile-inner {
    377. font-size: 15px;
    378. }
    379. }
    380. .tile.tile-super .tile-inner {
    381. color: #f9f6f2;
    382. background: #3c3a32;
    383. font-size: 30px;
    384. }
    385. @media screen and (max-width:520px) {
    386. .tile.tile-super .tile-inner {
    387. font-size: 10px;
    388. }
    389. }
    390. .tile-merged .tile-inner {
    391. z-index: 20;
    392. }
    393. .above-game:after {
    394. content: "";
    395. display: block;
    396. clear: both;
    397. }
    398. .game-intro {
    399. float: left;
    400. line-height: 42px;
    401. margin-bottom: 0;
    402. }
    403. .restart-button {
    404. display: inline-block;
    405. background: #8f7a66;
    406. border-radius: 3px;
    407. padding: 0 20px;
    408. text-decoration: none;
    409. color: #f9f6f2;
    410. height: 40px;
    411. line-height: 42px;
    412. display: block;
    413. text-align: center;
    414. float: right;
    415. }
    416. .game-explanation {
    417. margin-top: 50px;
    418. }
    419. @media screen and (max-width:520px) {
    420. html, body {
    421. font-size: 15px;
    422. }
    423. body {
    424. margin: 20px 0;
    425. padding: 0 20px;
    426. }
    427. .title {
    428. font-size: 27px;
    429. margin-top: 15px;
    430. }
    431. /*.container {
    432. width: 280px;
    433. margin: 0 auto;
    434. }*/
    435. .score-container, .best-container {
    436. margin-top: 0;
    437. padding: 15px 10px;
    438. min-width: 40px;
    439. }
    440. .heading {
    441. margin-bottom: 10px;
    442. }
    443. .game-intro {
    444. width: 55%;
    445. display: block;
    446. box-sizing: border-box;
    447. line-height: 1.65;
    448. }
    449. .restart-button {
    450. width: 42%;
    451. padding: 0;
    452. display: block;
    453. box-sizing: border-box;
    454. margin-top: 2px;
    455. }
    456. .game-container {
    457. margin-top: 17px;
    458. position: relative;
    459. padding: 10px;
    460. cursor: default;
    461. -webkit-touch-callout: none;
    462. -ms-touch-callout: none;
    463. -webkit-user-select: none;
    464. -moz-user-select: none;
    465. -ms-user-select: none;
    466. -ms-touch-action: none;
    467. touch-action: none;
    468. background: #bbada0;
    469. border-radius: 6px;
    470. width: 280px;
    471. height: 280px;
    472. -webkit-box-sizing: border-box;
    473. -moz-box-sizing: border-box;
    474. box-sizing: border-box;
    475. }
    476. .game-container .game-message {
    477. display: none;
    478. position: absolute;
    479. top: 0;
    480. right: 0;
    481. bottom: 0;
    482. left: 0;
    483. background: rgba(238, 228, 218, 0.5);
    484. z-index: 100;
    485. text-align: center;
    486. }
    487. .game-container .game-message .over-msg {
    488. display: block;
    489. font-size: 30px;
    490. font-weight: bold;
    491. height: 30px;
    492. line-height: 30px;
    493. /*margin-top: 222px;*/
    494. margin-top: 59px;
    495. }
    496. .game-container .game-message .lower {
    497. display: block;
    498. margin-top: 59px;
    499. }
    500. .game-container .game-message .retry-button {
    501. display: inline-block;
    502. background: #8f7a66;
    503. border-radius: 3px;
    504. padding: 0 20px;
    505. text-decoration: none;
    506. color: #f9f6f2;
    507. height: 40px;
    508. line-height: 42px;
    509. margin-left: 9px;
    510. }
    511. .game-container .game-message .keep-playing-button {
    512. display: none;
    513. }
    514. .game-container .game-message.game-won {
    515. background: rgba(237, 194, 46, 0.5);
    516. color: #f9f6f2;
    517. }
    518. .game-container .game-message.game-won .keep-playing-button {
    519. display: inline-block;
    520. }
    521. .game-container .game-message.game-won, .game-container .game-message.game-over {
    522. display: block;
    523. }
    524. .grid-container {
    525. position: absolute;
    526. z-index: 1;
    527. }
    528. .grid-row {
    529. margin-bottom: 10px;
    530. }
    531. .grid-row:last-child {
    532. margin-bottom: 0;
    533. }
    534. .grid-row:after {
    535. content: "";
    536. display: block;
    537. clear: both;
    538. }
    539. .grid-cell {
    540. width: 57.5px;
    541. height: 57.5px;
    542. margin-right: 10px;
    543. float: left;
    544. border-radius: 3px;
    545. background: rgba(238, 228, 218, 0.35);
    546. }
    547. .grid-cell:last-child {
    548. margin-right: 0;
    549. }
    550. .tile, .tile .tile-inner {
    551. width: 58px;
    552. height: 58px;
    553. line-height: 58px;
    554. }
    555. .tile.tile-position-1-1 {
    556. -webkit-transform: translate(0px, 0px);
    557. -moz-transform: translate(0px, 0px);
    558. -ms-transform: translate(0px, 0px);
    559. transform: translate(0px, 0px);
    560. }
    561. .tile.tile-position-1-2 {
    562. -webkit-transform: translate(0px, 67px);
    563. -moz-transform: translate(0px, 67px);
    564. -ms-transform: translate(0px, 67px);
    565. transform: translate(0px, 67px);
    566. }
    567. .tile.tile-position-1-3 {
    568. -webkit-transform: translate(0px, 135px);
    569. -moz-transform: translate(0px, 135px);
    570. -ms-transform: translate(0px, 135px);
    571. transform: translate(0px, 135px);
    572. }
    573. .tile.tile-position-1-4 {
    574. -webkit-transform: translate(0px, 202px);
    575. -moz-transform: translate(0px, 202px);
    576. -ms-transform: translate(0px, 202px);
    577. transform: translate(0px, 202px);
    578. }
    579. .tile.tile-position-2-1 {
    580. -webkit-transform: translate(67px, 0px);
    581. -moz-transform: translate(67px, 0px);
    582. -ms-transform: translate(67px, 0px);
    583. transform: translate(67px, 0px);
    584. }
    585. .tile.tile-position-2-2 {
    586. -webkit-transform: translate(67px, 67px);
    587. -moz-transform: translate(67px, 67px);
    588. -ms-transform: translate(67px, 67px);
    589. transform: translate(67px, 67px);
    590. }
    591. .tile.tile-position-2-3 {
    592. -webkit-transform: translate(67px, 135px);
    593. -moz-transform: translate(67px, 135px);
    594. -ms-transform: translate(67px, 135px);
    595. transform: translate(67px, 135px);
    596. }
    597. .tile.tile-position-2-4 {
    598. -webkit-transform: translate(67px, 202px);
    599. -moz-transform: translate(67px, 202px);
    600. -ms-transform: translate(67px, 202px);
    601. transform: translate(67px, 202px);
    602. }
    603. .tile.tile-position-3-1 {
    604. -webkit-transform: translate(135px, 0px);
    605. -moz-transform: translate(135px, 0px);
    606. -ms-transform: translate(135px, 0px);
    607. transform: translate(135px, 0px);
    608. }
    609. .tile.tile-position-3-2 {
    610. -webkit-transform: translate(135px, 67px);
    611. -moz-transform: translate(135px, 67px);
    612. -ms-transform: translate(135px, 67px);
    613. transform: translate(135px, 67px);
    614. }
    615. .tile.tile-position-3-3 {
    616. -webkit-transform: translate(135px, 135px);
    617. -moz-transform: translate(135px, 135px);
    618. -ms-transform: translate(135px, 135px);
    619. transform: translate(135px, 135px);
    620. }
    621. .tile.tile-position-3-4 {
    622. -webkit-transform: translate(135px, 202px);
    623. -moz-transform: translate(135px, 202px);
    624. -ms-transform: translate(135px, 202px);
    625. transform: translate(135px, 202px);
    626. }
    627. .tile.tile-position-4-1 {
    628. -webkit-transform: translate(202px, 0px);
    629. -moz-transform: translate(202px, 0px);
    630. -ms-transform: translate(202px, 0px);
    631. transform: translate(202px, 0px);
    632. }
    633. .tile.tile-position-4-2 {
    634. -webkit-transform: translate(202px, 67px);
    635. -moz-transform: translate(202px, 67px);
    636. -ms-transform: translate(202px, 67px);
    637. transform: translate(202px, 67px);
    638. }
    639. .tile.tile-position-4-3 {
    640. -webkit-transform: translate(202px, 135px);
    641. -moz-transform: translate(202px, 135px);
    642. -ms-transform: translate(202px, 135px);
    643. transform: translate(202px, 135px);
    644. }
    645. .tile.tile-position-4-4 {
    646. -webkit-transform: translate(202px, 202px);
    647. -moz-transform: translate(202px, 202px);
    648. -ms-transform: translate(202px, 202px);
    649. transform: translate(202px, 202px);
    650. }
    651. .tile .tile-inner {
    652. font-size: 35px;
    653. }
    654. .game-message p {
    655. font-size: 30px !important;
    656. height: 30px !important;
    657. line-height: 30px !important;
    658. margin-top: 90px !important;
    659. }
    660. .game-message .lower {
    661. margin-top: 30px !important;
    662. }
    663. }

    调用js文件

    1. var app = getApp();
    2. var Grid = require('./grid.js');
    3. var Tile = require('./tile.js');
    4. var GameManager = require('./game_manager.js');
    5. var config = {
    6. data: {
    7. hidden: false,
    8. // 游戏数据可以通过参数控制
    9. grids: [],
    10. over: false,
    11. win: false,
    12. score: 0,
    13. highscore: 0,
    14. overMsg: '游戏结束'
    15. },
    16. onLoad: function() {
    17. this.GameManager = new GameManager(4);
    18. this.setData({
    19. grids: this.GameManager.setup(),
    20. highscore: wx.getStorageSync('highscore') || 0
    21. });
    22. },
    23. onReady: function() {
    24. var that = this;
    25. // 页面渲染完毕隐藏loading
    26. that.setData({
    27. hidden: true
    28. });
    29. },
    30. onShow: function() {
    31. // 页面展示
    32. },
    33. onHide: function() {
    34. // 页面隐藏
    35. },
    36. onUnload: function() {
    37. // 页面关闭
    38. },
    39. // 更新视图数据
    40. updateView: function(data) {
    41. // 游戏结束
    42. if(data.over){
    43. data.overMsg = '游戏结束';
    44. }
    45. // 获胜
    46. if(data.win){
    47. data.overMsg = '恭喜';
    48. }
    49. this.setData(data);
    50. },
    51. // 重新开始
    52. restart: function() {
    53. this.updateView({
    54. grids: this.GameManager.restart(),
    55. over: false,
    56. won: false,
    57. score: 0
    58. });
    59. },
    60. touchStartClienX: 0,
    61. touchStartClientY: 0,
    62. touchEndClientX: 0,
    63. touchEndClientY: 0,
    64. isMultiple: false, // 多手指操作
    65. touchStart: function(events) {
    66. // 多指操作
    67. this.isMultiple = events.touches.length > 1;
    68. if (this.isMultiple) {
    69. return;
    70. }
    71. var touch = events.touches[0];
    72. this.touchStartClientX = touch.clientX;
    73. this.touchStartClientY = touch.clientY;
    74. },
    75. touchMove: function(events) {
    76. var touch = events.touches[0];
    77. this.touchEndClientX = touch.clientX;
    78. this.touchEndClientY = touch.clientY;
    79. },
    80. touchEnd: function(events) {
    81. if (this.isMultiple) {
    82. return;
    83. }
    84. var dx = this.touchEndClientX - this.touchStartClientX;
    85. var absDx = Math.abs(dx);
    86. var dy = this.touchEndClientY - this.touchStartClientY;
    87. var absDy = Math.abs(dy);
    88. if (Math.max(absDx, absDy) > 10) {
    89. var direction = absDx > absDy ? (dx > 0 ? 1 : 3) : (dy > 0 ? 2 : 0);
    90. var data = this.GameManager.move(direction) || {
    91. grids: this.data.grids,
    92. over: this.data.over,
    93. won: this.data.won,
    94. score: this.data.score
    95. };
    96. var highscore = wx.getStorageSync('highscore') || 0;
    97. if(data.score > highscore){
    98. wx.setStorageSync('highscore', data.score);
    99. }
    100. this.updateView({
    101. grids: data.grids,
    102. over: data.over,
    103. won: data.won,
    104. score: data.score,
    105. highscore: Math.max(highscore, data.score)
    106. });
    107. }
    108. }
    109. };
    110. Page(config);

    代码量不大,学习还是很有价值的

    补充,项目结构图:

    image.png

  • 相关阅读:
    HTML的学习-通过创建相册WEB学习HTML-第二部分
    记录--源码视角,Vue3为什么推荐使用ref而不是reactive
    百度搜索业务交付无人值守实践与探索
    为什么说元宇宙正在创造社交连接的未来?
    数据结构—查找方法改进流程
    关于序列化与反序列化解题
    Apache HTTP Server、IIS反向代理设置
    Http请求-hutool工具类的使用
    重磅 | 思特威获得ISO 26262:2018汽车功能安全ASIL D流程认证证书
    练习-Java输入输出之File类之获取文件信息(2)
  • 原文地址:https://blog.csdn.net/hanjiepo/article/details/133026402