欢迎光临,了解微信小程序开发,就上易用通!

当前位置 : 易用通 > 小程序模板
贪吃蛇;canvas运用贪吃蛇;canvas运用
立即下载

贪吃蛇;canvas运用

模板分类 : 小程序模板 模板编号 : Y942 源码文件 : 完全开源 下载权限 : VIP会员
模板大小 :  模板指数 :  更新时间 : 2018-03-15 09:52 模板等级 : ☆☆☆☆☆

模板截图:

点评:主要使用了canvas

贪吃蛇;canvas运用(图1) 代码示例:
[AppleScript] 纯文本查看 复制代码
?
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//点击坐标
var startX = 0;
var startY = 0;
//移动的坐标
var moveX = 0;
var moveY = 0;
//移动的距离
var X = 0;
var Y = 0;
//蛇头的坐标
var x = 0;
var y = 0;
 
//蛇对象
var python = {
  color:'#ff0000',
  x:0,
  y:0,
  w:20,
  h:20
}
//蛇身体
var pythonBody = [];
//方向
var fx = null;
var pythonfx = 'right';
 
Page({
    canvasStart:function(e){
      // console.log(e);
      startX = e.touches[0].x;
      startY = e.touches[0].y;
  },
  canvasMove:function(e){
    moveX = e.touches[0].x;
    moveY = e.touches[0].y;
    
    X = moveX - startX;
    Y = moveY - startY;
 
    if(Math.abs(X) > Math.abs(Y) && X > 0){
      fx = 'right'
    }else if(Math.abs(X) > Math.abs(Y) && X < 0){
      fx = 'left'
    }else if(Math.abs(X) < Math.abs(Y) && Y > 0){
      fx = 'buttom'
    }else{
      fx = 'top'
    }
  },
  canvasEnd:function(){
   
    pythonfx = fx;
  },
  onReady: function(){
        var cvs = wx.createContext();
        //帧数,模拟机60桢每秒
        var zs = 0;
        function draw(obj){
           cvs.setFillStyle(obj.color);
            cvs.beginPath();
            cvs.rect(obj.x, obj.y, obj.w, obj.h);
            cvs.closePath();
            cvs.fill();
        }
        function animate(){
            zs++;
            if(zs%20 == 0){
              //给蛇身体添加一节
              pythonBody.push({
                x:python.x,
                y:python.y,
                w:20,
                h:20,
                color:"#00ff00"
              })
              //获取方向
              switch(pythonfx){
                case "right":
                  python.x += python.w;
                break;
                case "left":
                  python.x -= python.w;
                break;
                case "top":
                  python.y -= python.h;
                break;
                case "buttom":
                  python.y += python.h;
                break;
              }
              if(pythonBody.length > 4){
                pythonBody.shift()
              }
          }
 
          //绘制蛇头
          draw(python);
 
          //绘制身体
          for(var i=0; i<pythonBody.length; i++){
            var pythonBodys = pythonBody[i];
            draw(pythonBodys)
          }
          wx.drawCanvas({
            canvasId: 'String',
            actions: cvs.getActions()
          })
          requestAnimationFrame(animate);
        }
        animate();
    }
})
 

加入收藏
立即下载
分享到微信朋友圈
X

免责声明:

1. 本站所有素材(未指定商用),仅限学习交流,请勿用于商业用途。
2. 本站所有小程序模板Demo和图片均来自用户分享上传和网络收集,模板和图片版权归原作者及原出处所有。
3. 未经合法授权,会员不得以任何形式发布、传播、复制、转售该素材。