How to implement Snake Game in WeChat Mini Program

小云云
Release: 2018-01-05 09:59:27
Original
4096 people have browsed it

This article mainly introduces the Snake game implemented by the WeChat applet, and analyzes the related interface layout and code logic operation skills of the WeChat applet to implement the Snake game function in the form of examples. It also comes with source code for readers to download for reference. Friends can refer to it, I hope it can help everyone.

The example in this article describes the Snake game implemented by WeChat applet. Share it with everyone for your reference, the details are as follows:

Let’s take a look at the running effect first:

The specific code is as follows:

Interface layout pages/snake/snake/snake.wxml:


   snake  得分 {{score}}   历史最高 {{maxscore}}          
Copy after login

Logical function pages/snake/snake/snake.js:


//snake.js var app = getApp(); Page({ data:{ score: 0,//比分 maxscore: 0,//最高分 startx: 0, starty: 0, endx:0, endy:0,//以上四个做方向判断来用 ground:[],//存储操场每个方块 rows:28, cols:22,//操场大小 snake:[],//存蛇 food:[],//存食物 direction:'',//方向 modalHidden: true, timer:'' } , onLoad:function(){ var maxscore = wx.getStorageSync('maxscore'); if(!maxscore) maxscore = 0 this.setData({ maxscore:maxscore }); this.initGround(this.data.rows,this.data.cols);//初始化操场 this.initSnake(3);//初始化蛇 this.creatFood();//初始化食物 this.move();//蛇移动 }, //计分器 storeScore:function(){ if(this.data.maxscore < this.data.score){ this.setData({ maxscore:this.data.score }) wx.setStorageSync('maxscore', this.data.maxscore) } }, //操场 initGround:function(rows,cols){ for(var i=0;i 5 || Math.abs(shu) > 5){ var direction = (Math.abs(heng) > Math.abs(shu)) ? this.computeDir(1, heng):this.computeDir(0, shu); switch(direction){ case 'left': if(this.data.direction=='right')return; break; case 'right': if(this.data.direction=='left')return; break; case 'top': if(this.data.direction=='bottom')return; break; case 'bottom': if(this.data.direction=='top')return; break; default: } this.setData({ startx:0, starty:0, endx:0, endy:0, direction:direction }) } }, computeDir: function(heng, num){ if(heng) return (num > 0) ? 'right' : 'left'; return (num > 0) ? 'bottom' : 'top'; }, creatFood: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] }); }, changeDirection:function(dir){ switch(dir){ case 'left': return this.changeLeft(); break; case 'right': return this.changeRight(); break; case 'top': return this.changeTop(); break; case 'bottom': return this.changeBottom(); break; default: } }, changeLeft:function(){ var arr=this.data.snake; var len=this.data.snake.length; var snakeHEAD=arr[len-1][1]; var snakeTAIL=arr[0]; var ground=this.data.ground; ground[snakeTAIL[0]][snakeTAIL[1]]=0; for(var i=0;i=this.data.rows||snakeHEAD[1]>=this.data.cols||snakeHEAD[1]<0){ clearInterval(this.data.timer); this.setData({ modalHidden: false, }) } for(var i=0;i
        
Copy after login

Related recommendations:

"Snake"--H5 mini game development

How to develop and implement with js Simple Snake Game

Use JavaScript to create a web version of the Snake Game

The above is the detailed content of How to implement Snake Game in WeChat Mini Program. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!