演示效果:(说明文章的后面有本文中涉及到的所有源码打包下载)
第二个下面有源码解析:
8月28日5:45分更新,有游戏地图,速度选择:
第一帧:AS:
Stage.showMenu=false;//不显菜单
stop();
//---------------start帧AS-------------------
var s_total = 500;//总蛇数
var w_col = 30;//行,宽数
var w_row = 30;
var w_w = 15;//每个方块象素
var myarray;//地图数组
for (var i = 0; i<s_total; i++) {
_root.attachMovie("snake", "s"+i, 2000+i);//载入蛇
}
var level = 1;//为扩展作装备,级别1
for (var i = 0; i<10; i++) { //加载食物
_root.attachMovie("food", "f"+i, 1000+i);
}
gotoAndPlay("gotono");
//------------gotono帧AS:----------------------------------
var s_len = 5;//蛇长
var snake_x = 1; //蛇的位置1行1列
var snake_y = 1;
var snake_fangx = 0;//蛇移动的方向值,向右为0,向下为1,向左为2,向上为3
var snake_tou = 0;//蛇头
for (var i = 0; i<s_total; i++) {//使蛇的MC跳到第一帧,
eval("s"+i).gotoAndStop(1);
}
s0._x = 15;//蛇头位置15象素.
s0._y = 15;
s0.gotoAndStop(2); //显示蛇头
s0.x = snake_x;//存蛇头的行列位置
s0.y = snake_y;
tm=random(3)+1;
gotoAndPlay("no"+tm);//随机跳入no1,no2,no3;
// no1帧:--------------------------------------
myarray=[[.......................]] //建地图数组30*30,见源文件
eatfood = 0; //吃掉的食物变量
guan = 1;//第一关
bzhuan(); //显示地图
bfood(); //显示食物
gotoAndPlay("s_move"); //跳到蛇移动的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 和no3 的帧AS中只是地图数组不同.
//smove帧:----------------------------------------------
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 帧:
_root.attachMovie("over","o",10000);
o._x=100;
o._y=100;