服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - 基于VC 6.0使用C语言实现俄罗斯方块

基于VC 6.0使用C语言实现俄罗斯方块

2021-06-26 16:07田刚 C/C++

这篇文章主要为大家详细介绍了基于VC 6.0使用C语言实现俄罗斯方块,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C语言实现俄罗斯方块的具体代码,供大家参考,具体内容如下

裸写的俄罗斯方块的代码,有意见或者想征用,直接评论留言即可。

效果如下:

基于VC 6.0使用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
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
/***************************************************************/
/*俄罗斯方块的实现
* 基于VC 6.0 编译链接即可运行
* 已实现的功能:
* 1、初步的规划及背景图案的显示
* 2、四种方块实现左右移动、下键加速、上键变形(两种变形)功能
* 3、下落方块碰壁及触碰到其它方块的检测
* 4、方块积满一行的消除并加分的功能
* 5、预测方块的功能
* 6、引入set_windows_pos函数解决闪屏问题
* 未解决的缺陷或者代码存在的问题
* 1、预测方块处莫名其妙的出现背景块
* 2、代码耦合性太高,接口封装还是不够好
* 2017.3.22
* 版权:通渭县西关小学四年级一班田刚
*/
/***************************************************************/
#include<stdio.h>
#include<windows.h>
#include<conio.h>
#include<string.h>
 
#define BACK        176//背景图案
#define BACK_INT      -80//背景的整数表示
#define FRAME        178//分割框图案
#define NODE        219//方块图案
#define NODE_INT      -37//方块的整数表示      
#define ERROR        -1
#define OK         0
 
int score = 0;//计分
static int time = 500;//时间,初始为500毫秒
char back[20][30] = {0};//竖直为x轴,横轴为y轴,原点在右上角
int block_type = 0;//方块类型
 
void backgroud_init(void);//画面背景的初始化
void set_windows_pos(int i, int j);//指定光标位置
void block_display(int block_type, int dir_type, int coor_x, int coor_y, int color_type);//显示或者消除方块
void time_add(void);//加速函数
void block_type_change(void);//方块类型变化
int block_move_check_x(int block_type, int dir_type, int coor_x, int coor_y);//检测方块是否触到下面已经停止的方块
int block_move_check_y(int block_type, int dir_type, int coor_x, int coor_y, int dir_block);//检测方块是否触碰到左右的方块
int new_back_y_check(int block_type, int dir_type, int coor_y);//检测方块是否触碰到墙壁 
int block_clear_sort(void);//检测是否满一行,需要消除得分
void block_clear_x(int row);//消除一个满行(都是方块)
 
int main(void)
{
  int c = 0;
  int new_dir_type = 0;//新的方块方向
  int befor_dir_type = 0;//旧的方块方向
  int new_back_x = 0;//新的方块头坐标x
  int new_back_y = 0;//新的方块头坐标y
  int befor_back_x = 0;//旧的方块头坐标x
  int befor_back_y = -1;//旧的方块头坐标y
  int block_dir = -1;//方块的移动方向
 
  backgroud_init();//背景的显示
 
  while(1)
  {
   
    block_type_change();//方块类型变化
    new_back_y = 8;//每次方块从最顶端出现时,其y坐标都为8
    time = 500;
     
    //怎样在这里清除键盘输入,flush不起作用
 
    for(new_back_x = 0; new_back_x < 20; new_back_x++)
    {
      befor_dir_type = new_dir_type;
      befor_back_x = new_back_x - 1;
      befor_back_y = new_back_y;
      block_dir = -1;//方块的方向变量初始为-1
       
      block_display(block_type, 0, 17, 26, NODE);//画上预测区域的方块
 
      if(kbhit())//检测有输入
      {
        c = getch();
        if(c > 0)
        {      
          switch(c)
          {
            case 119://上
                if(0 == new_dir_type)
                {
                  new_dir_type = 1;//竖向
                }
                else
                {
                  new_dir_type = 0;//横向
                }
                break;
            case 97://左
                new_back_y--;
                block_dir = 0;
                break;
            case 100://右
                new_back_y++;
                block_dir = 1;
                break;
            case 115://下
                time_add();//加速
                break;
            default://ESC
                break;
          }
        }
      }
 
      new_back_y = new_back_y_check(block_type, new_dir_type, new_back_y);//检查是否触碰到左右壁 
       
      block_display(block_type, befor_dir_type, befor_back_x, befor_back_y, BACK);//原来的方块位置清除掉
 
      if(-1 != block_dir)//左右移动了方块
      {
        if(ERROR == block_move_check_y(block_type, new_dir_type, new_back_x, new_back_y, block_dir))//检查移动的方块是触碰到左右的方块
        {
          new_back_y = befor_back_y;
        }
      }
       
      block_display(block_type, new_dir_type, new_back_x, new_back_y, NODE);//画下一个方块位置
 
      if(ERROR == block_move_check_x(block_type, new_dir_type, new_back_x, new_back_y))//检查移动的方块是否触发到下面的方块
      {
        break;
      }
       
      Sleep(time);
    }
 
    block_display(block_type, 0, 17, 26, BACK);//清楚预测区域的方块 
     
    if(OK == block_clear_sort())//检查下面方块是否等够得分,能得分则消除得分
    {
      set_windows_pos(8, 22);//更新得分
      printf("%d", score);
    }
  }
 
  return 0;
}
 
