-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame.hx
155 lines (100 loc) · 2.73 KB
/
Game.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.ColorTransform;
import flash.events.KeyboardEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
class Game extends Sprite
{
var board:Board;
var text_format:TextFormat;
var score_label:TextField;
var top_score_label:TextField;
var top_score:Int;
var score:Int;
var kongregate:Kongregate;
public function new()
{
super();
kongregate=new Kongregate();
top_score=0;
text_format=new TextFormat(flash.text.Font.enumerateFonts()[0].fontName,18);
newGame();
gameOver();
Main.stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
}
public function onKeyDown(e:KeyboardEvent)
{
if(e.keyCode==32 && board==null)
newGame();
}
public function newGame()
{
score=0;
clear();
board=new Board(this);
board.x=19;
board.y=18;
addChild(board);
var background=Utils.load("resources.background");
addChild(background);
top_score_label=new TextField();
top_score_label.defaultTextFormat=text_format;
top_score_label.autoSize=TextFieldAutoSize.LEFT;
top_score_label.text="000000";
top_score_label.embedFonts=true;
top_score_label.x=295;
top_score_label.y=202;
addChild(top_score_label);
score_label=new TextField();
score_label.defaultTextFormat=text_format;
score_label.autoSize=TextFieldAutoSize.LEFT;
score_label.text="000000";
score_label.embedFonts=true;
score_label.x=295;
score_label.y=245;
addChild(score_label);
updateScore(0);
}
public function clear()
{
while(numChildren > 0)
{
removeChildAt(0);
}
}
public function gameOver()
{
if(kongregate!=null)
{
kongregate.submitStat("lines_cleared",board.lines_cleared);
kongregate.submitStat("blocks_dropped",board.blocks_dropped);
kongregate.submitStat("level",board.level);
}
trace("game over");
var bitmapData=new BitmapData(Std.int(width),Std.int(height));
bitmapData.draw(this);
bitmapData.colorTransform(bitmapData.rect, new ColorTransform(0.125,0.125,0.125,1.0,192,192,192));
clear();
board.destroy();
board=null;
addChild(new Bitmap(bitmapData));
var label=Utils.load("resources.press_to_play");
label.x=(width-label.width)/2;
label.y=(height-label.height)/2;
addChild(label);
}
public function updateScore(score:Int)
{
this.score=score;
top_score=score>top_score?score:top_score;
top_score_label.text=""+top_score;
while(top_score_label.text.length<6)
top_score_label.text="0"+top_score_label.text;
score_label.text=""+score;
while(score_label.text.length<6)
score_label.text="0"+score_label.text;
}
}