找回密码
 立即注册

扫一扫,访问微社区

查看: 5261|回复: 7

微信小程序demo:Math Game;数字游戏

  [复制链接]
发表于 2017-3-7 14:36:36 | 显示全部楼层 |阅读模式
本帖最后由 柿霖不是林 于 2017-3-7 14:38 编辑

undefined好像我小时候玩的七巧板;




[mw_shl_code=html,true]//index.js
//获取应用实例
var app = getApp()
var mapNumber = ['1','2','3','4','5','6','7','8','']
var emptyIndex = 8
var moveFirstPoint
var moveLastPoint
Page({
  data: {
    time:'00:00\n',
    num: mapNumber
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    console.log('onLoad')
    var that = this
    that.initMap()

  },
  initMap: function() {
    console.log('initMap')
    var tempNumbers = ['1','2','3','4','5','6','7','8','']
    for(var i = 0; i < 10; i++){
      var indexA = Math.round(Math.random()*7)
      var indexB = Math.round(Math.random()*7)
      console.log('swap:' + indexA + '<->' + indexB)
      var tempNum = tempNumbers[indexA]
      tempNumbers[indexA] = tempNumbers[indexB]
      tempNumbers[indexB] = tempNum
    }
    mapNumber = tempNumbers
    emptyIndex = 8
    this.setData({
      num:mapNumber
    })
     
  },

  checkresult: function() {
    var win = true;
    for(var i = 0; i < 8; i++){
      if((i+1).toString() != mapNumber){
        win = false;
        break;
      }
    }
    if(win){
      wx.showToast({
        title: '成功',
        icon: 'success',
      })
      var that = this
      setTimeout(function(){
        that.initMap();
      },2000)
      
    }
     
  },

  swapMap: function(indexA,indexB){
    var tempNum = mapNumber[indexA]
    mapNumber[indexA] = mapNumber[indexB]
    mapNumber[indexB] = tempNum
    this.setData({
      num:mapNumber
    })
  },

  movedirection: function(direction){//1:left, 2:right, 3:up, 4:down
    console.log('move direction' + direction)
    var newIndex = -1
    if(direction == 1 && (emptyIndex%3) != 2){
      newIndex = emptyIndex + 1
    }else if(direction == 2 && (emptyIndex%3 != 0)){
      newIndex = emptyIndex - 1
    }else if(direction == 3 && (emptyIndex/3 < 2)){
      newIndex = emptyIndex + 3
    }else if(direction == 4 && (emptyIndex/3 != 0)){
      newIndex = emptyIndex - 3
    }

    if(newIndex >= 0){
       this.swapMap(newIndex,emptyIndex)
       emptyIndex = newIndex
       this.checkresult()
    }
     
  },

  handletouchmove: function(e){
    if(!moveFirstPoint){
      moveFirstPoint = e.touches[0]
    }else{
      moveLastPoint = e.touches[0]
    }
  },
  handletouchend: function(e){
    if(moveFirstPoint && moveLastPoint){
      var moveH = moveFirstPoint.pageX - moveLastPoint.pageX
      var moveV = moveFirstPoint.pageY - moveLastPoint.pageY
      moveFirstPoint = null
      moveLastPoint = null
      if(Math.abs(moveH) < 30 && Math.abs(moveV) < 30){
        console.log('move ignore');
      }
      if(Math.abs(moveH) > Math.abs(moveV)){
        //h swipe
        if(moveH < 0){
          //swipe right
          this.movedirection(2)
        }else{
          this.movedirection(1)
        }
      }else{
        //V swipe
        if(moveV < 0){
          //swipe down
          this.movedirection(4)
        }else{
          this.movedirection(3)
        }
      }
    }
  },
  handletouchcancel: function(e){
    moveFirstPoint = null
    moveLastPoint = null
  },

  canvashandletouchmove: function(e) {
  }

})[/mw_shl_code]



游客,如果您要查看本帖隐藏内容请回复

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×

0

主题

17

回帖

37

金钱

新人求带

积分
0
发表于 2017-11-6 13:09:17 | 显示全部楼层
Very Good!
回复

使用道具 举报

0

主题

2

回帖

12

金钱

新人求带

积分
0
发表于 2018-3-1 16:34:21 | 显示全部楼层
正需要,支持楼主大人了!

0

主题

6

回帖

18

金钱

新人求带

积分
0
发表于 2018-8-8 22:41:37 | 显示全部楼层
看看,好像满给力···

0

主题

12

回帖

218

金钱

新人求带

积分
0
发表于 2018-8-20 14:55:55 | 显示全部楼层
:Math Game;数字游戏 [修改]

0

主题

17

回帖

70

金钱

新人求带

积分
0
发表于 2018-12-3 13:09:06 | 显示全部楼层
玩玩试试哈!

0

主题

28

回帖

68

金钱

新人求带

积分
0
发表于 2020-2-24 12:47:09 | 显示全部楼层
6666666666666666666

0

主题

353

回帖

830

金钱

新人求带

积分
0
发表于 2020-3-27 15:38:35 | 显示全部楼层
专注于与个人游戏开发者合作,有产品的朋友联系微信号jim20180688,详聊
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|微信小程序开发|教程|文档|资源汇总_即速论坛 ( 粤ICP备14097199号-1  

GMT+8, 2024-4-25 06:53 , Processed in 0.121729 second(s), 31 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表