/***************************************************************/
/***        画面背景的初始化             ***/
/***其中原点在右上角,竖轴为x轴,横轴为y轴。y(0 - 19)为方块区***/
/***域,20 - 30 为计分区域及方块预测提示区域         ***/
/***************************************************************/
void backgroud_init(void)
{
  int x = 0, y = 0;
 
  for(x = 0; x < 20; x++)
  {
    for(y = 0; y < 20; y++)
    {
      back[x][y] = BACK;
    }
  }
 
  for(x = 0; x < 20; x++)
  {
    for(y = 20; y < 30; y++)
    {
      if((0 == x) || (4 == x) || (10 == x) || (19 == x))
      {
        back[x][y] = FRAME;
      }
 
      if((20 == y) || (29 == y))
      {
        back[x][y] = FRAME;
      }
    }
  }
 
  //背景图案的显示
  for(x = 0; x < 20; x++)
  {
    for(y = 0; y < 30; y++)
    {
      printf("%c", back[x][y]);
    }
     
    printf("\n");
  }
 
  //显示TETRIS
  set_windows_pos(2, 22);//移动windows的光标
  printf("TETRIS\n");
 
  //显示积分
  set_windows_pos(6, 22);//移动windows的光标
  printf("SORT\n");
  set_windows_pos(8, 22);
  printf("%d\n", score);
 
  //显示下一个要出现的方块
  set_windows_pos(12, 22);//移动windows的光标
  printf("EXPECT\n");
}
 
/***************************************************************/
/***    设置windows光标,类似于TC中的gotoxy       ***/
/***i,j 为要传入的x,y坐标                  ***/
/***************************************************************/
void set_windows_pos(int i, int j)//设置windows光标,类似于TC中的gotoxy
  /*if((0 > i) || (0 > j))
  {
    return ERROR;
  }*/
 
  /*windows的横轴是x轴,而本程序设计的是竖轴是X轴
   这里做个转换*/
  COORD pos={j,i};
  HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
  SetConsoleCursorPosition(hOut,pos);
}  
 
