Flash Actionscript製作貪吃蛇簡陋版

來源:互聯網
上載者:User
全部代碼如下:

/************2005.5**************/
/*********貪吃蛇簡陋版************/
/***********寒冰(胡亂編寫)********/
var s_x = 0;
var s_y = 0;
var dir = [[-1, 0], [0, -1], [1, 0], [0, 1]];
//四個方向
var _dir;
var c_x = (Stage.width-256)/2;
var c_y = (Stage.height-256)/2;
var map;
var speed;
var level = 1;
var child_num;
var Child;
var snake_arr;
init();
function init() {
speed = 15;
child_num = 1;
snake_arr = [];
DrawOutRound();
//畫外框
DrawMap();
//畫地圖
SnakeRun();
//蛇移動
MakeWall();
//建立障礙物
MakeChild();
//建立食物(後來變成蛇身)
createTextField("info", getNextHighestDepth(), 30, 350, 400, 20);
createTextField("status", getNextHighestDepth(), 320, 370, 100, 20);
status.text = "狀態:playing";
info.text = "[用方向鍵控制方向,dead後請點滑鼠重新開始]____寒冰製造";
}
function DrawBox(name, color, w, h) {
//畫方塊
var _b = createEmptyMovieClip(name, getNextHighestDepth());
with (_b) {
lineStyle(0);
beginFill(color);
lineTo(w, 0);
lineTo(w, h);
lineTo(0, h);
endFill();
}
return _b;
}
function DrawOutRound() {
DrawBox("outround", 0xffffff, 400, 400);
}
function DrawMap() {
var _box = DrawBox("box", 0xffffff, 16, 16);
_box._visible = false;
map = [];
for (var i = 0; i<16; i++) {
map[i] = [];
for (var j = 0; j<16; j++) {
map[i][j] = 0;
_box.duplicateMovieClip("_map"+i+j, getNextHighestDepth(), {_x:c_x+j*16, _y:c_y+i*16});
}
}
//trace(map)
}
function MakeSnake() {
var Snake = DrawBox("snake", 0x000000, 16, 16);
snake_arr.push(snake);
Snake.x = s_x;
Snake.y = s_y;
//map[Snake.y][Snake.x] = 1;
Snake._x = c_x+s_x*16;
Snake._y = c_y+s_y*16;
return Snake;
}
function SnakeRun() {
var S = MakeSnake();
var i = 0;
_dir = dir[3];
SnakeControl();
S.onEnterFrame = function() {
if (++i>100/speed) {
ChildRun();
i = 0;
if (map[this.y+_dir[1]][this.x+_dir[0]] == 0) {
} else if (map[this.y+_dir[1]][this.x+_dir[0]] == 8) {
speed++;
map[this.y+_dir[1]][this.x+_dir[0]] == 0;
snake.swapDepths(getNextHighestDepth());
Child._name = Child._name.substr(1);
snake_arr.push(Child);
MakeChild();
} else {
status.text = "狀態:over";
delete this.onEnterFrame;
return;
}
this.x += _dir[0];
this.y += _dir[1];
this._x = c_x+16*this.x;
this._y = c_y+16*this.y;
}
};
//trace(S);
}
function SnakeControl() {
var obj = {};
obj.onKeyDown = function() {
var key = Key.getCode();
switch (key) {
case 37 :
_dir = dir[0];
break;
case 38 :
_dir = dir[1];
break;
case 39 :
_dir = dir[2];
break;
case 40 :
_dir = dir[3];
}
};
Key.addListener(obj);
}
function RndArr() {
//隨機亂數,為了取得隨機的障礙物的位置,不過這裡用隨機是為了偷懶,應該固定位置的.
var arr = [];
for (var i = 1; i<256; i++) {
arr[i-1] = i;
}
arr.sort(function () {
return random(2) ? 1 : -1;
});
return arr;
}
function MakeChild() {
var rgb = 0x0000ff;
Child = DrawBox("_child"+child_num, rgb, 16, 16);
Child._alpha = 0;
Child.onEnterFrame = function() {
if ((this._alpha += 5)>100) {
delete this.onEnterFrame;
}
};
do {
var x = random(16);
var y = random(16);
} while (map[y][x] != 0 or (x == 0 and y == 0));
Child.createTextField("txt", Child.getNextHighestDepth(), 0, 0, 16, 16);
//食物上的數字
Child.txt.textColor = "0xffffff";
Child.txt.text = child_num;
map[y][x] = 8;
Child._x = c_x+x*16;
Child._y = c_y+y*16;
child_num++;
}
function MakeWall() {
var _arr = RndArr();
for (var i = 0; i<level*10; i++) {
var x = _arr[i]%16;
var y = int(_arr[i]/16);
map[y][x] = 1;
var Wall = DrawBox("wall"+i, 0xff0000, 16, 16);
Wall._x = c_x+x*16;
Wall._y = c_y+y*16;
}
}
function ChildRun() {
for (var j in snake_arr) {
map[snake_arr[j].y][snake_arr[j].x] = 0;
}
for (var i = child_num; i>0; i--) {
var last_child = this["child"+(i-1)];
if (last_child == child0) {
last_child = snake;
}
this["child"+i].x = last_child.x;
this["child"+i].y = last_child.y;
map[this["child"+i].y][this["child"+i].x] = 1;
this["child"+i]._x = c_x+this["child"+i].x*16;
this["child"+i]._y = c_y+this["child"+i].y*16;
}
}
onMouseDown = function () {
// 滑鼠點擊進行重新整理,重新開始
for (var i in this) {
if (this[i].__proto__ == MovieClip.prototype) {
this[i].removeMovieClip();
} else if (this[i].__proto__ == TextField.prototype) {
this[i].removeTextField();
}
}
init();
}

//晚上邊玩邊弄的,居然寫了這麼多.bug多多哦,只是個大概樣子,唬唬人的: )

http://www.flash8.net/bbs/UploadFile/2005-5/20055465159343.swf




相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。