Davis 俄罗斯方块源码AS3.0
package
{
 import flash.display.Sprite;
 import flash.events.Event;
 import flash.events.KeyboardEvent;
 import flash.events.MouseEvent;
 import flash.events.TimerEvent;
 Point;
 TextField;
 import flash.ui.Keyboard;
 import flash.utils.Timer;
 import flash.*;
 /**
  * ...
  * @author sliz
  * @qq:405628079
 */
 [SWF(width="400", height="500", backgroundColor="0x000000", frameRate="60")]
 public class Main extends Sprite
 {
  private var deadBoxs:Array = new Array();
  private var liveBoxs:Array = new Array();
  private var xNum:int = 20;
  private var yNum:int = 40;
  private var w:Number = 10;
  private var world:Sprite = new Sprite();
  private var liveWrapper:Sprite = new Sprite();
 
  private var keyRight  :Boolean = false;
  private var keyLeft    :Boolean = false;
  private var keyUp:Boolean = false;
  private var keyRollLeft:Boolean = false;
  private var keyRollRight:Boolean = false;
 
  private var lives:Array = new Array();
  private var livesLength:int = 5;
 
  public function Main():void
  {
   init();
  }
 
  private function init():void
  {
   ////////////////////////////
   var lable1:TextField = new TextField();
   addChild(lable1);
  &Color = 0xffffff;
  & = ",,,,空格";
   var lable2:TextField = new TextField();
   addChild(lable2);
  &Color = 0xffffff;
  & = "sliz";
   lable2.width = 50;
   lable2.height=20
   lable2.addEventListener(MouseEvent.CLICK, clickHandler);
   lable2.x = stage.stageWidth - lable2.width;
   lable2.y = stage.stageHeight - lable2.height;
   ///////////////////////////
   var timer:Timer = new Timer(1000);
   var timer2:Timer = new Timer(100);
   addChild(world);
   world.x = stage.stageWidth / 2 - xNum * w / 2;
   world.y = stage.stageHeight / 2 - yNum * w / 2
   addChild(liveWrapper);
   for (var i:int = 0; i < yNum;i++ ) {
    deadBoxs[i] = new Array();
    for (var j:int = 0; j < xNum;j++ ) {
     deadBoxs[i][j] = 0;
    }
   }
   lives[0] = 1;
   for (i = 0; i < livesLength; i++) {
    initLive();
   }
   update();
   stage.addEventListener(MouseEvent.CLICK, clickHandler);
   stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
   stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
   stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
   timer.addEventListener(TimerEvent.TIMER, timeHandler);
   timer2.addEventListener(TimerEvent.TIMER, timeHandler2);
   timer2.start();
   timer.start();
  }
  private function initLive():void {
   var r:Number = Math.random();
   if (r < 0.2) {
    lives[lives[0]] = 0;
    liveBoxs[0]={ x:1, y:0 } ;
    liveBoxs[1]={ x:0, y:0} ;
    liveBoxs[2]={ x:2, y:0 } ;
    liveBoxs[3] = { x:3, y:0 } ;
   }else if (r < 0.4) {
    lives[lives[0]] = 1;
    liveBoxs[0]={ x:1, y:0 } ;
    liveBoxs[1]={ x:0, y:0} ;
    liveBoxs[2]={ x:1, y:1 } ;
    liveBoxs[3]={ x:2, y:1 } ;
   }else if (r < 0.6) {
    lives[lives[0]] = 2;
    liveBoxs[0]={ x:1, y:0 } ;
    liveBoxs[1]={ x:0, y:1} ;
    liveBoxs[2]={ x:1, y:1 } ;
    liveBoxs[3]={ x:2, y:0 } ;
   }else if (r < 0.8) {
    lives[lives[0]] = 3;
    liveBoxs[0]={ x:0, y:0 } ;
    liveBoxs[1]={ x:0, y:1} ;
    liveBoxs[2]={ x:1, y:1 } ;
    liveBoxs[3]={ x:1, y:0 } ;
   }else {
    lives[lives[0]] = 4;
    liveBoxs[0]={ x:1, y:1 } ;
    liveBoxs[1]={ x:1, y:0} ;
    liveBoxs[2]={ x:0, y:1 } ;
    liveBoxs[3]={ x:2, y:1 } ;
   }
   move((int)(xNum / 2), 1);
   if (lives[0] == livesLength) {
    lives[0] = 1;
   }else {
    lives[0]++;
   }
  }
  private function liveToDead():void {
   for (var i:int = 0; i < liveBoxs.length; i++) {
    deadBoxs[liveBoxs[i].y][liveBoxs[i].x] = 1;
   }
  }
  private function clear():void {
   for (var i:int = 0; i < yNum; i++ ) {
    var counter:int = 0;
    for (var j:int = 0; j < xNum;j++ ) {
     if (deadBoxs[i][j] != 1) {
      break;
     }
     counter++;
    }
    if (counter==xNum) {
     for (j = 0; j < xNum;j++ ) {
      deadBoxs[i][j] = 0;
     }
     for (var ii:int = i; ii > 0;ii-- ) { 
      for (j = 0; j < xNum;j++ ) {
       deadBoxs[ii][j] = deadBoxs[ii-1][j];
      }
     }
     for (j = 0; j < xNum;j++ ) {
      deadBoxs[0][j] = 0;
     }
    }
   }
  }
  private function isBottom():Boolean {
   var ret:Boolean = false;
   for (var i:int = 0; i < liveBoxs.length;i++ ) {
    if (liveBoxs[i].y ==yNum-1) {
     ret = true;
     break
    }
    for (var ii:int = 0; ii < yNum; ii++ ) {
     for (var j:int = 0; j < xNum; j++ ) {
      if ((deadBoxs[ii][j]==1)&&(liveBoxs[i].y==ii-1)&&(liveBoxs[i].x==j)) {
       ret = true;
       break
      }
     }
    }
   }
   return ret;
  }
  private function isLive():Boolean {
   var ret:Boolean = true;
   for (var i:int = 0; i < liveBoxs.length;i++ ) {
    if (
    (liveBoxs[i].x < 0)
    ||(liveBoxs[i].x> ( xNum - 1))
    ||(liveBoxs[i].y < 0)
    ||(liveBoxs[i].y>(yNum-1))
    ) {
     ret = false;
     break
    }
    for (var ii:int = 0; ii < yNum; ii++ ) {
     for (var j:int = 0; j < xNum; j++ ) {
      if ((deadBoxs[ii][j]==1)&&(liveBoxs[i].x==j)&&(liveBoxs[i].y==ii)) {
       ret = false;
       break
      }
     }
    }
   }
   return ret;
  }
  private function move(x:int=1, y:int=0):void {
   var liveBoxsShadow:Array = new Array();
   for (var i:int = 0; i < liveBoxs.length; i++ ) {
    liveBoxsShadow[i] = {x:liveBoxs[i].x,y:liveBoxs[i].y}; 
    liveBoxs[i].x += x;
    liveBoxs[i].y += y;
   }
   if (!isLive()) {
    for (i = 0; i < liveBoxs.length;i++ ) {
     liveBoxs[i] =  {x:liveBoxsShadow[i].x,y:liveBoxsShadow[i].y};
    }
   }
  }
  private function roll(b:Boolean = true):void {
   var liveBoxsShadow:Array = new Array();
   for (var i:int = 0; i < liveBoxs.length; i++ ) {
    liveBoxsShadow[i] = {x:liveBoxs[i].x,y:liveBoxs[i].y};
   }
   if (b) {
    for (i = 1; i < liveBoxs.length;i++ ) {
     var tempIX:int=liveBoxs[i].x;
     liveBoxs[i].x = liveBoxs[0].x+liveBoxs[i].y-liveBoxs[0].y;
     liveBoxs[i].y = liveBoxs[0].y -tempIX +liveBoxs[0].x;
    }
   }else {
    for (i= 1; i < liveBoxs.length;i++ ) {
     tempIX=liveBoxs[i].x;
     liveBoxs[i].x = liveBoxs[0].x-liveBoxs[i].y+liveBoxs[0].y;
     liveBoxs[i].y = liveBoxs[0].y +tempIX -liveBoxs[0].x;
    }
   }
   if (!isLive()) {
    for (i = 0; i < liveBoxs.length;i++ ) {
     liveBoxs[i] =  {x:liveBoxsShadow[i].x,y:liveBoxsShadow[i].y};
    }
   }
  }
  private function update():void {
  &aphics.clear();
  &aphics.lineStyle(1, 0xffffff);
  &aphics.lineTo(xNum * w , 0);
  &aphics.lineTo(xNum * w ,yNum * w);
  &aphics.lineTo(0, yNum * w );
  &aphics.lineTo(0,0);
  &aphics.lineStyle();
   
   for (var i:int = 0; i < yNum;i++ ) {
    for (var j:int = 0; j < xNum;j++ ) {
     if (deadBoxs[i][j] == 1) {
      aphics.beginFill(0x123456);
      aphics.drawRect(j * w, i * w, w, w);
     }
    }
   }
   var color:Number;
   if (lives[0]==4) {
    color = 0x00ffff;
   }else if (lives[0]==1) {
    color = 0xff0000;
   }else if (lives[0]==2) {
    color = 0x00ff00;
   }else if (lives[0]==3) {
    color=0x0000ff
   }else {
    color=0xffff00
   }
   for (i = 0; i < liveBoxs.length; i++ ) {
    aphics.beginFill(color,0.9);
    aphics.drawRect(liveBoxs[i].x*w,liveBoxs[i].y*w,w,w);
   }
  &aphics.lineStyle(1, 0xffffff, 0.5);
   for (i = 0; i < xNum-1;i++ ) {
    veTo(w * (i + 1), 0);
    aphics.lineTo(w * (i + 1), w*yNum);
   }
   for (i = 0; i < yNum-1;i++ ) {
    veTo(0, w*(i+1));
    aphics.lineTo(w * xNum , w*(i+1));
   }
游戏免费源码分享网站  }
  private function clickHandler(e:MouseEvent):void {
   navigateToURL( new URLRequest ("space.flash8/space/?534614" ) );
  }
  private function timeHandler2(e:TimerEvent):void {
   if (keyLeft) {
    move(-1, 0);
   }
   if (keyRight) {
    move(1, 0);
   }
   if (keyRollLeft) {
    roll();
   }
   if (keyRollRight) {
    roll(false);
   }
  }
  private function timeHandler(e:TimerEvent):void {
   move(0, 1);
   if (isBottom()) {
    liveToDead();
    clear();
    initLive();
   }
  }
  private function enterFrameHandler(e:Event):void {
   if (keyUp) {
    while (!isBottom()) {
     move(0, 1);
    }
    liveToDead();
    clear();
    initLive();
    keyUp = false;
   }
   update();
  }
  private function keyDownHandler(e:KeyboardEvent):void {
   switch(e.keyCode){
    case Keyboard.LEFT:
     keyLeft = true;
     keyRight = false;
     break;
    case Keyboard.RIGHT:
     keyRight = true;
     keyLeft = false;
     break;
    case Keyboard.SPACE:
     keyUp = true;
     break;
    case Keyboard.UP:
     keyRollLeft = true;
     keyRollRight = false;
     break;
    case Keyboard.DOWN:
     keyRollRight = true;
     keyRollLeft = false;
     break;
   }
  }
  private function keyUpHandler(e:KeyboardEvent):void {
   switch(e.keyCode){
    case Keyboard.LEFT:
     keyLeft = false;
     break;
    case Keyboard.RIGHT:
     keyRight = false;
     break;
    case Keyboard.UP:
     keyRollLeft = false;
     break;
    case Keyboard.DOWN:
     keyRollRight = false;
     break;
   }
  }
 }
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。