Array
Demo Effect: (explain the article after the article is covered by all the source code package download)
The second is the following source analysis:
August 28 5:45 Update, there is a game map, speed selection:
First frame: as:
stage.showmenu=false;//does not display menus
Stop ();
---------------Start frame as-------------------
var s_total = Total number of 500;//snakes
var w_col = 30;//line, Width
var w_row = 30;
var w_w = 15;//per square pixel
var myarray;//map Array
for (var i = 0; i<s_total; i++) {
_root.attachmovie ("Snake", "s" +i, 2000+i);/Load the snake
}
var level = 1;//for extended equipment, levels 1
for (var i = 0; i<10; i++) {//Load food
_root.attachmovie ("Food", "F" +i, 1000+i);
}
gotoAndPlay ("Gotono");
------------Gotono frame as:----------------------------------
var S_len = 5;//Snake length
var snake_x = 1; Snake position 1 rows, 1 columns
var snake_y = 1;
var snake_fangx = 0;//Snake moves in the direction of the value, to the right 0, down to 1, left to 2, up to 3
var Snake_tou = 0;//Snake Head
for (var i = 0; i<s_total; i++) {//The snake's MC jumps to the first frame,
Eval ("S" +i). gotoAndStop (1);
}
s0._x = 15;//Snake head position 15 pixels.
s0._y = 15;
S0.gotoandstop (2); Show Snake Head
s0.x = rank and column position of snake_x;//snake Head
S0.y = snake_y;
Tm=random (3) +1;
gotoAndPlay ("no" +tm);//randomly jump into NO1,NO2,NO3;
No1 Frames:--------------------------------------
Myarray=[[.... ...]//build map array 30*30, see source file (= = = = =//)
Eatfood = 0; Eat the food variable
Guan = 1;//First Pass
Bzhuan (); Show Map
Bfood (); Show food
gotoAndPlay ("S_move"); Jump to the snake moving as
function Bfood () {
for (var i = 0; i<10; i++) {
do {
var x = random (W_col);
var y = random (W_row);
while (Myarray[y][x]!= 0);
MYARRAY[Y][X] = i+1;
With (eval ("F" +i)) {
_x = X*w_w;
_y = Y*w_w;
gotoAndStop (Random (2) +2);
}
}
}
function Bzhuan () {
for (var i = 0; i<w_col*w_row; i++) {
if (eval ("B" +i)!= undefined) {
Eval ("B" +i). Removemovieclip ();
}
}
for (var i = 0; i<w_row; i++) {
for (var j = 0; j<w_col; j + +) {
if (Myarray[i][j]!= 0) {
var num = i*w_col+j;
_root.attachmovie ("Zhuan", "B" +num, num+1);
Eval ("B" +num). _x = J*w_w;
Eval ("B" +num). _y = I*w_w;
}
}
}
}
No2 and NO3 frame as are just different map arrays.
Smove Frames:----------------------------------------------
var snake_fx = 0;
var snake_fy = 0;
if (Snake_fangx = = 0) {
SNAKE_FX = 1;
}
if (Snake_fangx = = 1) {
Snake_fy = 1;
}
if (Snake_fangx = = 2) {
Snake_fx =-1;
}
if (Snake_fangx = = 3) {
Snake_fy =-1;
}
snake_x = (snake_x+snake_fx+w_col)%w_col;
Snake_y = (snake_y+snake_fy+w_row)%w_row;
if (myarray[snake_y][snake_x]>=64) {
gotoAndStop ("over");
Game Over
} else {
Eval ("S" +snake_tou). gotoAndStop (3);
Snake_tou = (snake_tou+1)%s_total;
Eval ("S" +snake_tou). x = snake_x;
Eval ("S" +snake_tou). y = snake_y;
Eval ("S" +snake_tou). _x = Snake_x*w_w;
Eval ("S" +snake_tou). _y = Snake_y*w_w;
Eval ("S" +snake_tou). gotoAndStop (2);
var S_wei = (snake_tou-s_len+s_total)%s_total;
var tmpx = eval ("s" +s_wei). x;
var tmpy = eval ("s" +s_wei). Y;
MYARRAY[TMPY][TMPX] = 0;
Eval ("S" +s_wei). gotoAndStop (1);
Food_n = myarray[snake_y][snake_x];
Myarray[snake_y][snake_x] = 64;
if (food_n>=1 && food_n<=10) {
gotoAndPlay ("eat");
Eat
} else {
gotoAndPlay ("Noeat");
No eat
}
}
Noeat
gotoAndPlay ("S_move");
Eat
var tmp = food_n-1;
Eval ("F" +tmp). gotoAndStop (1);
eatfood++;
S_len + 5;
if (Eatfood = = 10) {
Eatfood = 0;
Bfood ();
gotoAndPlay ("S_move");
} else {
gotoAndPlay ("S_move");
}
Over frame:
_root.attachmovie ("Over", "O", 10000);
o._x=100;
o._y=100;
O.guan_num=guan;
Stop ();
_root.onmousedown=function () {
if (O.hittest (_root._xmouse,_root._ymouse)) {
StartDrag ("_ROOT.O", false,0,0,250,220);
}
}
_root.onmouseup=function () {
Stopdrag ();
}
The keyboard response layer is an empty MC with only as:
Onclipevent (KeyDown) {
kd = Key.getcode ();
if (kd = = key.right) {
if (_root.snake_fangx!= 2) {
_ROOT.SNAKE_FANGX = 0;
}
else if (kd = = Key.down) {
if (_root.snake_fangx!= 3) {
_ROOT.SNAKE_FANGX = 1;
}
else if (kd = = Key.left) {
if (_root.snake_fangx!= 0) {
_ROOT.SNAKE_FANGX = 2;
}
else if (kd = = key.up) {
if (_root.snake_fangx!= 1) {
_ROOT.SNAKE_FANGX = 3;
}
}
}
Click here to download the source file