/***************************************************************/
/***********************消除/填上方块***************************/
/***输入:block_type 方块类型 1:长条 2:2型 3:7型 4:田型***/
/***输入:dir_type 方向类型 0:横向 1:竖向         ***/
/***输入:coor_x coor_y  方块当前头的坐标          ***/
/***输入:color_type  图标类型  BACK 背景色 NODE 方块色  ***/
/***初始coor_x,coor_y为一个方块的最右下的一个方块的坐标   ***/
/***************************************************************/
void block_display(int block_type, int dir_type, int coor_x, int coor_y, int color_type)
{
  int x = 0, y = 0;
 
  switch (block_type)
  {
    case 1://长条
      if(0 == dir_type)//横向
      {
        for(y = coor_y; y >= (coor_y - 3); y--)
        {
          back[coor_x][y] = color_type;//方块色
          set_windows_pos(coor_x, y);//移动windows的光标
          printf("%c", back[coor_x][y]);
        }
      }
      else if(1 == dir_type)//竖向
      {
        for(x = coor_x; (x >= (coor_x - 3)) && (x >= 0); x--)
        {
          back[x][coor_y] = color_type;//方块色
          set_windows_pos(x, coor_y);//移动windows的光标
          printf("%c", back[x][coor_y]);
        }
      }
      else
      {
        printf("dir_type is error!\n");
      }
      break;
    case 2://2型
      if(0 == dir_type)//横向
      {
        for(y = coor_y; y >= coor_y - 1; y--)
        {
          back[coor_x][y] = color_type;//方块色
          set_windows_pos(coor_x, y);//移动windows的光标
          printf("%c", back[coor_x][y]);
        }
         
        coor_x--;
        coor_y--;
 
        if(coor_x < 0)
        {
          return;
        }
         
        for(y = coor_y; y >= coor_y - 1; y--)
        {
          back[coor_x][y] = color_type;//方块色
          set_windows_pos(coor_x, y);//移动windows的光标
          printf("%c", back[coor_x][y]);
        }
      }
      else if(1 == dir_type)//竖向
      {
        for(x = coor_x; (x >= coor_x - 1) && (x >= 0); x--)
        {
          back[x][coor_y] = color_type;//方块色
          set_windows_pos(x, coor_y);//移动windows的光标
          printf("%c", back[x][coor_y]); 
        }
         
        coor_x--;
        coor_y--;
 
        if(coor_x < 0)
        {
          return;
        }
         
        for(x = coor_x; x >= coor_x - 1; x--)
        {
          back[x][coor_y] = color_type;//方块色
          set_windows_pos(x, coor_y);//移动windows的光标
          printf("%c", back[x][coor_y]); 
        }
      }
      else
      {
        printf("dir_type is error!\n");
      }
      break;
    case 3://7型
      if(0 == dir_type)//横向
      {
        for(y = coor_y; y >= coor_y - 2; y--)
        {
          back[coor_x][y] = color_type;//方块色
          set_windows_pos(coor_x, y);//移动windows的光标
          printf("%c", back[coor_x][y]);
        }
         
        coor_x--;
        coor_y = coor_y - 2;
 
        if(coor_x < 0)
        {
          return;
        }
         
        back[coor_x][coor_y] = color_type;//方块色
        set_windows_pos(coor_x, coor_y);//移动windows的光标
        printf("%c", back[coor_x][coor_y]);
      }
      else if(1 == dir_type)//竖向
      {
        for(x = coor_x; (x >= coor_x - 2) && (x >= 0); x--)
        {
          back[x][coor_y] = color_type;//方块色
          set_windows_pos(x, coor_y);//移动windows的光标
          printf("%c", back[x][coor_y]);
        }
         
        coor_x = coor_x - 2;
        coor_y--;
         
        if(coor_x < 0)
        {
          return;
        }
 
        back[coor_x][coor_y] = color_type;//方块色
        set_windows_pos(coor_x, coor_y);//移动windows的光标
        printf("%c", back[coor_x][coor_y]);
         
      }
      else
      {
        printf("dir_type is error!\n");
      }
      break;
    case 4://田型
      if((0 == dir_type) || (1 == dir_type))//横向
      {
        for(y = coor_y; y >= coor_y - 1; y--)
        {
          back[coor_x][y] = color_type;//方块色
          set_windows_pos(coor_x, y);//移动windows的光标
          printf("%c", back[coor_x][y]);
        }
         
        coor_x--;
 
        if(coor_x < 0)
        {
          return;
        }
         
        for(y = coor_y; y >= coor_y - 1; y--)
        {
          back[coor_x][y] = color_type;//方块色
          set_windows_pos(coor_x, y);//移动windows的光标
          printf("%c", back[coor_x][y]);
        }
      }
      else
      {
        printf("dir_type is error!\n");
      }
      break;
    default:
      printf("block_type is error!\n");
      break;
  }
}
 
