Flash game making: a beginner's tutorial on snake-gluttonous classic games

Source: Internet
Author: User
Tags rar touch
Tutorial This is the first game I wrote. The function is not perfect yet. Only the most basic function. Let's take a rough look first.
Effect and source file: greedy snake. rar

/uploadpic/2007-7/20077814321316.swf


In fact, the whole game to solve a number of problems.
1. How do I get snakes to move in the direction of the keyboard?
2. How to make food randomly distributed?
3. How to let the snake touch the food when the food disappears snake body lengthened?
4. How to move the box outside or touch the snake body is game over. Let's start with the first question.
。 1. How do I get snakes to move in the direction of the keyboard? Simplify the complex issues first. As far as an MC is concerned. How do we move through the keyboard?
New MC.. And will be named Snakehead (Snake Head) and then in a new layer. Named as layer ...
Write function Onenterframe () {//on the frame to transfer the following code at the frame frequency
if (Key.isdown (65)) {//If the keyboard presses down, let snakehead name the MCX axis coordinate minus 10
snakehead._x-=10; } if (Key.isdown) {snakehead._x+=10} if (Key.isdown (87))
{snakehead._y-=10;}  if (Key.isdown () {snakehead._y+=10}}} Here are some of the main points of knowledge: function Onenterframe () {What does that mean? What he means is how much code a second of flash passes through the number of frames he eats in a second. We used him to detect whether the button on the keyboard was pressed ...
Key.isdown (68) What does it mean? What he means is that key represents the meaning of the keyboard. Isdown means. The keys on the keyboard are pressed ... (68) What is the key? Here's 68 is ASCII keying code value is not clear friends can check the Flash help ...
Through the above talk should be able to understand how to use the keyboard to control the movement of film cutting it.
Copied out of the body of the snake. It is not necessary to detect whether the body of the snake touches the food when it detects the touch. Only detect whether the snake head and food touch-like.
Let me explain this statement.
if (_root.snakehead.hittest (_root["Mcfood" +j)) {
To determine if the snake meets the food.
n++;
Removemovieclip (_root["Mcfood" +j));
Zynum + 10;
j + +;
Mcfoodadd ();
Translate it into Chinese for easy understanding.
if (snakehead movie cropping for the main time frame intersects with a movie crop that is named "Mcfood" +j on the main time frame) {
N=n+1 Deletes a movie cut on the main time frame named "Mcfood" +J;
The value of the variable is Zynum +10.
j=j+1; Call function Mcfoodadd (); The purpose of calling this function is to randomly appear in the second food
}
Tell me about how to distribute food randomly. When it comes to random distributions. The first idea that everyone thought of was a random function. Although Falsh now does not recommend using random, the Math.random () of the math class is recommended. Because the latter is randomly more accurate ... But what I need is a whole value. So use random. The following is the code for this section.
function Mcfoodadd () {
Food random appearance function
_root.attachmovie ("Mcfood", "Mcfood" +j, 100+j);
Load the Mcfood to the main scene in the library. Name it "Mcfood" +j. Set depth to 100+j;
_root["Mcfood" +j]._x = random (50) *10+31;
Set the x coordinate of the movie
_root["Mcfood" +j]._y = random (50) *10+31;
Set the y-coordinate of a movie
Take a moment to explain all the statements in detail. I hope to help you. Original works hope everyone support AH!!!!
function Onenterframe () {
if (tm = = 0) {//Let the food just appear once in the beginning. TM is used to determine if there is food in the scene.
Mcfoodadd ()//If not, call the thing function. Randomly produce a food
TM = 1;//TM to 1 indicates that the scene is already loaded with food.
}
function Mcfoodadd () {
Food random appearance function
_root.attachmovie ("Mcfood", "Mcfood" +j, 100+j);
A movie crop with the connection name "Mcfood" inside the copy symbol. Name and depth of each
_root["Mcfood" +j]._x = random (50) *10+31;//new resulting x-coordinate
_root["Mcfood" +j]._y = random (50) *10+31;//new resulting y-coordinate
}
i++;
if (i>=n) {
To judge if a snake is long enough
i = 1;
}
_root["Snakehead" +i]._alpha-= 10;//Set the newly created snake body transparency smaller
For (k=0 k<=n; k++) {//used to traverse the snake head if it touches the snake body
if (Snakehead.hittest (_root["snakehead" +k)) {//If you encounter it over
gotoAndStop ("Gameover");
}
}
if (snakehead._x<28) {//whether the boundary is reached
gotoAndStop ("Gameover");
}
if (snakehead._x>520) {//whether the boundary is reached
gotoAndStop ("Gameover");
}
if (snakehead._y>520) {//whether the boundary is reached
gotoAndStop ("Gameover");
}
if (snakehead._y<28) {//whether the boundary is reached
gotoAndStop ("Gameover");
}
if (Key.isdown (65)) {//keyboard key pressed
if (XM!= 10) {//When the snake moves to the right, it cannot move in the opposite direction.
XM =-10;
ym = 0;
}
}
if (Key.isdown (68)) {//keyboard key pressed
if (XM!=-10) {//When the snake moves to the left, it cannot move in the opposite direction.
XM = 10;
ym = 0;
}
}
if (Key.isdown (87)) {//keyboard key pressed
if (YM!= 10) {When the snake moves downward, it cannot move in the opposite direction.
XM = 0;
YM =-10;
}
}
if (Key.isdown (83)) {//keyboard key pressed
if (YM!=-10) {When the snake moves upwards, it cannot move in the opposite direction.
XM = 0;
YM = 10;
}
}
if (_root.snakehead.hittest (_root["Mcfood" +j)) {
To determine if the snake meets the food.
n++;
Removemovieclip (_root["Mcfood" +j))/To remove food
Zynum + = 10;//at the beginning of the work
j + +;
Mcfoodadd ();
}
This refers to the XM. And YM is the speed at which the snake moves at the beginning.
Xm=10;ym=0 at the initial time;
function Onenterframe () {//repeated execution
snakehead._x+=xm;//The x coordinate of the snake at a frame rate of 10 images at a time.
This is the case. The snake moves like the right.
In this case, when the button is pressed down. Change XM. Or the value of a YM is quite the same as changing the direction of the snake's motion.
The purpose of the preceding judgment is to keep the snake from moving in the opposite direction. Because if you move the snake head in the opposite direction, you bump into the snake body.
This does not conform to the reality of the situation.
I hope you can understand.
Saturday Sunday Rest in Add play shaft sword did not come up ^_^ embarrassed.
I hope you can understand ...
There are two source files attached below. One is to control the direction of the control without a button.
You compare.
keyboard control move. RAR
auto move. RAR

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.