metanohi-misc-subsites/subsites/projects/x/snake.htm

77 lines
1.3 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3c.org/1999/xhtml">
<head>
<title>Snake</title>
<style type='text/css'>
* {
margin:0;
padding:0;
}
body {
position:absolute;
top:50%;
left:50%;
margin-top:-200px;
}
div {
width:10px;
height:10px;
float:left;
top:200px;
position:relative;
background:#000;
}
</style>
<script type='text/javascript'>
len=40
d=document
px="px"
stopped=false
function snake() {
nodetop=200
for (i=0;i<d.body.childNodes.length;i++) {
node=d.body.childNodes[i]
top=node.offsetTop
oldtop=nodetop
do {
rand=Math.floor(Math.random()*18+1)
if (rand>9) rand=rand-19
nodetop=top+rand
if (nodetop<0) nodetop=0
} while (nodetop<oldtop-9 || nodetop>oldtop+9)
node.style.top=nodetop+px
}
t=setTimeout("snake()",1)
}
function buildsnake() {
d.body.style.marginLeft="-"+len*5+"px"
for (i=0; i<len; i++) {
ne=document.createElement("div")
d.body.appendChild(ne)
}
snake()
}
function action() {
if (stopped) {
t=setTimeout("snake()",1)
stopped=false
document.title="Snake"
}
else {
clearTimeout(t)
stopped=true
document.title+=" :: PAUSED"
}
}
window.onload=buildsnake
d.onclick=action
</script>
</head>
<body></body>
</html>