• 接物游戏demo


    接物游戏demo:

    接物元素设置了不同分值

    指定时间内,接到元素则加分,接到炸弹则减分,计时结束,游戏停止

     demo代码:

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    6. <meta name="viewport" content="width=750, user-scalable=no">
    7. <meta http-equiv="pragma" content="no-cache">
    8. <meta http-equiv="Cache-Control" content="no-cache">
    9. <meta http-equiv="Expires" content="0">
    10. <meta name="apple-mobile-web-app-capable" content="yes">
    11. <meta name="apple-mobile-web-app-status-bar-style" content="black">
    12. <meta name="format-detection" content="telephone=no">
    13. <meta name="apple-mobile-web-app-title" content="">
    14. <title>接物demotitle>
    15. <style>
    16. @charset "utf-8";
    17. *{ margin:0px; padding:0px; box-sizing:border-box; -webkit-tap-highlight-color:rgba(0,0,0,0); tap-highlight-color:rgba(0,0,0,0); cursor:pointer;}
    18. html{ max-width:750px; margin:0 auto;}
    19. body{ font-family:"PingFangSC-Regular","STHeitiSC-Light","微软雅黑","Microsoft YaHei","sans-serif"; font-size:28px; line-height:1.6; color:#333;
    20. -webkit-user-select:none; user-select:none;
    21. -webkit-touch-callout:none; touch-callout:none;
    22. }
    23. li{ list-style:none;}
    24. i{ font-style:normal;}
    25. [v-cloak]{ display: none;}
    26. html, body{ height:100%; background-color: #fff; position:relative; overflow: hidden;}
    27. .page{ width:100%; height:100%; position:absolute; left:0; top:0; overflow: hidden;}
    28. .page.game{ background-color: skyblue; overflow-y: auto; -webkit-overflow-scrolling: touch;}
    29. .game_body{}
    30. .show_time{ width: 100%; padding: 20px; text-align: center; position: absolute; left: 0; top: 0;}
    31. .show_time span{ margin: 0 10px;}
    32. .show_time span i{ margin: 0 5px; color: palevioletred; font-size: 32px; font-weight: bold;}
    33. #imgShowList{ width: 100%; height: 100vh; position: relative; overflow: hidden;}
    34. .basket{ width: 100%; position: fixed; left: 0; top: 1100px;}
    35. #gameBtn{ width: 200px; height: 80px; position: absolute; left: 0; top: 0;}
    36. .game_start_btn{ padding: 0 15px; height: 60px; line-height: 60px; border-radius: 30px 0 0 30px; background-color: pink; position: absolute; right: 0; top: 60vh;}
    37. style>
    38. head>
    39. <body>
    40. <section id="app" v-cloak>
    41. <div class="page game">
    42. <div class="game_body">
    43. <div id="imgShowList">
    44. <img-down v-on:set_score="addScore" :countdown_time="countdownTime" :game_btn_height="gameBtnHeight" :game_btn_width="gameBtnWidth" :game_btn_top="gameBtnTop" :game_btn_left="gameLeftNum" :down_img="item" v-for="item in gameImgList">img-down>
    45. div>
    46. <div class="basket">
    47. <img id="gameBtn" src="btn.png" >
    48. div>
    49. <div v-if="!inGame" @click="gameStart" class="game_start_btn">开始游戏div>
    50. <div class="show_time"><span>剩余<i>{{countdownTime}}i>sspan> | <span>得分<i>{{currScore}}i>span>div>
    51. div>
    52. div>
    53. section>
    54. <template id="imgDownTemp">
    55. <div v-if="showTemp" :style="'text-align: center;position:absolute;width:' + down_img.imgW + 'px;left:' + down_img.imgX + 'px;top:' + imgY + 'px'">
    56. <p v-if="showScore" style="width: 100%;position: absolute;left: 0;bottom: -100%;">{{down_img.score > 0 ? '+' + down_img.score : down_img.score}}p>
    57. <img v-else :src="down_img.img" style="width: 100%;position: absolute;left: 0;bottom: -100%;" />
    58. div>
    59. template>
    60. <script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.7.14/vue.min.js">script>
    61. <script src="//cdnjs.cloudflare.com/ajax/libs/howler/2.2.4/howler.min.js">script>
    62. <script type="text/javascript">
    63. var soundYes = new Howl({
    64. src: ['game/yes.mp3']
    65. });
    66. var soundNo = new Howl({
    67. src: ['game/no.mp3']
    68. });
    69. script>
    70. <script type="text/javascript">
    71. Vue.component('img-down', {
    72. data: function () {
    73. return {
    74. showTemp:true, // 坠落图片Y轴超过区域时不再显示
    75. imgY:0, // 坠落图片Y轴
    76. showScore:false, // 坠落图片被“接住”时,展示加分或减分分值
    77. setpNum:1, // 坠落图片Y轴偏移量
    78. timer:null,
    79. timeStep:null, // 定时器执行时长(毫秒)
    80. // 定时器执行时长(坠落速度快、中、慢)(毫秒)
    81. timeStepData:{
    82. timeFast:5,
    83. timeslow:8,
    84. timeslowest:10,
    85. }
    86. }
    87. },
    88. props:['countdown_time','game_btn_height','game_btn_width','game_btn_top','game_btn_left','down_img'],
    89. template: '#imgDownTemp',
    90. mounted() {
    91. // 坠落图片Y轴偏移量根据随机数值设置,以此影响不同的坠落速度效果
    92. if(Math.random()*10 > 5){
    93. this.setpNum = 2;
    94. }
    95. // 计时到20秒、10秒时,坠落速度不同程度加快
    96. if(this.countdown_time <= 10){
    97. this.timeStep = this.timeStepData['timeFast'];
    98. }else if(this.countdown_time <= 20){
    99. this.timeStep = this.timeStepData['timeslow'];
    100. }else{
    101. if(this.setpNum == 1){
    102. this.timeStep = this.timeStepData['timeslow'];
    103. }else{
    104. this.timeStep = this.timeStepData['timeslowest'];
    105. }
    106. }
    107. this.timer = setInterval(this.imgShowDown,this.timeStep);
    108. },
    109. methods:{
    110. imgShowDown(){
    111. // 计时结束-游戏结束
    112. if(this.countdown_time <= 0){
    113. clearInterval(this.timer);
    114. }
    115. this.imgY+=this.setpNum;
    116. // 坠落图片到达接物篮子Y轴位置
    117. if(this.imgY > this.game_btn_top){
    118. // clearInterval(this.timer);
    119. // 接到了
    120. if((this.down_img.imgX + this.down_img.imgW) > this.game_btn_left && this.down_img.imgX < (this.game_btn_width + this.game_btn_left)){
    121. this.$emit('set_score',this.down_img.score);
    122. this.showScore = true;
    123. clearInterval(this.timer);
    124. setTimeout(()=>{
    125. this.showTemp = false;
    126. },100)
    127. if(this.down_img.score > 0){
    128. soundYes.play();
    129. }else{
    130. soundNo.play();
    131. }
    132. }else{
    133. // this.showTemp = false;
    134. }
    135. }
    136. //
    137. if(this.imgY > (this.game_btn_top + this.game_btn_height)){
    138. clearInterval(this.timer);
    139. this.showTemp = false;
    140. }
    141. }
    142. }
    143. })
    144. document.oncontextmenu = function(e){
    145. e.preventDefault();
    146. }
    147. var VM = new Vue({
    148. el:"#app",
    149. data:{
    150. winW:0, // 页面宽度
    151. gameBtn:'', // 接物篮子
    152. gameBtnHeight:0, // 接物篮子高度
    153. gameBtnWidth:0, // 接物篮子宽度
    154. gameBtnWidthHalf:0, // 接物篮子宽度一半
    155. gameBtnMaxLeft:0, // 接物篮子最大left值
    156. gameBtnTop:0, // 接物篮子对顶部距离
    157. gameLeftNum:0, // 接物篮子left值
    158. inGame:false, // 是否游戏进行中
    159. currScore:0, // 当前得分
    160. countdownTiming:0,
    161. countdownTimeDefault:30, // 初始化倒计时时间(秒)
    162. countdownTime:0,
    163. gameImgW:60, // 坠落图片宽
    164. gameImgMaxLeft:0, // 坠落图片的最大left值
    165. gameImgList:[], // 坠落图片-最终展示的列表数据
    166. // 坠落图片-原始配置数据(图片及对应分值),游戏时从中随机取一个追加至gameImgList中
    167. gameImgData:[
    168. {
    169. img:'game/1.png',
    170. score:1,
    171. },
    172. {
    173. img:'game/2.png',
    174. score:2,
    175. },
    176. {
    177. img:'game/3.png',
    178. score:3,
    179. },
    180. {
    181. img:'game/4.png',
    182. score:4,
    183. },
    184. {
    185. img:'game/5.png',
    186. score:5,
    187. },
    188. {
    189. img:'game/6.png',
    190. score:-5,
    191. },
    192. {
    193. img:'game/6.png',
    194. score:-5,
    195. },
    196. ],
    197. },
    198. created() {
    199. },
    200. mounted() {
    201. this.initParameter();
    202. },
    203. watch: {
    204. },
    205. methods:{
    206. // 游戏参数初始化数据
    207. initParameter(){
    208. this.winW = document.documentElement.offsetWidth || document.body.offsetWidth;
    209. this.gameBtn = document.getElementById('gameBtn');
    210. this.gameBtnHeight = this.gameBtn.offsetHeight;
    211. this.gameBtnWidth = this.gameBtn.offsetWidth;
    212. this.gameBtnWidthHalf = this.gameBtnWidth / 2;
    213. this.gameBtnMaxLeft = this.winW - this.gameBtnWidthHalf * 2;
    214. this.gameBtnTop = document.getElementsByClassName('basket')[0].offsetTop;
    215. this.gameImgMaxLeft = this.winW - this.gameImgW;
    216. this.gameBtn.style.left = (this.winW / 2 - this.gameBtnWidthHalf) + 'px';
    217. },
    218. // 拖动接物篮子
    219. touchmove(e){
    220. e.preventDefault();
    221. this.gameLeftNum = e.touches[0].pageX - this.gameBtnWidthHalf;
    222. this.gameLeftNum = this.gameLeftNum < 0 ? 0 : this.gameLeftNum > this.gameBtnMaxLeft ? this.gameBtnMaxLeft : Math.floor(this.gameLeftNum);
    223. this.gameBtn.style.left = this.gameLeftNum + 'px';
    224. // console.log(this.gameLeftNum , this.gameBtn.offsetLeft)
    225. },
    226. // 开始游戏,计时等设置
    227. startGame(){
    228. this.inGame = true;
    229. this.currScore = 0;
    230. this.currScoreData = [];
    231. this.gameImgList = [];
    232. this.gameBtn.addEventListener('touchmove',this.touchmove,false);
    233. this.gameBtn.style.left = (this.winW / 2 - this.gameBtnWidthHalf) + 'px';
    234. this.countdownTiming = 0;
    235. this.countdownTime = this.countdownTimeDefault;
    236. this.changeTime();
    237. },
    238. // 计时
    239. // timing , rafId;
    240. changeTime(k){
    241. // console.log(k);
    242. if(!this.timing && k){
    243. this.timing = k
    244. }
    245. // 1秒执行60次
    246. this.rafId = requestAnimationFrame(this.changeTime);
    247. // 倒计时计算
    248. this.countdownTiming++;
    249. // 1秒(1000ms)执行一次
    250. if(this.countdownTiming % 60 == 0){
    251. this.countdownTime-= 1;
    252. }
    253. if(this.countdownTime <= 0){
    254. this.gameEnd();
    255. cancelAnimationFrame(this.rafId);
    256. }
    257. // 动态添加坠落图片(时间越短,添加的坠落图片越多)
    258. if(this.countdownTiming % 40 == 0){
    259. this.setImgToPage();
    260. }
    261. },
    262. // 动态添加坠落图片
    263. setImgToPage(){
    264. let randomKey = Math.floor(Math.random() * this.gameImgData.length);
    265. let imgX = Math.floor(Math.random() * this.gameImgMaxLeft);
    266. this.gameImgList.push({imgX,imgY:0,imgW:this.gameImgW,...this.gameImgData[randomKey]})
    267. },
    268. // 加分减分统计
    269. addScore(score){
    270. this.currScore += score;
    271. this.currScoreData.push(score);
    272. },
    273. ///
    274. // 开始游戏
    275. gameStart(){
    276. this.startGame();
    277. },
    278. // 计时结束-游戏结束
    279. gameEnd(){
    280. console.log(this.currScoreData);
    281. this.inGame = false;
    282. this.gameBtn.removeEventListener('touchmove',this.touchmove,false);
    283. },
    284. }
    285. })
    286. script>
    287. body>
    288. html>

  • 相关阅读:
    【示波器专题】示波器探头不同的衰减比对测量的影响
    【业务架构】价值实现、价值定位、价值创造
    2023年9月电子学会Python等级考试试卷(四级)答案解析
    虚拟号码认证如何开通?
    二维码智慧门牌管理系统升级解决方案:高效服务审核流程
    PHP数组处理$arr1转换为$arr2
    C语言 cortex-A7核UART总线实验
    【第八章 多线程、并行并发、多线程的创建方式】
    MySQL索引下推
    JAVA经典百题之9的次数
  • 原文地址:https://blog.csdn.net/qq_16494241/article/details/133952319