微信小程序:贪吃蛇
wx.js
var startX = 0 ;
var startY = 0 ;
var moveX = 0 ;
var moveY = 0 ;
var X = 0 ;
var Y = 0 ;
var snake = { x: 0 , y: 0 , color: "#ff0000" , w: 20 , h: 20 ,
}
var food = { x: 0 , y: 0 , color: "#ff0000" , w: 20 , h: 20 ,
}
var snakeArr = [ ] ;
var direction = "right" ;
var directio = null ;
var windowWidth = wx. getSystemInfoSync ( ) . windowWidth;
var windowHeight = wx. getSystemInfoSync ( ) . windowHeight;
var foodArr = [ ] ;
var foodNum = 0 ; Page ( {
canvasStart: function ( e) { startX = e. touches[ 0 ] . x; startY = e. touches[ 0 ] . y;
} , canvasMove: function ( e) { moveX = e. touches[ 0 ] . x; moveY = e. touches[ 0 ] . y; X = moveX - startX; Y = moveY - startY; if ( Math. abs ( X ) > Math. abs ( Y ) && X > 0 ) { directio= "right" } else if ( Math. abs ( X ) > Math. abs ( Y ) && X < 0 ) { directio = "left" } else if ( Math. abs ( Y ) > Math. abs ( X ) && Y > 0 ) { directio = "bottom" } else if ( Math. abs ( Y ) > Math. abs ( X ) && Y < 0 ) { directio = "top" } direction = directio;
} , onReady: function ( ) { var context = wx. createCanvasContext ( "snakCanvas" , this ) ; function draw ( obj) { context. setFillStyle ( obj. color) ; context. beginPath ( ) ; context. rect ( obj. x, obj. y, obj. w, obj. h) ; context. closePath ( ) ; context. fill ( ) ; } setInterval ( function animate ( ) { switch ( direction) { case "left" : snake. x -= 20 ; break ; case "right" : snake. x += 20 ; break ; case "top" : snake. y -= 20 ; break ; case "bottom" : snake. y += 20 ; break ; } draw ( snake) for ( var i = 0 ; i < foodArr. length; i++ ) { var food = foodArr[ i] ; draw ( food) ; if ( collide ( snake, food) ) { food. x = rand ( 0 , windowWidth) ; food. y = rand ( 0 , windowHeight) ; food. color = "rgb(" + rand ( 0 , 255 ) + "," + rand ( 0 , 255 ) + "," + rand ( 0 , 255 ) + ")" ; var w = rand ( 10 , 20 ) ; food. w = w; food. h = w; draw ( food) ; } } snakeArr. push ( { x: snake. x, y: snake. y, w: 20 , h: 20 , color: "#fff000" , } ) ; if ( snakeArr. length> foodNum) { snakeArr. shift ( ) } for ( let i= 0 ; i< snakeArr. length- 1 ; i++ ) { let body= snakeArr[ i] draw ( body) } context. draw ( ) ; if ( snake. x> windowWidth) { snake. x= - 20 } else if ( snake. x< 0 ) { snake. x= windowWidth } else if ( snake. y> windowHeight) { snake. y= - 20 } else if ( snake. y< 0 ) { snake. y= windowHeight } } , 300 ) function rand ( max, min) { return parseInt ( Math. random ( ) * ( max - min) + min) } function collide ( obj1, obj2) { var l1 = obj1. x; var r1 = l1 + obj1. w; var t1 = obj1. y; var b1 = t1 + obj1. h; var l2 = obj2. x; var r2 = l2 + obj2. w; var t2 = obj2. y; var b2 = t2 + obj2. h; if ( r1 > l2 && l1 < r2 && b1 > t2 && t1 < b2) { foodNum++ return true ; } } function foodstart ( ) { for ( let i= 0 ; i< 20 ; i++ ) { food. x = rand ( 0 , windowWidth) ; food. y = rand ( 0 , windowHeight) ; var w = rand ( 10 , 20 ) ; food. w = w; food. h = w; food. color = "rgb(" + rand ( 0 , 255 ) + "," + rand ( 0 , 255 ) + "," + rand ( 0 , 255 ) + ")" ; foodArr. push ( JSON . parse ( JSON . stringify ( food) ) ) } } foodstart ( ) ; }
} )
WX.ML
< canvas canvas- id= "snakCanvas" style= "width:100%;height:100%;background-color:#ccc"
bindtouchstart= "canvasStart" bindtouchmove= "canvasMove" > < / canvas>
WX.CSS
Page { height : 100%
}