-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
223 lines (194 loc) · 5.38 KB
/
main.ts
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
class LEDMap {
map: number[][];
constructor(map: number[][] | null) {
if (map == null || !this.is_valid_map(map)) {
map = this.generate_empty_map();
}
this.map = map;
}
show() {
for (let x = 0; x < 5; x++) {
for (let y = 0; y < 5; y++) {
led.plotBrightness(x, y, this.map[x][y]);
}
}
}
clear() {
this.map = this.generate_empty_map();
}
putBrightness(x: number, y: number, brightness: number) {
if (
0 <= x && x < 5 &&
0 <= y && y < 5 &&
0 <= brightness && brightness <= 255 &&
this.map[x][y] < brightness
) {
this.map[x][y] = brightness;
}
}
put(x: number, y: number) {
this.putBrightness(x, y, 255);
}
private generate_empty_map(): number[][] {
let map: number[][] = [];
for (let x = 0; x < 5; x++) {
map.push([]);
for (let y = 0; y < 5; y++) {
map[x].push(0);
}
}
return map;
}
private is_valid_map(map: number[][]): boolean {
let x = 0;
if (map.length == 5) {
for (x = 0; x < 5; x++) {
if (map[x].length != 5) {
break;
}
}
}
if (x != 5) {
return false;
}
return true;
}
}
class Bird {
position: number;
loops_per_action: number;
brightness: number;
constructor() {
this.position = 2;
this.loops_per_action = 50;
this.brightness = 128;
}
is_action_timing(n: number): boolean {
return n % this.loops_per_action == 0
}
up() {
if (this.position > 0) {
this.position--;
}
}
down() {
if (this.position < 4) {
this.position++;
}
}
}
class Stage {
map: number[][];
progress: number;
loops_per_action: number;
constructor(map: number[][] | null) {
if (map == null || !this.is_valid_map(map)) {
map = this.generate_random_map();
}
this.map = map;
this.progress = 0;
this.loops_per_action = 150;
}
is_action_timing(n: number): boolean {
return n % this.loops_per_action == 0
}
is_finished(): boolean {
return this.progress >= this.map.length-10
}
next() {
if (this.progress < this.map.length-5) {
this.progress++;
}
}
get_square_map(): number[][] {
let square_map: number[][] = [];
this.map.slice(this.progress, this.progress+5).forEach(l => square_map.push(l.slice()));
return square_map;
}
get_led_map(): LEDMap {
let square_map = this.get_square_map();
for (let x = 0; x < 5; x++) {
for (let y = 0; y < 5; y++) {
square_map[x][y] = square_map[x][y] == 0 ? 0: 255;
}
}
return new LEDMap(square_map);
}
private is_valid_map(map: number[][]): boolean {
let x = 0;
for (; x < map.length; x++) {
if (map[x].length != 5) {
break;
}
}
if (x != map.length-1) {
return false;
}
return true;
}
private add_margin_to_map(map: number[][]): number[][]{
let empty_line = [0, 0, 0, 0, 0];
let margin_map = [];
for (let x = 0; x < 5; x++) {
margin_map.push(empty_line);
}
map = margin_map.concat(map).concat(margin_map).concat(margin_map);
return map;
}
private generate_random_map(): number[][] {
let map: number[][] = [];
let section_number = randint(4, 6); // 障壁の数
let loophole_position: number;
for (let x = 0; x < (section_number-1)*4; x++) {
let line = [0, 0, 0, 0, 0];
if (x % 4 == 0) {
loophole_position = randint(0, 4);
for (let y = 0; y < 5; y++) {
if (y != loophole_position) {
line[y] = 1;
}
}
} else if (x % 2 == 0) {
if (randint(0, 100) <= 80) {
line[loophole_position] = 1;
}
}
map.push(line);
}
return this.add_margin_to_map(map);
}
}
function start_game() {
basic.clearScreen();
const bird = new Bird();
const stage = new Stage(null);
let screen = new LEDMap(null);
let n = 0;
while (true) {
if (bird.is_action_timing(n)) {
if (input.isGesture(Gesture.LogoUp)) {
bird.down();
} else if (input.isGesture(Gesture.LogoDown)) {
bird.up();
}
}
if (stage.is_action_timing(n)) {
if (bird.brightness < screen.map[0][bird.position]) {
basic.showIcon(IconNames.Sad, 2000);
break;
}
if (stage.is_finished()) {
basic.showIcon(IconNames.Happy, 3000);
break;
}
stage.next();
}
screen = stage.get_led_map();
screen.putBrightness(0, bird.position, bird.brightness);
screen.show();
n++;
basic.pause(1);
}
input.onGesture(Gesture.Shake, function(){control.reset()});
}
start_game();