SoFunction
Updated on 2025-03-02

Javascript Snake implementation code


// JavaScript Document
alert("The keyboard's arrow keys control the direction, the space key pauses.\nLIFE-making\nhttp:///anhulife");
// Add basic graphics blocks, that is, 160 10*10 2D matrix
var rowindex = new Array(40);
var colindex;
var cell;
// Definition of image units
var backcolor = "black";
for(var i = 0; i < 40; i ++ )
{
colindex = new Array(40);
for(var j = 0; j < 40; j ++ )
{
// Set the properties of each unit
cell = ("div");
= backcolor;
= "10px";
= "10px";
= "absolute";
= "" + (j * 10 + 100) + "px";
= "" + (i * 10 + 100) + "px";
= "hidden";
// Add unit
(cell);
//Fill the column group
colindex[j] = cell;
}
// Fill in row groups
rowindex[i] = colindex;
}
// The definition of gluttonous snakes is based on basic image units
function snake()
{
// Define the body of the snake and initialize it
= "white";
= new Array();
for(var i = 20; i < 25; i ++ )
{
rowindex[20][i]. = ;
// The first coordinate of rowindex is the row marker, and the second is the column marker
(rowindex[20][i]);
}
// Define the head coordinates of the snake, the first one is the row mark and the second one is the column mark
= [20, 20];
// Define the direction of the snake, 0 represents the left, 1 represents the bottom, 2 represents the right, 3 represents the top
= 0;
}
// Mobile module
function move()
{
// Calculate the coordinates of the head according to the direction of progress
switch()
{
case 0 :
[1] -= 1;
break;
case 1 :
[0] += 1;
break;
case 2 :
[1] += 1;
break;
case 3 :
[0] -= 1;
break;
}
// Determine whether it is crossing the line
if([0] < 0 || [0] > 39 || [1] < 0 || [1] > 39)
{
// If crossing the bound, return false
return false;
}
else
// If there is no cross-border, check the properties of the next element, eat it if it is food, and regenerate the food. If it is itself, return false
if([0] == food[0] && [1] == food[1])
{
// If it's food
rowindex[[0]][[1]]. = ;
(rowindex[[0]][[1]]);
score++;
makefood();
return true;
}
else
// If it's itself
if(rowindex[[0]][[1]]. == )
{
if(rowindex[[0]][[1]] == ())// If it is its tail
{
(rowindex[[0]][[1]]);
return true;
}
// If not the tail
return false;
}
// None of the above situations
(). = backcolor;
rowindex[[0]][[1]]. = ;
(rowindex[[0]][[1]]);
return true;
}
= move;
// Generate food module
var foodcolor = "blue";
var food = [20, 17];
rowindex[food[0]][food[1]]. = foodcolor;
function makefood()
{
var tempfood;
var tempelement;
out :
while(true)
{
tempfood = [(() * 39), (() * 39)];
tempelement = rowindex[tempfood[0]][tempfood[1]];
for(var i in )
{
if([i] == tempelement)
{
// If the randomly generated food is on the snake's body, jump out and continue
continue out;
}
// Successfully generated food
break out;
}
}
food = tempfood;
rowindex[food[0]][food[1]]. = foodcolor;
}
// Steering module and pause module
= turnorstop;
function turnorstop(event)
{
if( != undefined)
{
if(parseInt()==32)
{
alert("take a break");
}
else
{
switch(parseInt())
{
case 37 :
if(!=2)
= 0;
break;
case 38 :
if(!=1)
= 3;
break;
case 39 :
if(!=0)
= 2;
break;
case 40 :
if(!=3)
= 1;
break;
}
}
}
else
{
if(parseInt()==32)
{
alert("take a break");
}
else
{
switch(parseInt())
{
case 37 :
if(!=2)
= 0;
break;
case 38 :
if(!=1)
= 3;
break;
case 39 :
if(!=0)
= 2;
break;
case 40 :
if(!=3)
= 1;
break;
}
}
}
}
// Start the game module
var s = new snake();
var time = 60;//Snake's speed index
function startmove()
{
if(())
{
setTimeout(startmove, time);
}
else
{
alert("GAME OVER\nYour score is:"+score+"point");
}
}
//Score settings
var score = -1;
//Run the game
startmove();