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

forked from: 普通のテトリス - Tetris

参考サイト - http://www.nhk.or.tv/kow/program/program_121.php
Get Adobe Flash player
by kanariia 19 Oct 2009
// forked from rsakane's 普通のテトリス - Tetris
package
{
	/*
	 * 参考サイト - http://www.nhk.or.tv/kow/program/program_121.php
	 */
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.geom.Matrix;
	import flash.utils.ByteArray;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFieldAutoSize;
	import flash.ui.Keyboard;
	import frocessing.color.ColorHSV;
	
	[SWF(width="465", height="465", frameRate="30", backgroundColor="0x393939")]
	public class Main extends Sprite
	{
		private var block:Array;
		private var field:Array;
		private var px:int = 4;
		private var py:int = 0;
		private var frameCount:int = 0;
		private var blockType:int;
		private var nextBlock:int = Math.random() * Block.block.length;
		private var nextDrawBlock:Array;
		private var lines:int = 0;
		private var linesText:TextField;
		
		public function Main()
		{
			Wonderfl.capture_delay(25);
			createTextField(null, "Next:", 270, 35, 20);
			createTextField(null, "Move", 270, 200, 20);
			createTextField(null, "[A, S, D], [←, ↓, →]", 270, 220, 13);
			createTextField(null, "Rotate", 270, 250, 20);
			createTextField(null, "W, ↑, Enter, Space", 270, 270, 13);
			createTextField(null, "一回、画面をクリックしないと", 270, 400, 13);
			createTextField(null, "キーが反応しないかもしれません", 270, 420, 13);
			
			linesText = createTextField(null, "lines: 0", 270, 120, 20);
			block = new Array(21);
			for (var i:int = 0; i < block.length; i++) block[i] = new Array(12);
			
			for (var y:int = 0; y < 21; y++)
			{
				for (var x:int = 0; x < 12; x++)
				{
					if (y == 20 || x == 0 || x == 11) block[y][x] = 9;
					else block[y][x] = 0;
				}
			}
			field = clone(block);
			
			createBlock();
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
		}
		
		private function onEnterFrame(event:Event):void
		{
			if (frameCount++ % 15 != 0) return;
			
			if (checkOverlap(py + 1, px)) lockBlock(), createBlock();
			else moveBlock(py + 1, px);
			draw();
		}
		
		private function draw():void
		{
			graphics.clear();
			var hsv:ColorHSV = new ColorHSV();
			var matrix:Matrix = new Matrix();
			var color:int; // 変数定義、重複避け
			matrix.createGradientBox(20, 20, 45 * Math.PI / 180);
			
			for (var y:int = 0; y < 21; y++)
			{
				for (var x:int = 0; x < 12; x++)
				{
					matrix.createGradientBox(20, 20, 45 * Math.PI / 180, x * 20, y * 20);
					if (block[y][x] == 0)
					{
						graphics.beginGradientFill("linear", [0xAAAAAA, 0xEEEEEE], [1.0, 1.0], [0, 255], matrix);
						graphics.drawRect(x * 20, y * 20, 20, 20);
						graphics.endFill();
					}
					else if (block[y][x] == 9)
					{
						graphics.beginGradientFill("linear", [0xFF556F, 0xFF979C], [1.0, 1.0], [0, 255], matrix);
						graphics.drawRect(x * 20, y * 20, 20, 20);
						graphics.endFill();
					}
					else
					{
						color = Block.color[block[y][x] - 1];
						hsv.r = color >> 16 & 0xFF, hsv.g = color >> 8 & 0xFF, hsv.b = color & 0xFF;
						hsv.s = 0.9;
						hsv.v = 0.9;
						graphics.beginGradientFill("linear", [hsv.value, Block.color[block[y][x] - 1]], [1.0, 1.0], [0, 255], matrix);
						graphics.drawRect(x * 20, y * 20, 20, 20);
						graphics.endFill();
					}
				}
			}
			
			color = Block.color[nextBlock];
			hsv.r = color >> 16 & 0xFF, hsv.g = color >> 8 & 0xFF, hsv.b = color & 0xFF;
			hsv.s = 0.9;
			hsv.v = 0.9;
			
			
			for (y = 0; y < 4; y++)
			{
				for (x = 0; x < 4; x++)
				{
					if (nextDrawBlock[y][x])
					{
						matrix.createGradientBox(20, 20, 45 * Math.PI / 180, 350 + x * 20, 10 + y * 20);
						graphics.beginGradientFill("linear", [hsv.value, Block.color[nextBlock]], [1.0, 1.0], [0, 255], matrix);
						graphics.drawRect(350 + x * 20, 10 + y * 20, 20, 20);
						graphics.endFill();
					}
				}
			}
		}
		
		private function createBlock():void
		{
			py = 0;
			px = 4;
			
			blockType = nextBlock;
			nextBlock = Math.random() * Block.block.length;
			nextDrawBlock = clone(Block.block[nextBlock]);
			
			for (var y:int = 0; y < 4; y++)
			{
				for (var x:int = 0; x < 4; x++)
				{
					if (Block.block[blockType][y][x] && block[py + y][px + x]) removeEventListener(Event.ENTER_FRAME, onEnterFrame), createTextField(null, "GAME OVER", 270, 330, 20);
					block[py + y][px + x] += Block.block[blockType][y][x];
				}				
			}
		}
		
		private function moveBlock(yy:int, xx:int):void
		{
			for (var y:int = 0; y < 4; y++)
			{
				for (var x:int = 0; x < 4; x++)
				{
					block[py + y][px + x] -= Block.block[blockType][y][x];
				}				
			}
			
			py = yy;
			px = xx;
			
			for (y = 0; y < 4; y++)
			{
				for (x = 0; x < 4; x++)
				{
					block[py + y][px + x] += Block.block[blockType][y][x];
				}				
			}
			
			draw();
		}
		
		private function checkOverlap(yy:int, xx:int):Boolean
		{
			for (var y:int = 0; y < 4; y++)
			{
				for (var x:int = 0; x < 4; x++)
				{
					if (Block.block[blockType][y][x])
					{
						if (field[yy + y][xx + x] != 0) return true;
					}
				}				
			}
			
			return false;
		}
		
		private function lockBlock():void
		{
			for (var y:int = 0; y < 21; y++)
			{
				for (var x:int = 0; x < 12; x++)
				{
					field[y][x] = block[y][x];
				}
			}
			
			checkLines();
			
			for (y = 0; y < 21; y++)
			{
				for (x = 0; x < 12; x++)
				{
					block[y][x] = field[y][x];
				}
			}
		}
		
		private function controlBlock(yy:int, xx:int):void
		{
			if (!checkOverlap(py + yy, px + xx)) moveBlock(py + yy, px + xx)
		}
		
		private function onKeyDown(event:KeyboardEvent):void
		{
			if (event.keyCode == 37) controlBlock(0, -1); // ←
			if (event.keyCode == 40) controlBlock(1,  0); // ↓
			if (event.keyCode == 39) controlBlock(0,  1); // →
			if (event.keyCode == 38) turnBlock(); // ↑
			if (event.keyCode == 65) controlBlock(0, -1); // A
			if (event.keyCode == 68) controlBlock(0,  1); // D
			if (event.keyCode == 83) controlBlock(1,  0); // S
			if (event.keyCode == 87) turnBlock(); // W
			if (event.keyCode == Keyboard.SPACE) turnBlock();
			if (event.keyCode == Keyboard.ENTER) turnBlock();
		}
		
		private function turnBlock():void
		{
			var temp:Array = clone(Block.block[blockType]);
			for (var y:int = 0; y < 4; y++)
			{
				for (var x:int = 0; x < 4; x++)
				{
					Block.block[blockType][y][x] = temp[3 - x][y];
				}
			}
			
			if (checkOverlap(py, px))
			{
				Block.block[blockType] = clone(temp);
				return;
			}
			
			for (y = 0; y < 4; y++)
			{
				for (x = 0; x < 4; x++)
				{
					block[py + y][px + x] -= temp[y][x];
					block[py + y][px + x] += Block.block[blockType][y][x];
				}
			}
					
			draw();
		}
		
		private function checkLines():void
		{
			var found:Boolean;
			
			for (var y:int = 19; y >= 0; y--)
			{
				found = true;
				for (var x:int = 1; x <= 11; x++)
				{
					if (field[y][x] == 0)
					{
						found = false;
						break;
					}
				}
				if (found)
				{
					for (var xx:int = 1; xx <= 11; xx++) field[y][xx] == 0;
					for (var yy:int = y; yy >= 1; yy--)
					{
						for (xx = 1; xx <= 11; xx++)
						{
							field[yy][xx] = field[yy - 1][xx];
						}
					}
					y++;
					linesText.text = "lines: " + String(++lines);
				}
			}
		}
		
		private function createTextField(parent:Sprite = null, text:String = "", x:int = 0, y:int = 0, fontSize:int = 13):TextField
		{
			var tf:TextField = new TextField();
			tf.x = x, tf.y = y;
			tf.defaultTextFormat = new TextFormat("_typeWriter", fontSize, 0xFFFFFF);
			tf.text = text;
			tf.selectable = false;
			tf.autoSize = TextFieldAutoSize.LEFT;
			if (parent) parent.addChild(tf);
			else this.addChild(tf);
			
			return tf;
		}
		
		private function clone(arg:*):*
		{
			var b:ByteArray = new ByteArray();
			b.writeObject(arg);
			b.position = 0;
			return b.readObject();
		}
	}
}

