-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlock.as
117 lines (106 loc) · 2.84 KB
/
Block.as
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
/*
This file is part of EseaTetris.
EseaTetris is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
EseaTetris is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with EseaTetris. If not, see <http://www.gnu.org/licenses/>.
*/
package {
import flash.display.Sprite;
public class Block extends Sprite {
public var map:Array;
private static function _dotArray(ary:Array,val:uint):Array {
for(var i:int=0;i<ary.length;i++) {
for(var j:int=0;j<ary[i].length;j++) {
ary[i][j] *= val;
}
}
return ary;
}
public static function randomPiece():Block {
return newPiece(Math.floor(Math.random()*7));
}
public static function newPiece(type:uint):Block {
switch(type) {
case 0:
return new Block(_dotArray(new Array(
new Array(0,1,0),
new Array(0,1,0),
new Array(0,1,0),
new Array(0,1,0)),
0x00ffff));
case 1:
return new Block(_dotArray(new Array(
new Array(1,0,0),
new Array(1,1,1),
new Array(0,0,0)),
0xff8800));
case 2:
return new Block(_dotArray(new Array(
new Array(0,0,0),
new Array(1,1,1),
new Array(1,0,0)),
0x0000ff));
case 3:
return new Block(_dotArray(new Array(
new Array(1,1),
new Array(1,1)),
0xffff00));
case 4:
return new Block(_dotArray(new Array(
new Array(1,1,0),
new Array(0,1,1)),
0x00ff00));
case 5:
return new Block(_dotArray(new Array(
new Array(0,1,1),
new Array(1,1,0)),
0xff0000));
case 6:
default:
return new Block(_dotArray(new Array(
new Array(0,1,0),
new Array(1,1,1),
new Array(0,0,0)),
0x8800ff));
}
}
public function Block(map:Array) {
this.map = map;
}
public function draw():void {
graphics.clear();
for(var i:int=0;i<map.length;i++) {
for(var j:int=0;j<map[i].length;j++) {
if(map[i][j]!=0) {
graphics.lineStyle(1,0xffffff);
graphics.beginFill(map[i][j]);
graphics.drawRect(i*10, j*10, 10, 10);
graphics.endFill();
}
}
}
}
public function tryTurnRight():Array {
var res1:uint = map.length;
var res2:uint = map[0].length;
var retval:Array = new Array(res2);
for(var i:uint=0;i<res2;i++) {
retval[i] = new Array(res1);
for(var j:uint=0;j<res1;j++) {
retval[i][j] = map[j][res2-1-i];
}
}
return retval;
}
public function turnRight():void {
map = tryTurnRight();
}
}
}