Snake
Auteur: leejoo.nl - 09 september 2004 - 15:04 - Gekeurd door: Dennisvb - Hits: 6188 - Aantal punten: 2.53 (16 stemmen)
Plak dit script op je pagina. Let wel op dat je head en body uit elkaar houd.
En dan heb je snake op je website
|
Code: |
<head><style>
body{font-size:10px;font-family:verdana;}
#board
{
border:1px solid #CCFFCC;
position:absolute;
background:#666666 url('');
background-repeat: no-repeat;
background-position: center;
}
.panel_part
{
position:absolute;
border:0px;
}
.panel_part2
{
position:absolute;
}
.score_field
{
background-color:#404040;
border:0px;
font-size:12px;
font-style:Haettenschweiler; color=#CCFFCC;
font:bold;
}
</style>
<title>snake</title>
<script langauge="JavaScript">
var N = (document.all)? 0 : 1;
if(N){
document.captureEvents(Event.ONKEYDOWN);
}
document.onkeydown = mover;
var step = 20; // Grootte en breedte
var direction; //
var speed = 120 // Snelheid van d eslang
var bgc = ""; //
var skc = "white"; //kleur slang
var spc = "red"; // kleur stippen
var hdc = "black"; //kleur kop van slang
var stepr = 18;
var stepc = 18;
var timerID;
var eaten = false;
var snake = new Array();
var score = -30;
var crach = false;
var lst_dir;
var img_left = new Image();
img_left.src = "playerl.gif";
var img_right = new Image();
img_right.src = "playerr.gif";
var pauze = false;
function init(start_val,dir)
{
if (N)
{
document.getElementById("board").style.width = ((step*stepc));
document.getElementById("board").style.height = ((step*(stepr+1)) + 4);
}
else
{
document.getElementById("board").style.width = ((step*stepc)+2);
document.getElementById("board").style.height = ((step*(stepr+1)));
}
start_array = start_val.split("|");
stop = start_array.length/2;
for (i=0;i<stop;i++)
{
topr = start_array[i*2];
leftc = start_array[(i*2)+1];
obj = document.getElementById("pr"+topr+"c"+leftc);
if (i != 0) obj.style.backgroundColor= skc;
else obj.style.backgroundColor = hdc;
snake[i] = new Array(topr,leftc);
}
direction = dir
lst_dir = dir
set_food();
document.frm_score.txtscore.value = "Gebruik de spacebar om het spel te starten!!!";
}
function tracking()
{
if((direction == "right") && (lst_dir == "left")) direction = lst_dir;
else
{
if((direction == "left") && (lst_dir == "right")) direction = lst_dir;
else
{
if((direction == "up") && (lst_dir == "down")) direction = lst_dir;
else
{
if((direction == "down") && (lst_dir == "up")) direction = lst_dir;
}}}
if (direction == "right")
{
if ((snake[0][1] != 0) && (document.getElementById("pr"+snake[0][0]+"c"+(snake[0][1]-1)).style.backgroundColor != skc))
{
if(document.getElementById("pr"+snake[0][0]+"c"+(snake[0][1]-1)).style.backgroundColor == spc)
{
snake[snake.length] = new Array(snake[snake.length-1][0],snake[snake.length-1][0]);
eaten = true;
}
else
{
obj = document.getElementById("pr"+snake[snake.length-1][0]+"c"+snake[snake.length-1][1]);
obj.style.backgroundColor = bgc;
}
for(i=snake.length-1;i>=1;i--)
{snake[i][1] = snake[i-1][1];
snake[i][0] = snake[i-1][0];}
snake[0][1]--;
}
else crach = true;
}
if (direction == "left")
{
if ((snake[0][1] < (stepc-1)) && (document.getElementById("pr"+snake[0][0]+"c"+(snake[0][1]+1)).style.backgroundColor != skc))
{
if(document.getElementById("pr"+snake[0][0]+"c"+(snake[0][1]+1)).style.backgroundColor == spc)
{
snake[snake.length] = new Array(snake[snake.length-1][0],snake[snake.length-1][0]);
eaten = true;
}
else
{
obj = document.getElementById("pr"+snake[snake.length-1][0]+"c"+snake[snake.length-1][1]);
obj.style.backgroundColor = bgc;
}
for(i=snake.length-1;i>=1;i--)
{snake[i][1] = snake[i-1][1];
snake[i][0] = snake[i-1][0];}
snake[0][1]++;
}
else crach = true;
}
if (direction == "up")
{
if((snake[0][0] != 0) && (document.getElementById("pr"+(snake[0][0]-1)+"c"+snake[0][1]).style.backgroundColor != skc))
{
if(document.getElementById("pr"+(snake[0][0]-1)+"c"+(snake[0][1])).style.backgroundColor == spc)
{
snake[snake.length] = new Array(snake[snake.length-1][0],snake[snake.length-1][0]);
eaten = true;
}
else
{
obj = document.getElementById("pr"+snake[snake.length-1][0]+"c"+snake[snake.length-1][1]);
obj.style.backgroundColor = bgc;}
for(i=snake.length-1;i>=1;i--)
{snake[i][1] = snake[i-1][1];
snake[i][0] = snake[i-1][0];}
snake[0][0]--;
}
else crach = true;
}
if (direction == "down")
{
if((snake[0][0]< (stepr-1)) && (document.getElementById("pr"+(parseInt(snake[0][0])+1)+"c"+snake[0][1]).style.backgroundColor != skc))
{
if(document.getElementById("pr"+(parseInt(snake[0][0])+1)+"c"+(snake[0][1])).style.backgroundColor == spc)
{
snake[snake.length] = new Array(snake[snake.length-1][0],snake[snake.length-1][0]);
eaten = true;
}
else
{
obj = document.getElementById("pr"+snake[snake.length-1][0]+"c"+snake[snake.length-1][1]);
obj.style.backgroundColor = bgc;}
for(i=snake.length-1;i>=1;i--)
{snake[i][1] = snake[i-1][1];
snake[i][0] = snake[i-1][0];}
snake[0][0]++;
}
else crach = true;
}
obj = document.getElementById("pr"+snake[1][0]+"c"+snake[1][1]);
obj.style.backgroundColor = skc;
obj = document.getElementById("pr"+snake[0][0]+"c"+snake[0][1]);
obj.style.backgroundColor = hdc;
lst_dir = direction;
if (eaten) set_food();
if(crach) set_crach(1)
else timerID = setTimeout("tracking()",speed);
}
function set_crach(msg)
{
if(msg == 1)txt = "Jammer , Druk de spacebar om opnieuw te beginnen!";
else txt = "Jouw score is: " + score;
document.frm_score.txtscore.value = txt
timerID = setTimeout("set_crach("+ (-msg) +")",3000);
}
function end_crach()
{
for(i=0;i<stepr;i++)
{
for(j=0;j<stepc;j++)
{
document.getElementById("pr"+i+"c"+j).style.backgroundColor = "";
}}
snake = new Array();
score = -30;
speed = 120;
crach = false;
pauze = false;
clearTimeout(timerID);
init('2|10|2|11|2|12|2|13|2|14|2|15|2|16|2|17','right');
}
function set_food()
{
eaten = false;
ok = false;
if (score%300 == 0) {speed = speed - 3;}
while (ok != true)
{
f_left = Math.floor((Math.random()*stepc));
f_top = Math.floor((Math.random()*stepr));
color = document.getElementById("pr"+f_top+"c"+f_left).style.backgroundColor;
if((color != skc) && (color != hdc))
{
score= score + 30;
ok = true;
document.getElementById("pr"+f_top+"c"+f_left).style.backgroundColor = spc ;}
}
txt = "Score: " +score;
document.frm_score.txtscore.value = txt
}
function mover(e)
{
if (N)
{
mykey = e.which;
}
else mykey = event.keyCode;
if (mykey == 32)
{
if(crach != true)
{
if (pauze == true) end_pauze();
else set_pauze();
}
else end_crach();
}
if (mykey == 40)
{
direction = "down"
}
if (mykey == 38)
{
direction = "up"
}
if (mykey == 39)
{
direction = "left"
}
if (mykey == 37)
{
direction = "right"
}
}
function set_pauze()
{ pauze = true;
if(document.frm_score.txtscore.value == "Gebruik de spacebar om te starten!!!")
{tracking();
document.frm_score.txtscore.value = "Score: "+score;}
else{
document.frm_score.txtscore.value = "Score: "+score;
tracking();}
}
function end_pauze()
{
pauze=false;
clearTimeout(timerID);
document.frm_score.txtscore.value = "Pauze, gebruik de spacebar om verder te gaan!!!";
}
</script>
</head>
<BODY onload="init('2|10|2|11|2|12|2|13|2|14|2|15|2|16|2|17','right')">
<div id=board name=board>
<script language="JavaScript">
for(i=0;i<=stepr;i++)
{
for(j=0;j<stepc;j++)
{
if(i != stepr)document.write("<div class=panel_part id='pr"+i+"c"+j+"' style='top:"+(i*step)+";left:"+(j*step)+";width:"+(step)+"px;height:"+step+"px'></div>");
else {document.write("<div class=panel_part2 name='score' id='score' style='position:absolute;top:"+(i*step)+";left:"+(j*step)+";width:"+(step*stepc)+";height:"+step+";border-top: 1px solid #635489;'><div style='position:absolute'><form name=frm_score id=frm_score></div><input type=text name=txtscore id=txtscore class='score_field' readonly style='width:"+((stepc*step)-0)+"'><div style='position:absolute'></form></div></div>");
j=stepc;}
}
}
</script>
</div> Download code (.txt)
|
|
|
Stemmen |
Niet ingelogd. |
|