class Block
{
	public static var block:Array = [[[0, 0, 0, 0],
								      [0, 1, 1, 0],
							          [0, 1, 1, 0],
							          [0, 0, 0, 0]],
									 [[0, 0, 2, 0],
								      [0, 0, 2, 0],
							          [0, 0, 2, 0],
							          [0, 0, 2, 0]],
									 [[0, 0, 0, 0],
								      [0, 3, 3, 0],
							          [0, 3, 0, 0],
							          [0, 3, 0, 0]],
									 [[0, 0, 4, 0],
								      [0, 4, 4, 0],
							          [0, 4, 0, 0],
							          [0, 0, 0, 0]],
									 [[0, 5, 0, 0],
								      [0, 5, 5, 0],
							          [0, 0, 5, 0],
							          [0, 0, 0, 0]],
									 [[0, 0, 0, 0],
								      [0, 6, 0, 0],
							          [6, 6, 6, 0],
							          [0, 0, 0, 0]],
									 [[0, 0, 0, 0],
								      [0, 7, 7, 0],
							          [0, 0, 7, 0],
							          [0, 0, 7, 0]]];
	
	//
	public static var color:Array = [0xFFFF00, 0x00FFFF, 0x0000FF, 0xFF0000, 0x00FF00, 0xAA00FF, 0xFFA500]; 
	
	public function Block(){}
}