/**
* Copyright yukifuruyoru ( http://wonderfl.net/user/yukifuruyoru )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/mZRQ
*/
/*ChiquiTRiS*/
/*Yukifuruyoru 2010*/
/*
Controls:
RIGHT,LEFT,DOWN: Move
Z,X: Spin
C: Hard drop
A: Hold
P: Pause/Resume game
R: Reset game
*/
package {
import flash.display.Sprite;
public class Main extends Sprite {
public function Main() {
var game:Game=new Game(this);
}
}
}
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.geom.Matrix;
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
class Game{
private var datI:BitmapData=(new Dna([[4,2],[0x0,0xFF00ffff],[[1,1,1,1]]])).bmp;
private var datJ:BitmapData=(new Dna([[4,2],[0x0,0xFF0000ff],[[1],[1,1,1]]])).bmp;
private var datL:BitmapData=(new Dna([[4,2],[0x0,0xFFff5500],[[0,0,1],[1,1,1]]])).bmp;
private var datS:BitmapData=(new Dna([[4,2],[0x0,0xFF00ff00],[[0,1,1],[1,1]]])).bmp;
private var datZ:BitmapData=(new Dna([[4,2],[0x0,0xFFff0000],[[1,1],[0,1,1]]])).bmp;
private var datT:BitmapData=(new Dna([[4,2],[0x0,0xFFaa00ff],[[0,1],[1,1,1]]])).bmp;
private var datO:BitmapData=(new Dna([[4,2],[0x0,0xFFffff00],[[0,1,1],[0,1,1]]])).bmp;
private var fra:BitmapData=(new Dna([[30,30],[0xff5500ff,0x0],[[0],[0],[0,0,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1],[0,0,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1],[0,0,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1],[0,0,1,1,1,1,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1,0,0,0,1,0,0,1],[0,0,0,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1],[0,0,0,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1]]])).bmp;
private var tgt:Sprite;
private var teatro:Sprite=new Sprite;
private var bplate:Sprite=new Sprite;
private var bottle:BitmapData=new BitmapData(30,30,true,0x0);
private var scoreboard:BitmapData=new BitmapData(4,4,true,0x0);
private var neon:Sprite=new Sprite;
private var sboard:Sprite=new Sprite;
private var bg:Sprite=new Sprite;
private var stack:Vector.<Sprite>=new Vector.<Sprite>(0);
private var _pause:Boolean=true;
private var holden:Sprite;
private var droppen:Sprite;
private var holdable:Boolean;
private var gravT:Timer=new Timer(800);
private var pausT:Timer=new Timer(500);
private var deleT:Timer=new Timer(250,1);
private var P:Point=new Point(0,0);
private var caution:Boolean;
private var endFlag:Boolean=true;
private var score:uint=0;
public function Game(s:Sprite):void{
tgt=s;
initLayers();
initCtrl();
initTimers();
tgt.addChild(teatro);
reset();
}
private function initTimers():void{
gravT.addEventListener(TimerEvent.TIMER, onGrav);
pausT.addEventListener(TimerEvent.TIMER, onPaus);
deleT.addEventListener(TimerEvent.TIMER, cleanup);
}
private function onGrav(e:TimerEvent):void{
if(droppen is Object){
move(2);
if(caution){tfix();}
}
else{next();}
}
private function set pause(io:Boolean):void{
_pause=io;
if(endFlag){reset();}
if(pause){
neon.visible=true;
pausT.start();
gravT.stop();
magni(9);
}
else{
pausT.stop();
neon.visible=false;
gravT.start();
magni(1);
}
}
private function get pause():Boolean{return _pause}
private function onPaus(e:TimerEvent):void{
neon.visible=!neon.visible;
}
private function next():void{
holdable=true;
if(bplate.numChildren){bplate.removeChildAt(0);}
droppen=null;
drop(stack.shift());
var spr:Sprite=tetromino(Math.floor(Math.random()*7));
stack.push(spr);
teatro.addChild(spr);
place();
}
private function drop(spr:Sprite):void{
caution=false;
spr.x=13;spr.y=7;
bplate.addChild(spr);
droppen=spr;
P=new Point(0,0);
if(crates){
dive();
pause=true;
magni(1);
endFlag=true;
bottle.threshold(bottle,new Rectangle(0,0,30,30),new Point(0,0),">",0xFF000000,0xFFeeeeee);
}
}
private function hold(spr:Sprite):void{
if(!pause&&holdable){
if(holden is Object){drop(holden);}
else{next();}
spr.x=3;spr.y=2;
spr.getChildAt(0).rotation=0;
teatro.addChild(spr);
holden=spr;
holdable=false;
}
}
private function incinerate():void{
var spr:Sprite;
while(stack.length){
spr=stack.pop();
teatro.removeChild(spr);
}
if(holden is Object){teatro.removeChild(holden);}
if(droppen is Object){bplate.removeChild(droppen);}
holden=null;
droppen=null;
}
private function reset():void{
score=0;
resco();
endFlag=false;
pause=true;
holdable=true;
caution=false;
incinerate();
bottle.fillRect(new Rectangle(0,0,30,30),0);
var spr:Sprite;
for(var i:uint=0;i<4;i++){
spr=tetromino(Math.floor(Math.random()*7));
stack.push(spr);
teatro.addChild(spr);
}
place();
magni(9);
}
private function place():void{
stack[0].x=13;
stack[0].y=2;
for(var i:uint=1;i<4;i++){
stack[i].x=23;
stack[i].y=5*i-3;
}
}
private function initCtrl():void{
tgt.stage.addEventListener(KeyboardEvent.KEY_DOWN,onKey);
}
private function onKey(e:KeyboardEvent):void{
if(48<e.keyCode&&e.keyCode<58){magni(e.keyCode-48);}
switch(e.keyCode){
case 80:{pause=!pause;}break;//P
case 90:{spin(false);}break;//Z
case 88:{spin(true);}break;//X
case 67:{hard();}break;//C
case 65:{hold(droppen);}break;//A
case 82:{reset();}break;//R
case 37:{move(0);}break;//LEFT
case 39:{move(1);}break;//RIGHT
case 40:{move(2);}break;//DOWN
}
}
private function spin(n:Boolean):void{
P=new Point(0,0);
var ang:uint=(n)?270:90;
var L:uint=0;
if(!pause && droppen is Object){
droppen.getChildAt(0).rotation+=ang;
L=crates?1:0;
}
if(L){
P.x=1;
if(!crates){droppen.x++;L=0;}
}
if(L){
P.x=-1;
if(!crates){droppen.x--;L=0;}
}
if(L){P.x=0;droppen.getChildAt(0).rotation-=ang;}
}
private function move(dir:uint):void{
if(!pause && droppen is Object){
switch(dir){
case 0:{P=new Point(-1,0);}break;
case 1:{P=new Point(1,0)}break;
case 2:{P=new Point(0,1)}break;
}
if(crates){
if(dir==2){
if(caution){tfix();}
else{caution=true;}
}
}
else{
switch(dir){
case 0:{droppen.x--;}break;
case 1:{droppen.x++;}break;
case 2:{droppen.y++;caution=false;}break;
}
}
}
}
private function hard():void{
if(droppen is Object){
while(!caution && droppen.y<30){move(2);}
move(2);
}
}
private function get crates():Boolean{
var sol:Boolean;
var A:BitmapData=new BitmapData(30,30,true,0);
A.draw(bplate);
var B:BitmapData=fra.clone();
B.draw(bottle);
sol=B.hitTest(new Point(0,0),255,A,P);
return sol
}
private function tfix():void{
caution=false;
P=new Point(droppen.x,droppen.y);
dive();
delta();
if(P.x){gravT.stop();deleT.reset();deleT.start();}
else{next();}
}
private function dive():void{
bottle.draw(bplate);
droppen.parent.removeChild(droppen);
droppen=null;
}
private function delta():void{
P.x=0;
var sheet:BitmapData=new BitmapData(10,20,true,0xFF000000);
var row:BitmapData=new BitmapData(10,1,true,0xFFffffff);
sheet.copyPixels(bottle,new Rectangle(10,P.y,10,4),new Point(0,0));
sheet.threshold(sheet,new Rectangle(0,0,10,4),new Point(0,0),"<",0xFF000000,0xFF000000);
sheet.threshold(sheet,new Rectangle(0,0,10,4),new Point(0,0),">",0xFF000000,0);
var hits:uint=0;
var io:Boolean;
for(var i:uint=0;i<4;i++){
io=sheet.hitTest(new Point(0,0),255,row,new Point(0,i));
if(!io){
hits+=(1<<i);
bottle.copyPixels(row,new Rectangle(0,0,10,1),new Point(10,P.y+i));
}
}
P.x=hits;
}
private function cleanup(e:TimerEvent):void{
var L:uint=0;
for(var i:uint=0;i<4;i++){
if(P.x&(1<<i)){
bottle.copyPixels(bottle,new Rectangle(10,0,10,P.y+i),new Point(10,1));
L++;
}
}
switch(L){
case 1:{score+=1;}break;
case 2:{score+=3;}break;
case 3:{score+=5;}break;
case 4:{score+=8;}break;
}
resco();
next();
gravT.start();
}
private function resco():void{
score%=0xFFFF;
scoreboard.fillRect(new Rectangle(0,0,4,4),0);
var cifra:uint;
var color:uint;
var w:uint;
var h:uint;
for(h=0;h<4;h++){
cifra=(score>>>(h*4)) & 0xF;
for(w=0;w<4;w++){
if(cifra & 1<<w){scoreboard.setPixel32(w,h,0xFFffffff);}
}
}
}
private function initLayers():void{
var shadow:Sprite=new Sprite;
shadow.graphics.beginFill(0);
shadow.graphics.drawRect(0,0,30,30);
shadow.graphics.endFill();
bg.graphics.beginBitmapFill(bottle);
bg.graphics.drawRect(0,0,30,30);
bg.graphics.endFill();
sboard.graphics.beginBitmapFill(scoreboard);
sboard.graphics.drawRect(0,0,4,4);
sboard.graphics.endFill();
sboard.x=27;sboard.y=22;
sboard.scaleX=-1;
sboard.scaleY=-1;
neon.graphics.beginFill(0xffffff);
neon.graphics.drawRect(0,0,6,6);
neon.graphics.endFill();
neon.x=23;neon.y=23;
var deco:Bitmap=new Bitmap(fra);
teatro.addChild(shadow);
teatro.addChild(bg);
teatro.addChild(bplate);
teatro.addChild(sboard);
teatro.addChild(neon);
teatro.addChild(deco);
magni(1);
}
private function magni(n:uint):void{
teatro.scaleX=n;
teatro.scaleY=n;
teatro.x=Math.round(tgt.stage.stageWidth/2-teatro.width/2);
teatro.y=Math.round(tgt.stage.stageHeight/2-teatro.height/2);
}
private function tetromino(n:uint):Sprite{
var dat:BitmapData;
var tilt:Number=0.5;
switch(n){
case 0:{dat=datI;tilt=0;}break;
case 1:{dat=datJ;}break;
case 2:{dat=datL;}break;
case 3:{dat=datS;}break;
case 4:{dat=datZ;}break;
case 5:{dat=datT;}break;
case 6:{dat=datO;tilt=0;}break;
}
var tetro:Sprite=new Sprite;
var mino:Sprite=new Sprite;
tetro.addChild(mino);
mino.x=2-tilt;
mino.y=2+tilt;
mino.graphics.beginBitmapFill(dat,new Matrix(1,0,0,1,-2+tilt,-1-tilt));
mino.graphics.drawRect(-2+tilt,-1-tilt,4,2);
mino.graphics.endFill();
return tetro;
}
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
import flash.display.BitmapData;
class Dna{
public var size:Vector.<uint>=Vector.<uint>([1,1]);
public var palette:Vector.<uint>=Vector.<uint>([0,0xFFffffff]);
public var map:Vector.<Vector.<uint>>=Vector.<Vector.<uint>>([new Vector.<uint>(1)]);
public function Dna(code:Array){
size=Vector.<uint>(code[0]);
palette=Vector.<uint>(code[1]);
code[2].forEach(
function(row:Array,h:uint,...args):void{
map[h]=Vector.<uint>(row);});
}
public function get bmp():BitmapData{
var sheep:BitmapData=new BitmapData(size[0],size[1],true,palette[0]);
var L:uint=palette.length;
map.forEach(function(row:Vector.<uint>,h:uint,...args):void{
row.forEach(function(c:uint,w:uint,...args):void{if(c&&(c<L)){sheep.setPixel32(w,h,palette[c]);}});
});
return sheep;
}
}