Translation of Snake Game Code

Source: Internet
Author: User

Today, I translated a piece of classic Snake code. After the translation, I feel that there are still many places that are not appropriate. I hope you can give me more advice on many things...

Original article:

// --- Flash MX Snake Game 1kb by strille. Version 2.2, 746 bytes
// --- Paste this code on frame 1 and set scene size to 512x280 and frame rate to 16
// --- The code is not written with speed in mind, only small file size. Not that it is slow
Createtextfield ("T", 1, 1,255,511, 32); // create a text field to write score and instructions
T. Text = "Snake Game/t-/tpress space"; // show start text
Beginfill (0 xeeeeee); linestyle (1); lineto (511, 0); lineto (511,256); lineto (0,256); endfill (); // draw background with border
Key. addlistener (t); // use an existing object as key listener (we don't waste bytes by creating a new object)
T. onkeydown = function () {// define an anonymous method to execute when a key is pressed
C = key. getcode ()-37; // get key code (C is a variable used "locally" several times)
If (! (C> 2) {// arrow keys pressed (C = 0, 1, 2 or 3)
If (C! = Q [0]) // only add to the queue if it is a new ction
Q. unshift (C );
Return; // Save the turn in the queue and exit Method
}
// Space or another key other than an arrow key has been pressed
X = 32*8 + 32*520; // snake start pos (left and right side of + can be viewed as X and Y coord
Q = []; // a queue to store key presses (so that X number of key presses during one frame are spread over X number of frames)
M = []; // create an array to store food POs and Snake
Createemptymovieclip ("S", W = 0); // create MC to store the snake and the food MC and reset snake counter (W)
E = 2 * (M [x-520] = 2 * (r = 1); // set erase counter (e) to 4, set current direction (r) to up (1) and set food on the position the snake will be over the first time to place food
Onenterframe = function () {// Main Function
C = Q. Pop (); // pick the next turn in the queue (may be undefined if queue is empty)
If (C % 2! = R % 2) // and check that it is not undefined and not a 180 degree turn (annoying to be able to turn into the snake with one key press)
If (C! = Undefined)
R = C; // change current ction to the new value
X + = [-1,-65, 1, 65] [r] * 8; // move the snake to a new X position (-1 = left, -65 = up, 1 = right, 65 = down)
If (M [x] = 1 or! (X % 520) or! (INT (x/520) % 33) {// game over if it is a snake block or outside the map on the next position
Delete onenterframe; // quit looping Main Function
T. Text + = "/tgame over! "; Return; // type game over text and Exit Main
}
With (S. createemptymovieclip (W, W) {// place a snake block (or food block the first loop)
Beginfill (255 <16); // red food color first time
If (W ++) // Blue Snake color the other times
Beginfill (0x555588 );
_ X = x % 520; _ y = int (x/520) * 8; // set snake block position
Lineto (-7, 0); lineto (-7,-7); lineto (0,-7); endfill (); // draw a square
}
M [x] + = 1; // set current POS as "occupied" by a snake Block
If (M [x] = 3) {// check if there is a food block on the new POs
T. TEXT = "score:" + (W-(E-= 5)-2) * 2; // delay erase counter with 5 (the snake will grow 5 blocks each time ), calculate and type score (+ 10 p for a food block)
Do {} while (M [c = (s [0]. _ x = 8 + random (64) * 8) + (s [0]. _ y = 8 + random (32) * 8) * 65]); // pick a free spot to place the food, save that number, place the food MC
M [c] = 2; // set the position picked on the line above to 2
}
If (e) {// if not food MC (s [0]) then erase last snake MC and entry in array m
C = s [E]; // get last MC
Delete M [C. _ x + 65 * C. _ y]; removemovieclip (c); // Delete the value in the array m and delete the MC
}
E ++; // increase erase snake counter
}
}

Translation:

// --- Create strille. Version 2.2 for Flash MX Snake Game (1 kb), totaling 746 bytes
// --- Copy the following code. The first frame of the main scenario has a size of 512x280, and the FPS is 16.
Createtextfield ("T", 1, 1,255,511, 32 );
// Create a text field to write score and instructions
// Create a text box for outputting scores and instructions
T. Text = "Snake Game/t-/tpress space ";
// Display the start information
Beginfill (0 xeeeeee); linestyle (1); lineto (511, 0); lineto (511,256); lineto (0,256); endfill ();
// Draw the background along the border
Key. addlistener (t );
// Use an existing object for keyboard frame listening (in this case, no need to create a new obejct, thus saving space)
T. onkeydown = function (){
// When the keyboard is pressed, execute the custom method.
C = key. getcode ()-37;
// Obtain the key ASCII code (variable C obtains the relative ASCII code each time)
If (! (C> 2 )){
// Direction key representation (C = 0, 1, 2 or 3)
If (C! = Q [0])
// Only store the new direction keys in the queue Q
Q. unshift (C );
Return;
// Save it in the queue and end the method (function)
}
// Spaces or other keys are different from the direction keys pressed
X = 32*8 + 32*520;
// Start coordinate of the Snake (left + Right: X and Y coordinates can be considered)
Q = [];
// It is used to store the queue of keys (So changing the X coordinate in a frame takes effect for the X coordinate in all frames)
M = [];
// Create an array to store the coordinates and snakes of food
Createemptymovieclip ("S", W = 0 );
// Create an empty video clip for storing snakes and food and reset the snake counter (W)
E = 2 * (M [x-520] = 2 * (r = 1 ));
// Set the erasure counter (e) to 4 and set the current direction (r) To up (1). When the snake passes through the food, immediately set the food location to the current setting.
Onenterframe = function (){
// Main Function
C = Q. Pop ();
// Extract the next round of transformation in the queue (when the queue is empty, the number of extracts is undefined)
If (C % 2! = R % 2)
// Check if it does not belong to undefined or 180-degree rotation (avoid changing the direction of the snake after any one of the following keys)
If (C! = Undefined)
R = C;
// Change the current direction to a new direction
X + = [-1,-65, 1, 65] [r] * 8;
// Move the snake to a new X position (-1 = left,-65 = up, 1 = right, 65 = down)
If (M [x] = 1 or! (X % 520) or! (INT (x/520) % 33 )){
// If the new position is on the snake or the boundary is exceeded, game over
Delete onenterframe;
// Exit the main cyclic Function
T. Text + = "/tgame over! "; Return;
// Output game over! And exit the main program
}
With (S. createemptymovieclip (W, W )){
// Place the snake body (for storing food during the first cycle)
Beginfill (255 <16 );
// Set the food to red first
If (W ++) // Blue Snake color the other times
Beginfill (0x555588 );
_ X = x % 520; _ y = int (x/520) * 8;
// Set the position of the snake body
Lineto (-7, 0); lineto (-7,-7); lineto (0,-7); endfill ();
// Draw a square
}
M [x] + = 1;
// Set the current position to "occupied" as the snake body
If (M [x] = 3 ){
// Check whether there is any food in the new location
T. Text = "score:" + (W-(E-= 5)-2) * 2;
// Latency erasure counter 5 (each time the snake increases by 5), calculate and output the score (a food plus 10 points)
Do {} while (M [c = (s [0]. _ x = 8 + random (64) * 8) + (s [0]. _ y = 8 + random (32) * 8) * 65]);
// Find an empty position placement point, store the value, and set the food video clip
M [c] = 2;
// Set the selected line to be greater than 2
}
If (e ){
// If not food MC (s [0]) then erase last snake MC and entry in array m
C = s [E];
// Obtain the last video clip
Delete M [C. _ x + 65 * C. _ y]; removemovieclip (C );
// Delete the value of this element in array m and delete the video clip
}
E ++;
// Add the snake's erasure counter to one
}
}

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.