Get the App
SLTechnology News&Howtos  ›  Development  › 

How does WeChat Mini Programs realize the game of gluttonous snake

Shulou Source: shulou.com Published: 2022-05-31 21:14:18 09月22日 Update

This article mainly explains "how to realize the gluttonous Snake Game by WeChat Mini Programs". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how WeChat Mini Programs realizes the game of gluttonous snakes".

Let's take a look at the running effect:

The specific code is as follows:

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

Snake score {{score}} record high {{maxscore}} Game over, do you want to start over?

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

/ / snake.jsvar app = getApp () Page ({data: {score: 0SCR / score maxscore: 0SCA / highest score startx: 0, starty: 0, endx:0, endy:0,//) to use ground: [], / / store each square of the playground rows:28, cols:22,// playground size snake: [], / / store snake food: [], / / store food direction:'' / / 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); / / initialize playground this.initSnake (3); / / initialize snake this.creatFood (); / / initialize food this.move (); / / Snake movement}, / / Scaler 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 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 focus 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 iTuno [I)

Tags: Playground program highest content direction food learning practical deeper code interest function history size practicality practicality layout score simple operation effect Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Information Xiaomi MariaDB Huawei