void time_add(void)//加速函数
{
  if(500 == time)
  {
    time = 100;//减少的100毫秒
  }
  else if(100 == time)
  {
    time = 500;
  }
  else
  {
    ;//暂时留空
  }
}
 
void block_type_change(void)//方块类型变化
{
  block_type++;
   
  if(block_type > 4)
  {
    block_type = 1;
  }
}
 
/***************************************************************/
/*******************方块y坐标撞墙检测***************************/
/***输入:block_type 方块类型 1:长条 2:2型 3:7型 4:田型***/
/***输入:dir_type 方向类型 0:横向 1:竖向         ***/
/***输入:coor_y  方块当前头(右下角第一个方块)的坐标     ***/
/***************************************************************/
int new_back_y_check(int block_type, int dir_type, int coor_y)
{
  if(coor_y > 19)
  {
    coor_y = 19;
  }
   
  switch (block_type)
  {
    case 1://长条
      if(0 == dir_type)//横向
      {  
        if(coor_y - 3 < 0)
        {
          coor_y = 3;
        }
      }
      else if(1 == dir_type)//竖向
      {
        if(coor_y < 0)
        {
          coor_y = 0;
        }
      }
      else
      {
        printf("dir_type is error!\n");
      }
      break;
    case 2://2型
      if(0 == dir_type)//横向
      {
        if(coor_y - 2 < 0)
        {
          coor_y = 2;
        }
      }
      else if(1 == dir_type)//竖向
      {
        if(coor_y - 1 < 0)
        {
          coor_y = 1;
        }
      }
      else
      {
        printf("dir_type is error!\n");
      }
      break;
    case 3://7型
      if(0 == dir_type)//横向
      {
        if(coor_y - 2 < 0)
        {
          coor_y = 2;
        }
      }
      else if(1 == dir_type)//竖向
      {
        if(coor_y - 1 < 0)
        {
          coor_y = 1;
        }
      }
      else
      {
        printf("dir_type is error!\n");
      }
      break;
    case 4://田型
      if((0 == dir_type) || (1 == dir_type))//横向
      {
        if(coor_y - 1 < 0)
        {
          coor_y = 1;
        }
      }
      else
      {
        printf("dir_type is error!\n");
      }
      break;
    default:
      printf("block_type is error!\n");
      break;
  }
   
  return coor_y;
}
 
/*
检查方块是否触到下面已经停止的方块,触碰到就返回ERROR
*/
/***************************************************************/
/****检查方块是否触到下面已经停止的方块,触碰到就返回ERROR  ***/
/***输入:block_type 方块类型 1:长条 2:2型 3:7型 4:田型***/
/***输入:dir_type 方向类型 0:横向 1:竖向         ***/
/***输入:coor_x coor_y  方块当前头的坐标          ***/
/***初始coor_x,coor_y为一个方块的最右下的一个方块的坐标   ***/
/***************************************************************/
int block_move_check_x(int block_type, int dir_type, int coor_x, int coor_y)
{
  int ret = OK;
  int x = 0, y = 0;
   
  switch (block_type)
  {
    case 1://长条
      if(0 == dir_type)//横向
      {  
        for(y = coor_y; y >= coor_y - 3; y--)
        {
          if(NODE_INT == back[coor_x + 1][y])
          {
            ret = ERROR;
            break;
          }
        }
      }
      else if(1 == dir_type)//竖向
      {
        if(NODE_INT == back[coor_x + 1][coor_y])
        {
          ret = ERROR;
        }
      }
      else
      {
        printf("dir_type is error!\n");
      }
      break;
    case 2://2型
      if(0 == dir_type)//横向
      {
        for(y = coor_y; y >= coor_y - 1; y--)
        {
          if(NODE_INT == back[coor_x + 1][coor_y])
          {
            ret = ERROR;
            break;
          }
        }
         
        coor_x--;
        coor_y =coor_y - 2 ;
         
        if(NODE_INT == back[coor_x + 1][coor_y])
        {
          ret = ERROR;
        }
      }
      else if(1 == dir_type)//竖向
      {
        if(NODE_INT == back[coor_x + 1][coor_y])
        {
          ret = ERROR;
        }
         
        coor_x--;
        coor_y--;
         
        if(NODE_INT == back[coor_x + 1][coor_y])
        {
          ret = ERROR;
        }
      }
      else
      {
        printf("dir_type is error!\n");
      }
      break;
    case 3://7型
      if(0 == dir_type)//横向
      {
        for(y = coor_y; y >= coor_y - 2; y--)
        {
          if(NODE_INT == back[coor_x + 1][y])
          {
            ret = ERROR;
            break;
          }
        }
      }
      else if(1 == dir_type)//竖向
      {
        if(NODE_INT == back[coor_x + 1][coor_y])
        {
          ret = ERROR;
        }
         
        coor_x = coor_x - 2;
        coor_y--;
         
        if(NODE_INT == back[coor_x + 1][coor_y])
        {
          ret = ERROR;
        }
      }
      else
      {
        printf("dir_type is error!\n");
      }
      break;
    case 4://田型
      if((0 == dir_type) || (1 == dir_type))//横向
      {
        for(y = coor_y; y >= coor_y - 1; y--)
        {
          if(NODE_INT == back[coor_x + 1][y])
          {
            ret = ERROR;
            break;
          }
        }
      }
      else
      {
        printf("dir_type is error!\n");
      }
      break;
    default:
      printf("block_type is error!\n");
      break;
  }
       
  return ret;
}
 
