-
Notifications
You must be signed in to change notification settings - Fork 1
/
GUI.cpp
515 lines (436 loc) · 13.6 KB
/
GUI.cpp
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#include <sstream>
#include <fstream>
#include "table.h"
#include "table.cpp"
#include "console.h"
#include "GUI.h"
GUI::GUI( )
{
level = 1; //Level ban dau
COLOR = LIGHTGREEN; //Mau mac dinh cua game
//Tao ten file chua highscore va README
strcpy ( pathHighScore, "HighScore.txt" ) ;
strcpy ( pathREADME, "README.txt" ) ;
//Toa noi dung file README
sstringREADME << "\t\t\t----------------------------------------------- \n\n";
sstringREADME << "\t\t\tHOW TO PLAY \n\n";
sstringREADME << "\t\t\t - USE \n\n";
sstringREADME << "\t\t\t W OR ^ TO PLAY \n";
sstringREADME << "\t\t\t A S D < v > \n\n";
sstringREADME << "\t\t\t - PAUSE : ESC | P \n\n";
sstringREADME << "\t\t\t----------------------------------------------- \n\n";
sstringREADME << "\t\t\tDeveloped by: \n\n";
sstringREADME << "\t\t\t Nguyen Duc Huy \n";
sstringREADME << "\t\t\t Nguyen Viet Hoang \n";
}
// ================================ Main Menu ================================
void GUI::mainMenu( )
{
exit = FALSE;
while ( !exit ) //chay den khi co lenh thoat
{
setTextColor( COLOR );
printHeader( );
gotoxy( 20, 10); std::cout << " MAIN MENU ";
gotoxy( 20, 13); std::cout << " START ";
gotoxy( 20, 15); std::cout << " SETTINGS ";
gotoxy( 20, 17); std::cout << " HIGH SCORE ";
gotoxy( 20, 19); std::cout << " INFO ";
gotoxy( 20, 21); std::cout << " QUIT ";
//Thuc hien lenh tuy theo toa do con tro tuy chon
switch ( cursor( 13, 21 , 5 ) )
{
case 1:
start( );
break;
case 2:
settings( );
break;
case 3:
printHighScore( );
break;
case 4:
info( );
break;
case 5:
quit( );
}
}
}
void GUI::start( )
{
std::cout <<"Don't delete this line";
score = play( );
if ( score > -1 )
{
setTextColor( COLOR );
gotoxy( 20, 9 ); std::cout << " ";
gotoxy( 20, 10); std::cout << "_______________________GAME OVER___________________";
gotoxy( 20, 11); std::cout << " ";
gotoxy( 20, 12); std::cout << " YOUR SCORE : " << score << " ";
gotoxy( 20, 13); std::cout << " ";
//luu lai diem cao
setHighScore( score );
Sleep(1500);
getch();
}
}
void GUI::settings( )
{
while ( TRUE )
{
printHeader( );
gotoxy( 20, 10); std::cout << " SETTINGS ";
gotoxy( 20, 13); std::cout << " COLOR ";
gotoxy( 20, 15); std::cout << " LEVEL ";
gotoxy( 20, 17); std::cout << " BACK ";
//Thuc hien lenh tuy theo toa do con tro tuy chon
switch ( cursor( 13, 17 , 3) )
{
case 1:
settingsColor( );
break;
case 2:
settingsLevel( );
break;
case 3:
return;
}
}
}
void GUI::info( ) //In ra thong tin game
{
printHeader( );
//Mo file README
fileREADME.open( pathREADME, std::ios::in );
if ( fileREADME.fail() )
{
fileREADME.open( pathREADME, std::ios::out ); // Tao file moi neu chua co
fileREADME << sstringREADME.str( );
}
fileREADME.close();
SetFileAttributes( pathREADME, FILE_ATTRIBUTE_READONLY );
//In ra noi dung file README
gotoxy( 0, 10 );
std::cout << sstringREADME.str();
getch();
}
void GUI::quit( ) //thoat game
{
if ( confirm( "EXIT" ) )
exit = TRUE; //thoat game neu confirm = true
else
exit = FALSE;
}
// ================================ End Main Menu ================================
// ================================ START Menu ================================
int GUI::play( )
{
Table t; // choi tren Table nay
srand(time(NULL)); //Sinh so ngau nhien
double timeDelay = 1000 - level * 100 ; //thoi gian de roi
double tempTimeDelay = timeDelay;
int IDBrick, IDNextBrick; //So thu tu cua gach
score = 0; //Diem ban dau
lines = 0; //So hang da an duoc ban dau
system( "cls" );
//Tao 2 khoi gach dau tien
IDBrick = createIDBrick();
IDNextBrick = createIDBrick();
//In ra background, level, line va score ban dau
printBackground( );
gotoxy( 12, 4 );
std::cout << level;
gotoxy( 12, 6 );
std::cout << score;
gotoxy( 12, 8 );
std::cout << lines;
do
{
t.create( IDBrick ); //tao khoi moi
while ( t.checkEmpty( -1, 0 ) )
{
//In ra vien gach tiep theo va mau cua no
setTextColor( IDNextBrick + 7 );
t.printNextBrick( IDNextBrick );
Sleep( ( tempTimeDelay ) );
int count = 5; //So lan xoay toi da trong timeDelay
while (--count && kbhit() ) //doi phim an
{
KEY = key_press( );
if ( KEY == KEY_LEFT ) // dich trai neu nhap phim trai
t.moveLeft( );
else if ( KEY == KEY_RIGHT ) // dich phai neu nhap phim phai
t.moveRight( );
else if ( KEY == KEY_DOWN ) // roi luon neu nhap phim xuong
tempTimeDelay /= 10;
else if ( KEY == KEY_UP ) // xoay neu nhap phim len
t.rotate( );
else if ( KEY == PAUSE || KEY == ESCAPE ) // tam dung pause
while ( 1 ) //man hinh pause
{
system( "cls" ); //xoa man hinh, chong gian lan
//In ra tuy chon o man hinh pause
setTextColor( COLOR );
gotoxy( 20, 9 );
std::cout << " _____PAUSE_____ ";
gotoxy( 20, 12 );
std::cout << " RESUME ";
gotoxy( 20, 14 );
std::cout << " RESTART ";
gotoxy( 20, 16 );
std::cout << " MAIN MENU ";
int chosenMenu = cursor( 12, 16, 1 );
//Thuc hien lenh tuy theo toa do con tro tuy chon
if(chosenMenu == 1 ) //tiep tuc
{
//In ra nen game, level, lines va score hien tai va tiep tuc game
printBackground( );
gotoxy( 12, 4 );
std::cout << level;
gotoxy( 12, 6 );
std::cout << score;
gotoxy( 12, 8 );
std::cout << lines;
break;
}
else if(chosenMenu == 2 && confirm("RESTART") ) //choi lai ( can xac nhan )
return play( );
else if(chosenMenu == 3 && confirm("BACK TO MAIN MENU") ) //quay ve
return -1;
//return restart( );
}
}
t.moveDown( ); // dich xuong
}
t.setBrickNum( IDBrick + 7 );
t.getFullRows( ); //Lay diem
//Tinh diem hien tai
score = score + level + level * level * t.getDeletedLines() * t.getDeletedLines();
lines += t.getDeletedLines();
//In ra level, lines va score hien tai
setTextColor ( COLOR );
gotoxy( 12, 4 );
std::cout << level;
gotoxy( 12, 6 );
std::cout << score;
gotoxy( 12, 8 );
std::cout << lines;
IDBrick = IDNextBrick; //Gan khoi cu thanh khoi moi
IDNextBrick = createIDBrick(); //Tao khoi gach tiep theo
tempTimeDelay = timeDelay *= 0.99;
level = ( 1000 - timeDelay ) / 100;
} while ( t.checkGameOver() == 0 );
return score; //lay diem
}
// ================================ SETTINGS Menu ================================
void GUI::settingsColor( )
{
char key;
gotoxy( 24, 15); std::cout << "NOTE: If you don't see any color of 8 colors";
gotoxy( 24, 16); std::cout << " Perhaps it's your background color ";
gotoxy( 24, 17); std::cout << " ";
gotoxy( 28, 18);
setTextColor(LIGHTGRAY); std::cout << "1.GRAY ";
setTextColor(LIGHTBLUE); std::cout << "2.BLUE ";
setTextColor(LIGHTGREEN); std::cout << "3.GREEN ";
setTextColor(LIGHTCYAN); std::cout << "4.CYAN ";
gotoxy( 28, 20);
setTextColor(LIGHTRED); std::cout << "5.RED ";
setTextColor(LIGHTPURPLE); std::cout << "6.PURPLE ";
setTextColor(LIGHTYELLOW); std::cout << "7.YELLOW ";
setTextColor(LIGHTWHITE); std::cout << "8.WHITE ";
while ( 1 )
{
key = getch( );
if ( key >= '1' && key <= '8' )
{
COLOR = key - '0' + 7;
setTextColor( COLOR );
return;
} //ket thuc ham khi da doi mau
}
}
void GUI::settingsLevel( )
{
char key = 0;
gotoxy( 20, 19);
std::cout << " CHOOSE LEVEL: 1. EASY 2. NORMAL 3. HARD ";
while( ! ( key >= '1' && key <= '3' ) ) //lap den khi doc dung phim
{
key = getch( );
}
//tao level cua game
if ( key == '1' ) level = 1;
else if ( key == '2' ) level = 4;
else if ( key == '3' ) level = 7;
}
// ================================ Ham ho tro ================================
void GUI::setHighScore(int score)
{
int i;
//Tao highscore ban dau
for(i = 0; i < 10; i++)
{
h_score[i] = 0;
}
SetFileAttributes( pathHighScore, FILE_ATTRIBUTE_NORMAL );
fileHighScore.open(pathHighScore, std::ios::in | std::ios::out);
//Nhap diem neu da co file high score
if(fileHighScore.is_open())
{
for(i = 0; i < 10; i++)
{
fileHighScore >> h_score[i];
}
}
//Tra ve neu khong dat high score
if(score < h_score[9])
{
gotoxy( 24, 14 );
std::cout << " You can't make it to the Hall of Fame :( ";
gotoxy( 24, 15 );
std::cout << " Do better next time ! ";
gotoxy( 24, 16 );
std::cout << " ";
return;
}
//Them diem hien tai neu dat high score
for(i = 8; i >= 0; i--)
{
if(score > h_score[i])
h_score[i+1] = h_score[i];
else
break;
}
h_score[i+1] = score;
//In ra thong bao thang va vi tri tren bang high score
if(i == -1) //Dat diem cao nhat
{
gotoxy( 20, 14 );
std::cout << "CONGRATULATIONS. YOU HAVE ARCHIEVED THE HIGHEST SCORE !!!";
}
else
{
gotoxy ( 20, 14 );
std::cout << " Well done!!! You are now rank " << i+2 << " " ;
}
gotoxy( 24, 15 );
std::cout << " ";
//Tao file moi neu chua co
if(fileHighScore.fail())
fileHighScore.open( pathHighScore, std::ios::out );
//Ghi ra file sau khi da them high score
fileHighScore.seekp(0, std::ios::beg);
for(i = 0; i < 10; i++)
{
fileHighScore << h_score[i] << std::endl;
}
fileHighScore.close();
SetFileAttributes( pathHighScore, FILE_ATTRIBUTE_READONLY );
}
void GUI::printHighScore( )
{
int i;
fileHighScore.open ( pathHighScore, std::ios::in );
//Lay diem da luu tu file fileHighScore
if(fileHighScore.is_open()) //Luu diem neu da co file high score
{
for(i = 0; i < 10; i++)
{
fileHighScore >> h_score[i];
}
fileHighScore.close();
}
else
{
//Tao file high score neu chua co
fileHighScore.open( pathHighScore, std::ios::out );
SetFileAttributes( pathHighScore, FILE_ATTRIBUTE_NORMAL );
for(i = 0; i < 10; i++)
{
h_score[i] = 0;
fileHighScore << h_score[i] << std::endl;
}
fileHighScore.close();
}
//In ra 10 diem cao nhat
printHeader( );
gotoxy( 20, 10 ); std::cout << " HIGH SCORE ";
for(i = 0; i < 10; i++)
{
if(i < 3)
{
if(i == 0)
setTextColor( LIGHTYELLOW );
if(i == 1)
setTextColor( LIGHTWHITE );
if(i == 2)
setTextColor( GRAY );
gotoxy( 35, 13 + 2*i );
std::cout << i+1 << ". " << h_score[i];
}
else if(i < 5)
{
setTextColor( LIGHTGRAY );
gotoxy( 35, 13 + 2*i );
std::cout << i+1 << ". " << h_score[i];
}
else if(i < 9)
{
gotoxy( 50, 13 + 2*(i-5) );
std::cout << i+1 << ". " << h_score[i];
}
else
{
gotoxy( 50, 13 + 2*(i-5) );
std::cout << i+1 << ". " << h_score[i];
}
}
SetFileAttributes( pathHighScore, FILE_ATTRIBUTE_READONLY );
getch();
}
void GUI::printHeader( )
{
system( "cls" );
gotoxy( 20, 1 ); std::cout << " __________ ________ __________ _________ ___ ___________";
gotoxy( 20, 2 ); std::cout << "/___ ___// _____//___ ___// // // ________/";
gotoxy( 20, 3 ); std::cout << " / / / /___ / / / /____// // /_______";
gotoxy( 20, 4 ); std::cout << " / / / ____/ / / / ___ // //_______ /";
gotoxy( 20, 5 ); std::cout << " / / / /____ / / / / / // / _______/ /";
gotoxy( 20, 6 ); std::cout << "/__/ /_______/ /__/ /__/ /__//__/ /_________/";
}
void GUI::printBackground( )
{
int i;
setTextColor( COLOR );
system( "cls" );
gotoxy( 3, 2 );
for ( int j = 0; j < 16; ++j )
std::cout << (char)205;
gotoxy( 3, 4 );
std::cout << " Level : ";
gotoxy( 3, 6 );
std::cout << " Score : ";
gotoxy( 3, 8 );
std::cout << " Lines : ";
gotoxy( 3, 10 );
for ( int j = 0; j < 16; ++j )
std::cout << (char)205;
gotoxy(58, 2);
std::cout << "NEXT";
//in vien table
gotoxy( 25, 1 );
for ( int j = 0; j < 22; ++j )
std::cout << (char)220;
for ( int i = 20 ; i >= 1; i-- )
{
gotoxy( 25, 22 - i );
std::cout << (char)221;
gotoxy( 46, 22 - i );
std::cout << (char)222;
}
gotoxy( 25, 22 );
for ( int j = 0; j < 22; ++j )
std::cout << (char)223;
}