使用Html5+JS做的贪吃蛇小游戏
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用Html5+JS做的贪吃蛇小游戏,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5494字,纯文字阅读大概需要8分钟。
内容图文
学习了Html5的Canvas的使用,和JS创建对象,做了一个贪吃蛇小游戏,来巩固JS面向对象编程和Canvas的用法。
Node.js
1 /* * 2 * 格子类,组成贪吃蛇和舞台 3 */ 4 function Node(x,y){ 5 this.x=x; 6this.y=y; 7/** 8 * 比较两个格子是否重合 9*/10this.equals=function(x,y){ 11returnthis.x==x&&this.y==y; 12 } 13 }
Snake.js
1 /* 定义全局变量UP,表示方向向上 */ 2 var UP=0; 3/*定义全局变量LEFT,表示方向向左*/ 4var LEFT=1; 5/*定义全局变量RIGHT,表示方向向右*/ 6var RIGHT=2; 7/*定义全局变量DOWN,表示方向向下*/ 8var DOWN=3; 9/*初始化方向向右*/ 10var DIR=RIGHT; 11/*初始化分数为0*/ 12var SCORE=0; 13/*游戏是否结束*/ 14var GAMEOVER=false; 15/*等级*/ 16var LEVEL=1; 17/*速度*/ 18var SPEED=30; 19/*最高等级*/ 20var MAXLEVEL=6; 21/*绘画的时间间隔*/ 22var TIME=200; 23/** 24 * 贪吃蛇类 25*/ 26function Snake(){ 27/** 28 * 初始化贪吃蛇 29*/ 30this.nodes=[]; 31this.nodes.push(new Node(20,20)); 32this.nodes.push(new Node(20,21)); 33this.nodes.push(new Node(20,22)); 34this.nodes.push(new Node(20,23)); 35this.nodes.push(new Node(21,23)); 36this.nodes.push(new Node(22,23)); 37this.nodes.push(new Node(23,23)); 38this.nodes.push(new Node(23,24)); 39this.nodes.push(new Node(23,25)); 40this.nodes.push(new Node(23,26)); 41/** 42 * 判断蛇身是否包含当前坐标 43*/ 44this.contains=function(x,y){ 45for(i=0;i<this.nodes.length;i++){ 46var node=this.nodes[i]; 47if(node.equals(x,y)){ 48returntrue; 49 } 50 } 51returnfalse; 52 }; 53/** 54 * 获得一个与蛇身不重合的格子节点 55*/ 56this.randomFood=function(){ 57var x; 58var y; 59do{ 60 x=Math.floor(Math.random()*50); 61 y=Math.floor(Math.random()*50); 62 }while(this.contains(x,y)); 63returnnew Node(x,y); 64 }; 65/** 66 * 初始化食物 67*/ 68this.food=this.randomFood(); 69 70/** 71 * 向前走一步 72 * 添加头节点 73 * 删除尾节点 74*/ 75this.step=function(){ 76var oldHead=this.nodes[0]; 77//根据方向计算新头节点 78var newHead; 79switch (DIR){ 80case UP: 81 newHead=new Node(oldHead.x,oldHead.y-1); 82if(newHead.y==-1){ 83 newHead.y=49; 84 } 85break; 86case DOWN: 87 newHead=new Node(oldHead.x,oldHead.y+1); 88if(newHead.y==50){ 89 newHead.y=0; 90 } 91break; 92case LEFT: 93 newHead=new Node(oldHead.x-1,oldHead.y); 94if(newHead.x==-1){ 95 newHead.x=49; 96 } 97break; 98case RIGHT: 99 newHead=new Node(oldHead.x+1,oldHead.y); 100if(newHead.x==50){ 101 newHead.x=0; 102 } 103break; 104default: 105break; 106 } 107/** 108 * 如果吃到自己则结束游戏 109*/110if(this.contains(newHead.x,newHead.y)){ 111 GAMEOVER=true; 112 } 113/** 114 * 每走一步,添加新的头节点 115*/116this.nodes.unshift(newHead); 117/** 118 * 如果新添加的头节点与食物重合,即吃掉食物, 119 * 重新为食物赋值,并且分数加1 120*/121if(newHead.x==this.food.x&&newHead.y==this.food.y){ 122this.food=this.randomFood(); 123 SCORE+=1; 124/*每吃5个食物,则等级加1*/125if(SCORE%5==0){ 126/*最高等级为5*/127if(LEVEL<MAXLEVEL){ 128 LEVEL+=1; 129 } 130 } 131 }else{ 132/*如果没有吃掉食物,则删除尾节点*/133this.nodes.pop(); 134 } 135//为时间间隔重新赋值136 TIME=200-LEVEL*SPEED; 137 }; 138139 }
Stage.js
1 /* * 2 * 舞台类 3 */ 4 function Stage(){ 5 this.width=50; 6this.height=50; 7this.snake=new Snake(); 8this.print=function(ctx){ 9//绘制舞台的背景10 ctx.fillStyle="#abcdef"; 11 ctx.fillRect(0,0,500,500); 12//绘制一条蛇13 ctx.fillStyle="chartreuse"; 14for(i=0;i<this.snake.nodes.length;i++){ 15var node=this.snake.nodes[i]; 16 ctx.fillRect(node.x*10,node.y*10,9,9); 17 } 18//绘制食物19 ctx.fillStyle="#0000ff"; 20 ctx.fillRect(this.snake.food.x*10,this.snake.food.y*10,9,9); 21//绘制分数22 ctx.font="20px 微软雅黑"; 23 ctx.fillStyle="green"; 24 ctx.fillText("分数:"+SCORE,5,25); 25//绘制等级26 ctx.font="20px 微软雅黑"; 27 ctx.fillStyle="green"; 28 ctx.fillText("等级:"+LEVEL,100,25); 29 } 30 }
snake.html
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>贪吃蛇</title> 6 <style type="text/css"> 7 canvas{ 8 border: 1px solid black; 9 } 10 </style> 11 <script type="text/javascript" src="Node.js" ></script> 12 <script type="text/javascript" src="Snake.js" ></script> 13 <script type="text/javascript" src="Stage.js" ></script> 14 <script type="text/javascript"> 15var game; 16function load(){ 17var stage=new Stage(); 18//获取用于绘制canvas的Context对象19var canvas=document.getElementById("mCanvas"); 20var ctx=canvas.getContext("2d"); 21//绘制初始界面22 stage.print(ctx); 23 game=window.setInterval(function(){ 24 stage.snake.step(); 25 stage.print(ctx); 26if(GAMEOVER){ 27 ctx.fillStyle="red"; 28 ctx.font="50px 微软雅黑"; 29 ctx.fillText("GAMEOVER!",100,250); 30 window.clearInterval(game); 31 } 32 },TIME); 33 } 34/** 35 * 方向键控制贪吃蛇的方向 36 * @param {Object} dir 37*/38function changeDir(dir){ 39switch (dir){ 40case UP: 41if(DIR!=DOWN){ 42 DIR=dir; 43 } 44break; 45case DOWN: 46if(DIR!=UP){ 47 DIR=dir; 48 } 49break; 50case LEFT: 51if(DIR!=RIGHT){ 52 DIR=dir; 53 } 54break; 55case RIGHT: 56if(DIR!=LEFT){ 57 DIR=dir; 58 } 59break; 60default: 61break; 62 } 63 } 64 </script> 65 </head> 66 <body onload="load()"> 67 <canvas id="mCanvas" width="500" height="500"></canvas> 68 <!--控制方向--> 69 <table cellpadding="0px" cellspacing="0px"> 70 <tr> 71 <td></td> 72 <td><input type="button" value="上" onclick="changeDir(UP)"/></td> 73 <td></td> 74 </tr> 75 <tr> 76 <td><input type="button" value="左" onclick="changeDir(LEFT)"></td> 77 <td></td> 78 <td><input type="button" value="右" onclick="changeDir(RIGHT)"/></td> 79 </tr> 80 <tr> 81 <td></td> 82 <td><input type="button" value="下" onclick="changeDir(DOWN)"/></td> 83 <td></td> 84 </tr> 85 </table> 86 </body> 87 </html>
原文:http://www.cnblogs.com/fighting9527/p/5146374.html
内容总结
以上是互联网集市为您收集整理的使用Html5+JS做的贪吃蛇小游戏全部内容,希望文章能够帮你解决使用Html5+JS做的贪吃蛇小游戏所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。