当前位置:文档之家› FLASH游戏之贪吃蛇游戏源代码

FLASH游戏之贪吃蛇游戏源代码

scorecommand("allowscale", "false");
fscommand("showmenu", "false");
loadedbytes=0;
total=_root.getBytesTotal();
loadedbytes=_root.getBytesLoaded()
if (loadedbytes==total){nextScene();}
else{bfb=int((loadedbytes/total*100))+"%";}
stop();
fscommand("allowscale", "false");
fscommand("showmenu", "false");
music=new Sound()
music.attachSound("music");
music.stop()
music.start(0,10000)
musicstart=true
onClipEvent(keyDown){
temp=Key.getCode();
if(!keyon){;}
else if(game=="stop" and (temp==189 or temp==109)){ if (speed>1) {
speed--;
rate = speed;}}
else if (game=="stop" and (temp==107 or temp==187)) { if (speed<maxspeed) {
speed ++;
rate =speed;}
}else if(game<>"start"){enter();
}else if(temp==Key.ENTER){
enter();
}else if (temp == Key.DOWN and y1>=0) {
x1=0;
y1 = 1;
walk();
timestart()
}else if (temp == Key.UP and y1<=0) {
x1=0;
y1 = -1;
walk();
timestart()
}else if (temp == Key.LEFT and x1<=0) {
y1=0;
x1 = -1;
walk();
timestart()
}else if (temp == Key.RIGHT and x1>=0) {
y1=0;
x1 = 1;
walk();
timestart()
}
}
walks=new Sound();
walks.attachSound("walks");
die=new Sound();
die.attachSound("die");
eat=new Sound();
eat.attachSound("eat");
snakebody="snake1";
game="stop";
keyon=true;
startlong = 7;
maxspeed=10;
speed = 4;
time=new Array(0,1000,800,600,400,250,175,100,75,50,30) rate = speed;
function walk() {
x = x+x1;
y = y+y1;
if (x<0 or x>19 or y<0 or y>19) {
gotoAndStop("over");
} else if (map[x][y] == 2) {
eat.start();
body(x, y);
egg();
long++;
score=score+speed;
} else {
map[snake[0]._x/8][snake[0]._y/8] = 0;
if (map[x][y] == 1) {
gotoAndStop("over");
} else {
snake.push(snake[0]);
snake.shift(0);
snake[long-1]._x = x*8;
snake[long-1]._y = y*8;
map[x][y] = 1;
walks.start();
}
}
}
function egg() {
var x2, y2;
x2 = random(20);
y2 = random(20);
while (map[x2][y2] == 1) {
x2 = random(20);
y2 = random(20);
}
map[x2][y2] = 2;
egg2._x = x2*8;
egg2._y = y2*8;
}
function body(x3, y3) {
window.attachMovie(snakebody, "snake"+long,long);
temp=eval("window.snake"+long);
temp._x=x3*8;
temp._y=y3*8;
snake.push(temp);
map[x3][y3] = 1;
}
function enter(){
if(game=="stop"){
stopallsound()
gotoandplay("start");
}else if(game=="pause"){
game="start";
gotoandplay("play");
}else if(game=="start"){
game="pause";
gotoandplay("pause")
}
}
function clear(){
if (long>startlong) {
for (i=0; i<=long; i++) {
snake[i].removeMovieClip();
}
}
}
function timestart(){
clearInterval(Interval);
Interval=setInterval(walk,time[speed],x1,y1); }
clear();
score=0;
x = startlong-1;
y = 0;
x1 = 1;
y1 = 0;
map = new Array();
for (i=0; i<20; i++) {
map[i] = new Array(20);
}
snake = new Array();
x = startlong-1;
y = 0;
for (var long = 0; long<startlong; long++) { body(long, 0);
}
egg();
game="start";
运行结果如图:。

相关主题