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

游戏主逻辑:
- // pages/game/programmer.js
- var game2048 = require('../../utils/game2048.js');
- var gameServer = require('../../utils/gameServer.js');
- var util = require('../../utils/util.js');
- var app = getApp()
- // 胜利音效
- const winAudio = wx.createInnerAudioContext()
- winAudio.src = 'http://demo.infinitysia.com/dys/2048/assets/win.mp3'
- winAudio.obeyMuteSwitch = false
- // move音效
- const moveAudio = wx.createInnerAudioContext()
- moveAudio.src = 'http://demo.infinitysia.com/dys/2048/assets/move.mp3'
- moveAudio.obeyMuteSwitch = false
- // 失败音效
- const failAudio = wx.createInnerAudioContext()
- failAudio.src = 'http://demo.infinitysia.com/dys/2048/assets/lose.mp3'
- failAudio.obeyMuteSwitch = false
- Page({
- data:{
- // 游戏数组值
- gridValue:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
- // 游戏重新开始提示
- restartPrompt:"再来亿把",
- // 游戏难度:1.时间160ms;2.时间80ms;3.时间40ms;4.时间20ms;5.时间10ms;
- gameLevel:[1,2,3,4,5],
- // 游戏难度初始值 index of gameLevel
- level: 3,
- // 游戏难度提示
- levelPrompt:"难度",
- // 游戏模式: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;
- gameMode: [1,2,3,4,5],
- // 游戏模式初始值 index of gameMode
- mode: 1,
- // 游戏模式提示
- modePrompt:"模式",
- // 游戏运行时间
- gameTime:"00:00:00",
- // 游戏运行 开始时间
- gameStartDate:0,
- // 游戏运行 结束时间
- gameEndDate:0,
- // 游戏分数
- gameScore: 0,
- // 游戏触摸控制
- gameTouchInfo: {
- pointOrigin: {
- x: 0,
- y: 0
- },
- pointTarget: {
- x: 0,
- y: 0
- },
- isValid: false
- },
- // 游戏触摸控制阈值
- gameDistanceThreshold:10,
- // 排行榜
- chartsUsers: [
- {
- avatar:"http://wx.qlogo.cn/mmopen/vi_32/l8W3SEfertzlK6csQd23scfZG30hXDVP0mT2ODFqoPlkmmeic1ZXoiczVicppy68kPiajjEIbA5Daf7d6erlRdib5uQ/0",
- name:"chenxi****",
- score:"2048",
- level:"3级",
- mode:"3",
- time:"05:34"
- }, {
- name:"**********",
- score:"4096",
- level:"5级",
- mode:"5",
- time:"15:03"
- }
- ],
- // 胜利声音
- winAudio: null
- },
- onLoad:function(options){
- // 页面初始化 options为页面跳转所带来的参数
- const winAudio = wx.createInnerAudioContext()
- // winAudio.src = '../../assets/win.mp3'
- winAudio.src = 'http://demo.infinitysia.com/dys/2048/assets/win.mp3'
- // winAudio.autoPlay = true
- winAudio.obeyMuteSwitch = false
- // innerAudioContext.play()
- console.log(options)
- this.setData({
- winAudio
- })
- winAudio.onPlay(() => {
- console.log('playyyyy')
- })
- },
- onReady:function(){
- // 页面渲染完成
- game2048.resetGame();
- this.setData({
- gridValue:game2048.getGameArray().slice()
- });
- // 用户数据获取
- // this.inspectUserServer();
- // time
- console.log("gameStartDate:", this.data.gameStartDate);
- var that = this;
- setInterval(function() {
- var _gameEndDate = Date.now();
- var playTime = Math.floor((_gameEndDate - that.data.gameStartDate) / 1000);
- that.setData({
- gameEndDate: _gameEndDate,
- gameTime: util.formatSecondsTime(playTime)
- });
- },1000);
- },
- onShow:function(){
- // 页面显示
- game2048.printAuthor();
- // test
- this.setData({
- gameStartDate:new Date().getTime()
- });
- // 排行榜
- // this.getRank();
- },
- onHide:function(){
- // 页面隐藏
- },
- onUnload:function(){
- // 页面关闭
- },
- handleRestart:function(event){
- // 游戏重新开始
- game2048.resetGame();
- this.setData({
- gridValue:game2048.getGameArray().slice(),
- gameStartDate:new Date().getTime(),
- gameTime:"00:00:00"
- });
- },
- handleTouchMove:function(event){
- // 游戏2048网格界面触摸移动
- // console.log(event);
- // game2048.playGame("moveButtom");
- // this.setData({
- // gridValue:game2048.getGameArray().slice()
- // });
- },
- handleTouchStart:function(event){
- // 游戏2048网格界面触摸开始
- // console.log(event);
- if (!this.data.gameTouchInfo.isValid) {
- this.setData({
- 'gameTouchInfo.pointOrigin.x': event['changedTouches'][0].pageX,
- 'gameTouchInfo.pointOrigin.y': event['changedTouches'][0].pageY,
- 'gameTouchInfo.isValid': true
- });
- }
- },
- handleTouchEnd:function(event){
- // 游戏2048网格界面触摸结束
- // console.log(event);
- if (this.data.gameTouchInfo.isValid) {
- this.setData({
- 'gameTouchInfo.pointTarget.x': event['changedTouches'][0].pageX,
- 'gameTouchInfo.pointTarget.y': event['changedTouches'][0].pageY,
- 'gameTouchInfo.isValid': false
- });
- var direction = this.getTouchDirection(this.data.gameTouchInfo.pointOrigin,
- this.data.gameTouchInfo.pointTarget, this.data.gameDistanceThreshold)
- console.log(direction);
- game2048.playGame(direction);
- // 停止所有音频
- // winAudio.stop()
- // failAudio.stop()
- // moveAudio.stop()
- const that = this
- if (game2048.getGameStatus() === 'end') {
- failAudio.play()
- wx.showModal({
- title: '倔倔',
- content: '没办法,被天克',
- showCancel: false,
- confirmText: '再来亿把',
- success: function (res) {
- if (res.confirm) {
- // console.log('用户点击确定')
- that.handleRestart()
- failAudio.stop()
- } else if (res.cancel) {
- console.log('用户点击取消')
- }
- }
- })
- } else if (game2048.getGameStatus() === 'win') {
- winAudio.play()
- wx.showModal({