In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

snake game

Get Adobe Flash player
by tapiokamen 17 Mar 2009
package {
    
    import flash.display.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.events.*;
    import flash.utils.*;
    
    [SWF(width="465", height="465", backgroundColor="0x000000", frameRate="60")]
    
    public class snake extends Sprite
    {
        private const   M_BOARD:uint = 400;
        private const   LEFT:int = 1;
        private const   UP:int = 2;
        private const   RIGHT:int = 3;
        private const   DOWN:int = 4;                        
                
        private var textField:TextField=text_create(100,350,200,20,0xffffff);
        private var messageField:TextField=text_create(50,400,250,20,0xffffff);        
        private var timer:Timer=new Timer(100,0);
        private var screen:BitmapData = new BitmapData(465, 465, false, 0);
        private var rect:Rectangle = new Rectangle(0,0,15,15);
        private var board:Array = new Array(M_BOARD);
        private var p_pos:Array = new Array(M_BOARD);
        private var p_dir:uint = LEFT;
        private var p_len:uint = 0;
        private var game_stat:uint = 0;
        
        public function snake()
        {            

            addChild(new Bitmap(screen));
            addChild(textField);
            addChild(messageField);            
            timer.addEventListener(TimerEvent.TIMER,upda);            
            stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);            
            timer.start();
            init();
        }
        
        private function init():void
        {
            var i:uint;
            for( i = 0; i < M_BOARD; i++)
            {
                board[i] = 1;
                if( i > 19 && i < M_BOARD - 20 && (i+1) % 20 != 0   && i % 20 )    board[i] = 0;
            }
            p_pos[0] = 310;
            board[ add_block() ] = 2;
            p_dir = LEFT;
            p_len = 0;              
        }
        
        private function upda(event:TimerEvent):void
        {
            if( game_stat == 0 )
            {
                drawboard();
                move();
                messageField.text = String("");                
            }
            if( game_stat == 1 )
            {
                messageField.text = String("スペース押して再スタート");                
            }

            textField.text = String("score = ") + String(p_len + 1);

        } 
        
        private function text_create(x:int, y:int, width:int, size:int, color:int):TextField
        {
            var fm:
            TextFormat = new TextFormat, fi:
            TextField = new TextField;
            fm.font = "_typewriter";
            fm.bold = true;
            fm.size = size;
            fm.color = color;
            fm.align = TextFormatAlign.LEFT;            
            fi.defaultTextFormat = fm;
            fi.x = x; fi.y = y;
            fi.width = width; fi.selectable = false;
            return fi;
        }
        private function keyDown(evt:KeyboardEvent):void
        {
            if(evt.keyCode == 32 && game_stat == 1)  //スペース
            {                                     
                game_stat = 0;              
                init();                
            }
            
            if(evt.keyCode == 37 )                                      //左キー
                if( p_dir == UP || p_dir == DOWN )       p_dir = LEFT; 
                
            if(evt.keyCode == 38 )                                      //上キー
                if( p_dir == LEFT || p_dir == RIGHT )    p_dir = UP;

            if(evt.keyCode == 39 )                                      //右キー
                if( p_dir == UP || p_dir == DOWN )       p_dir = RIGHT;       
                
            if(evt.keyCode == 40 )                                      //下キー
                if( p_dir == LEFT || p_dir == RIGHT )    p_dir = DOWN;

        }
        private function drawboard():void
        {
            var i:uint;
            var pos:uint;
            screen.lock();
            screen.fillRect(screen.rect, 0x000000);
            for( i = 0; i < M_BOARD; i++)
            {
                rect.x = (i % 20) * 16;
                pos = i / 20;
                rect.y = pos * 16+ 5;
                if(board[i] == 2) screen.fillRect(rect, 0xffffff);                
                if(board[i] == 1) screen.fillRect(rect, 0xff0000);
                if(board[i] == 0) screen.fillRect(rect, 0x0000ff);
            }
            for( i = 0; i <= p_len; i++)
            {
                pos = p_pos[i] % 20;
                rect.x = pos * 16;
                pos = p_pos[i] / 20;
                rect.y = pos * 16 + 5;
                screen.fillRect(rect, 0x00ff00);
            }
            screen.unlock();
        }

        private function rand(max:uint):uint
        {
            var temp:uint;
            temp =  Math.random() *10000000;
            temp %= max;
            return temp;
        }
        
        private function move():void
        {
            var i:uint;

            for( i = p_len; i > 0; i-- )
            {
                p_pos[i] = p_pos[i - 1];
            }
            if     ( p_dir == LEFT  )    p_pos[0] -=1;            
            else if( p_dir == UP    )    p_pos[0] -=20;
            else if( p_dir == RIGHT )    p_pos[0] +=1;
            else if( p_dir == DOWN  )    p_pos[0] +=20;           
            check();
        }
        
        private function check():void
        {
            var i:uint;
            for( i = 1; i < p_len; i++)
            {
                if(p_pos[0] == p_pos[i] )  gameover();
            }
            if(board[ p_pos[0] ] == 1 )    gameover();
            if(board[ p_pos[0] ] == 2 )
            {
                board[ p_pos[0] ] = 0;
                add_player();
                board[ add_block() ] = 2;
            }            
        }
        
        private function add_player():void
        {
            var i:uint;            
            p_len++;
            p_pos[p_len] = p_pos[0];
        }
        
        private function add_block():int
        {
            var temp:uint = 0;
            var i:uint = 0;
            var flag:uint = 0;
            while(1)
            {
                flag = 0;
                temp = rand(M_BOARD);
                for(i = 0; i <= p_len;i++)
                {
                    if( temp == p_pos[i] )
                    {
                        flag = 1;
                        break;
                    }
                }
                if( flag == 0 && board[temp] == 0  )    return temp;
            }
            return temp;
        }
        
        private function gameover():void
        {
            game_stat = 1;
        }
    }
}