首页

源码搜藏网

首页 > 微信小程序 >

微信小程序版2048小游戏 德云色来了源码免费下载

创建时间:2018-08-09 16:48  浏览

微信小程序版2048小游戏 德云色来了源码免费下载
微信小程序版2048小游戏 德云色来了源码免费下载
暂无演示进入下载地址列表

微信小程序版2048小游戏德云色来了


游戏主逻辑:
  1. // pages/game/programmer.js
  2. var game2048 = require('../../utils/game2048.js');
  3. var gameServer = require('../../utils/gameServer.js');
  4. var util = require('../../utils/util.js');
  5. var app = getApp()
  6.  
  7. // 胜利音效
  8. const winAudio = wx.createInnerAudioContext()
  9. winAudio.src = 'http://demo.infinitysia.com/dys/2048/assets/win.mp3'
  10. winAudio.obeyMuteSwitch = false
  11.  
  12. // move音效
  13. const moveAudio = wx.createInnerAudioContext()
  14. moveAudio.src = 'http://demo.infinitysia.com/dys/2048/assets/move.mp3'
  15. moveAudio.obeyMuteSwitch = false
  16.  
  17. // 失败音效
  18. const failAudio = wx.createInnerAudioContext()
  19. failAudio.src = 'http://demo.infinitysia.com/dys/2048/assets/lose.mp3'
  20. failAudio.obeyMuteSwitch = false
  21.  
  22. Page({
  23.   data:{
  24.     // 游戏数组值
  25.     gridValue:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  26.     // 游戏重新开始提示
  27.     restartPrompt:"再来亿把",
  28.     // 游戏难度:1.时间160ms;2.时间80ms;3.时间40ms;4.时间20ms;5.时间10ms;
  29.     gameLevel:[1,2,3,4,5],
  30.     // 游戏难度初始值 index of gameLevel
  31.     level: 3,
  32.     // 游戏难度提示
  33.     levelPrompt:"难度",
  34.     // 游戏模式:1.输出2,4;2.输出2.4.8;3.输出2.4.8.16;4.输出2.4.8.16.32;5.输出2.4.8.16.32.64;
  35.     gameMode: [1,2,3,4,5],
  36.     // 游戏模式初始值 index of gameMode
  37.     mode: 1,
  38.     // 游戏模式提示
  39.     modePrompt:"模式",
  40.     // 游戏运行时间
  41.     gameTime:"00:00:00",
  42.     // 游戏运行 开始时间
  43.     gameStartDate:0,
  44.     // 游戏运行 结束时间
  45.     gameEndDate:0,
  46.     // 游戏分数
  47.     gameScore: 0,
  48.     // 游戏触摸控制
  49.     gameTouchInfo: {
  50.         pointOrigin: {
  51.             x: 0,
  52.             y: 0
  53.         },
  54.         pointTarget: {
  55.             x: 0,
  56.             y: 0
  57.         },
  58.         isValid: false
  59.     },
  60.     // 游戏触摸控制阈值
  61.     gameDistanceThreshold:10,
  62.     // 排行榜
  63.     chartsUsers: [
  64.       {
  65.         avatar:"http://wx.qlogo.cn/mmopen/vi_32/l8W3SEfertzlK6csQd23scfZG30hXDVP0mT2ODFqoPlkmmeic1ZXoiczVicppy68kPiajjEIbA5Daf7d6erlRdib5uQ/0",
  66.         name:"chenxi****",
  67.         score:"2048",
  68.         level:"3级",
  69.         mode:"3",
  70.         time:"05:34"
  71.       }, {
  72.         name:"**********",
  73.         score:"4096",
  74.         level:"5级",
  75.         mode:"5",
  76.         time:"15:03"
  77.       }
  78.     ],
  79.  
  80.     // 胜利声音
  81.     winAudio: null
  82.   },
  83.   onLoad:function(options){
  84.     // 页面初始化 options为页面跳转所带来的参数
  85.     const winAudio = wx.createInnerAudioContext()
  86.     // winAudio.src = '../../assets/win.mp3'
  87.     winAudio.src = 'http://demo.infinitysia.com/dys/2048/assets/win.mp3'
  88.     // winAudio.autoPlay = true
  89.     winAudio.obeyMuteSwitch = false
  90.  
  91.     // innerAudioContext.play()
  92.     console.log(options)
  93.  
  94.     this.setData({
  95.       winAudio
  96.     })
  97.  
  98.     winAudio.onPlay(() => {
  99.       console.log('playyyyy')
  100.     })
  101.   },
  102.   onReady:function(){
  103.     // 页面渲染完成
  104.     game2048.resetGame();
  105.     this.setData({
  106.       gridValue:game2048.getGameArray().slice()
  107.     });
  108.     // 用户数据获取
  109.     // this.inspectUserServer();
  110.     // time
  111.     console.log("gameStartDate:", this.data.gameStartDate);
  112.     var that = this;
  113.     setInterval(function() {
  114.       var _gameEndDate = Date.now();
  115.       var playTime = Math.floor((_gameEndDate - that.data.gameStartDate) / 1000);
  116.       that.setData({
  117.         gameEndDate: _gameEndDate,
  118.         gameTime: util.formatSecondsTime(playTime)
  119.       });
  120.       },1000);
  121.   },
  122.   onShow:function(){
  123.     // 页面显示
  124.     game2048.printAuthor();
  125.     // test
  126.     this.setData({
  127.       gameStartDate:new Date().getTime()
  128.     });
  129.     // 排行榜
  130.     // this.getRank();
  131.   },
  132.   onHide:function(){
  133.     // 页面隐藏
  134.   },
  135.   onUnload:function(){
  136.     // 页面关闭
  137.   },
  138.   handleRestart:function(event){
  139.     // 游戏重新开始
  140.     game2048.resetGame();
  141.     this.setData({
  142.       gridValue:game2048.getGameArray().slice(),
  143.       gameStartDate:new Date().getTime(),
  144.       gameTime:"00:00:00"
  145.     });
  146.   },
  147.   handleTouchMove:function(event){
  148.     // 游戏2048网格界面触摸移动
  149.     // console.log(event);
  150.     // game2048.playGame("moveButtom");
  151.     // this.setData({
  152.     //  gridValue:game2048.getGameArray().slice()
  153.     // });
  154.   },
  155.   handleTouchStart:function(event){
  156.     // 游戏2048网格界面触摸开始
  157.     // console.log(event);
  158.     if (!this.data.gameTouchInfo.isValid) {
  159.       this.setData({
  160.         'gameTouchInfo.pointOrigin.x': event['changedTouches'][0].pageX,
  161.         'gameTouchInfo.pointOrigin.y': event['changedTouches'][0].pageY,
  162.         'gameTouchInfo.isValid': true
  163.       });
  164.     }
  165.   },
  166.   handleTouchEnd:function(event){
  167.     // 游戏2048网格界面触摸结束
  168.     // console.log(event);
  169.     if (this.data.gameTouchInfo.isValid) {
  170.       this.setData({
  171.         'gameTouchInfo.pointTarget.x': event['changedTouches'][0].pageX,
  172.         'gameTouchInfo.pointTarget.y': event['changedTouches'][0].pageY,
  173.         'gameTouchInfo.isValid': false
  174.       });
  175.       var direction = this.getTouchDirection(this.data.gameTouchInfo.pointOrigin,
  176.         this.data.gameTouchInfo.pointTarget, this.data.gameDistanceThreshold)
  177.       console.log(direction);
  178.       game2048.playGame(direction);
  179.       
  180.       // 停止所有音频
  181.       // winAudio.stop()
  182.       // failAudio.stop()
  183.       // moveAudio.stop()
  184.       
  185.       const that = this
  186.       if (game2048.getGameStatus() === 'end') {
  187.         failAudio.play()
  188.         wx.showModal({
  189.           title: '倔倔',
  190.           content: '没办法,被天克',
  191.           showCancel: false,
  192.           confirmText: '再来亿把',
  193.           success: function (res) {
  194.             if (res.confirm) {
  195.               // console.log('用户点击确定')
  196.               that.handleRestart()
  197.               failAudio.stop()
  198.             } else if (res.cancel) {
  199.               console.log('用户点击取消')
  200.             }
  201.           }
  202.         })
  203.       } else if (game2048.getGameStatus() === 'win') {
  204.         winAudio.play()
  205.         wx.showModal({
上一篇:微信小程序事件倒计时,备忘录小程序源码免费下载
下一篇:微信小程序补品、保健品商城demo源码

相关内容

热门推荐