Eternal Nightmare
December 15, 2019, 03:40:39 am
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Breaking News: We Are Growing Fast. Keep Up The Good Work Guys
 
  Home Help Search Arcade Staff List Login Register  

scripting and explaining on how to make a simple snake game

Pages: [1]
  Print  
Author Topic: scripting and explaining on how to make a simple snake game  (Read 823 times)
danny3153
Starter
*

OWned +2/-0
Offline Offline

Gender: Male
My Mood: Sad:
Posts: 21



« on: August 14, 2009, 11:36:51 pm »

the script
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[i]._x == food._x && aPieceList[i]._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[i].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[i]._x = aPieceList[i - 1]._x;
                        aPieceList[i]._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[i]._x && aPieceList[0]._y == aPieceList[i]._y)
                        {
                                gameOver();
                        }
                }
        };
}
Read more: http://www.webdesign.org/web/flash-&-swish/flash-tutorials/snake-game.11079.html#ixzz0OCNfWv0Y


the script with explanation

I started by defining the variables I would use later in the game.

Code:
var unit = 15;  //The size of each square
var uwh = 20;  //The number of squares across and down.
var canMove = false;  //The snake will not be moving until there is a mouse click to start the game.
var dir = 2;  //The snake will start by moving downwards, UP=0, LEFT=1, DOWN=2, RIGHT=3
var score = 0;  //Your initial score is 0.
aPieceList = new Array();  //Creates an empty Array to store all of the movie clips that make up the snake.
Now we will define the mouseListener Object. This waits for a mouse click, when there is a mouse click the onMouseDown function is called.

mouseListener = new Object();
mouseListener.onMouseDown = function()
{
        if (!canMove)  //This prevents you from clicking in the middle of the game and starting a new game.  (If you are not moving, start the game).
        {
                canMove = true;  //Let's you move now.
                startGame();  //Calls the start game function explained later.
        }
};
Mouse.addListener(mouseListener);
Now we define the keyListener. The onKeyDown function activates when a key on the keyboard is pressed.

k = new Object();
k.onKeyDown = function()
{
        var k = Key.getCode();  //Gets the ASCII code of the key that is pressed.
        if (k == Key.UP & & dir != 2 & & canMove)  //If they pressed UP and you are not currently moving down (as to not hit yourself) and you are able to move.
        {
                dir = 0;  //sets your direction to Up
                canMove = false;  //sets the ability to change direction to false to prevent you from changing directions before the next frame causing some bugs.
        }
        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);
Now we have the addPiece function. When this function is called a piece is added to the end of the snake to increase its length.

function addPiece()
{
        var p = this.attachMovie("piece", "piece" + aPieceList.length, aPieceList.length);  //attaches a piece to the stage
        p._x = aPieceList[aPieceList.length - 1]._x;  //sets the x coordinate of the piece attached to the one in front of it.
        p._y = aPieceList[aPieceList.length - 1]._y; //sets the y coordinate of the piece attached to the one in front of it.
        aPieceList.push(p);  //adds the piece to the array of pieces.
}
Now we define the moveFood function which moves the food when the snake touches it.

function moveFood()
{
        var moveIt = true;
        while (moveIt)  //keeps moving the food until it is not touching the snake.
        {
                food._x = Math.floor(Math.random() * uwh) * unit;  //chooses a random x value for the food.
                food._y = Math.floor(Math.random() * uwh) * unit;  //chooses a random y value for the food.
                moveIt = false;
                for (var i = 0; i < aPieceList.length; i++)
                {
                        if (aPieceList[i]._x == food._x & & aPieceList[i]._y == food._y)  //Checks if you are moving the food to a spot the snake already occupies, if you are then try again.
                        {
                                moveIt = true;
                        }
                }
        }
}
Now we will define the gameOver function which is called when your snake hits itself.

function gameOver()
{
        delete this.onEnterFrame;  //deletes the onEnterFrame which was causing the snake to move every frame.
        tScore.text = "You Lose.  Score: " + score;  //Displays your score in the text field.
        canMove = false;  //Sets it so you cannot move anymore.
}
The startGame function is where the most important parts of the ActionScript are. This includes the function that moves the snake every frame and checks if it is hitting itself, also adding the snake initially to the stage.

