Advertisement
PifyZ

Tetris

Jul 6th, 2014
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 10.55 KB | None | 0 0
  1. import flash.display.MovieClip;
  2. import flash.display.Sprite;
  3. import flash.text.TextFormat;
  4. import flash.text.TextField;
  5. import flash.text.TextFieldAutoSize;
  6. import flash.Lib;
  7. import Math;
  8. import flash.events.Event;
  9. import flash.utils.Timer;
  10. import flash.events.TimerEvent;
  11. import flash.events.KeyboardEvent;
  12.  
  13.  
  14. class Square extends Sprite
  15. {
  16.   public var color: Int;
  17.   public var side: Int;
  18.   public function new(c: Int, s: Int)
  19.   {
  20.     super();
  21.     color = c; side = s;
  22.     draw();    
  23.   }
  24.  
  25.   public function setcolor(c: Int)
  26.   {
  27.     color = c;
  28.     draw();
  29.   }
  30.  
  31.   public function draw()
  32.   {
  33.     this.graphics.clear();
  34.     this.graphics.beginFill(color);
  35.     this.graphics.drawRect(0, 0, side, side);
  36.     this.graphics.endFill();
  37.  
  38.   }
  39. }
  40.  
  41. //no functions outside of classes?
  42. class M
  43. {
  44.   public static function matr_copy(m: Array<Array<Int>>)
  45.   {
  46.     var m_new: Array<Array<Int>> = [];
  47.     for(i in 0...m.length)
  48.       {
  49.     var row: Array<Int>;
  50.     row = [];
  51.     for(j in 0...m[i].length)
  52.       {
  53.         row.push(m[i][j]);
  54.       }
  55.     m_new.push(row);
  56.       }
  57.     return m_new;
  58.   }
  59.  
  60.  
  61.   public static function rotate(m: Array<Array<Int>>)
  62.   {
  63.     var m_new = [];
  64.     var i_n = -1;
  65.     //how to do backward iteration?
  66.     var j_first=m[0].length-1; var j_last = 0; var j_step = -1;
  67.     var j = j_first;
  68.     var i_n = -1;
  69.     var j_n = -1;
  70.     while(j >= j_last)
  71.       {
  72.     i_n += 1; j_n = -1;
  73.     var n_row = [];
  74.     for(i in 0...m.length)
  75.       {
  76.         j_n += 1;
  77.         n_row.push(m[i][j]);
  78.       }
  79.     j += j_step;
  80.     m_new.push(n_row);
  81.       }
  82.     return m_new;
  83.   }
  84.  
  85.   public static function does_collide(m_parent: Array<Array<Int>>, m_child: Array<Array<Int>>, c_row: Int, c_col: Int)
  86.   {
  87.     var ni, nj: Int;
  88.     for(i in 0...m_child.length)
  89.       {
  90.     for(j in 0...m_child[i].length)
  91.       {
  92.         ni = i + c_row; nj = j + c_col;
  93.         if( (0 <= ni) && (ni < m_parent.length) && (0 <= nj) && (nj <= m_parent[0].length) )
  94.           {
  95.         if( (m_child[i][j] != 0) && (m_parent[ni][nj] != 0)  ) return true;
  96.           }
  97.       }
  98.       }
  99.     return false;
  100.   }
  101.  
  102.   public static function matr_combine(m_parent : Array<Array<Int>>, m_child : Array<Array<Int>>, c_row : Int, c_col : Int)
  103.   {
  104.     var m_new = matr_copy(m_parent);
  105.     for(i in 0...m_child.length)
  106.       for(j in 0...m_child[0].length)
  107.     {
  108.       if( m_child[i][j] > 0  ) m_new[i+c_row][j+c_col] = m_child[i][j];
  109.     }
  110.     return m_new;
  111.   }
  112.  
  113.   public static function matr_print(m: Array<Array<Int>>)
  114.   {
  115.     for(r in m)
  116.       {
  117.     trace(r);
  118.       }
  119.   }
  120.  
  121.   public static function matrix_0(rows: Int, cols: Int)
  122.   {
  123.     var matr_new: Array<Array<Int>> = [];
  124.     var row: Array<Int>;
  125.     for(i in 0...rows)
  126.       {
  127.     row = [];
  128.     for(j in 0...cols)
  129.       {
  130.         row.push(0);
  131.       }
  132.     matr_new.push(row);
  133.       }
  134.     return matr_new;
  135.   }
  136.  
  137. }
  138.  
  139. class Glass
  140. {
  141.   public var matr: Array<Array<Int>>;
  142.   //var twall: Int = 1; - doesn't seem to be allowed here
  143.   var twall: Int;
  144.   var padding: Int;
  145.   public var full: Bool;
  146.   var width: Int;
  147.   var height: Int;
  148.   public var cleared: Int;
  149.  
  150.   public function new(h: Int, w: Int)
  151.   {
  152.     twall = 1; padding = 1; full = false; cleared = 0;
  153.     width = w;
  154.     height = h;
  155.  
  156.     matr = M.matrix_0(padding+height+twall+padding, padding+twall+width+twall+padding);
  157.     for(i in padding...padding+height)
  158.       {
  159.     matr[i][padding] = 1;
  160.     matr[i][padding+twall+width] = 1;
  161.       }
  162.     for(i in padding...padding+width+twall+1)
  163.       {
  164.     matr[padding+height][i] = 1;
  165.       }
  166.  
  167.   }
  168.  
  169.   function is_row_full(n_row: Int)
  170.   {
  171.     for(i in padding+twall...padding+twall+width)
  172.       {
  173.     if(matr[n_row][i] == 0) return false;
  174.       }
  175.     return true;
  176.   }
  177.  
  178.   function is_row_empty(n_row: Int)
  179.   {
  180.     for(i in padding+twall...padding+twall+width)
  181.       {
  182.     if(matr[n_row][i] != 0) return false;
  183.       }
  184.     return true;
  185.   }
  186.  
  187.   function destroy_row(n_row: Int)
  188.   {
  189.     //for(i in n_row...padding, -1) - backward iteration, which doesn't work (?)
  190.     var i: Int = n_row;
  191.     while(i > padding)
  192.       {
  193.     for(j in padding+twall...padding+twall+width) matr[i][j] = matr[i-1][j];
  194.     i -= 1;
  195.       }
  196.   }
  197.  
  198.   function compact()
  199.   {
  200.     for(i in 0...padding+height)
  201.       {
  202.     if(is_row_full(i))
  203.       {
  204.         destroy_row(i);
  205.         cleared += 1;
  206.       }
  207.       }
  208.     if(!is_row_empty(padding)) full = true;
  209.   }
  210.  
  211.   public function settle(fig: Figure)
  212.   {
  213.     matr = M.matr_combine(matr, fig.matr, fig.row, fig.col);
  214.     compact();
  215.   }
  216. }
  217.  
  218. class Figure
  219. {
  220.   public var matr: Array<Array<Int>>;
  221.   public var row: Int;
  222.   public var col: Int;
  223.   var glass: Glass;
  224.   public var settled: Bool;
  225.  
  226.   public function new(m: Array<Array<Int>>, g: Glass, r: Int, c: Int)
  227.   {
  228.     settled = false;
  229.     glass = g;
  230.     matr = M.matr_copy(m);
  231.     row = r;
  232.     col = c;
  233.   }
  234.  
  235.   public function rotate()
  236.   {
  237.     var matr_new = M.rotate(matr);
  238.     if(!M.does_collide(glass.matr, matr_new, row, col)) matr = matr_new;
  239.   }
  240.  
  241.   public function drop()
  242.   {
  243.     if(settled) return;
  244.     if(!M.does_collide(glass.matr, matr, row+1, col)) row += 1;
  245.     else
  246.       {
  247.     settled = true;
  248.     glass.settle(this);
  249.       }
  250.   }
  251.  
  252.   public function move_left()
  253.   {
  254.     if(!M.does_collide(glass.matr, matr, row, col-1)) col -= 1;
  255.   }
  256.      
  257.   public function move_right()
  258.   {
  259.     if(!M.does_collide(glass.matr, matr, row, col+1)) col += 1;
  260.   }
  261. }
  262.  
  263.  
  264. class TetServer
  265. {
  266.   public var glass: Glass;
  267.   public var figure: Figure;
  268.   public var falling_speed: Float;
  269.   public var falling_acc: Float;
  270.  
  271.   public var fmatrices: Array<Array<Array<Int>>>;
  272.  
  273.   public function full()
  274.   {
  275.     return glass.full;
  276.   }
  277.  
  278.   public function new()
  279.   {
  280.     falling_speed = 0.5;
  281.     falling_acc = 0.0;
  282.     glass = new Glass(20, 10);
  283.     fmatrices = [];
  284.  
  285.     var I = [ [0, 0, 0, 0],
  286.           [8, 8, 8, 8],
  287.           [0, 0, 0, 0]];
  288.  
  289.  
  290.     var L = [ [0, 0, 0, 0, 0],
  291.           [0, 2, 2, 2, 0],
  292.           [0, 2, 0, 0, 0],
  293.           [0, 0, 0, 0, 0]];
  294.  
  295.     var J = [ [0, 0, 0, 0, 0],
  296.           [0, 3, 3, 3, 0],
  297.           [0, 0, 0, 3, 0],
  298.           [0, 0, 0, 0, 0]];
  299.  
  300.     var O = [ [0, 0, 0, 0],
  301.           [0, 4, 4, 0],
  302.           [0, 4, 4, 0],
  303.           [0, 0, 0, 0]];
  304.  
  305.     var S = [[0, 0, 0, 0, 0],
  306.          [0, 0, 5, 5, 0],
  307.          [0, 5, 5, 0, 0],
  308.          [0, 0, 0, 0, 0]];
  309.  
  310.     var T = [ [0, 0, 0, 0, 0],
  311.           [0, 0, 6, 0, 0],
  312.           [0, 6, 6, 6, 0],
  313.           [0, 0, 0, 0, 0],
  314.           [0, 0, 0, 0, 0]];
  315.  
  316.     var Z = [ [0, 0, 0, 0, 0],
  317.           [0, 7, 7, 0, 0],
  318.           [0, 0, 7, 7, 0],
  319.           [0, 0, 0, 0, 0]];
  320.  
  321.     fmatrices = [I, L, J, O, S, T, Z];
  322.  
  323.     next_figure();
  324.   }
  325.  
  326.   public function cleared_lines()
  327.   {
  328.     return glass.cleared;
  329.   }
  330.  
  331.   public function frame()
  332.   {
  333.     return M.matr_combine(glass.matr, figure.matr, figure.row, figure.col);
  334.   }
  335.  
  336.   public function next_figure()
  337.   {
  338.     var i = Math.round(Math.random()*(fmatrices.length-1));
  339.     figure = new Figure(fmatrices[i], glass, 0, 5);
  340.   }
  341.  
  342.   public function fall_auto()
  343.   {
  344.     if(figure.settled) next_figure();
  345.  
  346.     falling_acc += falling_speed;
  347.     if(falling_acc >= 1.0)
  348.       {
  349.     falling_acc = 0.0;
  350.     figure.drop();
  351.       }
  352.   }
  353.  
  354.   public function fall(amount: Int)
  355.   {
  356.     for(i in 0...amount)
  357.       {
  358.     figure.drop();
  359.       }
  360.   }
  361.  
  362.   public function move_left()
  363.   {
  364.     figure.move_left();
  365.   }
  366.  
  367.   public function move_right()
  368.   {
  369.     figure.move_right();
  370.   }
  371.  
  372.   public function rotate()
  373.   {
  374.     figure.rotate();
  375.   }
  376. }
  377.  
  378.  
  379. class TetClient
  380. {
  381.   var gColors: Array<Int>;
  382.   var leds: Array<Array<Square>>;
  383.   var matr: Array<Array<Int>>;
  384.   var tserv: TetServer;
  385.   var paused: Bool;
  386.   var timer: Timer;
  387.   var pausedText: TextField;
  388.   var linesText: TextField;
  389.   var ptf: TextFormat;
  390.   public function new()
  391.   {
  392.     gColors = [0x000000, 0xFFFFFF, 0xF0A000, 0xF00000, 0x00F0F0, 0x0000F0, 0xF0F000, 0x00F000, 0xA000F0];
  393.  
  394.     tserv = new TetServer();
  395.     matr = tserv.frame();
  396.     leds = [];
  397.    
  398.     var n_r = 20;
  399.     for(i in 0...matr.length)
  400.       {
  401.     var n_c = 50;
  402.     var row: Array<Square> = [];
  403.     for(j in 0...matr[0].length)
  404.       {
  405.         var r = new Square(gColors[matr[i][j]], 20);
  406.         r.x = n_c; r.y = n_r;
  407.         Lib.current.addChild(r);
  408.         row.push(r);
  409.         n_c += 20;
  410.       }
  411.     leds.push(row);
  412.     n_r += 20;
  413.       }
  414.    
  415.     ptf = new TextFormat();
  416.     ptf.color = 0x00FFFF;
  417.     ptf.size = 30;
  418.      
  419.     pausedText = new TextField();
  420.     pausedText.autoSize = TextFieldAutoSize.LEFT;
  421.     pausedText.text = "";
  422.     pausedText.setTextFormat(ptf);
  423.     pausedText.selectable = false;
  424.     pausedText.x = 100; pausedText.y = 200;
  425.     Lib.current.addChild(pausedText);
  426.    
  427.     linesText = new TextField();
  428.     linesText.autoSize = TextFieldAutoSize.LEFT;
  429.     linesText.text = "Lines: 0";
  430.     linesText.setTextFormat(ptf);
  431.     linesText.selectable = false;
  432.     linesText.x = 100; linesText.y = 500;
  433.     Lib.current.addChild(linesText);
  434.      
  435.   }
  436.  
  437.   public function start()
  438.   {
  439.     timer = new Timer(500, 0);
  440.     timer.addEventListener(TimerEvent.TIMER, onTimer);
  441.     timer.start();
  442.  
  443.     Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKey);
  444.   }
  445.  
  446.   public function stop()
  447.   {
  448.     timer.removeEventListener(TimerEvent.TIMER, onTimer);
  449.     Lib.current.stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKey);
  450.   }
  451.  
  452.   function onTimer(evt: TimerEvent)
  453.   {
  454.     if(paused) return;
  455.     tserv.fall_auto();
  456.     doDraw();
  457.   }
  458.  
  459.   function onKey(evt: KeyboardEvent)
  460.   {
  461.     if(evt.keyCode == 80)
  462.       {
  463.     paused = !paused;
  464.    
  465.     if(paused) pausedText.text = "...PAUSED...";
  466.     else pausedText.text = "";
  467.    
  468.     pausedText.setTextFormat(ptf);
  469.       }
  470.  
  471.     if(paused) return;
  472.    
  473.     switch(evt.keyCode)
  474.       {
  475.       case 37: tserv.move_left();
  476.       case 38: tserv.rotate();
  477.       case 39: tserv.move_right();
  478.       case 40: tserv.fall(3);
  479.       default: return;
  480.       }
  481.  
  482.     doDraw();    
  483.    
  484.   }
  485.  
  486.   function doDraw()
  487.   {
  488.     matr = tserv.frame();
  489.     for(i in 0...matr.length)
  490.       {
  491.     for(j in 0...matr[0].length)
  492.       {
  493.         leds[i][j].setcolor(gColors[matr[i][j]]);
  494.       }
  495.       }
  496.    
  497.     linesText.text = "Lines: " + Std.string(tserv.cleared_lines());
  498.     linesText.setTextFormat(ptf);
  499.    
  500.     if(tserv.full())
  501.        {
  502.      timer.stop();
  503.      pausedText.text = "...FINISH...";
  504.      pausedText.setTextFormat(ptf);
  505.        }
  506.   }
  507. }
  508.  
  509.  
  510. class Tetris
  511. {
  512.   public function new()
  513.   {
  514.     Lib.current.addEventListener( Event.ENTER_FRAME, firstframe );
  515.   }
  516.  
  517.   static function main()
  518.   {
  519.     var self = new Tetris();
  520.   }
  521.  
  522.   public function firstframe(evt:Event)
  523.   {
  524.     Lib.current.removeEventListener( Event.ENTER_FRAME, firstframe );
  525.     var app = new TetClient();
  526.     app.start();
  527.   }
  528. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement