forked from: niconico Tetris
// forked from flashrod's Tetris
// forked from nitoyon's
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.display.Loader;
import flash.display.BitmapData;
import flash.geom.Matrix;
import flash.net.URLRequest;
public class Tetris extends Sprite {
private const W:int = 10;
private const H:int = 20;
private const UNIT:int = 22;
private const COLOR:Array =[0x000000, 0x00FFFF, 0xFFFF00, 0x22FF22, 0xFF2222, 0x4444FF, 0xFF8844, 0xFF22FF];
//padroes
private const PAT:Array =[[[1, 1, 1, 1]],
[[0, 2, 0], [2, 2, 2]],
[[3, 3, 0], [0, 3, 3]],
[[0, 4, 4], [4, 4, 0]],
[[5, 5], [5, 0], [5, 0]],
[[6, 6], [0, 6], [0, 6]],
[[7, 7], [7, 7]]];
private const SPEED:Array = [30, 20, 10, 5];
private const VK_H:int = 72; // h
private const VK_J:int = 74; // j
private const VK_K:int = 75; // k
private const VK_L:int = 76; // l
private const VK_LEFT:int = 37; // left
private const VK_RIGHT:int = 39; // right
private const VK_UP:int = 38; // up
private const VK_DOWN:int = 40; // down
private const VK_SPC:int = 32; // space
private var field:Array = [];
private var piece:Array;
private var next:Array;
private var text:TextField = new TextField();
private var keytable:Array = [];
private var count:int = 0;
private var step:int = 0;
private var px:int;
private var py:int;
public static var param:int = 0;
public function Tetris() {
text.x = W*UNIT;
text.text= "Next:";
addChild(text);
var t:TextField = new TextField();
t.x = W * UNIT;
t.y = 8 * UNIT;
t.text = "usage:\n h(or left),j(or up),k,\n l(or right),SPACE(or down)";
t.autoSize = "left";
addChild(t);
field = new Array(H).map(function():*{
return new Array(W).map(function():*{
return 0;
})
});
//teclado
keytable[VK_H] = keytable[VK_LEFT] = function():void {px -= space(px-1, py, piece)};
keytable[VK_J] = keytable[VK_UP] = function():void {rotate(true)};
keytable[VK_K] = function():void {rotate(false)};
keytable[VK_L] = keytable[VK_RIGHT] = function():void {px += space(px+1, py, piece)};
keytable[VK_SPC] = keytable[VK_DOWN] = function():void {drop(); pick();};
stage.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent):void {
if (keytable[e.keyCode]) {
//chamada de funcao
keytable[e.keyCode]();
repaint();
}
});
pick();
pick();
addEventListener(Event.ENTER_FRAME, function(e:Event):void {
param = (param + 9) % 360;
for (var j:int = 0; j < numChildren; j++) {
if(getChildAt(j) is Body) Body(getChildAt(j)).update();
}
if (--step < 0) {
step=SPEED[int(count/10)];
if (space(px, py+1, piece)) {
++py;
repaint();
} else {
drop();
pick();
}
}
});
loadBitmap();
}
private function rotate(clock:Boolean):void {
var r:Array = new Array(piece[0].length).map(function():*{return [];});
for (var j:int = 0; j<piece.length; ++j)
for (var i:int = 0; i < r.length; ++i)
if (clock)
r[i][piece.length-1-j] = piece[j][i];
else
r[r.length-1-i][j] = piece[j][i];
if (space(px, py, r))
piece = r;
}
private function repaint():void {
for (var j:int = numChildren - 1; j >= 0; j--) {
if (getChildAt(j) is Body) removeChildAt(j);
}
graphics.clear();
graphics.lineStyle(0);
graphics.drawRect(0, 0, W*UNIT, H*UNIT);
graphics.endFill();
for (j = 0; j < H; ++j)
for (var i:int = 0; i < W; ++i) {
var g:int = 0;
if (py <= j && j < (py+piece.length) && px <= i && i < (px+piece[0].length))
g = piece[j-py][i-px];
if (g == 0)
g = field[j][i];
if(g == 0) continue;
var body:Body = new Body(COLOR[g], myBitmap);
addChild(body);
body.x = (i+.5)*UNIT;
body.y = (j-.5)*UNIT;
}
for (j = 0; j < next.length; ++j)
for (i = 0; i < next[j].length; ++i) {
if(next[j][i] == 0) continue;
var body2:Body = new Body(COLOR[next[j][i]], myBitmap);
addChild(body2);
body2.x = (i+W+1)*UNIT;
body2.y = (j+2)*UNIT;
}
}
private function space(x:int, y:int, p:Array):int {
for (var j:int = 0; j < p.length; ++j) {
if (0 > (y+j) || (y+j) >= H)
return 0;
for (var i:int = 0; i < p[j].length; ++i) {
if (0 > (x+i) || (x+i) >= W)
return 0;
if (p[j][i] && field[y+j][x+i])
return 0;
}
}
return 1;
}
private function drop():void {
for (; space(px, py+1, piece); py++)
;
for (var j:int = 0; j < piece.length; ++j)
for (var i:int = 0; i < piece[j].length; ++i)
if (piece[j][i])
field[py+j][px+i] = piece[j][i];
for (j=0; j<H; ++j)
if (field[j].indexOf(0) < 0) {
field.splice(j, 1);
field.unshift([]);
for (i=0; i<W; ++i)
field[0][i] = 0;
}
count++;
if (count/10 >= SPEED.length)
count = 0;
}
private function pick():void {
piece = next;
if (piece != null) {
px = (W-piece[0].length)/2;
py = 0;
if (!space(px, py, piece))
text.text="GAME OVER";
}
next = PAT[int(Math.random()*PAT.length)];
}
private var url:String = "http://wonderfl.net/img/common/ico_fork.gif";
private var loader:Loader = new Loader();
private var myBitmap:BitmapData = null;
private function loadBitmap() {
var request:URLRequest = new URLRequest(url);
loader.load(request);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, drawImage);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}
public function drawImage(event:Event):void {
var mySprite:Sprite = new Sprite();
myBitmap = new BitmapData(loader.width, loader.height, true, 0);
myBitmap.draw(loader);
}
private function ioErrorHandler(event:IOErrorEvent):void {
trace("Unable to load image: " + url);
}
}
}
import flash.display.Sprite;
import flash.filters.GlowFilter;
import flash.display.BitmapData;
import flash.geom.Matrix;
//corpo = celulas das peças: quadrado
class Body extends Sprite{
private var eye1:Sprite;
private var eye2:Sprite;
private var antena:Sprite;
public function Body(color:uint, bitmapData:BitmapData = null){
scaleX = scaleY = 1;
// quadrado externo
var s2:Sprite = new Sprite();
s2.graphics.lineStyle(1, 0x000000);
s2.graphics.beginFill(color);
s2.graphics.drawRect(-10, 0, 20, 20);
s2.graphics.endFill();
s2.y = 10;
addChild(s2);
// quadrado interno
var s3:Sprite = new Sprite();
if (bitmapData) {
var matrix:Matrix = new Matrix();
matrix.translate(8,0);
s3.graphics.beginBitmapFill(bitmapData, matrix);
s3.graphics.drawRect(-7.5, 0, 16, 16);
s3.graphics.endFill();
s3.y = 12;
addChild(s3);
}
update();
}
public function update():void{
// -1 < s < 1
// seno?
// var s:Number = Math.cos(Tetris.param / 180 * Math.PI);
}
}