1、我们需要建立四个头文件,然后分别设置蛇的状态,上下左右,这是蛇能够有方向可走的前提,然后我们再设置蛇身的节点,定义一个简单的函数,这样蛇的全身以及他的行走方向就弄完了。
3、第二步,一个函数这个函数的目的是贪吃蛇不能穿墙,很简单的代码分别设置长宽的最大位移,在内部范围内设置为一即可通过,否则不能穿墙。贪吃蛇随机生成一个食物。
4、设置一个随机函数。这样贪吃蛇代码就做好了。
慧编程是一款面向STEAM教育领域的积木式和代码编程软件,基于图形化编程开发。
微信贪吃蛇代码怎么输入:首先说明一下,微信小程序是不能发布游戏的。
代码输入:手指按下,滑动,d起,确定蛇头转的方向,代码如下
//获取手指按下坐标
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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)