Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import flash.display.MovieClip;
- import flash.display.Sprite;
- import flash.text.TextFormat;
- import flash.text.TextField;
- import flash.text.TextFieldAutoSize;
- import flash.Lib;
- import Math;
- import flash.events.Event;
- import flash.utils.Timer;
- import flash.events.TimerEvent;
- import flash.events.KeyboardEvent;
- class Square extends Sprite
- {
- public var color: Int;
- public var side: Int;
- public function new(c: Int, s: Int)
- {
- super();
- color = c; side = s;
- draw();
- }
- public function setcolor(c: Int)
- {
- color = c;
- draw();
- }
- public function draw()
- {
- this.graphics.clear();
- this.graphics.beginFill(color);
- this.graphics.drawRect(0, 0, side, side);
- this.graphics.endFill();
- }
- }
- //no functions outside of classes?
- class M
- {
- public static function matr_copy(m: Array<Array<Int>>)
- {
- var m_new: Array<Array<Int>> = [];
- for(i in 0...m.length)
- {
- var row: Array<Int>;
- row = [];
- for(j in 0...m[i].length)
- {
- row.push(m[i][j]);
- }
- m_new.push(row);
- }
- return m_new;
- }
- public static function rotate(m: Array<Array<Int>>)
- {
- var m_new = [];
- var i_n = -1;
- //how to do backward iteration?
- var j_first=m[0].length-1; var j_last = 0; var j_step = -1;
- var j = j_first;
- var i_n = -1;
- var j_n = -1;
- while(j >= j_last)
- {
- i_n += 1; j_n = -1;
- var n_row = [];
- for(i in 0...m.length)
- {
- j_n += 1;
- n_row.push(m[i][j]);
- }
- j += j_step;
- m_new.push(n_row);
- }
- return m_new;
- }
- public static function does_collide(m_parent: Array<Array<Int>>, m_child: Array<Array<Int>>, c_row: Int, c_col: Int)
- {
- var ni, nj: Int;
- for(i in 0...m_child.length)
- {
- for(j in 0...m_child[i].length)
- {
- ni = i + c_row; nj = j + c_col;
- if( (0 <= ni) && (ni < m_parent.length) && (0 <= nj) && (nj <= m_parent[0].length) )
- {
- if( (m_child[i][j] != 0) && (m_parent[ni][nj] != 0) ) return true;
- }
- }
- }
- return false;
- }
- public static function matr_combine(m_parent : Array<Array<Int>>, m_child : Array<Array<Int>>, c_row : Int, c_col : Int)
- {
- var m_new = matr_copy(m_parent);
- for(i in 0...m_child.length)
- for(j in 0...m_child[0].length)
- {
- if( m_child[i][j] > 0 ) m_new[i+c_row][j+c_col] = m_child[i][j];
- }
- return m_new;
- }
- public static function matr_print(m: Array<Array<Int>>)
- {
- for(r in m)
- {
- trace(r);
- }
- }
- public static function matrix_0(rows: Int, cols: Int)
- {
- var matr_new: Array<Array<Int>> = [];
- var row: Array<Int>;
- for(i in 0...rows)
- {
- row = [];
- for(j in 0...cols)
- {
- row.push(0);
- }
- matr_new.push(row);
- }
- return matr_new;
- }
- }
- class Glass
- {
- public var matr: Array<Array<Int>>;
- //var twall: Int = 1; - doesn't seem to be allowed here
- var twall: Int;
- var padding: Int;
- public var full: Bool;
- var width: Int;
- var height: Int;
- public var cleared: Int;
- public function new(h: Int, w: Int)
- {
- twall = 1; padding = 1; full = false; cleared = 0;
- width = w;
- height = h;
- matr = M.matrix_0(padding+height+twall+padding, padding+twall+width+twall+padding);
- for(i in padding...padding+height)
- {
- matr[i][padding] = 1;
- matr[i][padding+twall+width] = 1;
- }
- for(i in padding...padding+width+twall+1)
- {
- matr[padding+height][i] = 1;
- }
- }
- function is_row_full(n_row: Int)
- {
- for(i in padding+twall...padding+twall+width)
- {
- if(matr[n_row][i] == 0) return false;
- }
- return true;
- }
- function is_row_empty(n_row: Int)
- {
- for(i in padding+twall...padding+twall+width)
- {
- if(matr[n_row][i] != 0) return false;
- }
- return true;
- }
- function destroy_row(n_row: Int)
- {
- //for(i in n_row...padding, -1) - backward iteration, which doesn't work (?)
- var i: Int = n_row;
- while(i > padding)
- {
- for(j in padding+twall...padding+twall+width) matr[i][j] = matr[i-1][j];
- i -= 1;
- }
- }
- function compact()
- {
- for(i in 0...padding+height)
- {
- if(is_row_full(i))
- {
- destroy_row(i);
- cleared += 1;
- }
- }
- if(!is_row_empty(padding)) full = true;
- }
- public function settle(fig: Figure)
- {
- matr = M.matr_combine(matr, fig.matr, fig.row, fig.col);
- compact();
- }
- }
- class Figure
- {
- public var matr: Array<Array<Int>>;
- public var row: Int;
- public var col: Int;
- var glass: Glass;
- public var settled: Bool;
- public function new(m: Array<Array<Int>>, g: Glass, r: Int, c: Int)
- {
- settled = false;
- glass = g;
- matr = M.matr_copy(m);
- row = r;
- col = c;
- }
- public function rotate()
- {
- var matr_new = M.rotate(matr);
- if(!M.does_collide(glass.matr, matr_new, row, col)) matr = matr_new;
- }
- public function drop()
- {
- if(settled) return;
- if(!M.does_collide(glass.matr, matr, row+1, col)) row += 1;
- else
- {
- settled = true;
- glass.settle(this);
- }
- }
- public function move_left()
- {
- if(!M.does_collide(glass.matr, matr, row, col-1)) col -= 1;
- }
- public function move_right()
- {
- if(!M.does_collide(glass.matr, matr, row, col+1)) col += 1;
- }
- }
- class TetServer
- {
- public var glass: Glass;
- public var figure: Figure;
- public var falling_speed: Float;
- public var falling_acc: Float;
- public var fmatrices: Array<Array<Array<Int>>>;
- public function full()
- {
- return glass.full;
- }
- public function new()
- {
- falling_speed = 0.5;
- falling_acc = 0.0;
- glass = new Glass(20, 10);
- fmatrices = [];
- var I = [ [0, 0, 0, 0],
- [8, 8, 8, 8],
- [0, 0, 0, 0]];
- var L = [ [0, 0, 0, 0, 0],
- [0, 2, 2, 2, 0],
- [0, 2, 0, 0, 0],
- [0, 0, 0, 0, 0]];
- var J = [ [0, 0, 0, 0, 0],
- [0, 3, 3, 3, 0],
- [0, 0, 0, 3, 0],
- [0, 0, 0, 0, 0]];
- var O = [ [0, 0, 0, 0],
- [0, 4, 4, 0],
- [0, 4, 4, 0],
- [0, 0, 0, 0]];
- var S = [[0, 0, 0, 0, 0],
- [0, 0, 5, 5, 0],
- [0, 5, 5, 0, 0],
- [0, 0, 0, 0, 0]];
- var T = [ [0, 0, 0, 0, 0],
- [0, 0, 6, 0, 0],
- [0, 6, 6, 6, 0],
- [0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0]];
- var Z = [ [0, 0, 0, 0, 0],
- [0, 7, 7, 0, 0],
- [0, 0, 7, 7, 0],
- [0, 0, 0, 0, 0]];
- fmatrices = [I, L, J, O, S, T, Z];
- next_figure();
- }
- public function cleared_lines()
- {
- return glass.cleared;
- }
- public function frame()
- {
- return M.matr_combine(glass.matr, figure.matr, figure.row, figure.col);
- }
- public function next_figure()
- {
- var i = Math.round(Math.random()*(fmatrices.length-1));
- figure = new Figure(fmatrices[i], glass, 0, 5);
- }
- public function fall_auto()
- {
- if(figure.settled) next_figure();
- falling_acc += falling_speed;
- if(falling_acc >= 1.0)
- {
- falling_acc = 0.0;
- figure.drop();
- }
- }
- public function fall(amount: Int)
- {
- for(i in 0...amount)
- {
- figure.drop();
- }
- }
- public function move_left()
- {
- figure.move_left();
- }
- public function move_right()
- {
- figure.move_right();
- }
- public function rotate()
- {
- figure.rotate();
- }
- }
- class TetClient
- {
- var gColors: Array<Int>;
- var leds: Array<Array<Square>>;
- var matr: Array<Array<Int>>;
- var tserv: TetServer;
- var paused: Bool;
- var timer: Timer;
- var pausedText: TextField;
- var linesText: TextField;
- var ptf: TextFormat;
- public function new()
- {
- gColors = [0x000000, 0xFFFFFF, 0xF0A000, 0xF00000, 0x00F0F0, 0x0000F0, 0xF0F000, 0x00F000, 0xA000F0];
- tserv = new TetServer();
- matr = tserv.frame();
- leds = [];
- var n_r = 20;
- for(i in 0...matr.length)
- {
- var n_c = 50;
- var row: Array<Square> = [];
- for(j in 0...matr[0].length)
- {
- var r = new Square(gColors[matr[i][j]], 20);
- r.x = n_c; r.y = n_r;
- Lib.current.addChild(r);
- row.push(r);
- n_c += 20;
- }
- leds.push(row);
- n_r += 20;
- }
- ptf = new TextFormat();
- ptf.color = 0x00FFFF;
- ptf.size = 30;
- pausedText = new TextField();
- pausedText.autoSize = TextFieldAutoSize.LEFT;
- pausedText.text = "";
- pausedText.setTextFormat(ptf);
- pausedText.selectable = false;
- pausedText.x = 100; pausedText.y = 200;
- Lib.current.addChild(pausedText);
- linesText = new TextField();
- linesText.autoSize = TextFieldAutoSize.LEFT;
- linesText.text = "Lines: 0";
- linesText.setTextFormat(ptf);
- linesText.selectable = false;
- linesText.x = 100; linesText.y = 500;
- Lib.current.addChild(linesText);
- }
- public function start()
- {
- timer = new Timer(500, 0);
- timer.addEventListener(TimerEvent.TIMER, onTimer);
- timer.start();
- Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKey);
- }
- public function stop()
- {
- timer.removeEventListener(TimerEvent.TIMER, onTimer);
- Lib.current.stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKey);
- }
- function onTimer(evt: TimerEvent)
- {
- if(paused) return;
- tserv.fall_auto();
- doDraw();
- }
- function onKey(evt: KeyboardEvent)
- {
- if(evt.keyCode == 80)
- {
- paused = !paused;
- if(paused) pausedText.text = "...PAUSED...";
- else pausedText.text = "";
- pausedText.setTextFormat(ptf);
- }
- if(paused) return;
- switch(evt.keyCode)
- {
- case 37: tserv.move_left();
- case 38: tserv.rotate();
- case 39: tserv.move_right();
- case 40: tserv.fall(3);
- default: return;
- }
- doDraw();
- }
- function doDraw()
- {
- matr = tserv.frame();
- for(i in 0...matr.length)
- {
- for(j in 0...matr[0].length)
- {
- leds[i][j].setcolor(gColors[matr[i][j]]);
- }
- }
- linesText.text = "Lines: " + Std.string(tserv.cleared_lines());
- linesText.setTextFormat(ptf);
- if(tserv.full())
- {
- timer.stop();
- pausedText.text = "...FINISH...";
- pausedText.setTextFormat(ptf);
- }
- }
- }
- class Tetris
- {
- public function new()
- {
- Lib.current.addEventListener( Event.ENTER_FRAME, firstframe );
- }
- static function main()
- {
- var self = new Tetris();
- }
- public function firstframe(evt:Event)
- {
- Lib.current.removeEventListener( Event.ENTER_FRAME, firstframe );
- var app = new TetClient();
- app.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement