Ideas:
1. First specify the Movement area width (stagew) and height of the snake (Stageh)
2. Increase the keyboard to listen, get the keying code, if the key and the previous key is reversed will not change
3. In the initialization, please note: The size of the snake head, snake body, food is 7 pixels
4. Each moving step (move) is 8 pixels in order to achieve the body of a lattice of the effect
5. After eating food, the length of the snake body is increased by 5 units, copying 5 bodies
6. Crawler forward: Starting from the tail, the last one to follow the position of the previous one, the first following the snake head
Icon:
1. The whole idea
Snaketree.jpg
2. Snake head, snake body, food size are 7 pixels, step 8 pixels
Snaketree.jpg
Step 1:
Snaketree.jpg
* Draw a square to do (on behalf of the snake head, snake body, food), the size of the random, Save as a movie clip
Registration point on the left (0,0), Connection-> Export-> Identifier "block"
*fps is about 18.
Step 2:
Add as Code
========== map out the area of snake activity ==========
var stagew:number = 447;
var stageh:number = 255;
Beginfill (0XEEEEEE);
MoveTo (0, 0);
LineTo (Stagew, 0);
LineTo (Stagew, Stageh);
LineTo (0, Stageh);
Endfill ();
========== Create a text box to display information ==========
_root.createtextfield ("txt",-100, 1, 255, 400, 30);
Txt.text = "The greedy Snake game presses ' the direction key ' to begin";
========== set up keyboard listening ==========
Key.addlistener (TXT);
Use an existing object TXT for listening, eliminates the new listener object, save space
var Nowkey:number;
Nowkey global variable to store the key that is currently pressed
Txt.onkeydown = function () {
var c = Key.getcode ()-37;
C= left 0 key 1 right key 2 keys 3
if ((c<4) && (Math.Abs (nowkey-c)!= 2)) {
Nowkey = C;
If the snake is moving to the left, then he can't move to the right, and if it's moving up, it can't move down.
The difference between left (0) and Right (2) is 2, upper (1) and lower (3) are 2, so if they are 2, the key is invalid
}
};
========== Below is a series of initialization ==========
_root.attachmovie ("Block", "Head",-1, {_width:7, _height:7});
First create the snake head, which is 7 pixels in size
var move:number = 8;
Each move is 1 pixels more than a snake's head, which makes it possible to make a grid.
head._x = Head._y=20*move;
Start position of the snake head (multiple of the moving distance)
var body:number = 0;
The initial length of the snake body
var Gameover:boolean = false;
Flag to see if the game is over
========== the function of creating food ==========
Createfood ();
function Createfood () {
_root.attachmovie ("block", "food", 0, {_width:7, _height:7});
The depth of the food is always 0, which eliminates the removal of the eaten food.
New Color (food). Setrgb (0xff0000);
The food is red block
var colum:number = Stagew/move;
var row:number = Stageh/move;
Colum is the total number of columns, row is the number of rows
food._x = Random (Colum) *move;
food._y = Random (Row) *move;
The place where food appears randomly (a multiple of the distance moved)
}
========== Main function ==========
_root.onenterframe = function () {
Switch (Nowkey) {
Determines the direction of movement of the snake head according to the direction key value
Case 0:
Head._x-= move;
Break
Case 1:
Head._y-= move;
Break
Case 2:
Head._x + = move;
Break
Case 3:
Head._y + = move;
}
if (head._x<0 | | head._x>stagew | | head._y<0 | | head._y>stageh) {
Gameover = true;
The game ends with the head out of bounds.
}
if (head.hittest (food)) {
Createfood ();
Head contact with food after the call for new food
for (var i = 1; i<=5; i++) {
_root.attachmovie ("block", "body" + (body+i), Body+i, {_width:7, _height:7, _x:-50, _y:-50});
At the same time, the snake's length increased by 5 units, initial size 7, the initial position in the scene outside
}
BODY = + 5;
Txt.text = "Score" +body;
}
for (var i = body; i>0; i--) {
if (Head.hittest (this["Body" +i])) {
Gameover = true;
Determine if the snake is in contact with the head, if the game is over
}
if (i = = 1) {
this["Body" +i]._x = head._x;
this["Body" +i]._y = head._y;
} else {
this["Body" +i]._x = this["Body" + (i-1)]._x;
this["Body" +i]._y = this["Body" + (i-1)]._y;
}
This cycle moves one unit forward from tail to head, coinciding with the nearest body of the snake head and the head of the snake.
}
if (Gameover) {
Sign bit Gameover is true, show game end, delete onenterframe, move stop
Txt.text = "Game Over";
Delete This.onenterframe;
}
};
Flash Charge 1:mc.attachmovie ()
Role: Copy a movie clip from a library
Grammar:
Mc.attachmovie (id:string, name:string, Depth:number, [Initobject:object])
Mc:movieclip-for the movie's parent movie
Id:string-The link name of the library component;
Name:string-The unique name of the movie clip instance;
Depth:number-Specifies the depth level of the location where the SWF file is placed;
Initobject:object [Optional]-object of the properties of the movie clip.
For example, there is a component identifier in the library that is "block"
_root.createemptymovieclip ("MC",-1);
for (var i = 0; i<10; i++) {
Mc.attachmovie ("block", "B" +i, I, {_width:30, _height:30, _x:i*30, _y:10});
}
Trace (mc["B" +0]._parent);
The parent movie for the copied movie clip is MC
Trace (mc["B" +0]._parent._parent);
The parent movie for the copied movie clip's parent movie is _root
Flash Charge 2:mc.duplicatemovieclip ()
Role: Copy movie clips on the stage
Grammar:
Mc.duplicatemovieclip (name:string, Depth:number, [Initobject:object])
Mc:movieclip-objects that are copied
Name:string-The unique name of the movie clip instance;
Depth:number-Specifies the depth level of the location where the SWF file is placed;
Initobject:object [Optional]-object of the properties of the movie clip.
For example, there is a movie clip instance named "MC" on the stage
for (var i = 0; i<10; i++) {
Mc.duplicatemovieclip ("M" +i, I, {_width:30, _height:30, _x:i*30, _y:10});
}
Flash Charge 3: Deletion of movie clips
Note: To distinguish between copied movie clips and non-copied movie clips when you delete them
Use Removemovieclip for a copied movie clip (target:object)
Use Unloadmovie for movie clips that are not copied (target:object)
Copied movie clips: refers to Duplicatemovieclip (), Movieclip.duplicatemovieclip (), Movieclip.createemptymovieclip (), or Movieclip.attachmovie () Creates a movie clip instance.
Non-copied movie clips: A movie clip that is present on the stage from the beginning, that is, a movie that is not copied with code.