# JavaScript 遊戲 : 貪吃蛇

javascript

<span>一條蛇，由蛇頭到蛇尾</span>

<!--
http://futurecom.vze.com
http://dewin.vze.com
http://dewin.126.com
http://dewin.tk

Start 2002-10-12 11:12
Finish  2002-10-16 07:43
-->

<body>

<script>

var Rows = 20;
var Cells = 30;

var MapW = 20;
var MapH = 20;

var BorderW = 5;
var oSpeed = 1
var Scores = 0;

var SnakeHeakColor = 'blue';
var SnakeBodyColor = 'orange';
var SnakeTailColor = 'yellow';

function CreatMainMap(){
MainMap = [];
for(var y=0;y<Rows;y++){
MainMap[y] = [];
for(var x=0;x<Cells;x++){
MainMap[y][x] = '';
}
}
}

function CreateFood(){
var x = parseInt(Math.random()*Cells);
var y = parseInt(Math.random()*Rows);
if(MainMap[y][x] == ''){
Score.innerHTML = Scores++;
MainMap[y][x] = 'F';
}
else CreateFood();
}

function CreateSnake(){
MainMap[SnakeY][SnakeX] = 'S';
}

var GoX = 0;
var GoY = 0;
var GoTime = 0;

function Dir(x,y){
GoX = (-GoX==x)?GoX:x;
GoY = (-GoY==y)?GoY:y;
if(!GoTime) GoTime = setInterval(Move,oSpeed);
}

function Move(){
SnakeX = (SnakeX+GoX<0)?Cells-1:((SnakeX+GoX>Cells-1)?0:SnakeX+GoX);
SnakeY = (SnakeY+GoY<0)?Rows-1:((SnakeY+GoY>Rows-1)?0:SnakeY+GoY);
if(MainMap[SnakeY][SnakeX] == ''){
AllSnakes[AllSnakes.length-1].style.background = SnakeBodyColor;
MainMap[AllSnakes[0].y][AllSnakes[0].x] = '';
AllSnakes[0].removeNode(true);
if(AllSnakes.length>1) AllSnakes[0].style.background = SnakeTailColor;
CreateSnake();
return;
}
if(MainMap[SnakeY][SnakeX] == 'F'){
AllSnakes[AllSnakes.length-1].style.background = SnakeBodyColor;
AllFoods[0].removeNode(true);
if(AllSnakes.length>1) AllSnakes[0].style.background = SnakeTailColor;
CreateSnake();
CreateFood();
return;
}
if(MainMap[SnakeY][SnakeX] == 'S'){
else window.close()
}
}

function document.onkeydown(){
switch(event.keyCode){
case 34:clearInterval(GoTime);oSpeed+=3;GoTime=setInterval(Move,oSpeed);break;//speed up
case 33:if(oSpeed-2>0){clearInterval(GoTime);oSpeed-=2;GoTime=setInterval(Move,oSpeed)};break;//speed down
case 37:Dir(-1,0);break;//left
case 38:Dir(0,-1);break;//up
case 39:Dir(1,0);break;//right
case 40:Dir(0,1);break;//down
case 83:clearInterval(GoTime);GoTime=0;break;
}
}

var MainMapWidth = 2*BorderW+Cells*MapW;
var MainMapHeight = 2*BorderW+Rows*MapH;
document.body.innerHTML += "<span id='Base' style='position:absolute;left:"+(document.body.clientWidth-MainMapWidth)/2+";top:"+(document.body.clientHeight-MainMapHeight)/2+";width:"+MainMapWidth+";height:"+MainMapHeight+";border:"+BorderW+" inset #0000CC;'></span><br><br><span id='Score'></span><br><br><font color=red>Page Down  to Speed down<br>Page Up  to Speed up</font>";
SnakeX = parseInt(Math.random()*Cells);
SnakeY = parseInt(Math.random()*Rows);
AllSnakes = Base.all.tags('SPAN');
AllFoods = Base.all.tags('DIV');
window.focus();
CreatMainMap();
CreateSnake();
CreateFood();
}

</script>

## Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

## Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

## Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD