-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.pde
351 lines (328 loc) · 8.65 KB
/
game.pde
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
class TetrisGame{
// tamanho da "caixa"
int nx, ny;
// nível
int level;
// grid dentro da "caixa"
int[][] grid;
// pontos
int score;
// linhas que foram limpas
int clearedLines;
// timer
Timer timer;
// timer para eventos especiais
Timer eventTimer;
// começa com peça nova
boolean spawn = true;
// ID peça atual
int currentShapeID;
// ID peça seguinte
int nextShapeID;
//peça atual
TetrisShape currentShape;
//pausa
boolean pause;
//re-começa
boolean restart;
//fim do jogo
boolean gameOver;
//próximo nível
boolean nextLevel;
//linhas bonus = número de linhas vazias quando atinge o objetivo
int bonusRows;
//level
TetrisLevel currentLevel;
// cria um jogo
TetrisGame(int x, int y, int l) {
nx = x;
ny = y;
level = l;
score = 0;
setAll();
}
// setup de variáveis tal como elas são no começo do nível
void setAll(){
pause = false;
restart = false;
gameOver = false;
nextLevel = false;
currentLevel = setLevel(level);
bonusRows = 0;
clearedLines = 0;
grid = makeState(nx, ny, level);
timer = new Timer();
timer.reset(600 - currentLevel.speed);
eventTimer = new Timer();
eventTimer.reset(currentLevel.eventSpeed * 100);
currentShapeID = nextShapeID;
nextShapeID = floor(random(7)) + 1;
currentShape = new TetrisShape(currentShapeID);
timer.setTime();
eventTimer.setTime();
}
//display
void display() {
//fundo
background(10);
fill(0);
stroke(100);
//desenha a caixa
rect(blocksize, blocksize, blocksize * nx, blocksize * (ny + 2));
//texto: nível, pontuação e por aí vai
textAlign(LEFT, TOP);
fill(120);
textSize(15);
text("score", blocksize * (nx + 2), blocksize);
fill(200);
textSize(20);
text(str(score), blocksize * (nx + 2), blocksize * 2);
fill(120);
textSize(15);
text("level", blocksize * (nx + 2), blocksize * 4);
fill(200);
textSize(20);
text(str(level + 1), blocksize * (nx + 2), blocksize * 5);
fill(120);
textSize(15);
text("goal", blocksize * (nx + 2), blocksize * 7);
fill(200);
textSize(20);
text(str(currentLevel.goalLines), blocksize * (nx + 2), blocksize * 8);
fill(120);
textSize(15);
text("cleared", blocksize * (nx + 2), blocksize * 10);
fill(200);
textSize(20);
text(str(clearedLines), blocksize * (nx + 2), blocksize * 11);
//desenha os blocos que já cairam
for (int i=0; i<nx; i++){
for (int j=0; j<ny; j++){
if (grid[i][j] > 0) {
block(i, j + 2, blocksize, palette[grid[i][j]]);
}
}
}
//desenha a próxima peça
for (int i=0; i<4; i++){
for (int j=0; j<4; j++){
if (shapelist[nextShapeID][j][i] > 0) {
block(i + nx + 1, j + 13, blocksize, palette[nextShapeID]);
}
}
}
//desenha a peça atual
for (int i=0; i<4; i++){
for (int j=0; j<4; j++){
if (currentShape.geom[j][i] > 0) {
block(i + currentShape.x, j + currentShape.y, blocksize, currentShape.col);
}
}
}
//Time to next event
if (currentLevel.addRandomLines | currentLevel.addRandomBlocks){
fill(120);
textSize(15);
text("next event", blocksize * (nx + 2), blocksize * 19);
fill(200);
textSize(20);
int sec = round((eventTimer.duration - eventTimer.getTime()) / 1000);
text(str(sec), blocksize * (nx + 2), blocksize * 20);
}
}
//atualiza
void update() {
//se pausa, não faz nada
if(pause){
return;
}
//se precisa soltar uma peça nova...
if (spawn) {
//peça seguinte vira peça atual
currentShapeID = nextShapeID;
//FAZER AMOSTRADOR, PARA LIDAR COM PEÇAS ESPECIAIS
nextShapeID = floor(random(7)) + 1;
// incializa a peça
currentShape = new TetrisShape(currentShapeID);
// zera o timer
timer.setTime();
spawn = false;
}
// se o jogo avança um passo
if(timer.updateStep()){
// testamos se a peça vai colidir
boolean test = currentShape.collision(0, 0);
if (!test) {
//peça cai um bloco
currentShape.y += 1;
} else {
//a peça cai na superfície
splatShape();
//precisamos gerar nova peça
spawn = true;
}
}
//realiza eventos
if(eventTimer.updateStep()){
if(currentLevel.addRandomLines){
addLine(); //<>//
}
if(currentLevel.addRandomBlocks){
addBlock();
}
// testamos se a peça vai colidir
boolean test = currentShape.collision(0, 0);
if (test) {
//a peça cai na superfície
splatShape();
//precisamos gerar nova peça
spawn = true;
}
}
}
// quando a peça cai
void splatShape() {
for (int i=0; i<currentShape.dim; i++){
int I = i + currentShape.x;
for (int j=0; j<currentShape.dim; j++){
int J = j + currentShape.y - 2;
// se a peça saiu do copo, termina o jogo
if (J <= 0) {
endGame();
return;
} else {
// adicionamos a peça ao grid
if (currentShape.geom[j][i] > 0) grid[I][J] = currentShapeID * currentShape.geom[j][i];
checkRows();
}
}
}
}
//fim do jogo
void endGame(){
tetris.gameOver = true;
//escreve mensagem de fim de jogo
textAlign(CENTER, TOP);
fill(250);
stroke(0);
textSize(40);
text("Game Over", blocksize * 6, blocksize * 10);
textSize(25);
text("Press R to restart", blocksize * 6, blocksize * 12);
}
// reinicia
void restart(){
tetris = new TetrisGame(10, 20, starts);
}
// calcula bonus: número de linhas vazias
void getBonus(){
for (int j=0; j< ny; j++){
int s = 0;
for (int i=0; i<nx; i++){
s += grid[i][j];
}
if (s == 0) {
bonusRows += 1;
} else {
j = ny;
break;
}
}
//adiciona o bonus à pontuação
score += bonusRows;
}
//desenha a mensagem de avançar nível
void nextLevelStats(int bonusRows){
textAlign(CENTER, TOP);
fill(250);
stroke(0);
textSize(30);
text("Bonus", blocksize * 6, blocksize * 8);
textSize(50);
text(str(bonusRows), blocksize * 6, blocksize * 10);
textSize(20);
text("Press N for next level", blocksize * 6, blocksize * 14);
}
//avança nível
void nextLevel(){
level += 1;
setAll();
}
//checa se alguma linha foi completada
void checkRows(){
int count = 0;
for (int j=ny-1; j>=0; j--){
boolean completed = true;
for (int i=0; i<nx; i++){
if (grid[i][j] == 0) {
completed = false;
i = nx;
}
}
//se a linha foi completada, remove ela e desce a parte do grid que estava em cima uma linha
if (completed) {
int[][] newgrid = grid;
for (int J=j; J>=1; J--){
for (int I=0; I<nx; I++){
newgrid[I][J] = grid[I][J-1];
}
}
grid = newgrid;
count += 1;
j += 1;
}
}
//atualiza a pontuação
if (count > 0){
score += count * count;
clearedLines += count;
eventTimer.setTime();
}
// se atingiu o objetivo, avança para o próximo
if (clearedLines >= currentLevel.goalLines){
nextLevel = true;
getBonus();
}
}
//adiciona linha
void addLine(){
int[][] newgrid = randomGrid(nx, ny, 1);
for (int J=ny-2; J>=0; J--){
for (int I=0; I<nx; I++){
newgrid[I][J] = grid[I][J+1];
}
}
grid = newgrid;
//checa se o jogo acabou:
boolean test = false;
for (int I=0; I<nx; I++){
if (grid[I][0] > 0) test = true;
}
if (test) endGame();
eventTimer.setTime();
}
//adiciona linha
void addBlock(){
int X = 0;
int Y = 0;
int I = floor(random(nx));
for (int J=0; J<ny; J++){
if (grid[I][J] > 0) {
Y = J - 1;
X = I;
break;
}
}
grid[X][Y] = 8;
println("!!!");
}
}
//desenha um bloco
//fiz função a parte porque queria fazer algo mais bonitinho depois
void block(int i, int j, int blocksize, color c){
fill(c);
stroke(255);
int X = (i + 1) * blocksize;
int Y = (j + 1) * blocksize;
rect(X, Y, blocksize, blocksize);
}