/***************************************************************/
/****检查方块在向下移动的过程中是否触到左右的方块      ***/
/***输入:block_type 方块类型 1:长条 2:2型 3:7型 4:田型***/
/***输入:dir_type 方向类型 0:横向 1:竖向         ***/
/***输入:coor_x coor_y  方块当前头(右下)的坐标      ***/
/***输入:dir_block  当前方块的移动方向(左右)        ***/
/***************************************************************/
int block_move_check_y(int block_type, int dir_type, int coor_x, int coor_y, int dir_block)
{
  int x = 0, y = 0;
  int ret = OK;
 
  switch (block_type)
  {
    case 1://长条
      if(0 == dir_type)//横向
      {  
        if(1 == dir_block)//右移
        {  
          if(NODE_INT == back[coor_x][coor_y])
          {
            ret = ERROR;
          }
        }
        else if(0 == dir_block)//左移
        {
          if(NODE_INT == back[coor_x][coor_y - 3])
          {
            ret = ERROR;
          }
        }
        else
        {
          printf("dir_block is error!");
        }
      }
      else if(1 == dir_type)//竖向
      {
        //当长条为竖向时,不用判断长条是往左移还是往右移
        for(x = coor_x; x >= coor_x - 3; x--)
        {
          if(NODE_INT == back[x][coor_y])
          {
            ret = ERROR;
            break;
          }
        }
      }
      else
      {
        printf("dir_type is error!\n");
      }
      break;
    case 2://2型
      if(0 == dir_type)//横向
      {  
        if(1 == dir_block)//右移
        {  
          if(NODE_INT == back[coor_x][coor_y])
          {
            ret = ERROR;
            break;
          }
           
          if(NODE_INT == back[coor_x - 1][coor_y - 1])
          {
            ret = ERROR;
          }
        }
        else if(0 == dir_block)//左移
        {
          if(NODE_INT == back[coor_x][coor_y - 1])
          {
            ret = ERROR;
            break;
          }
           
          if(NODE_INT == back[coor_x - 1][coor_y - 2])
          {
            ret = ERROR;
          }
        }
        else
        {
          printf("dir_block is error!");
        }
      }
      else if(1 == dir_type)//竖向
      {
        if(1 == dir_block)//右移
        {  
          for(x = coor_x; x >= coor_x - 1; x--)
          {
            if(NODE_INT == back[x][coor_y])
            {
              ret = ERROR;
              break;
            }  
          }
           
          if(NODE_INT == back[coor_x - 2][coor_y - 1])
          {
            ret = ERROR;
          }
        }
        else if(0 == dir_block)//左移
        {
          if(NODE_INT == back[coor_x][coor_y])
          {
            ret = ERROR;
            break;
          }
           
          coor_x--;
          coor_y--;
           
          for(x = coor_x; x >= coor_x - 1; x--)
          {
            if(NODE_INT == back[x][coor_y])
            {
              ret = ERROR;
              break;
            }
          }
        }
        else
        {
          printf("dir_block is error!");
        }  
      }
      else
      {
        printf("dir_type is error!\n");
      }
      break;
    case 3://7型
      if(0 == dir_type)//横向
      {
        if(1 == dir_block)//右移
        {  
          if(NODE_INT == back[coor_x][coor_y])
          {
            ret = ERROR;
            break;
          }
           
          if(NODE_INT == back[coor_x - 1][coor_y - 2])
          {
            ret = ERROR;
          }
        }
        else if(0 == dir_block)//左移
        {
          if(NODE_INT == back[coor_x][coor_y - 2])
          {
            ret = ERROR;
            break;
          }
           
          if(NODE_INT == back[coor_x - 1][coor_y - 2])
          {
            ret = ERROR;
          }
        }
        else
        {
          printf("dir_block is error!");
        }
      }
      else if(1 == dir_type)//竖向
      {
        if(1 == dir_block)//右移
        {  
          for(x = coor_x; (x >= coor_x - 2) && (x >= 0); x--)
          {
            if(NODE_INT == back[x][coor_y])
            {
              ret = ERROR;
              break;
            }
          }
        }
        else if(0 == dir_block)//左移
        {
          for(x = coor_x - 1; (x >= coor_x - 2) && (x >= 0); x--)
          {
            if(NODE_INT == back[x][coor_y])
            {
              ret = ERROR;
              break;
            }
          }
         
          coor_x = coor_x - 2;
          coor_y--;
 
          if(NODE_INT == back[coor_x][coor_y])
          {
            ret = ERROR;
          }
        }
        else
        {
          printf("dir_block is error!");
        }
      }
      else
      {
        printf("dir_type is error!\n");
      }
      break;
    case 4://田型
      if((0 == dir_type) || (1 == dir_type))//横向
      {
        if(1 == dir_block)//右移
        {  
          for(x = coor_x; x >= coor_x - 1; x--)
          {
            if(NODE_INT == back[x][coor_y])
            {
              ret = ERROR;
              break;
            }
          }
        }
        else if(0 == dir_block)//左移
        {
          for(x = coor_x; x >= coor_x - 1; x--)
          {
            if(NODE_INT == back[x][coor_y - 1])
            {
              ret = ERROR;
              break;
            }
          }
        }
        else
        {
          printf("dir_block is error!");
        }
      }
      else
      {
        printf("dir_type is error!\n");
      }
      break;
    default:
      printf("block_type is error!\n");
      break;
  }
       
  return ret;
}
 
