关闭顶部展开顶部

Flash贪吃蛇游戏AS代码翻译_Flash教程

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!

推荐:AS2.0面向对象编程的解释
有时间,有爱好,E文还行的可以直接上这看:http://www.kirupa.com/developer/oop2/AS2OOPindex.htm,很长,很具体。模板无忧我简单地把我理解的说一下,

今天翻译了一段经典的贪吃蛇代码,译后感觉还有很多地方不太妥当,很多不妥的地方希望大家多指教

原文:

//--- 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, 共计 746 字节
//--- 复制以下代码在主场景的第一帧场景大小为 512x280 , FPS 16
createTextField("t", 1, 1, 255, 511, 32);
// create a text field to write score and instructions
// 创建一个文本框用于输出成绩和指示
t.text = "Snake Game\t-\tPress SPACE";
// 显示开始信息
beginFill(0xeeeeee); lineStyle(1); lineTo(511, 0); lineTo(511, 256); lineTo(0, 256); endFill();
// 沿边框绘制背景
Key.addListener(t);
// 使用一个已存在的Object 作键盘帧听 (就样就不用再创建新Obejct,从而节约了空间)
t.onKeyDown = function() {
// 当键盘按下后,去执行自定义的这个方法
c = Key.getCode()-37;
// 获得按键的ASCII码 (变量 c 每次获取相对的ASCII码)
if (!(c>>2)) {
// 方向键的表示 (c = 0, 1, 2 or 3)
if (c != q[0])
// 只将新的方向键存入队列 q
q.unshift(c);
return;
// 在队列中保存,并结束该方法(函数)
}
// 空格或其它键不同于按下的方向键
x = 32*8 32*520;
// 蛇的起点坐标( 左边 右边:可被视为 x、y 坐标)
q = ;
// 用于存储按键的队列(因此改变在一帧中的X坐标对于所有帧中的X坐标都起作用)
m = ;
// 创建一个数组用于存储食物的坐标和蛇
createEmptyMovieClip("s", w=0);
// 创建一个空影片用于存储蛇和食物的影片剪辑,并重置蛇的计数器(w)
e = 2*(m[x-520] = 2*(r=1));
// 设置擦除计数器(e) to 4, 设置当前方向(r)为向上(1),当蛇经过食物后立即设置食物位置为当前设置的位置
onEnterFrame = function () {
// 主函数
c = q.pop();
// 在队列中提取出下一轮变换(当队列为空时,提取数是undefined的)
if (c%2 != r%2)
// 检查其不属于undefined和180度旋转(避免任意按下一个键后就改变蛇的方向)
if (c != undefined)
r = c;
// 改变当前方向为新的方向
x = [-1, -65, 1, 65][r]*8;
// 移动蛇到一个新的X位置 (-1 = left, -65 = up, 1 = right, 65 = down)
if (m[x] == 1 or !(xR0) or !(int(x/520) % 33)) {
// 假如新的位置在蛇身上或出了边界则 GAME OVER
delete onEnterFrame;
// 退出主循环函数
t.text = "\tGAME OVER!"; return;
// 输出 GAME OVER! 并退出主程序
}
with(s.createEmptyMovieClip(w, w)) {
// 放置蛇身 (第一次循环时用于放置食物)
beginFill(255<<16);
// 首先将食物设为红色
if (w ) // blue snake color the other times
beginFill(0x555588);
_x = xR0; _y = int(x/520)*8;
// 设置蛇身的位置
lineTo(-7, 0); lineTo(-7, -7); lineTo(0, -7); endFill();
// 绘制一个方形
}
m[x] = 1;
// 设置当前位置为"已占用"区作为蛇身
if (m[x] == 3) {
// 检查是否有食物在新的位置上
t.text = "Score: " (w-(e-=5)-2)*2;
// 延迟擦除计数器5(蛇身每次增长5), 计算并输出分数 (一个食物加10分)


do {} while (m[c = (s[0]._x = 8 random(64)*8) (s[0]._y = 8 random(32)*8)*65]);
// 寻找一个空位置放置点, 并存储该数值, 并设置食物的影片剪辑
m[c] = 2;
//设置选出的位置为为大于2的线路上
}
if (e) {
// if not food MC (s[0]) then erase last snake MC and entry in array m
c = s[e];
// 获得最后一个影片剪辑
delete m[c._x 65*c._y]; removeMovieClip(c);
// 删除数组 m 中该元素的值并删除影片剪辑
}
e ;
// 将蛇的擦除计数器加一
}
}

分享:合并不同帧频(fps)的Flash文件
Flash中要合并不同帧频(fps)的文件的确问题是一个令人困扰的话题,因为Flash本身并不提供任何动态改变帧速的函数。也无法写一个通用函数来达到这一目的。但

