WeChat mini-app-implemented greedy Snake game [Source Code download], greedy snake source code download

Source: Internet
Author: User

Mini-app-implemented greedy Snake game [Source Code download], greedy snake source code download

This article describes the snake games implemented by applets. We will share this with you for your reference. The details are as follows:

Let's take a look at the running effect:

The Code is as follows:

Page layout pages/snake. wxml:

<! -- Snake. wxml --> <view class = "control" bindtouchstart = "tapStart" bindtouchmove = "tapMove" bindtouchend = "tapEnd"> <view class = "score"> <view class = "title "> snake </view> <view class =" scoredetail "> <view class =" scoredesc "> score </view> <view class =" scorenumber ">{{ score}} </view> <view class = "scoredetail"> <view class = "scoredesc"> highest history </view> <view class = "scorenumber"> {{maxscore }}</view> <vi Ew class = "ground"> <view wx: for = "{ground}" class = "rows" wx: for-item = "cols"> <view wx: for = "{cols}" class = "block _ {item}"> </view> <modal class = "modal "hidden =" {modalHidden }}" no-cancel bindconfirm = "modalChange"> <view> the game is over, start again? </View> </modal> </view>

Logical Functions pages/snake. js:

// Snake. jsvar app = getApp (); Page ({data: {score: 0, // score maxscore: 0, // highest score startx: 0, starty: 0, endx: 0, endy: 0, // use ground: [] to determine the direction of the above four, // store the playground each square rows: 28, cols: 22, // playground size snake: [], // save snake food: [], // save food ction: '', // 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 the playground this. initSnake (3); // initialize the snake this. creatFood (); // initialize the food this. move (); // snake movement}, // shard storeScore: function () {if (this. data. maxscore <this. data. score) {this. setData ({maxscore: this. data. score}) wx. setStorageSync ('maxscore ', this. data. maxscore) }}, // playground initGround: function (rows, cols) {for (var I = 0; I <rows; I ++) {var arr = []; this. data. ground. push (arr); for (var j = 0; j <cols; j ++) {this. data. ground [I]. push (0) ;}}, // snake initSnake: function (len) {for (var I = 0; I <len; I ++) {this. data. ground [0] [I] = 1; this. data. snake. push ([0, I]) ;}}, // move function: function () {var that = this; this. data. timer = setInterval (function () {that. changeDirection (that. data. direction); that. setData ({ground: that. data. ground}) ;}, 400) ;}, TapStart: function (event) {this. setData ({startx: event. touches [0]. pageX, starty: event. touches [0]. pageY})}, tapMove: function (event) {this. setData ({endx: event. touches [0]. pageX, endy: event. touches [0]. pageY})}, tapEnd: function (event) {var heng = (this. data. endx )? (This. data. endx-this. data. startx): 0; var shu = (this. data. endy )? (This. data. endy-this. data. starty): 0; if (Math. abs (heng)> 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}) }}, c OmputeDir: 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 snkehead = arr [len-1] [1]; var snketail = arr [0]; var ground = this. data. ground; ground [snketail [0] [snketail [1] = 0; for (var I = 0; I <len-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 (snketail); for (var I = 1; I <len; I ++) {ground [arr [I] [0] [arr [I] [1] = 1;} this. setData ({ground: ground, snake: arr}); return true ;}, changeRight: function () {var arr = this. data. snake; var len = this. data. snake. length; var snkehead = arr [len-1] [1]; var snketail = arr [0]; var ground = this. data. ground; ground [snketail [0] [snketail [1] = 0; for (var I = 0; I <len-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 (snketail); for (var I = 1; I <len; I ++) {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; I <this. 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 <this. data. snake. length; j ++) {// this. data. ground [this. data. snake [j] [0] [this. data. snake [j] [1] = 1; //} return true;}, changeTop: function () {var arr = this. data. snake; var len = this. data. snake. length; var snkehead = arr [len-1] [1]; var snketail = arr [0]; var ground = this. data. ground; ground [snketail [0] [snketail [1] = 0; for (var I = 0; I <len-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 (snketail); for (var I = 1; I <len; I ++) {ground [arr [I] [0] [arr [I] [1] = 1;} this. setData ({ground: ground, snake: arr}); return true ;}, changeBottom: function () {var arr = this. data. snake; var len = this. data. snake. length; var snkehead = arr [len-1]; var snketail = arr [0]; var ground = this. data. ground; ground [snketail [0] [snketail [1] = 0; for (var I = 0; I <len-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 (snketail); for (var I = 1; I <len; I ++) {ground [arr [I] [0] [arr [I] [1] = 1;} this. setData ({ground: ground, snake: arr}); return true ;}, checkGame: function (snketail) {var arr = this. data. snake; var len = this. data. snake. length; var snkehead = arr [len-1]; if (snkehead [0] <0 | snkehead [0]> = this. data. rows | Snake Head [1]> = this. data. cols | Snake Head [1] <0) {clearInterval (this. data. timer); this. setData ({modalHidden: false,})} for (var I = 0; I <len-1; I ++) {if (arr [I] [0] = snkehead [0] & arr [I] [1] = snkehead [1]) {clearInterval (this. data. timer); this. setData ({modalHidden: false,}) }}if (snkehead [0] = this. data. food [0] & amp; snakhead [1] = this. data. food [1]) {arr. unshift (snketail); this. setData ({score: this. data. score + 10}); this. storeScore (); this. creatFood () ;}, modalChange: function () {this. setData ({score: 0, ground: [], snake: [], food: [], modalHidden: true, direction: ''}) this. onLoad ();}});

Appendix:Click here for the complete instance source codeDownload from this site.

I hope this article will help you develop small programs.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.