首先说明一下,微信小程序是不能发布游戏的。
关键代码一:手指按下,滑动,弹起,确定蛇头转的方向,代码如下
//获取手指按下坐标
touchStart:function(e){
startX = e.touches[0].x;
startY = e.touches[0].y;
},
//获取手指移动坐标
touchMove:function(e){
moveX = e.touches[0].x;
moveY = e.touches[0].y;
distX = moveX – startX;
distY = moveY – startY;
if(Math.abs(distX) > Math.abs(distY) && distX > 0){
console.log(“right”);
direction = “right”;
}else if(Math.abs(distX) > Math.abs(distY) && distX < 0){
console.log(“left”);
direction = “left”;
}else if(Math.abs(distX) < Math.abs(distY) && distY > 0){
console.log(“bottom”);
direction = “bottom”;
}else if(Math.abs(distX) < Math.abs(distY) && distY < 0){
console.log(“top”);
direction = “top”;
}
},
touchEnd:function(){
snakeDirection = direction;
},
关键代码二:碰撞检测