PageRenderTime 54ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/Game.hx

http://github.com/banthar/Hell-Tetris
Haxe | 155 lines | 100 code | 55 blank | 0 comment | 3 complexity | 96ee33fe68881b388e7f2746a45a5f0f MD5 | raw file
  1. import flash.display.Sprite;
  2. import flash.display.Bitmap;
  3. import flash.display.BitmapData;
  4. import flash.geom.ColorTransform;
  5. import flash.events.KeyboardEvent;
  6. import flash.text.TextField;
  7. import flash.text.TextFieldAutoSize;
  8. import flash.text.TextFormat;
  9. class Game extends Sprite
  10. {
  11. var board:Board;
  12. var text_format:TextFormat;
  13. var score_label:TextField;
  14. var top_score_label:TextField;
  15. var top_score:Int;
  16. var score:Int;
  17. var kongregate:Kongregate;
  18. public function new()
  19. {
  20. super();
  21. kongregate=new Kongregate();
  22. top_score=0;
  23. text_format=new TextFormat(flash.text.Font.enumerateFonts()[0].fontName,18);
  24. newGame();
  25. gameOver();
  26. Main.stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
  27. }
  28. public function onKeyDown(e:KeyboardEvent)
  29. {
  30. if(e.keyCode==32 && board==null)
  31. newGame();
  32. }
  33. public function newGame()
  34. {
  35. score=0;
  36. clear();
  37. board=new Board(this);
  38. board.x=19;
  39. board.y=18;
  40. addChild(board);
  41. var background=Utils.load("resources.background");
  42. addChild(background);
  43. top_score_label=new TextField();
  44. top_score_label.defaultTextFormat=text_format;
  45. top_score_label.autoSize=TextFieldAutoSize.LEFT;
  46. top_score_label.text="000000";
  47. top_score_label.embedFonts=true;
  48. top_score_label.x=295;
  49. top_score_label.y=202;
  50. addChild(top_score_label);
  51. score_label=new TextField();
  52. score_label.defaultTextFormat=text_format;
  53. score_label.autoSize=TextFieldAutoSize.LEFT;
  54. score_label.text="000000";
  55. score_label.embedFonts=true;
  56. score_label.x=295;
  57. score_label.y=245;
  58. addChild(score_label);
  59. updateScore(0);
  60. }
  61. public function clear()
  62. {
  63. while(numChildren > 0)
  64. {
  65. removeChildAt(0);
  66. }
  67. }
  68. public function gameOver()
  69. {
  70. if(kongregate!=null)
  71. {
  72. kongregate.submitStat("lines_cleared",board.lines_cleared);
  73. kongregate.submitStat("blocks_dropped",board.blocks_dropped);
  74. kongregate.submitStat("level",board.level);
  75. }
  76. trace("game over");
  77. var bitmapData=new BitmapData(Std.int(width),Std.int(height));
  78. bitmapData.draw(this);
  79. bitmapData.colorTransform(bitmapData.rect, new ColorTransform(0.125,0.125,0.125,1.0,192,192,192));
  80. clear();
  81. board.destroy();
  82. board=null;
  83. addChild(new Bitmap(bitmapData));
  84. var label=Utils.load("resources.press_to_play");
  85. label.x=(width-label.width)/2;
  86. label.y=(height-label.height)/2;
  87. addChild(label);
  88. }
  89. public function updateScore(score:Int)
  90. {
  91. this.score=score;
  92. top_score=score>top_score?score:top_score;
  93. top_score_label.text=""+top_score;
  94. while(top_score_label.text.length<6)
  95. top_score_label.text="0"+top_score_label.text;
  96. score_label.text=""+score;
  97. while(score_label.text.length<6)
  98. score_label.text="0"+score_label.text;
  99. }
  100. }