找回密码
 立即注册

扫一扫,访问微社区

查看: 5161|回复: 1

微信小程序学习用demo:贪吃蛇;canvas运用

[复制链接]
发表于 2017-1-13 12:48:40 | 显示全部楼层 |阅读模式
点评:主要使用了canvas






//点击坐标
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;
            draw(pythonBodys)
          }
          wx.drawCanvas({
            canvasId: 'String',
            actions: cvs.getActions()
          })
          requestAnimationFrame(animate);
        }
        animate();
    }
})

<canvas canvas-id="String" style="width:100%;height:100%; background:#ccc" bindtouchstart="canvasStart" bindtouchmove="canvasMove" bindtouchend="canvasEnd" />


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


本帖子中包含更多资源

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

×

0

主题

222

回帖

509

金钱

新人求带

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

本版积分规则

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

GMT+8, 2024-4-16 21:22 , Processed in 0.112814 second(s), 25 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

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