-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris.c
874 lines (834 loc) · 27.4 KB
/
tetris.c
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#include<conio.h>
char tetris[10][20][2]={0};
int score=0,picture[7][4][6]={0},line[20]={0},up=72,down=80,left=75,right=77,space=32,sec=500,row=0,cache_block[3],cache_change[3];
int root[4];
void show();//顯示遊戲畫面
void move_show();//顯示移動畫面
void pic();//建構方塊
void initialization();//初始化方塊降落
void mark(int point_x,int point_y,int block,int change,int clean);//刻印方塊進陣列
void line_check(int point_y,int block,int change);//橫行計算
void move(int y);//移動目標行上方所有方塊往下一格
void gotoxy(int xpos, int ypos);//座標回歸到最左上角
void game();//遊戲主程式
void homepage(int option);//顯示主頁面
void homepage_control();//主頁面控制
void pause(int option);//遊戲中暫停頁面
void gameover();//遊戲結束
void initialization(int *point_x,int *point_y,int *block,int *change);//初始化方塊
void shadow(int x2,int y2,int block,int change2,int clean);//影子計算
void mark_shadow(int point_x,int point_y,int block,int change);//刻印影子進陣列
void SetColor(int ForgC);//設定顏色
void ready_block(int *block,int *change,int a);//顯示預備方塊
void move_block(int x1,int y1,int x2,int y2,int block,int change1,int change2,int clean);//移動方塊優化
void set_color(int block);//設定顏色
void set_root(int point_x,int point_y,int block,int change);
void shadow_new_block(int point_x,int point_y,int block,int change);//新方塊影子
int shadow_drop_point(int point_x,int point_y,int block,int change);//計算影子落點
int coordinate(int a,int xy);
int pause_control();//暫停頁面控制
int che(int point_x,int point_y,int block,int change);//確認方塊有無重疊
int main(){
srand(time(NULL));//設定亂數種子
homepage_control();
return 0;
}
void game(){
int point_x=5,point_y=-2,block[4]={rand()%7,rand()%7,rand()%7,rand()%7},change[4]={rand()%4,rand()%4,rand()%4,rand()%4},control=0,check,game_control=0,clean=0;
set_root(point_x,point_y,block[0],change[0]);
int i=0;
for(i=0;i<3;i++){
cache_change[i]=change[i+1];
cache_block[i]=block[i+1];
}
pic();//建構方塊
clock_t t1,t2,t3,t4;
gotoxy(0,0);
show();
for(;;){//遊戲開始
check=0;
switch(game_control){//方塊落定
case 0:
shadow_new_block(point_x,point_y,block[0],change[0]);
gotoxy(34,1);
printf("SCORE");
gotoxy(34,2);
printf("%08d",score);
for(;point_y+picture[block[0]][change[0]][1]<0 && point_y+picture[block[0]][change[0]][3]<0 && point_y+picture[block[0]][change[0]][5]<0;point_y++);//將方塊移動至露出一格
check=che(point_x,point_y,block[0],change[0]);//檢測有無方塊重疊
ready_block(block,change,0);
if(check==1)break;//有其他方塊遊戲結束
t1=clock();
case 1://移動完回到此
//shadow(point_x,point_y,block[0],change[0],0);//刻上影子
//mark(point_x,point_y,block[0],change[0],0);//刻上落下方塊
move_block(root[0],root[1],point_x,point_y,block[0],root[3],change[0],clean);
shadow(point_x,point_y,block[0],change[0],clean);
clean=1;
set_root(point_x,point_y,block[0],change[0]);
case 2://無法移動時
control=0;
}
if(check==1)break;//switch裡判斷方塊到頂
game_control=0;
do{//取得輸入
t2=clock();
if(kbhit())control=getch();
}while((t2-t1)<sec && control!=224 && control!=space && control!=27);
if(control==224)control=getch();//上下左右鍵
if(control==27){//暫停頁面
t3=clock();
int home;
home=pause_control();
if(home==1)return;
gotoxy(0,0);
show();
ready_block(block,change,0);
t4=clock();
t1=t1+t4-t3;
game_control=1;
continue;
}
mark(point_x,point_y,block[0],change[0],1);//清除降落中方塊方便運算
if((t2-t1)>=sec){//時間到往下一格
if(point_y>=19){//超出範圍
move_block(root[0],root[1],point_x,point_y,block[0],root[3],change[0],1);
clean=0;
line_check(point_y,block[0],change[0]);
initialization(&point_x,&point_y,block,change);//初始化
continue;
}
point_y++;//往下一格開始判斷
if(point_y+picture[block[0]][change[0]][1]>19 || point_y+picture[block[0]][change[0]][3]>19 || point_y+picture[block[0]][change[0]][5]>19){//下落位置超過最底部
point_y--;//無法往下,倒退一格
move_block(root[0],root[1],point_x,point_y,block[0],root[3],change[0],1);
clean=0;
line_check(point_y,block[0],change[0]);
initialization(&point_x,&point_y,block,change);//初始化
continue;
}
else{
check=che(point_x,point_y,block[0],change[0]);//確認下落位置有無方塊
if(check==1){
point_y--;//無法往下,倒退一格
move_block(root[0],root[1],point_x,point_y,block[0],root[3],change[0],1);
clean=0;
line_check(point_y,block[0],change[0]);
initialization(&point_x,&point_y,block,change);//初始化
continue;
}
}
}
if(control==left){//左鍵
point_x--;//往左一格開始判斷
if(point_x+picture[block[0]][change[0]][0]>=0 && point_x+picture[block[0]][change[0]][2]>=0 && point_x+picture[block[0]][change[0]][4]>=0){//往左後沒超過範圍進入
check=che(point_x,point_y,block[0],change[0]);
if(check==1){
point_x++;//往左在範圍內,但有方塊,往右回去原位
game_control=2;
continue;
}
}
else{
point_x++;//超過範圍,退回去
game_control=2;
continue;
}
game_control=1;
continue;
}
else if(control==down){//下降鍵
point_y++;//下降一格開始判斷
if(point_y+picture[block[0]][change[0]][1]<=19 && point_y+picture[block[0]][change[0]][3]<=19 && point_y+picture[block[0]][change[0]][5]<=19){//往下後沒超過範圍進入
check=che(point_x,point_y,block[0],change[0]);
if(check==1){
point_y--;////往下在範圍內,但有方塊,往上回去原位
move_block(root[0],root[1],point_x,point_y,block[0],root[3],change[0],1);
clean=0;
line_check(point_y,block[0],change[0]);//計算行上方塊數
initialization(&point_x,&point_y,block,change);//初始化
continue;
}
}
else{
point_y--;
move_block(root[0],root[1],point_x,point_y,block[0],root[3],change[0],1);
clean=0;
line_check(point_y,block[0],change[0]);
initialization(&point_x,&point_y,block,change);//初始化
continue;
}
game_control=1;
continue;
}
else if(control==right){//右鍵
point_x++;
if(point_x+picture[block[0]][change[0]][0]<=9 && point_x+picture[block[0]][change[0]][2]<=9 && point_x+picture[block[0]][change[0]][4]<=9){//沒超出邊界
check=che(point_x,point_y,block[0],change[0]);//檢查移動後有無重疊
if(check==1){//有重疊退回,重新等待輸入
point_x--;
game_control=2;
continue;
}
}
else{//超出邊界
point_x--;//退回,等待新輸入
game_control=2;
continue;
}
game_control=1;
continue;
}
else if(control==up){//上鍵(旋轉)
if(change[0]<=2)change[0]++;
else change[0]=0;
if(point_x+picture[block[0]][change[0]][0]>9 || point_x+picture[block[0]][change[0]][2]>9 || point_x+picture[block[0]][change[0]][4]>9){//旋轉後超出右邊邊界
point_x--;//左移一格
check=che(point_x,point_y,block[0],change[0]);//確認有無重疊
if(check==1){//重疊
point_x++;//回歸原樣
if(change[0]>0)change[0]--;
else change[0]=3;
game_control=2;
continue;
}
}else if(point_x+picture[block[0]][change[0]][0]<0 || point_x+picture[block[0]][change[0]][2]<0 || point_x+picture[block[0]][change[0]][4]<0){//超出左邊邊界
point_x++;//右移一格
int a=0;
if(point_x+picture[block[0]][change[0]][0]<0 || point_x+picture[block[0]][change[0]][2]<0 || point_x+picture[block[0]][change[0]][4]<0){//再度超出左邊邊界
point_x++;//再度右移
a++;
}
check=che(point_x,point_y,block[0],change[0]);//確認有無重疊
if(check==1){
point_x--;
if(a==1)point_x--; //判斷右移次數回復原樣
if(change[0]>0)change[0]--;
else change[0]=3;
game_control=2;
continue;
}
if(a==0){
}
else if(a==1){
}
}
if(point_y+picture[block[0]][change[0]][1]>19 || point_y+picture[block[0]][change[0]][3]>19 || point_y+picture[block[0]][change[0]][5]>19){//超出最下面
point_y--;//上移
int a=0;
if(point_y+picture[block[0]][change[0]][1]>19 || point_y+picture[block[0]][change[0]][3]>19 || point_y+picture[block[0]][change[0]][5]>19){//再度超出最下面
point_y--;//
a++;
}
check=che(point_x,point_y,block[0],change[0]);//確認有無重疊
if(check==1){//重疊,回復原樣
point_y++;
if(a==1)point_y++;
if(change[0]>0)change[0]--;
else change[0]=3;
game_control=2;
continue;
}
}
check=che(point_x,point_y,block[0],change[0]);//沒超出邊界,確認旋轉後有無重疊
if(check==1){
if(change[0]>0)change[0]--;
else change[0]=3;
game_control=2;
continue;
}
game_control=1;
continue;
}
else if(control==space){//空格鍵瞬降
check=0;
do{
point_y++;
if(point_y+picture[block[0]][change[0]][1]<=19 && point_y+picture[block[0]][change[0]][3]<=19 && point_y+picture[block[0]][change[0]][5]<=19)
check=che(point_x,point_y,block[0],change[0]);
}while(check==0 && point_y+picture[block[0]][change[0]][1]<=19 && point_y+picture[block[0]][change[0]][3]<=19 && point_y+picture[block[0]][change[0]][5]<=19);
point_y--;
move_block(root[0],root[1],point_x,point_y,block[0],root[3],change[0],1);
clean=0;
line_check(point_y,block[0],change[0]);
initialization(&point_x,&point_y,block,change);
continue;
}
t1=clock();
game_control=1;
continue;
}
gameover();
}
void homepage_control(){//主頁面控制
int option=0,control=0;
for(;;){
control=0;
gotoxy(0,0);
homepage(option);
do{
if(kbhit())control=getch();
}while(control!=224 && control!=13);//等待輸入上下或enter鍵
if(control==224){//變更
if(option==1)option=0;
else option++;
continue;
}
switch(option){//按下enter鍵後判斷選項為何並執行
case 0:
int i,j;
for(i=0;i<20;i++){
for(int j=0;j<10;j++){
tetris[j][i][0]=0;
}
}
game();//進入遊戲
break;
case 1://關閉遊戲
return;
}
}
}
void homepage(int option){//主頁顯示
int start1=0,close1=0;
int start2=0,close2=0;
switch(option){//顯示箭頭
case 0:
start1=62;
start2=60;
break;
case 1:
close1=62;
close2=60;
break;
}
printf("╔═════════════════════╗\n");
printf("║ ║\n");
printf("║ ║\n");
//printf("═╦═ ╔══ ═╦═ ╔══╗ ╦ ╔══\n");
//printf(" ║ ╠══ ║ ╠═╦╝ ║ ╚══╗\n");
//printf(" ╚ ╚══ ╚ ╚ ╚ ╩ ══╝\n");
printf("║ ║\n");
printf("║ TETRIS ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ %c START %c ║\n",start1,start2);
printf("║ %c CLOSE %c ║\n",close1,close2);
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("║ ║\n");
printf("╚═════════════════════╝\n");
}
void show(){
printf("╔═════════════════════╦═════════╗\n");
int i=0,j=0;
for(i=0;i<20;i++){
printf("║ ");
for(j=0;j<10;j++){
if(tetris[j][i][0]=='O'){
switch(tetris[j][i][1]){
case 0:
SetColor(9);
break;
case 1:
SetColor(3);
break;
case 2:
SetColor(10);
break;
case 3:
SetColor(11);
break;
case 4:
SetColor(12);
break;
case 5:
SetColor(14);
break;
case 6:
SetColor(15);
break;
}
printf("■");
SetColor(7);
}
else if(tetris[j][i][0]=='.'){
SetColor(8);
printf("■");
SetColor(7);
}
else printf(" ");
}
if(i<14 && i!=4 && i!=9 && i!=14)printf("║ ║");
else if(i==4 || i==9)printf("╠═════════╣");
else if(i==14)printf("╠═════════╝");
else printf("║");
//if(i==0)printf(" SCORE");
//else if(i==1)printf(" %d",score);
printf("\n");
}
printf("╚═════════════════════╝\n");
}
void pic(){//刻入方塊相對應位置
picture[0][0][0]=-1;picture[0][0][3]=1;picture[0][0][4]=1;picture[0][0][5]=1;//Z型
picture[0][1][0]=1;picture[0][1][1]=-1;picture[0][1][2]=1;picture[0][1][5]=1;
picture[0][2][0]=-1;picture[0][2][3]=1;picture[0][2][4]=1;picture[0][2][5]=1;
picture[0][3][0]=1;picture[0][3][1]=-1;picture[0][3][2]=1;picture[0][3][5]=1;
picture[1][0][1]=1;picture[1][0][2]=-1;picture[1][0][3]=-1;picture[1][0][4]=-1;//ㄣ型
picture[1][1][1]=1;picture[1][1][2]=-1;picture[1][1][3]=1;picture[1][1][4]=1;
picture[1][2][1]=1;picture[1][2][2]=-1;picture[1][2][3]=-1;picture[1][2][4]=-1;
picture[1][3][1]=1;picture[1][3][2]=-1;picture[1][3][3]=1;picture[1][3][4]=1;
picture[2][0][0]=-1;picture[2][0][2]=-1;picture[2][0][3]=1;picture[2][0][5]=1;//田型
picture[2][1][0]=-1;picture[2][1][2]=-1;picture[2][1][3]=1;picture[2][1][5]=1;
picture[2][2][0]=-1;picture[2][2][2]=-1;picture[2][2][3]=1;picture[2][2][5]=1;
picture[2][3][0]=-1;picture[2][3][2]=-1;picture[2][3][3]=1;picture[2][3][5]=1;
picture[3][0][1]=-1;picture[3][0][3]=1;picture[3][0][5]=2;//1型
picture[3][1][0]=-2;picture[3][1][2]=-1;picture[3][1][4]=1;
picture[3][2][1]=-1;picture[3][2][3]=1;picture[3][2][5]=2;
picture[3][3][0]=-2;picture[3][3][2]=-1;picture[3][3][4]=1;
picture[4][0][1]=-1;picture[4][0][2]=1;picture[4][0][3]=-1;picture[4][0][5]=1;//ㄏ型
picture[4][1][0]=-1;picture[4][1][2]=1;picture[4][1][4]=1;picture[4][1][5]=1;
picture[4][2][1]=-1;picture[4][2][3]=1;picture[4][2][4]=-1;picture[4][2][5]=1;
picture[4][3][0]=-1;picture[4][3][1]=-1;picture[4][3][2]=-1;picture[4][3][4]=1;
picture[5][0][1]=-1;picture[5][0][2]=-1;picture[5][0][3]=-1;picture[5][0][5]=1;//7型
picture[5][1][0]=-1;picture[5][1][2]=1;picture[5][1][4]=1;picture[5][1][5]=-1;
picture[5][2][1]=-1;picture[5][2][3]=1;picture[5][2][4]=1;picture[5][2][5]=1;
picture[5][3][0]=-1;picture[5][3][2]=-1;picture[5][3][3]=1;picture[5][3][4]=1;
picture[6][0][1]=-1;picture[6][0][2]=1;picture[6][0][4]=-1;//倒T型
picture[6][1][0]=1;picture[6][1][3]=-1;picture[6][1][5]=1;
picture[6][2][0]=-1;picture[6][2][2]=1;picture[6][2][5]=1;
picture[6][3][0]=-1;picture[6][3][3]=-1;picture[6][3][5]=1;
}
void mark(int point_x,int point_y,int block,int change,int clean){//將方塊位置存入或清除tetris陣列,回傳值0印上 1清空
char a;
if(clean==0)a='O';
else a=0;
if(point_y>=0){
tetris[point_x][point_y][0]=a;
tetris[point_x][point_y][1]=block;
}
if(point_y+picture[block][change][1]>=0){
tetris[point_x+picture[block][change][0]][point_y+picture[block][change][1]][0]=a;
tetris[point_x+picture[block][change][0]][point_y+picture[block][change][1]][1]=block;
}
if(point_y+picture[block][change][3]>=0){
tetris[point_x+picture[block][change][2]][point_y+picture[block][change][3]][0]=a;
tetris[point_x+picture[block][change][2]][point_y+picture[block][change][3]][1]=block;
}
if(point_y+picture[block][change][5]>=0){
tetris[point_x+picture[block][change][4]][point_y+picture[block][change][5]][0]=a;
tetris[point_x+picture[block][change][4]][point_y+picture[block][change][5]][1]=block;
}
}
int che(int point_x,int point_y,int block,int change){//確認方塊位置是否重復,回傳1有重複,0沒重複
if(point_y>=0){
if(tetris[point_x][point_y][0]=='O')return 1;
}
if(point_y+picture[block][change][1]>=0){
if(tetris[point_x+picture[block][change][0]][point_y+picture[block][change][1]][0]=='O')return 1;
}
if(point_y+picture[block][change][3]>=0){
if(tetris[point_x+picture[block][change][2]][point_y+picture[block][change][3]][0]=='O')return 1;
}
if(point_y+picture[block][change][5]>=0){
if(tetris[point_x+picture[block][change][4]][point_y+picture[block][change][5]][0]=='O')return 1;
}
return 0;
}
void line_check(int point_y,int block,int change){//每行方塊數計算
line[point_y]++;
line[point_y+picture[block][change][1]]++;
line[point_y+picture[block][change][3]]++;
line[point_y+picture[block][change][5]]++;
int i=0,j=0,k=0,count[20];
for(i=0;i<20;i++)count[i]=-1;
for(i=0;i<20;i++){
if(line[i]==10){
line[i]=0;
row++;
count[j]=i;
j++;
for(k=0;k<10;k++)
tetris[i][k][0]==0;
}
}
if(j>0){
switch(j){
case 1:
score+=40;
break;
case 2:
score+=100;
break;
case 3:
score+=300;
break;
case 4:
score+=1200;
break;
}
for(i=0;i<5;i++){
for(j=0;count[j]!=-1;j++){//橫行消除動畫
gotoxy(coordinate(4-i,0),coordinate(count[j],1));
printf(" ");
gotoxy(coordinate(5+i,0),coordinate(count[j],1));
printf(" ");
gotoxy(0,22);
}
Sleep(50);
}
for(j=0;count[j]!=-1;j++){
move(count[j]);
gotoxy(0,22);
Sleep(50);
}
}
}
void move(int y){//將目標行數上的所有方塊往下移動一格
int i=0,j=0;
for(i=y;i>0;i--){//開始移動
for(j=0;j<10;j++){
tetris[j][i][0]=tetris[j][i-1][0];
tetris[j][i][1]=tetris[j][i-1][1];
gotoxy(coordinate(j,0),coordinate(i,1));
if(tetris[j][i][0]=='O'){
set_color(tetris[j][i][1]);
printf("■");
}
else
printf(" ");
}
line[i]=line[i-1];//每行方塊數移動
}
}
void gotoxy(int xpos, int ypos){
COORD scrn;
HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
scrn.X = xpos; scrn.Y = ypos;
SetConsoleCursorPosition(hOuput,scrn);
}
void gameover(){//顯示出局畫面
gotoxy(5,11);
printf("+ Game Over +");
gotoxy(0,22);
system("pause");
}
int pause_control(){//暫停頁面控制
int option=0,control=0;
for(;;){
//gotoxy(0,0);
//show();
control=0;
pause(option);
do{
if(kbhit)control=getch();
}while(control!=up && control!=down && control!=13 && control!=27);//等待上下鍵 enter鍵 esc鍵
if(control==27){//esc鍵退出
return 0;
}
if(control==up || control==down){
if(option==0)option=1;
else option=0;
}
pause(option);
if(control==13){//按下enter鍵控制
switch(option){
case 0:
return 1;
break;
case 1:
return 0;
break;
}
}
}
}
void pause(int option){//暫停頁面顯示
int home1=0,home2=0;
int continue1=0,continue2=0;
switch(option){
case 0:
home1=62;
home2=60;
break;
case 1:
continue1=62;
continue2=60;
break;
}
gotoxy(3,9);
printf("╔═══════════════╗\n");
gotoxy(3,10);
printf("║ %c HOME %c ║",home1,home2);
gotoxy(3,11);
printf("║ %c CONTINUE %c ║",continue1,continue2);
gotoxy(3,12);
printf("╚═══════════════╝\n");
}
void initialization(int *point_x,int *point_y,int *block,int *change){//初始化標點x y數值,方塊種類選擇,旋轉類型選擇
*point_x=5;
*point_y=-2;
int i=0;
for(i=0;i<3;i++){
block[i]=block[i+1];
change[i]=change[i+1];
}
block[3]=rand()%7;
change[3]=rand()%4;
}
void shadow(int x2,int y2,int block,int change2,int clean){//影子落點計算
if(clean==0)return;
int check,x1=root[0],y1=root[1],change1=root[3],temp=y2;
mark(x2,y2,block,change2,1);//清除降落中方塊方便運算
y2=shadow_drop_point(x2,y2,block,change2);
y1=shadow_drop_point(x1,y1,root[2],change1);
mark(x2,temp,block,change2,0);//恢復降落中方塊
set_color(7);//改色為顏色
if(y2>=0 && y2<=19){//覆蓋新方塊
if(tetris[x2][y2][0]=='O' || tetris[x2][y2][0]=='.')
tetris[x2][y2][1]=-1;
else{
tetris[x2][y2][0]='.';
tetris[x2][y2][1]=8;
gotoxy((x2+1)*2,y2+1);
printf("■");
}
}
int i=0;
for(i=0;i<3;i++){//覆蓋新方塊
if(y2+picture[block][change2][i*2+1]>=0 && y2+picture[block][change2][i*2+1]<=19){
if(tetris[x2+picture[block][change2][i*2]][y2+picture[block][change2][i*2+1]][0]=='O' || tetris[x2+picture[block][change2][i*2]][y2+picture[block][change2][i*2+1]][0]=='.')
tetris[x2+picture[block][change2][i*2]][y2+picture[block][change2][i*2+1]][1]=-1;
else{
tetris[x2+picture[block][change2][i*2]][y2+picture[block][change2][i*2+1]][0]='.';
tetris[x2+picture[block][change2][i*2]][y2+picture[block][change2][i*2+1]][1]=8;
gotoxy((x2+picture[block][change2][i*2]+1)*2,y2+picture[block][change2][i*2+1]+1);
printf("■");
}
}
}
SetColor(7);//恢復正常顏色
if(y1>=0 && y1<=19){//清除舊方塊
if(tetris[x1][y1][1]!=-1 && tetris[x1][y1][0]!='O'){
tetris[x1][y1][0]=0;
gotoxy((x1+1)*2,y1+1);
printf(" ");
}
else
tetris[x1][y1][1]=block;
}
for(i=0;i<3;i++){//清除舊方塊
if(y1+picture[block][change1][i*2+1]>=0 && y1+picture[block][change1][i*2+1]<=19){
if(tetris[x1+picture[block][change1][i*2]][y1+picture[block][change1][i*2+1]][1]!=-1 && tetris[x1+picture[block][change1][i*2]][y1+picture[block][change1][i*2+1]][0]!='O'){
tetris[x1+picture[block][change1][i*2]][y1+picture[block][change1][i*2+1]][0]=0;
gotoxy((x1+picture[block][change1][i*2]+1)*2,y1+picture[block][change1][i*2+1]+1);
printf(" ");
}
else
tetris[x1+picture[block][change1][i*2]][y1+picture[block][change1][i*2+1]][1]=block;
}
}
gotoxy(0,22);
}
int shadow_drop_point(int point_x,int point_y,int block,int change){
int check,a=0;
for(;point_y+picture[block][change][1]<=19 && point_y+picture[block][change][3]<=19 && point_y+picture[block][change][5]<=19;point_y++){
check=che(point_x,point_y,block,change);a++;
if(check==1){
break;
}
}
point_y--;
return point_y;
}
void shadow_new_block(int point_x,int point_y,int block,int change){
mark_shadow(point_x,shadow_drop_point(point_x,point_y,block,change),block,change);
set_color(7);
if(shadow_drop_point(point_x,point_y,block,change)>=0 && shadow_drop_point(point_x,point_y,block,change)
<=19){
gotoxy((point_x+1)*2,shadow_drop_point(point_x,point_y,block,change)+1);
printf("■");
}
int i;
for(i=0;i<3;i++){
if(shadow_drop_point(point_x,point_y,block,change)+picture[block][change][i*2+1]>=0 && shadow_drop_point(point_x,point_y,block,change)+picture[block][change][i*2+1]<=19){
gotoxy((point_x+picture[block][change][i*2]+1)*2,shadow_drop_point(point_x,point_y,block,change)+picture[block][change][i*2+1]+1);
printf("■");
}
}
SetColor(7);
gotoxy(0,22);
}
void mark_shadow(int point_x,int point_y,int block,int change){//刻上影子
int a='.';
if(tetris[point_x][point_y][0]!='O')
tetris[point_x][point_y][0]=a;
int i=0;
for(i=0;i<3;i++){
if(tetris[point_x+picture[block][change][2*i]][point_y+picture[block][change][2*i+1]][0]!='O')
tetris[point_x+picture[block][change][2*i]][point_y+picture[block][change][2*i+1]][0]=a;
}
}
void ready_block(int *block,int *change,int a){//顯示預備方塊
if(a==3)return;
set_color(block[a+1]);
int i,j,x=0,y=0,area[8][4]={0};//確認矛點
if((block[a+1]==0 && (change[a+1]==1 || change[a+1]==3)) || (block[a+1]==5 && change[a+1]==2) || (block[a+1]==6 && change[a+1]==1)){
x=2;
y=1;
}
else if(block[a+1]==4 && change[a+1]==0){
x=2;
y=2;
}
else if((block[a+1]==0 && (change[a+1]==0 || change[a+1]==2)) || (block[a+1]==1 && (change[a+1]==1 || change[a+1]==3)) || (block[a+1]==3 && (change[a+1]==0 || change[a+1]==2)) || (block[a+1]==4 && change[a+1]==1) || (block[a+1]==5 && change[a+1]==3) || (block[a+1]==6 && change[a+1]==2)){
x=3;
y=1;
}
else if((block[a+1]==4 && change[a+1]==3) || (block[a+1]==5 && change[a+1]==1) || (block[a+1]==6 && change[a+1]==0)){
x=3;
y=2;
}
else if((block[a+1]==1 && (change[a+1]==0 || change[a+1]==2)) || (block[a+1]==2 && (change[a+1]==0 ||change[a+1]==1 || change[a+1]==2 || change[a+1]==3)) ||(block[a+1]==3 && (change[a+1]==1 || change[a+1]==3)) || (block[a+1]==4 && change[a+1]==2) || (block[a+1]==6 && change[a+1]==3)){
x=4;
y=1;
}
else if(block[a+1]==5 && change[a+1]==0){
x=4;
y=2;
}
area[x][y]='O';
for(i=0;i<3;i++){
area[x+(picture[block[a+1]][change[a+1]][i*2])*2][y+picture[block[a+1]][change[a+1]][i*2+1]]='O';
}
for(i=0;i<4;i++){
for(j=0;j<8;j++){
gotoxy(24+j,1+i+a*5);
if(area[j][i]=='O'){
printf("■");
j++;
}
else if(area[j][i]==0)
printf(" ");
}
}
gotoxy(0,22);
SetColor(7);
ready_block(block,change,a+1);
}
void set_color(int block){
switch(block){
case 0:
SetColor(9);
break;
case 1:
SetColor(3);
break;
case 2:
SetColor(10);
break;
case 3:
SetColor(11);
break;
case 4:
SetColor(12);
break;
case 5:
SetColor(14);
break;
case 6:
SetColor(15);
break;
case 7://影子
SetColor(8);
break;
}
}
void move_block(int x1,int y1,int x2,int y2,int block,int change1,int change2,int clean){
set_color(block);//改色為方塊顏色
if(clean==1)mark(root[0],root[1],root[2],root[3],0);
int a='O';
//if(block2==7)a='.';
if(y2>=0 && y2<=19){//覆蓋新方塊
if(tetris[x2][y2][0]=='O')
tetris[x2][y2][1]=-1;
else{
tetris[x2][y2][0]=a;
tetris[x2][y2][1]=block;
gotoxy((x2+1)*2,y2+1);
printf("■");
}
}
int i=0;
for(i=0;i<3;i++){//覆蓋新方塊
if(y2+picture[block][change2][i*2+1]>=0 && y2+picture[block][change2][i*2+1]<=19){
if(tetris[x2+picture[block][change2][i*2]][y2+picture[block][change2][i*2+1]][0]=='O')
tetris[x2+picture[block][change2][i*2]][y2+picture[block][change2][i*2+1]][1]=-1;
else{
tetris[x2+picture[block][change2][i*2]][y2+picture[block][change2][i*2+1]][0]=a;
tetris[x2+picture[block][change2][i*2]][y2+picture[block][change2][i*2+1]][1]=block;
gotoxy((x2+picture[block][change2][i*2]+1)*2,y2+picture[block][change2][i*2+1]+1);
printf("■");
}
}
}
SetColor(7);//恢復正常顏色
if(clean==1){//方塊相同 清除舊方塊
if(y1>=0 && y1<=19){//清除舊方塊
if(tetris[x1][y1][1]!=-1){
tetris[x1][y1][0]=0;
gotoxy((x1+1)*2,y1+1);
printf(" ");
}
else
tetris[x1][y1][1]=block;
}
for(i=0;i<3;i++){//清除舊方塊
if(y1+picture[block][change1][i*2+1]>=0 && y1+picture[block][change1][i*2+1]<=19){
if(tetris[x1+picture[block][change1][i*2]][y1+picture[block][change1][i*2+1]][1]!=-1){
tetris[x1+picture[block][change1][i*2]][y1+picture[block][change1][i*2+1]][0]=0;
gotoxy((x1+picture[block][change1][i*2]+1)*2,y1+picture[block][change1][i*2+1]+1);
printf(" ");
}
else
tetris[x1+picture[block][change1][i*2]][y1+picture[block][change1][i*2+1]][1]=block;
}
}
}
gotoxy(0,22);
}
int coordinate(int a,int xy){//轉換gotoxy座標 0轉換x 1轉換y
if(xy==0)return (a+1)*2;
else if(xy==1)return a+1;
}
void set_root(int point_x,int point_y,int block,int change){
root[0]=point_x;
root[1]=point_y;
root[2]=block;
root[3]=change;
}
void SetColor(int ForgC){
WORD wColor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
//We use csbi for the wAttributes word.
if(GetConsoleScreenBufferInfo(hStdOut, &csbi)){
//Mask out all but the background attribute, and add in the forgournd color
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
SetConsoleTextAttribute(hStdOut, wColor);
}
return;
}