Flash ActionScript make a simple version of the greedy snake

Source: Internet
Author: User
All the code is as follows:

/************2005.5**************/
/********* Greedy Snake simple version ************/
/*********** Ice (random writing) ********/
var s_x = 0;
var s_y = 0;
var dir = [[-1, 0], [0,-1], [1, 0], [0, 1]];
Four Directions
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 ();
Draw the outer frame
Drawmap ();
Draw a Map
Snakerun ();
Snake move
Makewall ();
Creating obstacles
Makechild ();
Create food (later become a snake body)
Createtextfield ("Info", getnexthighestdepth (), 30, 350, 400, 20);
Createtextfield ("Status", Getnexthighestdepth (), 320, 370, 100, 20);
Status.text = "state: playing";
Info.text = "[with the direction key control direction, after dead, please click the mouse to restart]____ ice manufacturing";
}
function Drawbox (name, Color, W, h) {
Draw Squares
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 = "state: 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 () {
Random chaos number, in order to obtain the location of random obstacles, but here with random is to lazy, should be fixed position.
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);
The number on the food
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 () {
Click on the mouse to refresh and start again
for (var I and this) {
if (this[i].__proto__ = = Movieclip.prototype) {
This[i].removemovieclip ();
else if (this[i].__proto__ = = Textfield.prototype) {
This[i].removetextfield ();
}
}
Init ();
}

At night while playing, incredibly write so much. The bug is a lot oh, just a ballpark figure, bluffing:

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




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.