js小游戏——贪吃蛇(附带源码)
本帖最后由 杏阳 于 2012-8-15 17:30 编辑<style>
.Food{background-color:red}
.Snake{background-color:blue}
</style>
<script>
var Rows=20
var Cells=30
var Num=15
var BorderWidth=5
var SpeedUp=5000
//创建地图
function CreateMap(){
BW=eval(Cells*Num+2*BorderWidth)
BH=eval(Rows*Num+2*BorderWidth)
document.body.innerHTML='<div id=MainMap style=position:absolute;left:'+(document.body.clientWidth-BW)/2+';top:'+(document.body.clientHeight-BH)/2+';width:'+BW+';height:'+BH+';border-width:'+BorderWidth+';border-style:inset;border-color:#0000cc></div>'
Map=new Array()
for(y=0;y<Rows;y++){
Map=new Array()
for(x=0;x<Cells;x++){
Map='0'
}
}
Sx=parseInt(Math.random()*Cells)
Sy=parseInt(Math.random()*Rows)
CreateSnake()
CreatFood()
AllDiv=MainMap.all.tags('DIV')
AllSpan=MainMap.all.tags('SPAN')
}
//创建食物的位置
function CreatFood(){
Fx=parseInt(Math.random()*Cells)
Fy=parseInt(Math.random()*Rows)
if(Map=='0'){
MainMap.innerHTML+='<span style=position:absolute;left:'+Fx*Num+';top:'+Fy*Num+';width:'+Num+';height:'+Num+';overflow:hidden class=Food></span>'
Map='F'
}
else CreatFood()
}
//创建蛇的位置
function CreateSnake(){
MainMap.innerHTML+='<div x='+Sx+' y='+Sy+' style=position:absolute;left:'+Sx*Num+';top:'+Sy*Num+';width:'+Num+';height:'+Num+';overflow:hidden class=Snake></div>'
Map='S'
}
//主移动--判断蛇头前面的是什么
function Move(){
Sx+=GoX
Sy+=GoY
if(Sy<0||Sy>=Rows)Move1()
else{
SnakeFront=Map
if(SnakeFront=='0')Move2()
else{
if(SnakeFront=='F')Move3()
else Move1()
}
}
}
//重新开始
function Move1(){
ReStart=confirm("Game Over,重新开始?")
if(ReStart)window.location.reload()
}
var Times=200
//蛇前是空地时
function Move2(){
Map.y].x]='0'
AllDiv.removeNode(true)
CreateSnake()
setTimeout('Move()',Times)
}
//蛇前面是食物时
function Move3(){
CreateSnake()
AllSpan.removeNode(true)
CreatFood()
setTimeout('Move()',Times)
}
//蛇越行越快
function oTimes(){
Times-=5
if(Times>5)setTimeout('oTimes()',SpeedUp)
}
document.onkeydown=KeyDown
//方向
function KeyDown(){
Key=event.keyCode
switch(Key){
case 37:Dir(-1,0);break//左
case 39:Dir(1,0);break//右
case 38:Dir(0,-1);break//上
case 40:Dir(0,1);break}//下
return false
}
var Star=0
function Dir(x,y){
GoX=x
GoY=y
if(Star==0){
oTimes()
Star=1
Move()
}
}
//开始时运行
onload=CreateMap
</script>
贪吃蛇.html
http://kuai.xunlei.com/d/OZARAWEEFRIF 贪食蛇?推箱子? nkc3g4 发表于 2012-8-14 17:50 static/image/common/back.gif
贪食蛇?推箱子?
额,发错了啊,就当是两个吧,哈哈,反正源代码和连接不冲突,算了,还是给改了吧
页:
[1]