Flash貪吃蛇游戲AS代碼翻譯
互聯(lián)網(wǎng) 發(fā)布時(shí)間:2008-10-06 01:25:13 作者:佚名
我要評論
今天翻譯了一段經(jīng)典的貪吃蛇代碼,譯后感覺還有很多地方不太妥當(dāng),很多不妥的地方希望大家多指教
原文:
//--- 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 R
今天翻譯了一段經(jīng)典的貪吃蛇代碼,譯后感覺還有很多地方不太妥當(dāng),很多不妥的地方希望大家多指教
原文:
//--- 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(0xeeeeee); 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 direction
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 direction 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 !(xR0) 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 = xR0; _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 ( 10p 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
}
}
翻譯:
//--- Flash MX 貪吃蛇游戲(1Kb) 制作Strille. 版本 2.2, 共計(jì) 746 字節(jié)
//--- 復(fù)制以下代碼在主場景的第一幀場景大小為 512x280 , FPS 16
createTextField("t", 1, 1, 255, 511, 32);
// create a text field to write score and instructions
// 創(chuàng)建一個(gè)文本框用于輸出成績和指示
t.text = "Snake Game\t-\tPress SPACE";
// 顯示開始信息
beginFill(0xeeeeee); lineStyle(1); lineTo(511, 0); lineTo(511, 256); lineTo(0, 256); endFill();
// 沿邊框繪制背景
Key.addListener(t);
// 使用一個(gè)已存在的Object 作鍵盤幀聽 (就樣就不用再創(chuàng)建新Obejct,從而節(jié)約了空間)
t.onKeyDown = function() {
// 當(dāng)鍵盤按下后,去執(zhí)行自定義的這個(gè)方法
c = Key.getCode()-37;
// 獲得按鍵的ASCII碼 (變量 c 每次獲取相對的ASCII碼)
if (!(c>>2)) {
// 方向鍵的表示 (c = 0, 1, 2 or 3)
if (c != q[0])
// 只將新的方向鍵存入隊(duì)列 q
q.unshift(c);
return;
// 在隊(duì)列中保存,并結(jié)束該方法(函數(shù))
}
// 空格或其它鍵不同于按下的方向鍵
x = 32*8 32*520;
// 蛇的起點(diǎn)坐標(biāo)( 左邊 右邊:可被視為 x、y 坐標(biāo))
q = [];
// 用于存儲(chǔ)按鍵的隊(duì)列(因此改變在一幀中的X坐標(biāo)對于所有幀中的X坐標(biāo)都起作用)
m = [];
// 創(chuàng)建一個(gè)數(shù)組用于存儲(chǔ)食物的坐標(biāo)和蛇
createEmptyMovieClip("s", w=0);
// 創(chuàng)建一個(gè)空影片用于存儲(chǔ)蛇和食物的影片剪輯,并重置蛇的計(jì)數(shù)器(w)
e = 2*(m[x-520] = 2*(r=1));
// 設(shè)置擦除計(jì)數(shù)器(e) to 4, 設(shè)置當(dāng)前方向(r)為向上(1),當(dāng)蛇經(jīng)過食物后立即設(shè)置食物位置為當(dāng)前設(shè)置的位置
onEnterFrame = function () {
// 主函數(shù)
c = q.pop();
// 在隊(duì)列中提取出下一輪變換(當(dāng)隊(duì)列為空時(shí),提取數(shù)是undefined的)
if (c%2 != r%2)
// 檢查其不屬于undefined和180度旋轉(zhuǎn)(避免任意按下一個(gè)鍵后就改變蛇的方向)
if (c != undefined)
r = c;
// 改變當(dāng)前方向?yàn)樾碌姆较?br /> x = [-1, -65, 1, 65][r]*8;
// 移動(dòng)蛇到一個(gè)新的X位置 (-1 = left, -65 = up, 1 = right, 65 = down)
if (m[x] == 1 or !(xR0) or !(int(x/520) % 33)) {
// 如果新的位置在蛇身上或出了邊界則 GAME OVER
delete onEnterFrame;
// 退出主循環(huán)函數(shù)
t.text = "\tGAME OVER!"; return;
// 輸出 GAME OVER! 并退出主程序
}
with(s.createEmptyMovieClip(w, w)) {
// 放置蛇身 (第一次循環(huán)時(shí)用于放置食物)
beginFill(255<<16);
// 首先將食物設(shè)為紅色
if (w ) // blue snake color the other times
beginFill(0x555588);
_x = xR0; _y = int(x/520)*8;
// 設(shè)置蛇身的位置
lineTo(-7, 0); lineTo(-7, -7); lineTo(0, -7); endFill();
// 繪制一個(gè)方形
}
m[x] = 1;
// 設(shè)置當(dāng)前位置為"已占用"區(qū)作為蛇身
if (m[x] == 3) {
// 檢查是否有食物在新的位置上
t.text = "Score: " (w-(e-=5)-2)*2;
// 延遲擦除計(jì)數(shù)器5(蛇身每次增長5), 計(jì)算并輸出分?jǐn)?shù) (一個(gè)食物加10分)
do {} while (m[c = (s[0]._x = 8 random(64)*8) (s[0]._y = 8 random(32)*8)*65]);
// 尋找一個(gè)空位置放置點(diǎn), 并存儲(chǔ)該數(shù)值, 并設(shè)置食物的影片剪輯
m[c] = 2;
//設(shè)置選出的位置為為大于2的線路上
}
if (e) {
// if not food MC (s[0]) then erase last snake MC and entry in array m
c = s[e];
// 獲得最后一個(gè)影片剪輯
delete m[c._x 65*c._y]; removeMovieClip(c);
// 刪除數(shù)組 m 中該元素的值并刪除影片剪輯
}
e ;
// 將蛇的擦除計(jì)數(shù)器加一
}
}
原文:
//--- 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(0xeeeeee); 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 direction
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 direction 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 !(xR0) 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 = xR0; _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 ( 10p 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
}
}
翻譯:
//--- Flash MX 貪吃蛇游戲(1Kb) 制作Strille. 版本 2.2, 共計(jì) 746 字節(jié)
//--- 復(fù)制以下代碼在主場景的第一幀場景大小為 512x280 , FPS 16
createTextField("t", 1, 1, 255, 511, 32);
// create a text field to write score and instructions
// 創(chuàng)建一個(gè)文本框用于輸出成績和指示
t.text = "Snake Game\t-\tPress SPACE";
// 顯示開始信息
beginFill(0xeeeeee); lineStyle(1); lineTo(511, 0); lineTo(511, 256); lineTo(0, 256); endFill();
// 沿邊框繪制背景
Key.addListener(t);
// 使用一個(gè)已存在的Object 作鍵盤幀聽 (就樣就不用再創(chuàng)建新Obejct,從而節(jié)約了空間)
t.onKeyDown = function() {
// 當(dāng)鍵盤按下后,去執(zhí)行自定義的這個(gè)方法
c = Key.getCode()-37;
// 獲得按鍵的ASCII碼 (變量 c 每次獲取相對的ASCII碼)
if (!(c>>2)) {
// 方向鍵的表示 (c = 0, 1, 2 or 3)
if (c != q[0])
// 只將新的方向鍵存入隊(duì)列 q
q.unshift(c);
return;
// 在隊(duì)列中保存,并結(jié)束該方法(函數(shù))
}
// 空格或其它鍵不同于按下的方向鍵
x = 32*8 32*520;
// 蛇的起點(diǎn)坐標(biāo)( 左邊 右邊:可被視為 x、y 坐標(biāo))
q = [];
// 用于存儲(chǔ)按鍵的隊(duì)列(因此改變在一幀中的X坐標(biāo)對于所有幀中的X坐標(biāo)都起作用)
m = [];
// 創(chuàng)建一個(gè)數(shù)組用于存儲(chǔ)食物的坐標(biāo)和蛇
createEmptyMovieClip("s", w=0);
// 創(chuàng)建一個(gè)空影片用于存儲(chǔ)蛇和食物的影片剪輯,并重置蛇的計(jì)數(shù)器(w)
e = 2*(m[x-520] = 2*(r=1));
// 設(shè)置擦除計(jì)數(shù)器(e) to 4, 設(shè)置當(dāng)前方向(r)為向上(1),當(dāng)蛇經(jīng)過食物后立即設(shè)置食物位置為當(dāng)前設(shè)置的位置
onEnterFrame = function () {
// 主函數(shù)
c = q.pop();
// 在隊(duì)列中提取出下一輪變換(當(dāng)隊(duì)列為空時(shí),提取數(shù)是undefined的)
if (c%2 != r%2)
// 檢查其不屬于undefined和180度旋轉(zhuǎn)(避免任意按下一個(gè)鍵后就改變蛇的方向)
if (c != undefined)
r = c;
// 改變當(dāng)前方向?yàn)樾碌姆较?br /> x = [-1, -65, 1, 65][r]*8;
// 移動(dòng)蛇到一個(gè)新的X位置 (-1 = left, -65 = up, 1 = right, 65 = down)
if (m[x] == 1 or !(xR0) or !(int(x/520) % 33)) {
// 如果新的位置在蛇身上或出了邊界則 GAME OVER
delete onEnterFrame;
// 退出主循環(huán)函數(shù)
t.text = "\tGAME OVER!"; return;
// 輸出 GAME OVER! 并退出主程序
}
with(s.createEmptyMovieClip(w, w)) {
// 放置蛇身 (第一次循環(huán)時(shí)用于放置食物)
beginFill(255<<16);
// 首先將食物設(shè)為紅色
if (w ) // blue snake color the other times
beginFill(0x555588);
_x = xR0; _y = int(x/520)*8;
// 設(shè)置蛇身的位置
lineTo(-7, 0); lineTo(-7, -7); lineTo(0, -7); endFill();
// 繪制一個(gè)方形
}
m[x] = 1;
// 設(shè)置當(dāng)前位置為"已占用"區(qū)作為蛇身
if (m[x] == 3) {
// 檢查是否有食物在新的位置上
t.text = "Score: " (w-(e-=5)-2)*2;
// 延遲擦除計(jì)數(shù)器5(蛇身每次增長5), 計(jì)算并輸出分?jǐn)?shù) (一個(gè)食物加10分)
do {} while (m[c = (s[0]._x = 8 random(64)*8) (s[0]._y = 8 random(32)*8)*65]);
// 尋找一個(gè)空位置放置點(diǎn), 并存儲(chǔ)該數(shù)值, 并設(shè)置食物的影片剪輯
m[c] = 2;
//設(shè)置選出的位置為為大于2的線路上
}
if (e) {
// if not food MC (s[0]) then erase last snake MC and entry in array m
c = s[e];
// 獲得最后一個(gè)影片剪輯
delete m[c._x 65*c._y]; removeMovieClip(c);
// 刪除數(shù)組 m 中該元素的值并刪除影片剪輯
}
e ;
// 將蛇的擦除計(jì)數(shù)器加一
}
}
相關(guān)文章

