<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>贪吃傻瓜蛇</TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="jam_li;ztmdym">
<META NAME="Keywords" CONTENT="贪吃蛇">
<META NAME="Description" CONTENT="">
<style>
body{
font-size:15px;
padding-top:0px;
margin:5px;
}
#tip{
width:100%;
height:20px;
text-align:center;
background-color:#6699FF;
}
#setBoard{
position:absolute;
left:5px;
top:25px;
width:200px;
height:100px;
padding:1px;
background-color:#C0C0C0;
border-style:inset;
border-width:1px;
font-size:13px;
color:#3366CC
}
</style>
</HEAD>
<HTML>
<HEAD>
<TITLE>贪吃傻瓜蛇</TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="jam_li;ztmdym">
<META NAME="Keywords" CONTENT="贪吃蛇">
<META NAME="Description" CONTENT="">
<style>
body{
font-size:15px;
padding-top:0px;
margin:5px;
}
#tip{
width:100%;
height:20px;
text-align:center;
background-color:#6699FF;
}
#setBoard{
position:absolute;
left:5px;
top:25px;
width:200px;
height:100px;
padding:1px;
background-color:#C0C0C0;
border-style:inset;
border-width:1px;
font-size:13px;
color:#3366CC
}
</style>
</HEAD>
<BODY>
<span id="tip">正在初始化游戏空间,请稍候...</span>
<span id="setBoard" οnmοuseοut="this.style.borderStyle='inset';this.all[0].disabled=true" οnmοuseοver="{this.style.borderStyle='outset';this.all[0].disabled=false;}">
<fieldset style="border-style:solid;border-color:#801224;border-width:1px;width:100%;height:100%" disabled="true">
<legend align="right">设置</legend>
<input type="checkbox" id="acrossWall" οnclick="javascript:a2Wall=this.checked;" checked><label for="acrossWall">允许穿墙</label><input type="checkbox" id="autoMove" οnclick="javascript:autoMove=this.checked;"><label for="autoMove">自己玩去</label><br>
速度:<input type="text" value="500" id="txtSpeed" size="4" style="height:18px" onpropertychange="javascript:speed=parseInt(this.value)">ms/grid<input type="button" value="<" style="height:18px" οnclick="javascript:if(speed>11){txtSpeed.value=parseInt(txtSpeed.value)-10;}"><input type="button" value=">" style="height:18px" οnclick="javascript:txtSpeed.value=parseInt(txtSpeed.value)+10;">
<input type="button" value="reBuild" οnclick="javascript:initSpace()">
</fieldset>
</span>
<table id="tSpace" border="1" width="400" height="400" align="center" style="border-collapse:collapse;">
</table>
<SCRIPT LANGUAGE="JavaScript">
<!--
var bColor="#FFFFCC";//背景色
var fColor="#FF0000";//蛇身颜色
var fColor1="#000000";//食物颜色1
var fColor2="#FFFFFF";//食物颜色2
var tRows=40;//表格行数
var tCells=40;//表格列数
var tdWidth=5;//单元格宽度
var speed=500;//timeout毫秒数
var fX=-1;//x方向的偏转
var fY=-1;//y方向的偏转
var a2Wall=true;//允许穿墙
var autoMove=false;//自动模式
var temp;
//设置初始前进方向
var moveX=0;
var moveY=-1;
//设置全局变量
var tSpace=document.getElementById("tSpace");
var snake=new Array();
//表空间初始化
function initSpace(){
setTip("正在初始化,请稍候...");
var date=new Date();
for(var i=0;i<tRows;i++){
tSpace.insertRow(i);
for(var j=0;j<tCells;j++){
tSpace.rows[i].insertCell(j);
tSpace.rows[i].cells[j].width=tdWidth;
}
}
var date1=new Date();
setTip("初始化完毕(耗时:"+(new Date()-date)+"ms)");
setTimeout("foodManager();",500);
}
//蛇身初始化
function initSnake(){
snake[0]=parseInt(tRows/2)+","+parseInt(tCells/2);
}
//吞掉食物
function add2Snake(x,y){
if(x==fX&&y==fY) {
fX=-1;fY=-1;
snake[snake.length]=x+","+y;
show(x,y);
return true;
}
return false;
}
//移动蛇
function moveSnake(){
var tX=parseInt(snake[snake.length-1].split(",")[0])+moveX
var tY=parseInt(snake[snake.length-1].split(",")[1])+moveY;
//---这里设置穿墙---
if(tX>=tCells||tX<0||tY>=tRows||tY<0) {
if(!a2Wall){
setTip("<font color='red'>你的蛇死掉了!</font>");
return false;
}else{
if(tX>=tCells) tX=0;
if(tX<0) tX=tCells-1;
if(tY>=tRows) tY=0;
if(tY<0) tY=tRows-1;
}
}
//------
if(autoMove){
if(fX!=tX){
moveX=fX>tX?1:-1;
moveY=0;
}else{
moveX=0;
moveY=fY>tY?1:-1;
}
}
temp=tX+","+tY;
if(eval("add2Snake("+temp+")")){
setTimeout("moveSnake()",speed);
return;
}
for(var i=snake.length-1;i>=0;i--){
var tTemp=snake[i];
snake[i]=temp;
temp=tTemp;
eval("show("+snake[i]+")");
}
eval("hide("+temp+")");
setTimeout("moveSnake()",speed);
}
//显示指定蛇身块
function show(x,y){
tSpace.rows[y].cells[x].bgColor=fColor;
}
//隐藏指定块
function hide(x,y){
tSpace.rows[y].cells[x].bgColor="";
}
//随机生成食物
function foodManager(){
if(fX!=-1&&fY!=-1){
with (tSpace.rows[fY].cells[fX]){
if(bgColor==fColor1){
bgColor=fColor2;
}else{
bgColor=fColor1;
}
}
setTimeout("foodManager();",500);
return;
}
fX=Math.floor(Math.random()*tCells);
fY=Math.floor(Math.random()*tRows);
tSpace.rows[fY].cells[fX].bgColor=fColor1;
setTimeout("foodManager();",500);
return;
}
//设置蛇头前进方向
function makeWay(){
switch (event.keyCode){
case 37:
moveX=-1;
moveY=0;
break;
case 38:
moveX=0;
moveY=-1;
break;
case 39:
moveX=1;
moveY=0;
break;
case 40:
moveX=0;
moveY=1;
break;
default:
}
}
document.οnkeydοwn=makeWay;
//设置提示信息
function setTip(msg){
document.getElementById("tip").innerHTML=msg;
}
//主方法
function main(){
tSpace.bgColor=bColor;
setTimeout("initSpace();",1);
initSnake();
setTimeout("moveSnake()",500);
}
main();
//-->
</SCRIPT>
</BODY>
</HTML>
<span id="tip">正在初始化游戏空间,请稍候...</span>
<span id="setBoard" οnmοuseοut="this.style.borderStyle='inset';this.all[0].disabled=true" οnmοuseοver="{this.style.borderStyle='outset';this.all[0].disabled=false;}">
<fieldset style="border-style:solid;border-color:#801224;border-width:1px;width:100%;height:100%" disabled="true">
<legend align="right">设置</legend>
<input type="checkbox" id="acrossWall" οnclick="javascript:a2Wall=this.checked;" checked><label for="acrossWall">允许穿墙</label><input type="checkbox" id="autoMove" οnclick="javascript:autoMove=this.checked;"><label for="autoMove">自己玩去</label><br>
速度:<input type="text" value="500" id="txtSpeed" size="4" style="height:18px" onpropertychange="javascript:speed=parseInt(this.value)">ms/grid<input type="button" value="<" style="height:18px" οnclick="javascript:if(speed>11){txtSpeed.value=parseInt(txtSpeed.value)-10;}"><input type="button" value=">" style="height:18px" οnclick="javascript:txtSpeed.value=parseInt(txtSpeed.value)+10;">
<input type="button" value="reBuild" οnclick="javascript:initSpace()">
</fieldset>
</span>
<table id="tSpace" border="1" width="400" height="400" align="center" style="border-collapse:collapse;">
</table>
<SCRIPT LANGUAGE="JavaScript">
<!--
var bColor="#FFFFCC";//背景色
var fColor="#FF0000";//蛇身颜色
var fColor1="#000000";//食物颜色1
var fColor2="#FFFFFF";//食物颜色2
var tRows=40;//表格行数
var tCells=40;//表格列数
var tdWidth=5;//单元格宽度
var speed=500;//timeout毫秒数
var fX=-1;//x方向的偏转
var fY=-1;//y方向的偏转
var a2Wall=true;//允许穿墙
var autoMove=false;//自动模式
var temp;
//设置初始前进方向
var moveX=0;
var moveY=-1;
//设置全局变量
var tSpace=document.getElementById("tSpace");
var snake=new Array();
//表空间初始化
function initSpace(){
setTip("正在初始化,请稍候...");
var date=new Date();
for(var i=0;i<tRows;i++){
tSpace.insertRow(i);
for(var j=0;j<tCells;j++){
tSpace.rows[i].insertCell(j);
tSpace.rows[i].cells[j].width=tdWidth;
}
}
var date1=new Date();
setTip("初始化完毕(耗时:"+(new Date()-date)+"ms)");
setTimeout("foodManager();",500);
}
//蛇身初始化
function initSnake(){
snake[0]=parseInt(tRows/2)+","+parseInt(tCells/2);
}
//吞掉食物
function add2Snake(x,y){
if(x==fX&&y==fY) {
fX=-1;fY=-1;
snake[snake.length]=x+","+y;
show(x,y);
return true;
}
return false;
}
//移动蛇
function moveSnake(){
var tX=parseInt(snake[snake.length-1].split(",")[0])+moveX
var tY=parseInt(snake[snake.length-1].split(",")[1])+moveY;
//---这里设置穿墙---
if(tX>=tCells||tX<0||tY>=tRows||tY<0) {
if(!a2Wall){
setTip("<font color='red'>你的蛇死掉了!</font>");
return false;
}else{
if(tX>=tCells) tX=0;
if(tX<0) tX=tCells-1;
if(tY>=tRows) tY=0;
if(tY<0) tY=tRows-1;
}
}
//------
if(autoMove){
if(fX!=tX){
moveX=fX>tX?1:-1;
moveY=0;
}else{
moveX=0;
moveY=fY>tY?1:-1;
}
}
temp=tX+","+tY;
if(eval("add2Snake("+temp+")")){
setTimeout("moveSnake()",speed);
return;
}
for(var i=snake.length-1;i>=0;i--){
var tTemp=snake[i];
snake[i]=temp;
temp=tTemp;
eval("show("+snake[i]+")");
}
eval("hide("+temp+")");
setTimeout("moveSnake()",speed);
}
//显示指定蛇身块
function show(x,y){
tSpace.rows[y].cells[x].bgColor=fColor;
}
//隐藏指定块
function hide(x,y){
tSpace.rows[y].cells[x].bgColor="";
}
//随机生成食物
function foodManager(){
if(fX!=-1&&fY!=-1){
with (tSpace.rows[fY].cells[fX]){
if(bgColor==fColor1){
bgColor=fColor2;
}else{
bgColor=fColor1;
}
}
setTimeout("foodManager();",500);
return;
}
fX=Math.floor(Math.random()*tCells);
fY=Math.floor(Math.random()*tRows);
tSpace.rows[fY].cells[fX].bgColor=fColor1;
setTimeout("foodManager();",500);
return;
}
//设置蛇头前进方向
function makeWay(){
switch (event.keyCode){
case 37:
moveX=-1;
moveY=0;
break;
case 38:
moveX=0;
moveY=-1;
break;
case 39:
moveX=1;
moveY=0;
break;
case 40:
moveX=0;
moveY=1;
break;
default:
}
}
document.οnkeydοwn=makeWay;
//设置提示信息
function setTip(msg){
document.getElementById("tip").innerHTML=msg;
}
//主方法
function main(){
tSpace.bgColor=bColor;
setTimeout("initSpace();",1);
initSnake();
setTimeout("moveSnake()",500);
}
main();
//-->
</SCRIPT>
</BODY>
</HTML>