贪食蛇游戏的源代码(原创作品)

      Flash 2005-3-1 8:6
共分三个文件:snake.as creatBox.as snake.fla
import creatBox;
class snake {
    static var map:Array;
    static var mapheight:Number;
    static var mapwidth:Number;
    static var dir:Array = new Array([1, 0], [0, 1], [-1, 0], [0, -1]);
    static var bodynum:Array = new Array();
    static var myobj = new Object();
    static var keyobj = new Object();
    static var body:MovieClip;
    static var head:MovieClip;
    static var food:MovieClip;
    static var d:Number = 0;
    static var eatNum:Number = 0;
    static var moveSpeed;
    static var myL:Object;
    public function snake() {
        init();
        eatNum = 0;
        moveSpeed = setInterval(movesnake, 1000);
    }
    public function init() {
        map = new Array();
        mapheight = 10;
        mapwidth = 10;
        map = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]];
        _root.createEmptyMovieClip("mapMovie", 0);
        _root.createEmptyMovieClip("body", 1);
        _root.createEmptyMovieClip("head", 2);
        _root.createEmptyMovieClip("food", 3);
        body = _root["body"];
        head = _root["head"];
        food = _root["food"];
        for (var i = 0; i<4; i++) {
            duplicateMovieClip(body, "body"+i, 1000+i);
            new creatBox(_root["body"+i], 0, 0, 0xff0000);
            bodynum.push(_root["body"+i]);
        }
        head._x = 100;
        head._y = 100;
        food._x = (random(7)+1)*20;
        food._y = (random(7)+1)*20;
        map[food._x/20][food._y/20] = 2;
        new creatBox(body, 0, 0, 0xff0000);
        new creatBox(head, 0, 0, 0x00ff00);
        new creatBox(food, 0, 0, 0x0000ff);
        for (var i = 0; i<mapheight; i++) {
            for (var j = 0; j<mapwidth; j++) {
                if (map[i][j] == 1) {
                    new creatBox(_root["mapMovie"], j, i, 0xcccccc);
                }
            }
        }
    }
    public function movesnake() {
        myL = new Object();
        myL.onKeyDown = function() {
            if (Key.isDown(Key.RIGHT)) {
                if (d == 3 || d == 1) {
                    d = 0;
                }
            } else if (Key.isDown(Key.UP)) {
                if (d == 0 || d == 2) {
                    d = 3;
                }
            } else if (Key.isDown(Key.LEFT)) {
                if (d == 3 || d == 1) {
                    d = 2;
                }
            } else if (Key.isDown(Key.DOWN)) {
                if (d == 0 || d == 2) {
                    d = 1;
                }
            }
        };
        Key.addListener(myL);
        var tempx = head._x;
        var tempy = head._y;
        head._x += dir[d][0]*20;
        head._y += dir[d][1]*20;
        _root["body0"]._x = tempx;
        _root["body0"]._y = tempy;
        for (var i = (bodynum.length-1); i>0; i--) {
            _root["body"+i]._x = _root["body"+(i-1)]._x;
            _root["body"+i]._y = _root["body"+(i-1)]._y;
            map[_root["body"+i]._x/20][_root["body"+i]._y/20] = 1;
            map[_root["body"+(bodynum.length-1)]._x/20][_root["body"+(bodynum.length-1)]._y/20] = 0;
        }
        if (map[head._x/20][head._y/20] == 2) {
            eatNum++;
            duplicateMovieClip(body, "body"+(bodynum.length), 1000+(bodynum.length));
            new creatBox(_root["body"+(bodynum.length)], 0, 0, 0xff0000);
            bodynum.push(_root["body"+(bodynum.length)]);
            food._x = (random(7)+1)*20;
            food._y = (random(7)+1)*20;
            map[head._x/20][head._y/20] = 0;
            map[food._x/20][food._y/20] = 2;
        }
        if (map[head._x/20][head._y/20] == 1) {
            clearInterval(moveSpeed);
            trace("Failed!!");
        }
    }
}
-----------------------------
class creatBox{
    public function creatBox(obj,X,Y,Colo) {
    var Xs=X*20
    var Ys=Y*20;
    with(obj){
        lineStyle(1,0x000000,100)
        beginFill(Colo,100);
        moveTo(Xs,Ys)
        lineTo(Xs+20,Ys)
        lineTo(Xs+20,Ys+20)
        lineTo(Xs,Ys+20)
        lineTo(Xs,Ys)
        endFill();
        }
    }
    }
-------------------------
var mobj=new snake()
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}