game.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <template>
  2. <view>
  3. <view class="flex-container">
  4. <view class="result" :style="'visibility:' + isShow">
  5. <text>恭喜,您摇到的数字是:{{ total }}</text>
  6. </view>
  7. <view class="dice-box">
  8. <view v-if="num1 === 0" class="first face">
  9. <view class="pip"></view>
  10. </view>
  11. <view v-else-if="num1 === 1" class="second face">
  12. <view class="pip"></view>
  13. <view class="pip"></view>
  14. </view>
  15. <view v-else-if="num1 === 2" class="third face">
  16. <view class="pip"></view>
  17. <view class="pip third-item-center"></view>
  18. <view class="pip"></view>
  19. </view>
  20. <view v-else-if="num1 === 3" class="fourth face">
  21. <view class="column">
  22. <view class="pip"></view>
  23. <view class="pip"></view>
  24. </view>
  25. <view class="column">
  26. <view class="pip"></view>
  27. <view class="pip"></view>
  28. </view>
  29. </view>
  30. <view v-else-if="num1 === 4" class="fifth face">
  31. <view class="column">
  32. <view class="pip"></view>
  33. <view class="pip"></view>
  34. </view>
  35. <view class="column fifth-column-center">
  36. <view class="pip"></view>
  37. </view>
  38. <view class="column">
  39. <view class="pip"></view>
  40. <view class="pip"></view>
  41. </view>
  42. </view>
  43. <view v-else class="sixth face">
  44. <view class="column">
  45. <view class="pip"></view>
  46. <view class="pip"></view>
  47. <view class="pip"></view>
  48. </view>
  49. <view class="column">
  50. <view class="pip"></view>
  51. <view class="pip"></view>
  52. <view class="pip"></view>
  53. </view>
  54. </view>
  55. </view>
  56. <view class="second-row">
  57. <view class="dice-box">
  58. <view v-if="num2 === 0" class="first face">
  59. <view class="pip"></view>
  60. </view>
  61. <view v-else-if="num2 === 1" class="second face">
  62. <view class="pip"></view>
  63. <view class="pip"></view>
  64. </view>
  65. <view v-else-if="num2 === 2" class="third face">
  66. <view class="pip"></view>
  67. <view class="pip third-item-center"></view>
  68. <view class="pip"></view>
  69. </view>
  70. <view v-else-if="num2 === 3" class="fourth face">
  71. <view class="column">
  72. <view class="pip"></view>
  73. <view class="pip"></view>
  74. </view>
  75. <view class="column">
  76. <view class="pip"></view>
  77. <view class="pip"></view>
  78. </view>
  79. </view>
  80. <view v-else-if="num2 === 4" class="fifth face">
  81. <view class="column">
  82. <view class="pip"></view>
  83. <view class="pip"></view>
  84. </view>
  85. <view class="column fifth-column-center">
  86. <view class="pip"></view>
  87. </view>
  88. <view class="column">
  89. <view class="pip"></view>
  90. <view class="pip"></view>
  91. </view>
  92. </view>
  93. <view v-else class="sixth face">
  94. <view class="column">
  95. <view class="pip"></view>
  96. <view class="pip"></view>
  97. <view class="pip"></view>
  98. </view>
  99. <view class="column">
  100. <view class="pip"></view>
  101. <view class="pip"></view>
  102. <view class="pip"></view>
  103. </view>
  104. </view>
  105. </view>
  106. <view class="dice-box">
  107. <view v-if="num3 === 0" class="first face">
  108. <view class="pip"></view>
  109. </view>
  110. <view v-else-if="num3 === 1" class="second face">
  111. <view class="pip"></view>
  112. <view class="pip"></view>
  113. </view>
  114. <view v-else-if="num3 === 2" class="third face">
  115. <view class="pip"></view>
  116. <view class="pip third-item-center"></view>
  117. <view class="pip"></view>
  118. </view>
  119. <view v-else-if="num3 === 3" class="fourth face">
  120. <view class="column">
  121. <view class="pip"></view>
  122. <view class="pip"></view>
  123. </view>
  124. <view class="column">
  125. <view class="pip"></view>
  126. <view class="pip"></view>
  127. </view>
  128. </view>
  129. <view v-else-if="num3 === 4" class="fifth face">
  130. <view class="column">
  131. <view class="pip"></view>
  132. <view class="pip"></view>
  133. </view>
  134. <view class="column fifth-column-center">
  135. <view class="pip"></view>
  136. </view>
  137. <view class="column">
  138. <view class="pip"></view>
  139. <view class="pip"></view>
  140. </view>
  141. </view>
  142. <view v-else class="sixth face">
  143. <view class="column">
  144. <view class="pip"></view>
  145. <view class="pip"></view>
  146. <view class="pip"></view>
  147. </view>
  148. <view class="column">
  149. <view class="pip"></view>
  150. <view class="pip"></view>
  151. <view class="pip"></view>
  152. </view>
  153. </view>
  154. </view>
  155. </view>
  156. <view class="button-box">
  157. <button @tap="shakeClick" class="btnc" :type="buttonType">{{ buttonValue }}</button>
  158. </view>
  159. </view>
  160. </view>
  161. </template>
  162. <script>
  163. export default {
  164. data() {
  165. return {
  166. dice: ['first', 'second', 'third', 'fourth', 'fifth', 'sixth'],
  167. buttonType: 'primary',
  168. buttonValue: '摇一摇',
  169. isShow: 'hidden',
  170. num1: 0,
  171. num2: 0,
  172. num3: 0,
  173. total: 0,
  174. global: {
  175. timer: null,
  176. isRand: false
  177. }
  178. };
  179. },
  180. onHide() {
  181. if (this.global?.timer) {
  182. clearInterval(this.global.timer);
  183. this.global.isRand = false;
  184. }
  185. },
  186. onUnload() {
  187. if (this.global?.timer) {
  188. clearInterval(this.global.timer);
  189. this.global.isRand = false;
  190. }
  191. },
  192. methods: {
  193. shakeClick: function () {
  194. var that = this;
  195. this.global.isRand = !this.global.isRand;
  196. if (this.global.isRand) {
  197. this.global.timer = setInterval(() => {
  198. const a = Math.floor(6 * Math.random());
  199. const i = Math.floor(6 * Math.random());
  200. const s = Math.floor(6 * Math.random());
  201. this.num1 = a;
  202. this.num2 = i;
  203. this.num3 = s;
  204. this.total = a + i + s + 3; // 0-5映射为1-6
  205. }, 50);
  206. } else {
  207. clearInterval(this.global.timer);
  208. }
  209. this.dice = this.dice;
  210. this.buttonType = this.global.isRand ? 'default' : 'primary';
  211. this.buttonValue = this.global.isRand ? '停止' : '摇一摇';
  212. this.isShow = this.global.isRand ? 'hidden' : 'visible';
  213. }
  214. }
  215. };
  216. </script>
  217. <style>
  218. @import './game.css';
  219. </style>