一个小于1k的flash贪吃蛇


注:按空格开始游戏!
原文:http://www.strille.net/works/snake_game_1Kb.php#2.2
源代码:

None.gif // --- Flash MX Snake Game 1Kb by Strille. Version 2.2, 746 bytes
None.gif
// --- Paste this code on frame 1 and set scene size to 512x280 and Frame Rate to 16
None.gif
// --- The code is not written with speed in mind, only small file size. Not that it is slow :-)
None.gif

None.gifcreateTextField(
" t " 1 1 255 511 32 );  //  create a text field to write score and instructions
None.gif
t.text  =   " Snake Game\t-\tPress SPACE " //  show start text
None.gif
beginFill( 0xeeeeee ); lineStyle( 1 ); lineTo( 511 0 ); lineTo( 511 256 ); lineTo( 0 256 ); endFill();  //  draw background with border
None.gif

None.gifKey.addListener(t); 
//  use an existing object as key listener (we don't waste bytes by creating a new object)
ExpandedBlockStart.gifContractedBlock.gif
t.onKeyDown  =  function()  dot.gif // define an anonymous method to execute when a key is pressed
InBlock.gif
    c = Key.getCode()-37// get key code (c is a variable used "locally" several times)
ExpandedSubBlockStart.gifContractedSubBlock.gif
    if (!(c>>2)) dot.gif// arrow keys pressed (c = 0, 1, 2 or 3)
InBlock.gif
        if (c != q[0]) // only add to the queue if it is a new direction
InBlock.gif
            q.unshift(c);
InBlock.gif        
return// save the turn in the queue and exit method
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    
// SPACE or another key other than an arrow key has been pressed
InBlock.gif
    x = 32*8 + 32*520// snake start pos (left and right side of + can be viewed as x and y coord
InBlock.gif
    q = []; // a queue to store key presses (so that x number of key presses during one frame are spread over x number of frames)
InBlock.gif
    m = []; // create an array to store food pos and snake
InBlock.gif
    createEmptyMovieClip("s", w=0); // create MC to store the snake and the food MC and reset snake counter(w)
InBlock.gif
    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
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif    onEnterFrame 
= function () dot.gif// MAIN function
InBlock.gif
        c = q.pop(); // dot.gifpick the next turn in the queue (may be undefined if queue is empty)dot.gif
InBlock.gif
        if (c%2 != r%2// dot.gifand 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)
InBlock.gif
            if (c != undefined)
InBlock.gif                r 
= c; // change current direction to the new value
InBlock.gif

InBlock.gif        x 
+= [-1-65165][r]*8// move the snake to a new x position (-1 = left, -65 = up, 1 = right, 65 = down)
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (m[x] == 1 or !(x%520) or !(int(x/520% 33)) dot.gif// GAME OVER if it is a snake block or outside the map on the next position
InBlock.gif
            delete onEnterFrame; // quit looping main function
InBlock.gif
            t.text += "\tGAME OVER!"return// type game over text and exit main
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        with(s.createEmptyMovieClip(w, w)) 
dot.gif// place a snake block (or food block the first loop)
InBlock.gif
            beginFill(255<<16); // red food color first timedot.gif
InBlock.gif
            if (w++// dot.gifblue snake color the other times
InBlock.gif
                beginFill(0x555588);
InBlock.gif            _x 
= x%520; _y = int(x/520)*8// set snake block position
InBlock.gif
            lineTo(-70); lineTo(-7-7); lineTo(0-7); endFill(); // draw a square
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        m[x] 
+= 1// set current pos as "occupied" by a snake block
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (m[x] == 3dot.gif// check if there is a food block on the new pos
InBlock.gif
            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)
ExpandedSubBlockStart.gifContractedSubBlock.gif
            do dot.gif{} 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
InBlock.gif
            m[c] = 2// set the position picked on the line above to 2
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (e) dot.gif// if not food MC (s[0]) then erase last snake MC and entry in array m
InBlock.gif
            c = s[e]; // get last MC
InBlock.gif
            delete m[c._x+65*c._y]; removeMovieClip(c); // delete the value in the array m and delete the MC
ExpandedSubBlockEnd.gif
        }

InBlock.gif        e
++// increase erase snake counter
ExpandedSubBlockEnd.gif
    }

ExpandedBlockEnd.gif}

None.gif
None.gif

转载于:https://www.cnblogs.com/FireYang/archive/2006/10/13/528013.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值