Making snake game in flash

Status
Not open for further replies.

kauzy

Galvanizer
1.Start by creating a 300x350 document in Flash.

2.In the Property Inspector set the Frame Rate to 12. (Set it higher for a faster snake game)

3.Select the Rectangle Tool and draw a square on the stage.

4.Use the Selection Tool to double click on the square and in the Property Inspector set the following:
W: 300
H: 300
X: 0
Y: 0

5.Change the colors how you want, this will be the background of your snake game.

snakeGame1.gif


6.Select the Text Tool and in the Property Inspector set the Text Type to Dynamic.

7.Draw a text box in the white area of the stage, this is where your score will be displayed.

8.Inside the text box write "Click to start" and change the font face and color however you like.

9.With the text box selected, in the Property Inspector set the instance name to "tScore".

snakeGame2.gif


10.Now press Ctrl+F8 and type "piece" for the name and set the type to Movie Clip. Click on Advanced and check the box by "Export for ActionScript", the identifier should automatically be set to "piece", if not, set it as that and press OK.

snakeGame3.gif


11.Select the Rectangle Tool and draw a square on the stage.

12.Use the Selection Tool to double click on the square and in the Property 13.Inspector set the following:W: 15H: 15X: 0Y: 0This will be the piece that makes up the body of your snake. If you do not want your snake to be made of squares you can change it here, but keep the width and height to 15.

14.Return to Scene 1 and press Ctrl+F8 again and this time type "food" for the name and set the type to Movie Clip. Click on Advanced and check the box by "Export for ActionScript", the identifier should automatically be set to "food", if not, set it as that and press OK.

15.Select the Oval Tool and draw a circle on the stage.
Use the Selection Tool to double click on the circle and in the Property Inspector set the following:W: 15H: 15X: 0Y: 0This will be the food that your snake eats to gain length. If you do not want your food to be made of circles you can change it here, but keep the width and height to 15.

16.Return to Scene 1 and click on frame 1 and press F9 to open the Actions Panel and add the following code.

var unit = 15;var uwh = 20;var canMove = false;var dir = 2;var score = 0;aPieceList = new Array();mouseListener = new Object();mouseListener.onMouseDown = function(){ if (!canMove) { canMove = true; startGame(); }};Mouse.addListener(mouseListener);k = new Object();k.onKeyDown = function(){ var k = Key.getCode(); if (k == Key.UP && dir != 2 && canMove) { dir = 0; canMove = false; } else if (k == Key.LEFT && dir != 3 && canMove) { dir = 1; canMove = false; } else if (k == Key.DOWN && dir != 0 && canMove) { dir = 2; canMove = false; } else if (k == Key.RIGHT && dir != 1 && canMove) { dir = 3; canMove = false; }};Key.addListener(k);function addPiece(){ var p = this.attachMovie("piece", "piece" + aPieceList.length, aPieceList.length); p._x = aPieceList[aPieceList.length - 1]._x; p._y = aPieceList[aPieceList.length - 1]._y; aPieceList.push(p);}function moveFood(){ var moveIt = true; while (moveIt) { food._x = Math.floor(Math.random() * uwh) * unit; food._y = Math.floor(Math.random() * uwh) * unit; moveIt = false; for (var i = 0; i < aPieceList.length; i++) { if (aPieceList._x == food._x && aPieceList._y == food._y) { moveIt = true; } } }}function gameOver(){ delete this.onEnterFrame; tScore.text = "You Lose. Score: " + score; canMove = false;}function startGame(){ for (var i = aPieceList.length - 1; i >= 0; i--) { aPieceList.removeMovieClip(); aPieceList.pop(); } score = 0; var p = this.attachMovie("piece", "piece" + aPieceList.length, aPieceList.length); aPieceList.push(p); p._x = 10 * unit; p._y = 10 * unit; var food = this.attachMovie("food", "food", -1); var c = 0; moveFood(); var startingLength = 3; for (var i = 1; i < startingLength; i++) { addPiece(); } this.onEnterFrame = function() { canMove = true; tScore.text = score; for (var i = aPieceList.length - 1; i > 0; i--) { aPieceList._x = aPieceList[i - 1]._x; aPieceList._y = aPieceList[i - 1]._y; } if (dir == 0) { aPieceList[0]._y -= unit; } else if (dir == 1) { aPieceList[0]._x -= unit; } else if (dir == 2) { aPieceList[0]._y += unit; } else if (dir == 3) { aPieceList[0]._x += unit; } if (aPieceList[0]._y / unit == 20) { aPieceList[0]._y = 0; } else if (aPieceList[0]._y / unit == -1) { aPieceList[0]._y = 19 * unit; } else if (aPieceList[0]._x / unit == -1) { aPieceList[0]._x = 19 * unit; } else if (aPieceList[0]._x / unit == 20) { aPieceList[0]._x = 0; } if (aPieceList[0]._x == food._x && aPieceList[0]._y == food._y) { score += 10 * aPieceList.length / 2; moveFood(); addPiece(); } for (var i = 1; i < aPieceList.length; i++) { if (aPieceList[0]._x == aPieceList._x && aPieceList[0]._y == aPieceList._y) { gameOver(); } } };}


17.You may now test your movie

thnx

Guys... plz do rep me if u like my posts...
 
Status
Not open for further replies.