7,765
社区成员
发帖
与我相关
我的任务
分享
<script type="text/javascript" language="javascript">
//--------------------------------
//----制作者: ║LZ↗雨辰║
//--制作时间: 2010年5月21日
//--制作用时: 8小时
//--------QQ: *********
//--------------------------------
var key=39; //键值 39右键 37左键 38上建 下键40
var trailId=''; //记录头部ID
var tailId=''; //记录尾部ID
var tailArray = new Array(); //ID 拆分的数组
var randomL=20; //随机数 左ID
var randomR=30; //随机数 右ID
var foodCount=5; //食物数量
var count=0; //食物累计
var times=200; //行进速度
var timeSpace=10; //速度递增值 注意:值必须大于等于1
function $(id){
return document.getElementById(id);
}
function run(){ //程序入口
d(); //程序主体
r();
$('txtTime').value = times; //行进速度显示
}
function r(){ //食物随机函数
var randomID = '';
randomL = Math.floor(Math.random()*52+1);
randomR = Math.floor(Math.random()*48+1);
if(count < foodCount){
randomID = randomL + '_' + randomR;
if($(randomID).style.background == 'white' && $(randomID).title == ''){
$(randomID).style.background = 'black';
$(randomID).title = '0';
count++;
}
}
}
function d(){
var left=0;
var right=0;
var up=0;
var down=0;
var oId='';
tailArray = tailId.split('_'); //0 为左边(-1 上 +1 下) 1 为右边(-1 左 +1 右)尾部ID拆分
//alert(tailId);
//------------------------------到此尾部开始
left = Number(tailArray[1]) - 1;
if(left >= 1){ //左边预算
var id = tailArray[0] + '_' + left;
if($(id).style.background != 'white' && $(id).title == ''){//0 食物
oId = id;
}
}
right = Number(tailArray[1]) + 1;
if(right <= 50){ //右边预算
var id = tailArray[0] + '_' + right;
if($(id).style.background != 'white' && $(id).title == ''){//0 食物
oId = id;
}
}
up = Number(tailArray[0]) - 1;
if(up >= 1){ //上边预算
var id = up + '_' + tailArray[1];
if($(id).style.background != 'white' && $(id).title == ''){//0 食物
oId = id;
}
}
down = Number(tailArray[0]) + 1;
if(down <= 55){ //下边预算
var id = down + '_' + tailArray[1];
if($(id).style.background != 'white' && $(id).title == ''){//0 食物
oId = id;
}
}
//alert(tailId);
$(tailId).style.background='white';//将先前尾部变色
tailId = oId;//记录预算尾部ID
//------------------------------到此尾部结束
//------------------------------到此头部开始
tailArray = trailId.split('_'); //0 为左边(-1 上 +1 下) 1 为右边(-1 左 +1 右)头部ID拆分
if(key == 37){//左
left = Number(tailArray[1]) - 1;
if(left >= 1){ //左边预算
var id = tailArray[0] + '_' + left;
if($(id).style.background == 'white' && $(id).title == ''){//0 食物
$(id).style.background = 'black';
$(id).title = '';
trailId = id;
}else if($(id).style.background == 'black' && $(id).title == '0'){
var ids = tailArray[0] + '_' + (left-1);
$(id).style.background = 'black';
$(id).title = '';
$(ids).style.background = 'black';
$(ids).title = '';
trailId = ids;
count--;
//times -= timeSpace;
times = times > timeSpace ? times - timeSpace : timeSpace;
}else{
$('butNewStart').style.display = 'block';
alert('Game Over !');
times = 600000;
}
}else{
$('butNewStart').style.display = 'block';
alert('Game Over !');
times = 600000;
}
}else if(key == 38){//上
up = Number(tailArray[0]) - 1;
if(up >= 1){ //上边预算
var id = up + '_' + tailArray[1];
if($(id).style.background == 'white' && $(id).title == ''){//0 食物
$(id).style.background = 'black';
$(id).title = '';
trailId = id;
}else if($(id).style.background == 'black' && $(id).title == '0'){
var ids = (up-1) + '_' + tailArray[1];
$(id).style.background = 'black';
$(id).title = '';
$(ids).style.background = 'black';
$(ids).title = '';
trailId = ids;
count--;
//times -= timeSpace;
times = times > timeSpace ? times - timeSpace : timeSpace;
}else{
$('butNewStart').style.display = 'block';
alert('Game Over !');
times = 600000;
}
}else{
$('butNewStart').style.display = 'block';
alert('Game Over !');
times = 600000;
}
}else if(key == 39){//右
right = Number(tailArray[1]) + 1;
if(right <= 50){ //右边预算
var id = tailArray[0] + '_' + right;
if($(id).style.background == 'white' && $(id).title == ''){//0 食物
$(id).style.background = 'black';
$(id).title = '';
trailId = id;
}else if($(id).style.background == 'black' && $(id).title == '0'){
var ids = tailArray[0] + '_' + (right+1);
$(id).style.background = 'black';
$(id).title = '';
$(ids).style.background = 'black';
$(ids).title = '';
trailId = ids;
count--;
//times -= timeSpace;
times = times > timeSpace ? times - timeSpace : timeSpace;
}else{
$('butNewStart').style.display = 'block';
alert('Game Over !');
times = 600000;
}
}else{
$('butNewStart').style.display = 'block';
alert('Game Over !');
times = 600000;
}
}else if(key == 40){//下
down = Number(tailArray[0]) + 1;
if(down <= 55){ //下边预算
var id = down + '_' + tailArray[1];
if($(id).style.background == 'white' && $(id).title == ''){//0 食物
$(id).style.background = 'black';
$(id).title = '';
trailId = id;
}else if($(id).style.background == 'black' && $(id).title == '0'){
var ids = (down+1) + '_' + tailArray[1];
$(id).style.background = 'black';
$(id).title = '';
$(ids).style.background = 'black';
$(ids).title = '';
trailId = ids;
count--;
//times -= timeSpace;
times = times > timeSpace ? times - timeSpace : timeSpace;
}else{
$('butNewStart').style.display = 'block';
alert('Game Over !');
times = 600000;
}
}else{
$('butNewStart').style.display = 'block';
alert('Game Over !');
times = 600000;
}
}
//------------------------------到此头部结束
setTimeout('run()',times);
}
//开始 初始位置
function Start(id1,id2,id3){
trailId=id3;
tailId=id1;
times=200;
key=39;
count=0;
$(id1).style.background='black';
$(id2).style.background='black';
$(id3).style.background='black';
$('txtTime').value = times;
$('butStart').disabled = 'false';
run();
}
//重新开始
function newStart(){
$('butNewStart').style.display = 'none';
for(var i=1 ; i<=55 ; i++){
for(var j=1 ; j<=50 ; j++){
$(i+'_'+j).style.background='white';
$(i+'_'+j).title = '';
}
}
Start('1_1','1_2','1_3');
}
function jumpPage(){
if(event.keyCode == 37){//左
if(key != 39){
key=37;
}
}
if(event.keyCode == 38){//上
if(key != 40){
key=38;
}
}
if(event.keyCode == 39){//右
if(key != 37){
key=39;
}
}
if(event.keyCode == 40){//下
if(key != 38){
key=40;
}
}
}
document.onkeydown=jumpPage;
</script>