function startGame()
{
        for (var i = aPieceList.length - 1; i >= 0; i--)  //deletes old snake if one exists.
        {
                aPieceList[i].removeMovieClip();
                aPieceList.pop();
        }
        score = 0;  //sets score to 0.
        var p = this.attachMovie("piece", "piece" + aPieceList.length, aPieceList.length);  //Attaches the first piece of the snake.
        aPieceList.push(p);  //Adds that piece to the array of pieces.
        p._x = 10 * unit;  //Centers the snake horizontally.
        p._y = 10 * unit;  //Centers the snake vertically.
        var food = this.attachMovie("food", "food", -1);  //Attaches the food to the stage.
        moveFood();  //Moves the food to a random location on the Stage.
        var startingLength = 3;  //Sets the initial starting length of the snake to 3.
        for (var i = 1; i < startingLength; i++)  //Creates the rest of the snake.
        {
                addPiece();
        }
        this.onEnterFrame = function()  //Declares a function that is called on every frame.
        {
                canMove = true;  //Sets the snake's ability to move to true.
                tScore.text = score;  //Updates the Score text box with your score.
                for (var i = aPieceList.length - 1; i > 0; i--)  //Moves the snake (each piece moves to the piece in front of its location.)
                {
                        aPieceList[i]._x = aPieceList[i - 1]._x;
                        aPieceList[i]._y = aPieceList[i - 1]._y;
                }
                if (dir == 0)  //If you're facing up then move up.
                {
                        aPieceList[0]._y -= unit;
                }
                else if (dir == 1)  //If you're facing left then move left
                {
                        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)  //If you are lower than the allowed area, move your front snake piece to the top.
                {
                        aPieceList[0]._y = 0;
                }
                else if (aPieceList[0]._y / unit == -1)  //If you are higher than the allowed area, move your front snake piece to the right side.
                {
                        aPieceList[0]._y = 19 * unit;
                }
                else if (aPieceList[0]._x / unit == -1)  //If you are to the left of the allowed area.
                {
                        aPieceList[0]._x = 19 * unit;
                }
                else if (aPieceList[0]._x / unit == 20)  //If you are to the right of the allowed area.
                {
                        aPieceList[0]._x = 0;
                }
                if (aPieceList[0]._x == food._x & & aPieceList[0]._y == food._y)  //If your front piece is touching food then move the food and add points to the score, also add a piece to the end of the snake.
                {
                        score += 10 * aPieceList.length / 2;
                        moveFood();
                        addPiece();
                }
                for (var i = 1; i < aPieceList.length; i++)  //If your front piece is touching any part of your body, game over.
                {
                        if (aPieceList[0]._x == aPieceList[i]._x & & aPieceList[0]._y == aPieceList[i]._y)
                        {
                                gameOver();
                        }
                }
        };
}

I hope this explanation helped you understand how the ActionScript in the Snake Game tutorial worked. If you are unsure on what a function or field does, use the ActionScript dictionary built into flash.


Read more: http://www.webdesign.org/web/flash-&-swish/flash-tutorials/snake-game-actionscript-explained.11078.html#ixzz0OCOFDQHu
« Last Edit: August 15, 2009, 12:24:13 pm by Nick » Report Spam   Logged

Nick
Administrator
Devotee
*

OWned +28/-66
Offline Offline

Gender: Male
My Mood: Tired:
Posts: 729


Owner


WWW
« Reply #1 on: August 14, 2009, 11:43:02 pm »

Great tutorial danny

i just put that script in a code box to save people scrolling Cheesy
Report Spam   Logged




Crywolfz
Coding Team
Enthusiast
*

OWned +4/-2
Offline Offline

Gender: Male
My Mood: Crappy:
Posts: 134



« Reply #2 on: August 18, 2009, 12:22:54 am »

wait what do i do
Report Spam   Logged


Nick
Administrator
Devotee
*

OWned +28/-66
Offline Offline

Gender: Male
My Mood: Tired:
Posts: 729


Owner


WWW
« Reply #3 on: August 18, 2009, 01:08:02 am »

you put these scripts in the action for the frame

you only need 1 frame for the whole snake game...
Report Spam   Logged




Crywolfz
Coding Team
Enthusiast
*

OWned +4/-2
Offline Offline

Gender: Male
My Mood: Crappy:
Posts: 134



« Reply #4 on: August 22, 2009, 08:54:06 pm »

o ok
Report Spam   Logged


deadrok
Starter
*

OWned +0/-0
Offline Offline

Gender: Male
My Mood: Cool:
Posts: 22

Hi people!


« Reply #5 on: October 16, 2009, 07:28:43 am »

That's cool, might try it sometimes to see if I'm smart enough to actually understand what you said here.
Report Spam   Logged

*Nice to meet everyone!*

*This is a nice forum!*
dave1280
Newbie
*

OWned +0/-0
Offline Offline

Posts: 5


« Reply #6 on: February 24, 2010, 05:14:47 am »

Thanks for the awesome and interesting information and its been such a useful information.... thanks for sharing.......  Smiley      Smiley


_____________________
skate shoes
Vans Shoes
« Last Edit: February 24, 2010, 05:17:11 am by dave1280 » Report Spam   Logged
paolo4930
Starter
*

OWned +0/-0
Offline Offline

Posts: 18


« Reply #7 on: March 16, 2010, 06:36:07 am »

Hai friends, thank you for your information.




____________________
Office Furniture
Report Spam   Logged
trenton2345
Enthusiast
*

OWned +0/-0
Offline Offline

Posts: 104


« Reply #8 on: April 12, 2011, 03:58:01 am »

Thanks for the awesome and interesting information and its been such a useful information.... thanks for sharing.
______________________________________
Roller Blinds
Blinds
Report Spam   Logged
mannhurst.hurst6
Starter
*

OWned +0/-0
Offline Offline

My Mood: Aggressive:
Posts: 28


« Reply #9 on: May 20, 2013, 05:11:37 pm »

thanks for sharing the script. today i use this script and make a game ...................................




« Last Edit: October 09, 2013, 06:46:27 pm by mannhurst.hurst6 » Report Spam   Logged

Pages: [1]
  Print  
 
Jump to:  

Powered by EzPortal
Bookmark this site! | Upgrade This Forum
SMF For Free - Create your own Forum

Powered by SMF | SMF © 2016, Simple Machines
Privacy Policy