课程设计:使用JavaScript制作一个网页上的贪吃蛇游戏
贪吃蛇游戏的设计(1)使用JavaScript制作一个网页上的贪吃蛇游戏。(2)在游戏中可以选择游戏界面的长度和宽度,蛇移动的速度,显示的比例。(3)有简单的游戏界面,并...
贪吃蛇游戏的设计
(1)使用JavaScript制作一个网页上的贪吃蛇游戏。
(2)在游戏中可以选择游戏界面的长度和宽度,蛇移动的速度,显示的比例。
(3)有简单的游戏界面,并且具有完整的游戏规则。
我们的课程设计,希望大家给一个正确的,如果不方便,发我邮箱:hixiefengbin@yahoo.cn。我会给加分的。求给力 展开
(1)使用JavaScript制作一个网页上的贪吃蛇游戏。
(2)在游戏中可以选择游戏界面的长度和宽度,蛇移动的速度,显示的比例。
(3)有简单的游戏界面,并且具有完整的游戏规则。
我们的课程设计,希望大家给一个正确的,如果不方便,发我邮箱:hixiefengbin@yahoo.cn。我会给加分的。求给力 展开
5个回答
2011-07-01
展开全部
<html>
<head>
<title>贪吃蛇 Snake v2.4</title>
<style>
body{
font-size:9pt;
}
table{
border-collapse: collapse;
border:solid #333 1px;
}
td{
height: 10px;
width: 10px;
font-size: 0px;
}
.filled{
background-color:blue;
}
</style>
</head>
<script>
function $(id){return document.getElementById(id);}
/**************************************************************
* javascript贪吃蛇 v2.4 <br />
* author: sunxing007 05/14/2009<br />
* 转载请注明来自http://blog.csdn.net/sunxing007 谢谢!<br />
* v2.4修正了蛇身颜色可以随着蛇前进而移动
**************************************************************/
//贪吃蛇类
var Snake = {
tbl: null,
/**
* body: 蛇身,数组放蛇的每一节,
* 数据结构{x:x0, y:y0, color:color0},
* x,y表示坐标,color表示颜色
**/
body: [],
//当前移动的方向,取值0,1,2,3, 分别表示向上,右,下,左, 按键盘方向键可以改变它
direction: 0,
//定时器
timer: null,
//速度
speed: 250,
//是否已经暂停
paused: true,
//行数
rowCount: 30,
//列数
colCount: 30,
//初始化
init: function(){
var colors = ['red','orange','yellow','green','blue','purple','#ccc'];
this.tbl = $("main");
var x = 0;
var y = 0;
var colorIndex = 0;
//产生初始移动方向
this.direction = Math.floor(Math.random()*4);
//构造table
for(var row=0;row<this.rowCount;row++){
var tr=this.tbl.insertRow(-1);
for(var col=0;col<this.colCount;col++) {
var td=tr.insertCell(-1);
}
}
//产生20个松散节点
for(var i=0; i<10; i++){
x = Math.floor(Math.random()*this.colCount);
y = Math.floor(Math.random()*this.rowCount);
colorIndex = Math.floor(Math.random()*7);
if(!this.isCellFilled(x,y)){
this.tbl.rows[y].cells[x].style.backgroundColor = colors[colorIndex];
}
}
//产生蛇头
while(true){
x = Math.floor(Math.random()*this.colCount);
y = Math.floor(Math.random()*this.rowCount);
if(!this.isCellFilled(x,y)){
this.tbl.rows[y].cells[x].style.backgroundColor = "black";
this.body.push({x:x,y:y,color:'black'});
break;
}
}
this.paused = true;
//添加键盘事件
document.onkeydown= function(e){
if (!e)e=window.event;
switch(e.keyCode | e.which | e.charCode){
case 13: {
if(Snake.paused){
Snake.move();
Snake.paused = false;
}
else{
//如果没有暂停,则停止移动
Snake.pause();
Snake.paused = true;
}
break;
}
case 37:{//left
//阻止蛇倒退走
if(Snake.direction==1){
break;
}
Snake.direction = 3;
break;
}
case 38:{//up
//快捷键在这里起作用
if(event.ctrlKey){
Snake.speedUp(-20);
break;
}
if(Snake.direction==2){//阻止蛇倒退走
break;
}
Snake.direction = 0;
break;
}
case 39:{//right
if(Snake.direction==3){//阻止蛇倒退走
break;
}
Snake.direction = 1;
break;
}
case 40:{//down
if(event.ctrlKey){
Snake.speedUp(20);
break;
}
if(Snake.direction==0){//阻止蛇倒退走
break;
}
Snake.direction = 2;
break;
}
}
}
},
//移动
move: function(){
this.timer = setInterval(function(){
Snake.erase();
Snake.moveOneStep();
Snake.paint();
}, this.speed);
},
//移动一节身体
moveOneStep: function(){
if(this.checkNextStep()==-1){
clearInterval(this.timer);
alert("Game over!\nPress Restart to continue.");
return;
}
if(this.checkNextStep()==1){
var _point = this.getNextPos();
var _x = _point.x;
var _y = _point.y;
var _color = this.getColor(_x,_y);
this.body.unshift({x:_x,y:_y,color:_color});
//因为吃了一个食物,所以再产生一个食物
this.generateDood();
return;
}
//window.status = this.toString();
var point = this.getNextPos();
//保留第一节的颜色
var color = this.body[0].color;
//颜色向前移动
for(var i=0; i<this.body.length-1; i++){
this.body[i].color = this.body[i+1].color;
}
//蛇尾减一节, 蛇尾加一节,呈现蛇前进的效果
this.body.pop();
this.body.unshift({x:point.x,y:point.y,color:color});
//window.status = this.toString();
},
//探寻下一步将走到什么地方
pause: function(){
clearInterval(Snake.timer);
this.paint();
},
getNextPos: function(){
var x = this.body[0].x;
var y = this.body[0].y;
var color = this.body[0].color;
//向上
if(this.direction==0){
y--;
}
//向右
else if(this.direction==1){
x++;
}
//向下
else if(this.direction==2){
y++;
}
//向左
else{
x--;
}
//返回一个坐标
return {x:x,y:y};
},
//检查将要移动到的下一步是什么
checkNextStep: function(){
var point = this.getNextPos();
var x = point.x;
var y = point.y;
if(x<0||x>=this.colCount||y<0||y>=this.rowCount){
return -1;//触边界,游戏结束
}
for(var i=0; i<this.body.length; i++){
if(this.body[i].x==x&&this.body[i].y==y){
return -1;//碰到自己的身体,游戏结束
}
}
if(this.isCellFilled(x,y)){
return 1;//有东西
}
return 0;//空地
},
//擦除蛇身
erase: function(){
for(var i=0; i<this.body.length; i++){
this.eraseDot(this.body[i].x, this.body[i].y);
}
},
//绘制蛇身
paint: function(){
for(var i=0; i<this.body.length; i++){
this.paintDot(this.body[i].x, this.body[i].y,this.body[i].color);
}
},
//擦除一节
eraseDot: function(x,y){
this.tbl.rows[y].cells[x].style.backgroundColor = "";
},
paintDot: function(x,y,color){
this.tbl.rows[y].cells[x].style.backgroundColor = color;
},
//得到一个坐标上的颜色
getColor: function(x,y){
return this.tbl.rows[y].cells[x].style.backgroundColor;
},
//用于调试
toString: function(){
var str = "";
for(var i=0; i<this.body.length; i++){
str += "x:" + this.body[i].x + " y:" + this.body[i].y + " color:" + this.body[i].color + " - ";
}
return str;
},
//检查一个坐标点有没有被填充
isCellFilled: function(x,y){
if(this.tbl.rows[y].cells[x].style.backgroundColor == ""){
return false;
}
return true;
},
//重新开始
restart: function(){
if(this.timer){
clearInterval(this.timer);
}
for(var i=0; i<this.rowCount;i++){
this.tbl.deleteRow(0);
}
this.body = [];
this.init();
this.speed = 250;
},
//加速
speedUp: function(time){
if(!this.paused){
if(this.speed+time<10||this.speed+time>2000){
return;
}
this.speed +=time;
this.pause();
this.move();
}
},
//产生食物。
generateDood: function(){
var colors = ['red','orange','yellow','green','blue','purple','#ccc'];
var x = Math.floor(Math.random()*this.colCount);
var y = Math.floor(Math.random()*this.rowCount);
var colorIndex = Math.floor(Math.random()*7);
if(!this.isCellFilled(x,y)){
this.tbl.rows[y].cells[x].style.backgroundColor = colors[colorIndex];
}
}
};
</script>
<body onload="Snake.init();">
/*************************************************************<br />
* javascript贪吃蛇 v2.4<br />
* author: sunxing007 05/14/2009<br />
* 转载请注明来自 <a href="http://blog.csdn.net/sunxing007">http://blog.csdn.net/sunxing007</a> 谢谢!<br />
**************************************************************/<br />
<table id="main" border="1" cellspacing="0" cellpadding="0"></table>
<input type="button" id="btn" value="开始/暂停" />点左边按钮或按Enter开始/暂停游戏<br />
<input type="button" id="reset" value="重新开始" /><br />
<input type="button" id="upSpeed" value="加速" />点左边按钮或按Ctrl + ↑加速<br />
<input type="button" id="downSpeed" value="减速" />点左边按钮或按Ctrl + ↓减速
<script>
$('btn').onclick = function(){
if(Snake.paused){
Snake.move();
Snake.paused = false;
}
else{
Snake.pause();
Snake.paused = true;
}
};
$("reset").onclick = function(){
Snake.restart();
this.blur();
};
$("upSpeed").onclick = function(){
Snake.speedUp(-20);
};
$("downSpeed").onclick = function(){
Snake.speedUp(20);
};
</script>
</body>
</html>
<head>
<title>贪吃蛇 Snake v2.4</title>
<style>
body{
font-size:9pt;
}
table{
border-collapse: collapse;
border:solid #333 1px;
}
td{
height: 10px;
width: 10px;
font-size: 0px;
}
.filled{
background-color:blue;
}
</style>
</head>
<script>
function $(id){return document.getElementById(id);}
/**************************************************************
* javascript贪吃蛇 v2.4 <br />
* author: sunxing007 05/14/2009<br />
* 转载请注明来自http://blog.csdn.net/sunxing007 谢谢!<br />
* v2.4修正了蛇身颜色可以随着蛇前进而移动
**************************************************************/
//贪吃蛇类
var Snake = {
tbl: null,
/**
* body: 蛇身,数组放蛇的每一节,
* 数据结构{x:x0, y:y0, color:color0},
* x,y表示坐标,color表示颜色
**/
body: [],
//当前移动的方向,取值0,1,2,3, 分别表示向上,右,下,左, 按键盘方向键可以改变它
direction: 0,
//定时器
timer: null,
//速度
speed: 250,
//是否已经暂停
paused: true,
//行数
rowCount: 30,
//列数
colCount: 30,
//初始化
init: function(){
var colors = ['red','orange','yellow','green','blue','purple','#ccc'];
this.tbl = $("main");
var x = 0;
var y = 0;
var colorIndex = 0;
//产生初始移动方向
this.direction = Math.floor(Math.random()*4);
//构造table
for(var row=0;row<this.rowCount;row++){
var tr=this.tbl.insertRow(-1);
for(var col=0;col<this.colCount;col++) {
var td=tr.insertCell(-1);
}
}
//产生20个松散节点
for(var i=0; i<10; i++){
x = Math.floor(Math.random()*this.colCount);
y = Math.floor(Math.random()*this.rowCount);
colorIndex = Math.floor(Math.random()*7);
if(!this.isCellFilled(x,y)){
this.tbl.rows[y].cells[x].style.backgroundColor = colors[colorIndex];
}
}
//产生蛇头
while(true){
x = Math.floor(Math.random()*this.colCount);
y = Math.floor(Math.random()*this.rowCount);
if(!this.isCellFilled(x,y)){
this.tbl.rows[y].cells[x].style.backgroundColor = "black";
this.body.push({x:x,y:y,color:'black'});
break;
}
}
this.paused = true;
//添加键盘事件
document.onkeydown= function(e){
if (!e)e=window.event;
switch(e.keyCode | e.which | e.charCode){
case 13: {
if(Snake.paused){
Snake.move();
Snake.paused = false;
}
else{
//如果没有暂停,则停止移动
Snake.pause();
Snake.paused = true;
}
break;
}
case 37:{//left
//阻止蛇倒退走
if(Snake.direction==1){
break;
}
Snake.direction = 3;
break;
}
case 38:{//up
//快捷键在这里起作用
if(event.ctrlKey){
Snake.speedUp(-20);
break;
}
if(Snake.direction==2){//阻止蛇倒退走
break;
}
Snake.direction = 0;
break;
}
case 39:{//right
if(Snake.direction==3){//阻止蛇倒退走
break;
}
Snake.direction = 1;
break;
}
case 40:{//down
if(event.ctrlKey){
Snake.speedUp(20);
break;
}
if(Snake.direction==0){//阻止蛇倒退走
break;
}
Snake.direction = 2;
break;
}
}
}
},
//移动
move: function(){
this.timer = setInterval(function(){
Snake.erase();
Snake.moveOneStep();
Snake.paint();
}, this.speed);
},
//移动一节身体
moveOneStep: function(){
if(this.checkNextStep()==-1){
clearInterval(this.timer);
alert("Game over!\nPress Restart to continue.");
return;
}
if(this.checkNextStep()==1){
var _point = this.getNextPos();
var _x = _point.x;
var _y = _point.y;
var _color = this.getColor(_x,_y);
this.body.unshift({x:_x,y:_y,color:_color});
//因为吃了一个食物,所以再产生一个食物
this.generateDood();
return;
}
//window.status = this.toString();
var point = this.getNextPos();
//保留第一节的颜色
var color = this.body[0].color;
//颜色向前移动
for(var i=0; i<this.body.length-1; i++){
this.body[i].color = this.body[i+1].color;
}
//蛇尾减一节, 蛇尾加一节,呈现蛇前进的效果
this.body.pop();
this.body.unshift({x:point.x,y:point.y,color:color});
//window.status = this.toString();
},
//探寻下一步将走到什么地方
pause: function(){
clearInterval(Snake.timer);
this.paint();
},
getNextPos: function(){
var x = this.body[0].x;
var y = this.body[0].y;
var color = this.body[0].color;
//向上
if(this.direction==0){
y--;
}
//向右
else if(this.direction==1){
x++;
}
//向下
else if(this.direction==2){
y++;
}
//向左
else{
x--;
}
//返回一个坐标
return {x:x,y:y};
},
//检查将要移动到的下一步是什么
checkNextStep: function(){
var point = this.getNextPos();
var x = point.x;
var y = point.y;
if(x<0||x>=this.colCount||y<0||y>=this.rowCount){
return -1;//触边界,游戏结束
}
for(var i=0; i<this.body.length; i++){
if(this.body[i].x==x&&this.body[i].y==y){
return -1;//碰到自己的身体,游戏结束
}
}
if(this.isCellFilled(x,y)){
return 1;//有东西
}
return 0;//空地
},
//擦除蛇身
erase: function(){
for(var i=0; i<this.body.length; i++){
this.eraseDot(this.body[i].x, this.body[i].y);
}
},
//绘制蛇身
paint: function(){
for(var i=0; i<this.body.length; i++){
this.paintDot(this.body[i].x, this.body[i].y,this.body[i].color);
}
},
//擦除一节
eraseDot: function(x,y){
this.tbl.rows[y].cells[x].style.backgroundColor = "";
},
paintDot: function(x,y,color){
this.tbl.rows[y].cells[x].style.backgroundColor = color;
},
//得到一个坐标上的颜色
getColor: function(x,y){
return this.tbl.rows[y].cells[x].style.backgroundColor;
},
//用于调试
toString: function(){
var str = "";
for(var i=0; i<this.body.length; i++){
str += "x:" + this.body[i].x + " y:" + this.body[i].y + " color:" + this.body[i].color + " - ";
}
return str;
},
//检查一个坐标点有没有被填充
isCellFilled: function(x,y){
if(this.tbl.rows[y].cells[x].style.backgroundColor == ""){
return false;
}
return true;
},
//重新开始
restart: function(){
if(this.timer){
clearInterval(this.timer);
}
for(var i=0; i<this.rowCount;i++){
this.tbl.deleteRow(0);
}
this.body = [];
this.init();
this.speed = 250;
},
//加速
speedUp: function(time){
if(!this.paused){
if(this.speed+time<10||this.speed+time>2000){
return;
}
this.speed +=time;
this.pause();
this.move();
}
},
//产生食物。
generateDood: function(){
var colors = ['red','orange','yellow','green','blue','purple','#ccc'];
var x = Math.floor(Math.random()*this.colCount);
var y = Math.floor(Math.random()*this.rowCount);
var colorIndex = Math.floor(Math.random()*7);
if(!this.isCellFilled(x,y)){
this.tbl.rows[y].cells[x].style.backgroundColor = colors[colorIndex];
}
}
};
</script>
<body onload="Snake.init();">
/*************************************************************<br />
* javascript贪吃蛇 v2.4<br />
* author: sunxing007 05/14/2009<br />
* 转载请注明来自 <a href="http://blog.csdn.net/sunxing007">http://blog.csdn.net/sunxing007</a> 谢谢!<br />
**************************************************************/<br />
<table id="main" border="1" cellspacing="0" cellpadding="0"></table>
<input type="button" id="btn" value="开始/暂停" />点左边按钮或按Enter开始/暂停游戏<br />
<input type="button" id="reset" value="重新开始" /><br />
<input type="button" id="upSpeed" value="加速" />点左边按钮或按Ctrl + ↑加速<br />
<input type="button" id="downSpeed" value="减速" />点左边按钮或按Ctrl + ↓减速
<script>
$('btn').onclick = function(){
if(Snake.paused){
Snake.move();
Snake.paused = false;
}
else{
Snake.pause();
Snake.paused = true;
}
};
$("reset").onclick = function(){
Snake.restart();
this.blur();
};
$("upSpeed").onclick = function(){
Snake.speedUp(-20);
};
$("downSpeed").onclick = function(){
Snake.speedUp(20);
};
</script>
</body>
</html>
追问
这个只是代码,能说的详细些么
参考资料: http://blog.csdn.net/sunxing007/archive/2009/05/14/4187038.aspx
展开全部
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>贪吃蛇</title>
<style type="text/css">
div#gameBoard{ width: 1000px; height:500px; background:gray; margin: 0 auto; position: relative;}
div#gameControl{ width: 998px; height: 50px; border: 1px solid gray; margin: 0 auto; line-height: 50px;}
div.block{ width: 20px; height:20px; float:left;}
div.snake_block{ background: red; position: absolute;}
div.food_block{ background: green; position: absolute;}
span#banner{ float: left;}
div#buttons{ float: right; }
</style>
<!--[if lt IE 7]>
<style>
div#buttons{ float: right; padding-top: 14px;}
</style>
<![endif]-->
<script type="text/javascript">
function Snake(oBoard){//蛇的构造函数
this.snakeHead = null;//蛇头
this.snakeBody = [];//蛇身
this.moveDir = 2;//0:left 1:up 2:right 3:down
this.isDead = false;//标识蛇是否死掉
for(var i=3; i>0; i--){
var oDiv = document.createElement('div');
oDiv.className = "block snake_block";
oBoard.oDivBoard.appendChild(oDiv);
oDiv.style.left = i * 20 + "px";
oDiv.style.top = "40px";
this.snakeBody.push(oDiv);
}
this.turn = function(direction){//转向
this.moveDir = direction;//修改属性moveDir的值
};
this.move = function(){
//先得到蛇头,根据蛇头确定下一步移动的目标坐标
this.snakeHead = this.snakeBody[0];//取出蛇身体的第一节,做为蛇头
var nextPos = {left: parseInt(this.snakeHead.style.left), top: parseInt(this.snakeHead.style.top)};//表示蛇头下一步移动的坐标位置
switch(this.moveDir){
case 0://left
nextPos.left -= 20;
break;
case 1://up
nextPos.top -= 20;
break;
case 2://right;
nextPos.left += 20;
break;
case 3://down
nextPos.top += 20;
break;
}
var snakeTail = this.snakeBody.pop();//删除并取出数组中最后一个元素
snakeTail.style.left = nextPos.left + "px";
snakeTail.style.top = nextPos.top + "px";
this.snakeBody.unshift(snakeTail);//将蛇尾巴加到头上
this.eat();
};
this.eat = function(){
this.snakeHead = this.snakeBody[0];
//2.判断蛇是否吃到墙
if(parseInt(this.snakeHead.style.left) == -20 || parseInt(this.snakeHead.style.left) == 1000
|| parseInt(this.snakeHead.style.top) == -20 || parseInt(this.snakeHead.style.top) == 500){
this.isDead = true;
}
//3.判断蛇是否吃到自己的身体
for(var i=1; i<this.snakeBody.length; i++){
if(parseInt(this.snakeHead.style.left) == parseInt(this.snakeBody[i].style.left) &&
parseInt(this.snakeHead.style.top) == parseInt(this.snakeBody[i].style.top)){
this.isDead = true;
}
}
//1.蛇吃食物
if(this.snakeHead.style.left == oBoard.food.oDomDiv.style.left &&
this.snakeHead.style.top == oBoard.food.oDomDiv.style.top){
this.grow();
oBoard.food.locate();//食物重新定位
}
};
this.grow = function(){
//蛇增长身体
oBoard.food.oDomDiv.style.background = "red";
this.snakeBody.push(oBoard.food.oDomDiv);
};
}
function Food(oBoard){
this.oDomDiv = null;
this.locate = function(){//食物定位
do{
var xPos = Math.floor(Math.random() * 1000);//512
var yPos = Math.floor(Math.random() * 500);//512
xPos = xPos - xPos%20;
yPos = yPos - yPos%20;
var oDiv = document.createElement('div');
oDiv.className = "block food_block";
oBoard.oDivBoard.appendChild(oDiv);
oDiv.style.left = xPos + "px";
oDiv.style.top = yPos + "px";
this.oDomDiv = oDiv;
var reLocate = false;
//防止蛇和食物重合
for(var i=0; i<oBoard.snake.snakeBody.length; i++){
var oDivSnake = oBoard.snake.snakeBody[i];//取出蛇身体中的每一节
if(parseInt(oDivSnake.style.left) == xPos && parseInt(oDivSnake.style.top) == yPos){
reLocate = true;
break;
}
}
}while(reLocate);
};
this.locate();
}
function Board(){
this.oDivBoard = document.getElementById('gameBoard');
//画面板上的小网格
for(var rows=0; rows<25; rows++){
for(var cols=0; cols<50; cols++){
var oDiv = document.createElement('div');
oDiv.className = "block";
this.oDivBoard.appendChild(oDiv);
}
}
this.snake = new Snake(this);//将蛇类是需要的divBoard对象做为参数传递进去
this.food = new Food(this);//将面板对象做为一个整体传给food
}
function Game(){
this.board = null;
this.timer = null;
this.init = function(){//游戏初始化
this.board = new Board();//创建面板对象
};
this.startGame = function(){
var oBoard = this.board;
var game = this;
this.timer = setInterval(function(){
//蛇移动
if(!oBoard.snake.isDead){
oBoard.snake.move();
}else{
game.stopGame();
}
}, 300);
};//开始游戏
this.stopGame = function(){
alert("Game over!");
clearInterval(this.timer);
};//结束游戏
this.pauseGame = function(){
clearInterval(this.timer);
};//暂停游戏
this.resumeGame = function(){
this.startGame();
};//恢复游戏
}
window.onload = function(){
var game = new Game();//创建一个游戏对象
game.init();
var oBtnStart = document.getElementById('btnStart');
oBtnStart.onclick = function(){
if(this.value == "开始"){
game.startGame();
this.value = "结束";
}else if(this.value == "结束"){
game.stopGame();
this.value = "开始";
}
};
var oBtnPause = document.getElementById('btnPause');
oBtnPause.onclick = function(){
if(this.value == "暂停"){
game.pauseGame();
this.value = "恢复";
}else if(this.value == "恢复"){
game.resumeGame();
this.value = "暂停";
}
};
document.onkeydown = function(e){
e = e || event;
var keyNum = e.which || e.keyCode;
switch(keyNum){
case 37: //left
if(game.board.snake.moveDir == 2)
{
break;
}
game.board.snake.turn(0);
break;
case 38: //up
if(game.board.snake.moveDir == 3)
{
break;
}
game.board.snake.turn(1);
break;
case 39: //right
if(game.board.snake.moveDir == 0)
{
break;
}
game.board.snake.turn(2);
break;
case 40: //down
if(game.board.snake.moveDir == 1)
{
break;
}
game.board.snake.turn(3);
break;
}
}
};
</script>
</head>
<body>
<div id="gameBoard"></div>
<div id="gameControl">
<span id="banner">JavaScript 贪吃蛇 By SYSIT</span>
<div id="buttons">
<input type="button" value="开始" id="btnStart"/>
<input type="button" value="暂停" id="btnPause"/>
</div>
</div>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>贪吃蛇</title>
<style type="text/css">
div#gameBoard{ width: 1000px; height:500px; background:gray; margin: 0 auto; position: relative;}
div#gameControl{ width: 998px; height: 50px; border: 1px solid gray; margin: 0 auto; line-height: 50px;}
div.block{ width: 20px; height:20px; float:left;}
div.snake_block{ background: red; position: absolute;}
div.food_block{ background: green; position: absolute;}
span#banner{ float: left;}
div#buttons{ float: right; }
</style>
<!--[if lt IE 7]>
<style>
div#buttons{ float: right; padding-top: 14px;}
</style>
<![endif]-->
<script type="text/javascript">
function Snake(oBoard){//蛇的构造函数
this.snakeHead = null;//蛇头
this.snakeBody = [];//蛇身
this.moveDir = 2;//0:left 1:up 2:right 3:down
this.isDead = false;//标识蛇是否死掉
for(var i=3; i>0; i--){
var oDiv = document.createElement('div');
oDiv.className = "block snake_block";
oBoard.oDivBoard.appendChild(oDiv);
oDiv.style.left = i * 20 + "px";
oDiv.style.top = "40px";
this.snakeBody.push(oDiv);
}
this.turn = function(direction){//转向
this.moveDir = direction;//修改属性moveDir的值
};
this.move = function(){
//先得到蛇头,根据蛇头确定下一步移动的目标坐标
this.snakeHead = this.snakeBody[0];//取出蛇身体的第一节,做为蛇头
var nextPos = {left: parseInt(this.snakeHead.style.left), top: parseInt(this.snakeHead.style.top)};//表示蛇头下一步移动的坐标位置
switch(this.moveDir){
case 0://left
nextPos.left -= 20;
break;
case 1://up
nextPos.top -= 20;
break;
case 2://right;
nextPos.left += 20;
break;
case 3://down
nextPos.top += 20;
break;
}
var snakeTail = this.snakeBody.pop();//删除并取出数组中最后一个元素
snakeTail.style.left = nextPos.left + "px";
snakeTail.style.top = nextPos.top + "px";
this.snakeBody.unshift(snakeTail);//将蛇尾巴加到头上
this.eat();
};
this.eat = function(){
this.snakeHead = this.snakeBody[0];
//2.判断蛇是否吃到墙
if(parseInt(this.snakeHead.style.left) == -20 || parseInt(this.snakeHead.style.left) == 1000
|| parseInt(this.snakeHead.style.top) == -20 || parseInt(this.snakeHead.style.top) == 500){
this.isDead = true;
}
//3.判断蛇是否吃到自己的身体
for(var i=1; i<this.snakeBody.length; i++){
if(parseInt(this.snakeHead.style.left) == parseInt(this.snakeBody[i].style.left) &&
parseInt(this.snakeHead.style.top) == parseInt(this.snakeBody[i].style.top)){
this.isDead = true;
}
}
//1.蛇吃食物
if(this.snakeHead.style.left == oBoard.food.oDomDiv.style.left &&
this.snakeHead.style.top == oBoard.food.oDomDiv.style.top){
this.grow();
oBoard.food.locate();//食物重新定位
}
};
this.grow = function(){
//蛇增长身体
oBoard.food.oDomDiv.style.background = "red";
this.snakeBody.push(oBoard.food.oDomDiv);
};
}
function Food(oBoard){
this.oDomDiv = null;
this.locate = function(){//食物定位
do{
var xPos = Math.floor(Math.random() * 1000);//512
var yPos = Math.floor(Math.random() * 500);//512
xPos = xPos - xPos%20;
yPos = yPos - yPos%20;
var oDiv = document.createElement('div');
oDiv.className = "block food_block";
oBoard.oDivBoard.appendChild(oDiv);
oDiv.style.left = xPos + "px";
oDiv.style.top = yPos + "px";
this.oDomDiv = oDiv;
var reLocate = false;
//防止蛇和食物重合
for(var i=0; i<oBoard.snake.snakeBody.length; i++){
var oDivSnake = oBoard.snake.snakeBody[i];//取出蛇身体中的每一节
if(parseInt(oDivSnake.style.left) == xPos && parseInt(oDivSnake.style.top) == yPos){
reLocate = true;
break;
}
}
}while(reLocate);
};
this.locate();
}
function Board(){
this.oDivBoard = document.getElementById('gameBoard');
//画面板上的小网格
for(var rows=0; rows<25; rows++){
for(var cols=0; cols<50; cols++){
var oDiv = document.createElement('div');
oDiv.className = "block";
this.oDivBoard.appendChild(oDiv);
}
}
this.snake = new Snake(this);//将蛇类是需要的divBoard对象做为参数传递进去
this.food = new Food(this);//将面板对象做为一个整体传给food
}
function Game(){
this.board = null;
this.timer = null;
this.init = function(){//游戏初始化
this.board = new Board();//创建面板对象
};
this.startGame = function(){
var oBoard = this.board;
var game = this;
this.timer = setInterval(function(){
//蛇移动
if(!oBoard.snake.isDead){
oBoard.snake.move();
}else{
game.stopGame();
}
}, 300);
};//开始游戏
this.stopGame = function(){
alert("Game over!");
clearInterval(this.timer);
};//结束游戏
this.pauseGame = function(){
clearInterval(this.timer);
};//暂停游戏
this.resumeGame = function(){
this.startGame();
};//恢复游戏
}
window.onload = function(){
var game = new Game();//创建一个游戏对象
game.init();
var oBtnStart = document.getElementById('btnStart');
oBtnStart.onclick = function(){
if(this.value == "开始"){
game.startGame();
this.value = "结束";
}else if(this.value == "结束"){
game.stopGame();
this.value = "开始";
}
};
var oBtnPause = document.getElementById('btnPause');
oBtnPause.onclick = function(){
if(this.value == "暂停"){
game.pauseGame();
this.value = "恢复";
}else if(this.value == "恢复"){
game.resumeGame();
this.value = "暂停";
}
};
document.onkeydown = function(e){
e = e || event;
var keyNum = e.which || e.keyCode;
switch(keyNum){
case 37: //left
if(game.board.snake.moveDir == 2)
{
break;
}
game.board.snake.turn(0);
break;
case 38: //up
if(game.board.snake.moveDir == 3)
{
break;
}
game.board.snake.turn(1);
break;
case 39: //right
if(game.board.snake.moveDir == 0)
{
break;
}
game.board.snake.turn(2);
break;
case 40: //down
if(game.board.snake.moveDir == 1)
{
break;
}
game.board.snake.turn(3);
break;
}
}
};
</script>
</head>
<body>
<div id="gameBoard"></div>
<div id="gameControl">
<span id="banner">JavaScript 贪吃蛇 By SYSIT</span>
<div id="buttons">
<input type="button" value="开始" id="btnStart"/>
<input type="button" value="暂停" id="btnPause"/>
</div>
</div>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个视频很难找的!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个视频很难找的!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询