Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
JavaScript
var snake; var SIZE = 800; function setup() { createCanvas(SIZE,SIZE); frameRate(10); snake = new Snake(); food = new Food(); snake.generate(food); } function draw() { background(0); translate(width/2,height/2); snake.update(food); snake.show(); food.show(); } document.onkeydown = function(e) { switch (e.keyCode) { case 37: snake.setDirection('W'); break; case 38: snake.setDirection('N'); break; case 39: snake.setDirection('E'); break; case 40: snake.setDirection('S'); break; } }; function Snake(){ this.directions = ['N','E','S','W']; this.generate = function (food){ this.body = []; this.direction = this.directions[floor(random(4))]; this.x = floor(random(-width/2,width/2)/10); this.y = floor(random(-height/2,height/2)/10); this.body.push(new Array(this.x,this.y)); this.grow(); food.generate(); } this.grow = function(){ px = this.x; py = this.y; for(i= 1; i<4; i++){ if(this.direction == this.directions[0]){ py += 1; }else if (this.direction == this.directions[1]){ px -= 1; }else if(this.direction == this.directions[2]){ py -= 1; }else if(this.direction == this.directions[3]){ px += 1; } this.body.push(new Array(px,py)); } } this.update = function (food){ if(this.outOfBorder() || this.autoKill()){ // console.log("borde:" + this.outOfBorder()); // console.log("kill :" + this.autoKill()); this.generate(food); } if(this.eating(food)){ food.generate(); this.grow(); } fx = this.x; fy = this.y; if(this.direction == this.directions[0]){ this.y -= 1; }else if (this.direction == this.directions[1]){ this.x += 1; }else if(this.direction == this.directions[2]){ this.y += 1; }else if(this.direction == this.directions[3]){ this.x -= 1; } this.body[0][0] = this.x; this.body[0][1] = this.y; for(i=1; i
width/20 || this.y < -height/20 || this.y > height/20; } this.setDirection = function(direction){ this.direction = direction; } this.eating = function(food){ return food.x == this.x && food.y ==this.y; } } function Food(){ this.generate = function(snake){ this.x = floor(random(-width/2,width/2)/10); this.y = floor(random(-height/2,height/2)/10); } this.show = function(){ fill(255,0,0); push(); rect(this.x*10 , this.y *10, 10, 10); pop(); } }
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>贪吃蛇游戏-jq22.com</title> <script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script> <style>
</style> </head> <body>
<script>
</script>
</body> </html>
2012-2021 jQuery插件库版权所有
jquery插件
|
jq22工具库
|
网页技术
|
广告合作
|
在线反馈
|
版权声明
沪ICP备13043785号-1
浙公网安备 33041102000314号