void block_clear_x(int row)//消除某一行
{
  int x = 0, y = 0;
   
  char back_replace[20][30] = {0};//替代back
   
  memcpy(back_replace, back, sizeof(back));//将back暂存到back_replace中
   
  for(x = 0; x <= row; x++)
  {
    for(y = 0; y < 20; y++)
    {
      back[x][y] = BACK;//初始化未背景色
    }
  }
 
  for(x = row; x >= 1; x--)
  {
    for(y = 0; y < 20; y++)
    {  
      back[x][y] = back_replace[x - 1][y];//消除一行,方块下沉
    }
  }
 
  set_windows_pos(0, 0);//移动windows的光标
 
  for(x = 0; x < 20; x++)
  {
    for(y = 0; y < 20; y++)
    {
      printf("%c", back[x][y]);
    }
     
    printf("\n");
  }
}
 
/*
检查是否消行并且进行计分
*/
int block_clear_sort(void)
{
  int x = 0, y = 0;
  int ret = ERROR;
  int flag = 0;
   
  for(x = 19; x >= 0; x--)//行
  {
    flag = 0;
     
    for(y = 0; y < 20; y++)
    {
      if(NODE_INT == back[x][y])
      {  
        flag++;//一行的块计数
      }
       
      if(20 == flag)//表示一行有20个方块
      {
        block_clear_x(x);//消行
        score++;//加分
        ret = OK;
      }
    }
  }
   
  return ret;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/u012520551/article/details/65001296

延伸 · 阅读

精彩推荐