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

flash on 2009-2-11

テ○リス
操作方法
← → : ブロック移動
↓   : 高速落下
↑   : 超高速落下
SPACE : 右回転
ENTER : 左回転
Get Adobe Flash player
by hacker_ij48yrxl 10 Feb 2009
// テ○リス

// 操作方法
// ← → : ブロック移動
// ↓   : 高速落下
// ↑   : 超高速落下
// SPACE : 右回転
// ENTER : 左回転

package {
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    import flash.ui.*;
	public class Tetris extends Sprite
	{
		private var map:Array;
		private var block:Array;
		private var blocks:Array;
		private var xx:int;
		private var yy:int;
		private var colorTable:Array = [0x344454, 0x68C8E0, 0xB07CBC, 0xBC4C4C, 0x60A460, 0xE0B874, 0xD08860, 0x6C90D0];
		
		private const KEY_RIGHT:int = 0;
		private const KEY_LEFT:int = 1;
		private const KEY_UP:int = 2;
		private const KEY_DOWN:int = 3;
		private const KEY_A:int = 4;
		private const KEY_B:int = 5;
		
		private var keypad:int = 0;
		private var score:int = 0;
		private var scoreText:TextField = new TextField();
		private var message:TextField = new TextField();
   
                private var WIDTH:int;
                private var HEIGHT:int;
        
                private var W:int = 10;
                private var H:int = 10;
		
		
		public function Tetris() 
		{
			
			init();
			newBlock();
			
			scene = SCENE_PAUSE;
			setMessage("pause");
                        message.textColor = 0xFF0000;

                        score=0;
                        setScore(score);
			addChild(scoreText);
			addChild(message);

			addEventListener(KeyboardEvent.KEY_DOWN, keyDownEvent);
			addEventListener(KeyboardEvent.KEY_UP, keyUpEvent);
                        addEventListener(Event.ENTER_FRAME, loop_Proc);
		

			addEventListener(FocusEvent.FOCUS_IN, function(event:FocusEvent):void {
				scene = SCENE_PLAY;
				setMessage("");
			});
			addEventListener(FocusEvent.FOCUS_OUT, function(event:FocusEvent):void {
				if (scene == SCENE_OVER) {
					reset();
				}
				scene = SCENE_PAUSE;
				setMessage("pause");
			});
			addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void {
				if (scene == SCENE_OVER) {
					scene = SCENE_PLAY;
					setMessage("");
					reset();
				}
			});
		}

                public function setScore(score:Number):void{
                        scoreText.text = score + "点";
		        scoreText.x = WIDTH - scoreText.textWidth;
			scoreText.y = 0;
                }

                public function setMessage(str:String):void{
                        message.text = str;
		        message.x = (WIDTH - message.textWidth) / 2;
                        message.y = (HEIGHT - message.textHeight) / 2;
                }
		
		
		public function reset():void {
			map = newArray(20, 10);
			newBlock();
			score = 0;
			setScore(score);
		}
		public function keyDownEvent(event:KeyboardEvent):void
		{
			
			var code:uint = event.keyCode;
			switch(code) {
				case Keyboard.RIGHT:
				keypad |= (1 << KEY_RIGHT);
				break;
				case Keyboard.LEFT:
				keypad |= (1 << KEY_LEFT);
				break;
				case Keyboard.UP:
				keypad |= (1 << KEY_UP);
				break;
				case Keyboard.DOWN:
				keypad |= (1 << KEY_DOWN);
				break;
				case Keyboard.ENTER:
				keypad |= (1 << KEY_B);
				break;
				case Keyboard.SPACE:
				keypad |= (1 << KEY_A);
				break;
			}
		}
		
		public function keyUpEvent(event:KeyboardEvent):void
		{
			var code:uint = event.keyCode;
			switch(code) {
				case Keyboard.RIGHT:
				keypad &= ~(1 << KEY_RIGHT);
				break;
				case Keyboard.LEFT:
				keypad &= ~(1 << KEY_LEFT);
				break;
				case Keyboard.UP:
				keypad &= ~(1 << KEY_UP);
				break;
				case Keyboard.DOWN:
				keypad &= ~(1 << KEY_DOWN);
				break;
				case Keyboard.ENTER:
				keypad &= ~(1 << KEY_B);
				break;
				case Keyboard.SPACE:
				keypad &= ~(1 << KEY_A);
				break;
			}
		}
		
		private var sc:int = 0;
		private const STOP_COUNT:int = 5;
		private var dc:int = 0;
		private const DOWN_COUNT:int = 5;
		private var lastkey:int = keypad;
		private const SCENE_PLAY:int = 0;
		private const SCENE_OVER:int = 1;
		private const SCENE_PAUSE:int = 2;
		private var scene:int = SCENE_PLAY;
		
		public function loop_Proc(event:Event):void
		{
			if (scene == SCENE_PLAY) {
	
				if ((keypad & (1 << KEY_RIGHT)) != 0) {
					keypad &= ~(1 << KEY_RIGHT);
					if (!isCollision(map, block, xx + 1, yy)) {
						xx++;
						sc = 0;
					}
				}
				
				if ((keypad & (1 << KEY_LEFT)) != 0) {
					keypad &= ~(1 << KEY_LEFT);
					if (!isCollision(map, block, xx - 1, yy)) {
						xx--;
						sc = 0;
					}
				}
				
				if ((keypad & (1 << KEY_DOWN)) != 0) {
					//keypad &= ~(1 << KEY_DOWN);
					dc = DOWN_COUNT;
				}
				
				if ((keypad & (1 << KEY_A)) != 0) {
					keypad &= ~(1 << KEY_A);
					var rblock:Array = getRotateBlock(block);
					if (!isCollision(map, rblock, xx, yy)) {
						block = rblock;
						sc = 0;
					}else if (!isCollision(map, rblock, xx, yy-1)) {
						block = rblock;
						yy--;
						sc = 0;
					}else if (!isCollision(map, rblock, xx+1, yy)) {
						block = rblock;
						xx++;
						sc = 0;
					}else if (!isCollision(map, rblock, xx-1, yy)) {
						block = rblock;
						xx--;
						sc = 0;
					}
				}
				
				if ((keypad & (1 << KEY_B)) != 0) {
					keypad &= ~(1 << KEY_B);
					rblock = getRotateBlockR(block);
					if (!isCollision(map, rblock, xx, yy)) {
						block = rblock;
						sc = 0;
					}else if (!isCollision(map, rblock, xx, yy-1)) {
						block = rblock;
						yy--;
						sc = 0;
					}else if (!isCollision(map, rblock, xx+1, yy)) {
						block = rblock;
						xx++;
						sc = 0;
					}else if (!isCollision(map, rblock, xx-1, yy)) {
						block = rblock;
						xx--;
						sc = 0;
					}
				}

                                if ((keypad & (1 << KEY_UP)) != 0) {
                                    keypad &= ~(1 << KEY_UP);
                                    var cy:int = yy;
			            while (!isCollision(map, block, xx, cy + 1))	cy++;
                                    yy = cy;
                                    dc = DOWN_COUNT;
                                    sc = STOP_COUNT;
                                }
				
				if (++dc >= DOWN_COUNT) {
					dc = 0;
					if (!isCollision(map, block, xx, yy + 1)) {
						yy++;
						sc = 0;
					}else {
						if (++sc >= STOP_COUNT) {
							sc = 0;
							setMap();
							var t:int = clearMap(map);
							score += t * t * 10;
							setScore(score);
							newBlock();
							if (isCollision(map, block, xx, yy)) {
								scene = SCENE_OVER;
								setMessage("game over");
							}
						}
					}
				}
			}
			draw();
			
			lastkey = keypad;
			
		}
		
		public function setMap():void
		{
			for (var i:int = 0; i < block.length; i++) {
				for (var j:int = 0; j < block[0].length; j++) {
					var t:int = block[i][j];
					if (t != 0) {
						map[yy + i][xx + j] = t;
					}
				}
			}
		}
		
		public function newBlock():void
		{
			var t:int = int(Math.random() * 9999 % 7);
			block = blocks[t];
			xx = (map[0].length-block[0].length)/2;
			if (t == 1) {
				yy = -1;
			}else{
				yy = 0;
			}
		}
		
		public function draw():void {
			graphics.clear();
			
			graphics.beginFill(colorTable[0]);
			graphics.drawRect(0, 0, WIDTH, HEIGHT);
			graphics.endFill();
			
			graphics.lineStyle(2, 0x000000);
			for (var i:int = 0; i < map.length; i++){
				for (var j:int = 0; j < map[0].length; j++) {
					var t:int = map[i][j];
					if(t!=0){
						graphics.beginFill(colorTable[map[i][j]]);
						//graphics.drawRect(j * W, i * H, W, H);
						graphics.drawRoundRect(j * W, i * H, W, H, 5);
						graphics.endFill();
					}
				}
			}
			
			var cy:int = yy;
			while (!isCollision(map, block, xx, cy + 1))	cy++;
			for (i = 0; i < block.length; i++){
				for (j = 0; j < block[0].length; j++) {
					t = block[i][j];
					if(t!=0){
						graphics.beginFill(colorTable[t],0.3);
						//graphics.drawRect((xx + j) * W, (cy + i) * H, W, H);
						graphics.drawRoundRect((xx + j) * W, (cy + i) * H, W, H, 5);
						graphics.endFill();
					}
				}
			}
			for (i = 0; i < block.length; i++){
				for (j = 0; j < block[0].length; j++) {
					t = block[i][j];
					if(t!=0){
						graphics.beginFill(colorTable[t]);
						//graphics.drawRect((xx + j) * W, (yy + i) * H, W, H);
						graphics.drawRoundRect((xx + j) * W, (yy + i) * H, W, H, 5);
						graphics.endFill();
					}
				}
			}
			
		}
		
		public function clearMap(m:Array):int
		{
			var t:Array = new Array();
			var k:int = 0;
			for (var i:int = 0; i < m.length ; i++) {
				var b:Boolean = true;
				for (var j:int = 0; j < m[0].length; j++) {
					if (map[i][j] == 0) {
						b = false;
					}
				}
				if (b) {
					t[k++] = i;
				}
			}
			
			for (i = 0; i < t.length ; i++) {
				for (var ii:int = t[i]; ii >0; ii--) {
					for (var jj:int = 0; jj < map[0].length; jj++) {
						m[ii][jj] = m[ii - 1][jj]
						m[ii - 1][jj] = 0;
					}
				}
			}
			return t.length;
		}
		
		public function getRotateBlock(b:Array):Array
		{
			var t:Array = newArray(b[0].length,b.length);
			for (var i:int = 0; i < b[0].length; i++) {
				for (var j:int = 0; j < b.length; j++) {
					t[i][j] = b[b.length - j - 1][i];
				}
			}
			return t;
		}
		
		public function getRotateBlockR(b:Array):Array
		{
			var t:Array = newArray(b[0].length,b.length);
			for (var i:int = 0; i < b.length; i++){
				for (var j:int = 0; j < b[0].length; j++) {
					t[b[0].length - j - 1][i] = b[i][j];
				}
			}
			return t;
		}
		
		public function isCollision(m:Array, b:Array, x:int, y:int):Boolean
		{
			for (var i:int = 0; i < b.length; i++) {
				var yy:int = y + i;
				for (var j:int = 0; j < b[0].length; j++) {
					var xx:int = x + j;
					if (b[i][j] != 0 && (yy<0 || yy>=m.length || 
										xx<0 || xx>=m[0].length || 
										m[yy][xx] != 0)) {
						return true;
					}
				}
			}
			return false;
		}

		
		public function newArray(ii:int, jj:int):Array
		{
			var t:Array = new Array(ii);
			for (var i:int = 0; i < ii; i++)
			{
				t[i] = new Array(jj);
			}
			for (i = 0; i < t.length; i++) {
				for (var j:int = 0; j < t[0].length; j++) {
					t[i][j] = 0;
				}
			}
			return t;
		}
		public function init():void
		{
			map = newArray(20, 10);
			
			blocks = 
			[[
				[5, 5],
				[5, 5],
			 ],
			[	[0, 0, 0, 0],
				[1, 1, 1, 1],
				[0, 0, 0, 0],
			 ],
			[	
				[7, 0, 0],
				[7, 7, 7],
				[0, 0, 0], ],
			[	
				[0, 0, 6],
				[6, 6, 6],
				[0, 0, 0], ],
			[	[3, 3, 0],
				[0, 3, 3],
				[0, 0, 0],
				 ],
			[	[0, 4, 4],
				[4, 4, 0],
				[0, 0, 0],
				 ],
			[	
				[0, 2, 0],
				[2, 2, 2],
				[0, 0, 0], ],
			];

                        WIDTH = map[0].length * W;
                        HEIGHT = map.length * H;
		}

		
	}
}