<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <meta http-equiv="content-type" content="text/html; charset=ecukr"> <TITLE>지렁이게임</TITLE> <style> .block {width:25px;height:25px;border:solid 3;border-color:#999999;border-top-color:#ffffff;border-left-color:#ffffff} .onblock {width:25px;height:25px;border:solid 2;border-color:#CC0000;background-color:red;} .hblock{width:25px;height:25px;border:solid 2;border-color:#0033CC;background-color:blue;} .nblock{width:25px;height:25px;border:solid 2;border-color:#339900;background-color:#33CC33;} .xblock{width:25px;height:25px;border:solid 2;border-color:#000000;background-color:#000000;} </style> <script> ////////////////////////////////////////////////////// // 만든이 : 리트쿠우(LitQoo) / 김현수 // 홈페이지 : hsdooki.namoweb.net / litqoo.tistory.com // 이메일 : hsdooki@dreamwiz.com //////////////////////////////////////////////////////
var set_row=10; var set_col=10; var direction="t"; var sn = new Array(); var newblock = new snake(0,0); var cnt = 0; var timerid=0; var set_xblock=0; var ticker=0;
//구조체 function snake(x,y,st){ this.x=x; this.y=y; this.st=st; }
//랜덤함수 function snake_rand(max){ rand=0; while(rand==0)rand=(Math.floor(Math.random()*max)); return rand; }
//새꼬리 만드기 function make_new(){ while(1){ x=snake_rand(set_col+1); y=snake_rand(set_row+1); tempname = eval("game.block_"+x+"_"+y+""); if(tempname.className=="block"){ tempname.className="nblock"; break; } } newblock.x = x; newblock.y = y; }
//블럭디스플레이 function display(s,st){ tempname = eval("game.block_"+s.x+"_"+s.y+""); tempname.className = st; }
//타이머 function game_timer(){ ticker++; //꼬리옮기기 if(cnt>=1){ display(sn[cnt],"block"); for (i=cnt;i>=1;i--) { sn[i].x = sn[i-1].x; sn[i].y = sn[i-1].y; display(sn[i],"onblock"); } }
//머리옮기기 if(direction=="t")sn[0].y--; if(direction=="b")sn[0].y++; if(direction=="l")sn[0].x--; if(direction=="r")sn[0].x++; //끝났는지 범위 검사 if(sn[0].x<=0 || sn[0].y<=0 || sn[0].y>set_row || sn[0].x>set_col){game_end(); return false;} tempname = eval("game.block_"+sn[0].x+"_"+sn[0].y+""); //새블럭먹었을경우 꼬리 늘이고 새블럭 if(tempname.className=="nblock"){ cnt++; sn[cnt] = new snake(sn[cnt-1].x,sn[cnt-1].y); display(sn[cnt],"onblock"); make_new();
//끝났는지 블럭검사 }else if(tempname.className!="block"){ game_end(); return false; } //포인트계산 game.gp.value=cnt*10+set_xblock*100+ticker;
//머리블럭 나타내기 display(sn[0],"hblock"); //속력계산 turm = 800-cnt*20; if(turm<200)turm=200; game.speed.value = turm; timerid=setTimeout("game_timer()",turm); }
//그리기 function make_map(){ temp=""; for(y=1;y<=set_row;y++){ for(x=1;x<=set_col;x++){ temp+=("<input type=button name='block_"+x+"_"+y+"' class='block'>"); } temp+=("<br>"); } return temp; } //방향설정 function game_key(event){ if(event.keyCode==37)direction="l"; if(event.keyCode==38)direction="t"; if(event.keyCode==39)direction="r"; if(event.keyCode==40)direction="b"; } //게임시작, 초기화 function game_start(){ set_col = game.game_col.value*1; set_row = game.game_row.value*1; set_xblock = game.game_xblock.value*1; ticker=0; newblock.x=0; newblock.y=0; sn[0]=new snake(5,5); sn[1]=new snake(5,6); cnt = 1; //그리기 map = make_map(); document.getElementById("game_div").innerHTML=map; game_timer(); make_new(); make_xblock(); }
//장애물 생성 function make_xblock(){ check=1; while(set_xblock>=check){ x=snake_rand(set_col-1)+1; y=snake_rand(set_row-1)+1; xbox=new snake(x,y); tempname = eval("game.block_"+x+"_"+y+""); if(tempname.className=="block"){ display(xbox,"xblock"); check++; } } }
//게임끝 function game_end(){ game.gdata.value="가로:"+game.game_col.value+",세로:"+game.game_row.value+",장애물:"+game.game_xblock.value+",꼬리:"+cnt+",시간:"+ticker; clearTimeout(timerid); alert("게임끝"); }
</script> </HEAD> <BODY onload="" onkeydown="game_key(event)">
<form name=game method="post"> <INPUT TYPE="hidden" NAME="gid" value="snake"> <input type="hidden" name="gdata" value=""> <input type="hidden" name="gpoint" value="">
<TABLE border=1 align=center> <TR> <TD> 가로<input name=game_row value=10 size=3> 세로<input name=game_col value=10 size=3> 장애물<input name=game_xblock value=5 size=3><INPUT TYPE="button" VALUE="게임시작" ONCLICK="game_start()"></TD> </TR> <TR> <TD align=center> <div id="game_div"> <script> for(y=1;y<=set_row;y++){ for(x=1;x<=set_col;x++){ document.write("<input type=button name='block_"+x+"_"+y+"' class='block'>"); } document.write("<br>"); } </script> </div> </TD> </TR> <TR> <TD align=center>점수<input name=gp value=0 size=3 readonly> 속도<input name=speed value=0 size=3 readonly> </TD> </TR> </TABLE> </form> </BODY> </HTML>
소스를 다듬을 필요가 있을것 같은데 영 귀찮네요... 그나저나 링크엔 점수를 등록하여 랭킹을 표시할수있게 해놨는데 이걸 계속 뚫으시는 분들이 있네요. 이런 개구쟁이들 같으니.. 당췌 이걸 어떻게 막아야 할지.. |