Today, I wrote about snakes. Because I don't know much about them, I wrote a very spoof snake.
Running Method: Click start to use the upper, lower, and lower keys,
The following code is used:
<HTML>
<Head>
<Title> snake </title>
<SCRIPT>
// Var marginleft = "50px ";
// Var Height = "50 ";
VaR realkey;
VaR foot;
VaR sheshen;
Window. onload = function (){
// Alert ("onLoad ");
Bodydiv.style.css text = "position: absolute; left: 400px; top: 20px; width: 800px; Height: 600px; Border: 1px solid #000 ;";
}
// Timer
Function start (){
She ();
Footed ();
// Time = setinterval ("move ()", 300 );
Time = setinterval (function (){
Move ();
},300 );
}
// Create a snake
Function she (){
Alert ("She ()");
Sheshen = Document. createelement ("Div ");
Shesute. style. Position = "absolute ";
Sheshen. style. width = "50px ";
Sheshen. style. Height = "50px ";
Sheshen. style. Background = "red ";
Sheshen. style. Left = "50px ";
Sheshen. style. Top = "50px ";
Sheshen. innerhtml = "snake ";
// Sheshen. style. marginright = "50px ";
Document. getelementbyid ('bodydiv '). appendchild (sheschild );
}
// Create food
Function footed (){
Alert ("foot ()");
Foot = Document. createelement ("Div ");
Foot. style. Position = "absolute ";
Foot. style. width = "50px ";
Foot. innerhtml = "food ";
Foot. style. Height = "50px ";
// Foot. style. marginleft = "50px ";
Foot. style. Background = "red ";
Foot. style. Top = parseint (math. Random () * 9 );
// Alert (foot. style. Top );
Foot. style. Top = parseint (math. Random () * 9) * 50;
Foot. style. Left = parseint (math. Random () * 9) * 50;
// Foot. style. Left = "50px ";
// Foot. style. margintop = "50px ";
Document. getelementbyid ('bodydiv '). appendchild (foot );
}
// Move the location
Function move (){
// Downward
If (realkey = "("){
// Alert (realkey );
Shesint. style. Top = parseint (shesint. style. Top) + 50;
// Alert (foot. style. margintop );
If (sheshen. style. Top> "550px "){
// Alert ("gameover ");
Clearinterval (time );
}
If (parseint (shesint. style. width )! = 50 ){
Shesint. style. Height = parseint (shesint. style. Height) + 50;
Shesint. style. width = parseint (shesint. style. width)-50;
}
If (sheshen. style. Left = foot. style. Left & sheshen. style. Top = foot. style. Top ){
Foot. style. Top = parseint (math. Random () * 9) * 50;
Foot. style. Left = parseint (math. Random () * 9) * 50;
Shesint. style. Height = parseint (shesint. style. Height) + 50;
}
}
// Up
If (realkey = "&"){
Shesint. style. Top = parseint (shesint. style. Top)-50;
If (sheshen. style. Top <"0px "){
// Alert ("gameover ");
Clearinterval (time );
}
If (parseint (shesint. style. width )! = 50 ){
Shesint. style. Height = parseint (shesint. style. Height) + 50;
Shesint. style. width = parseint (shesint. style. width)-50;
}
If (sheshen. style. Left = foot. style. Left & sheshen. style. Top = foot. style. Top ){
Foot. style. Top = parseint (math. Random () * 9) * 50;
Foot. style. Left = parseint (math. Random () * 9) * 50;
Shesint. style. Height = parseint (shesint. style. Height) + 50;
}
}
// Left
If (realkey = "% "){
Shesint. style. Left = parseint (shesint. style. Left)-50;
If (sheshen. style. Left <"0px "){
// Alert ("gameover ");
Clearinterval (time );
}
If (parseint (shesint. style. Height )! = 50 ){
Shesint. style. Height = parseint (shesint. style. Height)-50;
Shesint. style. width = parseint (shesint. style. width) + 50;
}
If (sheshen. style. Left = foot. style. Left & sheshen. style. Top = foot. style. Top ){
Foot. style. Top = parseint (math. Random () * 9) * 50;
Foot. style. Left = parseint (math. Random () * 9) * 50;
Shesint. style. width = parseint (shesint. style. width) + 50;
}
}
// Right
If (realkey = "'"){
Shesint. style. Left = parseint (shesint. style. Left) + 50;
If (sheshen. style. Left> "750px "){
// Alert ("gameover ");
Clearinterval (time );
}
If (parseint (shesint. style. Height )! = 50 ){
Shesint. style. Height = parseint (shesint. style. Height)-50;
Shesint. style. width = parseint (shesint. style. width) + 50;
}
If (sheshen. style. Left = foot. style. Left & sheshen. style. Top = foot. style. Top ){
Foot. style. Top = parseint (math. Random () * 9) * 50;
Foot. style. Left = parseint (math. Random () * 9) * 50;
Shesint. style. width = parseint (shesint. style. width) + 50;
// Alert (shesert. style. Height );
}
}
}
Document. onkeydown = keydown;
Function keydown (e ){
Realkey = string. fromcharcode (E. Which );
// Alert (realkey );
}
</SCRIPT>
</Head>
<Body>
<Button onclick = "Start ()"> Start </button>
<Div id = "bodydiv">
<Div id = "div11" style = "position: absolute; width: 50; Height: 50; top: 50; left: 50; Background: yelloow"> </div>
</Div>
</Body>
</Html>