flash cs6鼠標(biāo)跟隨效果實(shí)現(xiàn)代碼分享
flash cs6想要實(shí)現(xiàn)鼠標(biāo)跟隨效果?該怎么制作呢?今天我們就來看看使用as2.0實(shí)現(xiàn)鼠標(biāo)跟隨效果的教程,需要的朋友可以參考下2019-05-19
Flash cs6怎么使用代碼輸入中英文文本?Flash cs6中可以使用文字工具直接輸入文本,也可以使用代碼來輸入文本,該怎么使用代碼輸入文本呢?請看下文詳細(xì)的教程,需要的朋友2018-03-11
flash as3.0抽象類怎么定義? as3.0中有很多抽象類,該怎么定義抽象類和抽象方法呢?下面我們就來看看簡單的例子,需要的朋友可以參考下http://www.fzitv.net/softs/408402.2018-02-28
flash cs6中怎么使用ActionScript3.0?
flash cs6中怎么使用ActionScript3.0?flash cs6中想要使用ActionScript3.0功能,該怎么使用呢?下面我們就來看看詳細(xì)的教程,需要的朋友可以參考下2018-01-25
Flash中怎么實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊決定圖像位置?
本教程給大家分享一個(gè)Flash小教程,教大家在Flash CS6中怎么實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊決定圖像位置?方法很簡單,感興趣的朋友歡迎前來一起分享學(xué)習(xí)2018-01-12
Flash中如何用代碼將圖片放在自己想要的舞臺(tái)位置?
本教程教腳本之家的ActionScript教程學(xué)習(xí)者在Flash中如何用代碼將圖片放在自己想要的舞臺(tái)位置,教程講解的詳細(xì),感興趣的朋友歡迎前來分享學(xué)習(xí)2017-11-20
在Flash CS6中使用with函數(shù)繪制背景圖教程
本教程教腳本之家的ActionScript教程學(xué)習(xí)者如何在Flash CS6中使用with函數(shù)繪制背景圖?教程一步步講解的挺詳細(xì),方法也不難,非常適合Flash新手入門學(xué)習(xí)2017-11-18
Flash怎么設(shè)置元件坐標(biāo)?flash使用代碼設(shè)置元件的坐標(biāo)的教程
Flash怎么設(shè)置元件坐標(biāo)?flash中導(dǎo)如的元件需要添加坐標(biāo),該怎么定位元件坐標(biāo)呢?下面我們就來看看flash使用代碼設(shè)置元件的坐標(biāo)的教程,需要的朋友可以參考下2017-10-11
Flash怎么制作來回?fù)u擺的花朵的動(dòng)畫?
Flash怎么制作來回?fù)u擺的花朵的動(dòng)畫?Flash中想要給花朵制作一段搖擺的動(dòng)畫效果,該怎么制作呢?下面我們就來看看詳細(xì)的教程,很簡單,需要的朋友可以參考下2017-05-23
Flash怎么制作流動(dòng)七彩色的文字?想要讓文字動(dòng)起來,該怎么使用flash給文字制作一個(gè)流動(dòng)七彩色的動(dòng)畫呢?下面我們就來看看詳細(xì)的教程,需要的朋友可以參考下2017-04-23











