本文主要介绍了微信小程序实现的贪吃蛇游戏,并结合实例分析了微信小程序实现贪吃蛇游戏的相关界面布局和代码逻辑操作技巧。附上源代码供读者下载参考,有需要的朋友可以参考一下。
本文描述了微信小程序实现的吃蛇游戏。分享给你,供你参考,如下:
我们先来看看运行效果:
具体代码如下:
页面布局/snake/snake/snake.wxml:
& lt!--snake . wxml --& gt;& ltview class = & quot控制& quotbindtouchstart = & quot点击开始& quotbindtouchmove = & quot点击移动& quotbindtouchend = & quottapEnd & quot& gt& ltview class = & quot得分& quot& gt& ltview class = & quot标题& quot& gtsnake & lt/view & gt;& ltview class = & quot得分详情& quot& gt& ltview class = & quotscoredesc & quot& gt得分
//snake . jsvar app = getApp();Page({ data:{ score: 0,// score maxscore: 0,//最高分为startx: 0,starty: 0,endx: 0,endy: 0,//以上四个用于方向判断使用ground:[],//存储操场各方格行数:28,列数:22,//操场大小蛇:[],//存储蛇食:[],//存储食物方向:& # 039;',//direction modalHidden: true,timer:& # 039;'},onLoad:function(){ var max score = wx . getstoragesync(& # 039;maxscore & # 039);如果(!max score)max score = 0 this . setdata({ max score:max score });this.initGround(this.data.rows,this . data . cols);//初始化playground this . init snake(3);//初始化snake this . creat food();//初始化food this . move();//snake moves},//scorer store score:function(){ if(this . data . max score 5){ var direction = (Math.abs(heng)>Math.abs(shu))?this.computeDir(1,恒):this.computeDir(0,舒);开关(方向){ case & # 039左& # 039;:if(this . data . direction = = & # 039;对& # 039;)返回;打破;案例& # 039;对& # 039;:if(this . data . direction = = & # 039;左& # 039;)返回;打破;案例& # 039;top & # 039:if(this . data . direction = = & # 039;底部& # 039;)返回;打破;案例& # 039;底部& # 039;:if(this . data . direction = = & # 039;top & # 039)返回;打破;default:} this . setdata({ startx:0,starty:0,endx:0,endy:0,direction:direction }) } },computeDir: function(heng,num){ if(heng)return(num & gt;0) ?'对& # 039;: '左& # 039;;return(num & gt;0) ?'底部& # 039;: 'top & # 039;}、creat food:function(){ var x = math . floor(math . random()* this . data . rows);var y = math . floor(math . random()* this . data . cols);var ground = this . data . ground;ground[x][y]= 2;this.setData({ ground:ground,food:[x,y]});},change direction:function(dir){ switch(dir){ case & # 039;左& # 039;:返回this . change left();打破;案例& # 039;对& # 039;:返回this . change right();打破;案例& # 039;top & # 039:返回this . change top();打破;案例& # 039;底部& # 039;:返回this . change bottom();打破;default: } },change left:function(){ var arr = this . data . snake;var len = this . data . snake . length;var snakeHEAD = arr[len -1][1];var snake tail = arr[0];var ground = this . data . ground;ground[snakeTAIL[0]][snakeTAIL[1]]= 0;for(var I = 0;我& ltlen -1;i++){ arr[I]= arr[I+1];};var x = arr[len -1][0];var y = arr[len -1][1]-1;arr[len-1]=[x,y];this.checkGame(蛇尾);for(var I = 1;我& ltleni++){ ground[arr[I][0]][arr[I][1]]= 1;} this.setData({ ground:ground,snake:arr });返回true},change right:function(){ var arr = this . data . snake;var len = this . data . snake . length;var snakeHEAD = arr[len -1][1];var snake tail = arr[0];var ground = this . data . ground;ground[snakeTAIL[0]][snakeTAIL[1]]= 0;for(var I = 0;我& ltlen -1;i++){ arr[I]= arr[I+1];};var x = arr[len -1][0];var y = arr[len -1][1]+1;arr[len-1]=[x,y];this.checkGame(蛇尾);for(var I = 1;我& ltleni++){ ground[arr[I][0]][arr[I][1]]= 1;} this.setData({ ground:ground,snake:arr });//var y = this . data . snake[0][1];//var x = this . data . snake[0][0];//this . data . ground[x][y]= 0;//console . log(this . data . ground[x]);//console . log(this . data . snake);//for(var I = 0;我& ltthis . data . snake . length -1;i++){//this . data . snake[I]= this . data . snake[I+1];//}//this . data . snake[this . data . snake . length -1][1]++;//for(var j = 1;j & ltthis . data . snake . length;j++){//this . data . ground[this . data . snake[j][0]][this . data . snake[j][1]]= 1;// }返回true},change top:function(){ var arr = this . data . snake;var len = this . data . snake . length;var snakeHEAD = arr[len -1][1];var snake tail = arr[0];var ground = this . data . ground;ground[snakeTAIL[0]][snakeTAIL[1]]= 0;for(var I = 0;我& ltlen -1;i++){ arr[I]= arr[I+1];};var x = arr[len -1][0]-1;var y = arr[len -1][1];arr[len-1]=[x,y];this.checkGame(蛇尾);for(var I = 1;我& ltleni++){ ground[arr[I][0]][arr[I][1]]= 1;} this.setData({ ground:ground,snake:arr });返回true},change bottom:function(){ var arr = this . data . snake;var len = this . data . snake . length;var snakeHEAD = arr[len -1];var snake tail = arr[0];var ground = this . data . ground;ground[snakeTAIL[0]][snakeTAIL[1]]= 0;for(var I = 0;我& ltlen -1;i++){ arr[I]= arr[I+1];};var x = arr[len -1][0]+1;var y = arr[len -1][1];arr[len-1]=[x,y];this.checkGame(蛇尾);for(var I = 1;我& ltleni++){ ground[arr[I][0]][arr[I][1]]= 1;} this.setData({ ground:ground,snake:arr });返回true}、check game:function(snakeTAIL){ var arr = this . data . snake;var len = this . data . snake . length;var snakeHEAD = arr[len -1];if(蛇头[0]& lt;0 | |黑鱼[0]& gt;= this . data . rows | | snakeHEAD[1]& gt;= this . data . cols | | snakeHEAD[1]& lt;0){ clear interval(this . data . timer);this . setdata({ modal hidden:false,})} for(var I = 0;我& ltlen -1;i++){ if(arr[I][0]= = snakeHEAD[0]& amp;& amparr[I][1]= = snakeHEAD[1]){ clear interval(this . data . timer);this . setdata({ modal hidden:false,})} } if(snakeHEAD[0]= = this . data . food[0]& amp;& amp蛇头[1]= = this . data . food[1]){ arr . un shift(snakeTAIL);this . setdata({ score:this . data . score+10 });this . store score();this . creat food();} },modal change:function(){ this . setdata({ score:0,ground:[],snake:[],food:[],modalHidden: true,direction:& # 039;'})this . onload();}});
评论前必须登录!
注册