santiago1983

学无止境

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

The tetris is coded by actionscript 3.0, i have uploaded on google code, if you want to use it as referrence, please go to http://code.google.com/p/flash-tetris-code/ to download it as your wish.

Here i give some snippet of codes about the project:

Main Class: 

/*
* tetris
* created date: 06/26/2012
* version: 1.0
* @Author: Santiago Chen
* @Email:  santiago1209@foxmail.com
*/
package com.santiago {
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.display.Sprite;
    import com.santiago.events.*;
    
    [SWF(width='400',height='600',frameRate='36',backgroundColor='0xcccccc')]
    public class Main extends Sprite {
    private var boardSp:Sprite;
    private var blockSp:Sprite;
    private var frameNo:int =  0;
    private var master:Master;
    private var painter:Painter;
    
    internal var gameStarted: Boolean = false;
    internal var gamePaused : Boolean = false;
    
    private var controller:Controller = new Controller;

        public function Main() {
            this.addEventListener(Event.ADDED_TO_STAGE,initHandler)
        }
        private function initHandler(event:Event):void{
            //debug
            //Model.debug = true;
            
            master = new Master();
            painter = new Painter();
            Model.painter = painter;
            this.addChild(Model.painter);
            
            Model.boardData = new Board(10,16);
            Model.board = painter.drawBoard(Model.boardData);
            
            Model.blockData = Block.newBlock();
            Model.block = painter.drawBlock(Model.blockData);
            
            this.addEventListener(Event.ENTER_FRAME, gameIteration);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler)
            }
            
            
        private function gameIteration(event:Event):void{
            if(!gameStarted) return;
            if(gamePaused)   return;
            
            frameNo++;
            master.doMasterActions(frameNo);
            if(master.gameOver){
                gameStarted = false;
                trace("Game Over!!!");
            }
            }
        private function onKeyDownHandler(event:KeyboardEvent):void{
            //trace(event.keyCode)
            switch(event.keyCode){
                case 13: // start or pause;
                gameStarted = true;
                break;
                case 65: //a move left
                //Model.blockData.xPos--;
                controller.tryMoveLeft(Model.blockData,  Model.boardData);
                break;
                case 68: //d move right
                controller.tryMoveRight(Model.blockData,  Model.boardData);
                break;
                case 83: //s move down
                controller.tryMoveDown(Model.blockData,  Model.boardData);
                break;
                case 37: //rotate anti-clockwise
                controller.tryRotate(1, Model.blockData, Model.boardData);
                break;
                case 39: //rotate clockwise
                controller.tryRotate(-1, Model.blockData, Model.boardData);
                break;
                }
                Model.painter.drawBlock(Model.blockData);
            }

    }
    
}

here it includes two parts:

one is for controller setting, defining the key Events for each of controlling of the game;

The other is game iteration, i use enter frame event as my looping base, of course, you can also use Timer class to build the iteration if you prefer. About the details of game logic, you can read more in my coding. 

If you want to contact me, please send email to me at santiago1209@foxmail.com 

posted on 2012-06-26 22:41  santiago1983  阅读(168)  评论(0编辑  收藏  举报