俄罗斯方块swf:
源文件:Tetris.rar
详细代码:
- package
- {
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.KeyboardEvent;
- import flash.events.MouseEvent;
- import flash.events.TimerEvent;
- import flash.geom.Point;
- import flash.text.TextField;
- import flash.ui.Keyboard;
- import flash.utils.Timer;
- import flash.net.*;
- /**
- * ...
- * @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);
- lable1.textColor = 0xffffff;
- lable1.text = "上,下,左,右,空格";
- var lable2:TextField = new TextField();
- addChild(lable2);
- lable2.textColor = 0xffffff;
- lable2.text = "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 = new Array();
- for (var j:int = 0; j < xNum;j++ ) {
- deadBoxs[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.y][liveBoxs.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[j] != 1) {
- break;
- }
- counter++;
- }
- if (counter==xNum) {
- for (j = 0; j < xNum;j++ ) {
- deadBoxs[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.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.y==ii-1)&&(liveBoxs.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.x < 0)
- ||(liveBoxs.x> ( xNum - 1))
- ||(liveBoxs.y < 0)
- ||(liveBoxs.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.x==j)&&(liveBoxs.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 = {x:liveBoxs.x,y:liveBoxs.y};
- liveBoxs.x += x;
- liveBoxs.y += y;
- }
- if (!isLive()) {
- for (i = 0; i < liveBoxs.length;i++ ) {
- liveBoxs = {x:liveBoxsShadow.x,y:liveBoxsShadow.y};
- }
- }
- }
- private function roll(b:Boolean = true):void {
- var liveBoxsShadow:Array = new Array();
- for (var i:int = 0; i < liveBoxs.length; i++ ) {
- liveBoxsShadow = {x:liveBoxs.x,y:liveBoxs.y};
- }
- if (b) {
- for (i = 1; i < liveBoxs.length;i++ ) {
- var tempIX:int=liveBoxs.x;
- liveBoxs.x = liveBoxs[0].x+liveBoxs.y-liveBoxs[0].y;
- liveBoxs.y = liveBoxs[0].y -tempIX +liveBoxs[0].x;
- }
- }else {
- for (i= 1; i < liveBoxs.length;i++ ) {
- tempIX=liveBoxs.x;
- liveBoxs.x = liveBoxs[0].x-liveBoxs.y+liveBoxs[0].y;
- liveBoxs.y = liveBoxs[0].y +tempIX -liveBoxs[0].x;
- }
- }
- if (!isLive()) {
- for (i = 0; i < liveBoxs.length;i++ ) {
- liveBoxs = {x:liveBoxsShadow.x,y:liveBoxsShadow.y};
- }
- }
- }
- private function update():void {
- world.graphics.clear();
- world.graphics.lineStyle(1, 0xffffff);
- world.graphics.lineTo(xNum * w , 0);
- world.graphics.lineTo(xNum * w ,yNum * w);
- world.graphics.lineTo(0, yNum * w );
- world.graphics.lineTo(0,0);
- world.graphics.lineStyle();
-
- for (var i:int = 0; i < yNum;i++ ) {
- for (var j:int = 0; j < xNum;j++ ) {
- if (deadBoxs[j] == 1) {
- world.graphics.beginFill(0x123456);
- world.graphics.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++ ) {
- world.graphics.beginFill(color,0.9);
- world.graphics.drawRect(liveBoxs.x*w,liveBoxs.y*w,w,w);
- }
- world.graphics.lineStyle(1, 0xffffff, 0.5);
- for (i = 0; i < xNum-1;i++ ) {
- world.graphics.moveTo(w * (i + 1), 0);
- world.graphics.lineTo(w * (i + 1), w*yNum);
- }
- for (i = 0; i < yNum-1;i++ ) {
- world.graphics.moveTo(0, w*(i+1));
- world.graphics.lineTo(w * xNum , w*(i+1));
- }
- }
- private function clickHandler(e:MouseEvent):void {
- navigateToURL( new URLRequest ("http://space.flash8.net/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;
- }
- }
- }
- }