·您的位置: 首页 » 资源教程 » 创意设计 » Flash » Flash Actionscript制作贪吃蛇简陋版

Flash Actionscript制作贪吃蛇简陋版

类别: Flash教程  评论数:0 总得分:0
全部代码如下:
/************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

« Full Screen »






-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1