来源:蓝色理想//所属分类:Flash教程/更新时间:2008-03-05
loading.. 评论加载中....
相关Flash教程
闂佹眹鍩勯崹閬嶆偤閺囶澁缍栭柛鈩冪⊕閳锋帗銇勯弴妤€浜惧銈忕秶閹凤拷
濠电偛顕慨顓㈠磻閹炬枼妲堥柡鍌濇硶婢ф稒淇婇懠顒夆偓婵嬫煟閵忊晛鐏查柟鍑ゆ嫹
濠电姷顣介埀顒€鍟块埀顒勵棑缁辩偛顓兼径瀣閻庣懓瀚竟鍡欐崲娑斾線鏌i姀鈺佺伈闁瑰嚖鎷�
濠电姷顣介埀顒€鍟块埀顒勵棑缁辩偛顓兼径濠勵吋闂佽鍨庨崟顓фК闂佽閰eḿ褍螞濞戙垺鍋夐柨鐕傛嫹
闂備胶枪缁绘劙骞婃惔銊ョ劦妞ゆ帒鍊哥敮鍫曞箹鐎涙ḿ鐭掔€规洘绻堥弫鎾绘晸閿燂拷
闂備胶枪缁绘劙骞婃惔銊ョ劦妞ゆ巻鍋撻柛姘儑缁﹪鏁傞崜褏鐓撻柣搴岛閺呮繈鎯屽▎鎴犵=濞撴艾锕ョ€氾拷
闂備浇銆€閸嬫挻銇勯弽銊р槈闁伙富鍣i弻娑樷攽閹邦亞鑳虹紓浣靛妽濡炶棄顕i妸鈺婃晬婵炲棙鍨电粭锟犳⒑閸濆嫬鈧骞婇幘鑸殿潟闁跨噦鎷�
闂備礁鎼崯鐗堟叏妞嬪海绀婂鑸靛姈閻擄綁鎮规潪鎷岊劅婵炲眰鍊曢湁闁挎繂妫欑粈鈧梺鍛娚戦悧鐘茬暦閹扮増鏅搁柨鐕傛嫹
婵犵妲呴崹顏堝礈濠靛棭鐔嗘俊顖氬悑鐎氱粯銇勯幘瀵哥畺閻庢熬鎷�
濠电姷顣介埀顒€鍟块埀顒勵棑缁辩偛顓奸崶銊ヮ伕濡炪倖鎸荤换鍐偓姘虫珪娣囧﹪顢涘Δ鈧晶鍙夌節椤喗瀚�
婵犵妲呴崹顏堝礈濠靛棭鐔嗘慨妞诲亾鐎规洦鍓熼、娆撳礂閻撳簶鍋撻悽鍛婄厸闁割偅绻勫瓭婵犳鍣幏锟�
婵犵妲呴崹顏堝礈濠靛棭鐔嗘慨妞诲亾闁哄苯鎳橀崺鈧い鎺嗗亾闁宠閰i獮鎴﹀箛闂堟稒顔嗛梻浣告惈鐎氭悂骞忛敓锟�
婵犵妲呴崹顏堝礈濠靛棭鐔嗘慨妞诲亾鐎规洩缍侀獮瀣攽閸偂绱�
濠电姷顣介埀顒€鍟块埀顒勵棑缁辩偛顓兼径濠勭厬闂佺懓鐡ㄧ换鍕敂鐎涙ü绻嗘い鏍殔婢у弶绻濋~顔藉
闂佽楠搁崢婊堝礈濠靛鍋嬮柟鎯版閻鈹戦悩鎻掓殭闁奸潧缍婇弻銈夋嚍閵夈儱顫嶉梺缁樼壄缂嶄礁鐣峰▎鎾存櫢闁跨噦鎷�
UB闂備礁婀辩划顖炲礉濡ゅ懐宓侀柛銉㈡櫆鐎氭岸鎮楀☉娅虫垿锝為敓锟�
闂備浇澹堟ご绋款潖婵犳碍鐒鹃悗鐢电《閸嬫捇鐛崹顔句痪濠电姭鍋撻柨鐕傛嫹
闂佽楠哥粻宥夊垂閸濆嫸鑰块柛銏㈠殰
闂備礁鎲″缁樻叏妞嬪海绀婂璺虹灱閸楁碍绻涢崱妤€顒㈤柛鐐差槹缁绘稓绱欓悩鍝勫帯闂佺ǹ楠忛幏锟�
缂傚倸鍊烽悞锕傛偡閿曞倸鍨傛繝濠傚椤╅攱銇勯幒宥囶槮缂佹彃婀遍埀顒傚仯閸婃繄绱撳棰濇晩闁跨噦鎷�
©2017 www.mb5u.com婵犵妲呴崹顏堝礈濠靛棭鐔嗘慨妞诲亾鐎殿噮鍣i幃鈺呭箵閹烘挸鐦�
闂備浇銆€閸嬫捇鏌熼婊冾暭妞ゃ儻鎷�&闂備礁鎲$敮鎺懳涢弮鍫燁棅闁跨噦鎷