服务器之家

服务器之家 > 正文

C语言俄罗斯方块游戏课程设计

时间:2021-11-16 15:28     来源/作者:C语言教程网

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

1、设计流程

C语言俄罗斯方块游戏课程设计

2、相关程序

?
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
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<time.h>
#include<dos.h>
#include<bios.h>
#define LEFT 0x4b00         /*键盘码*/
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
#define   TIMER   0x1c     /*   时钟中断的中断号   */
 
struct Snow
{
int x;
int y;
int speed;
}snow[100];
typedef struct
{
int box[4][4];
int color;
int next;
}SHAPE;
int x=0,y=4,form[16][12]={            /*x,y是用作记录每个方块的最左上角的编号*/
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1},
};                      /*俄罗斯方块初始化界面*/
SHAPE shapes[19]={
{1,0,0,0,
   1,0,0,0,
   1,1,0,0,
   0,0,0,0,CYAN,1},
{1,1,1,0,
   1,0,0,0,
   0,0,0,0,
   0,0,0,0,CYAN,2},
{1,1,0,0,
   0,1,0,0,
   0,1,0,0,
   0,0,0,0,CYAN,3},
{0,0,0,0,
   0,0,1,0,
   1,1,1,0,
   0,0,0,0,CYAN,0},
{0,1,0,0,
   0,1,0,0,
   1,1,0,0,
   0,0,0,0,MAGENTA,5},
{1,0,0,0,
   1,1,1,0,
   0,0,0,0,
   0,0,0,0,MAGENTA,6},
{1,1,0,0,
   1,0,0,0,
   1,0,0,0,
   0,0,0,0,MAGENTA,7},
{1,1,1,0,
   0,0,1,0,
   0,0,0,0,
   0,0,0,0,MAGENTA,4},
{1,0,0,0,
   1,1,0,0,
   0,1,0,0,
   0,0,0,0,YELLOW,9},
{0,1,1,0,
   1,1,0,0,
   0,0,0,0,
   0,0,0,0,YELLOW,8},
{0,1,0,0,
   1,1,0,0,
   1,0,0,0,
   0,0,0,0,BROWN,11},
{1,1,0,0,
   0,1,1,0,
   0,0,0,0,
   0,0,0,0,BROWN,10},
{0,1,0,0,
   1,1,1,0,
   0,0,0,0,
   0,0,0,0,WHITE,13},
{1,0,0,0,
   1,1,0,0,
   1,0,0,0,
   0,0,0,0,WHITE,14},
{1,1,1,0,
   0,1,0,0,
   0,0,0,0,
   0,0,0,0,WHITE,15},
{0,1,0,0,
   1,1,0,0,
   0,1,0,0,
   0,0,0,0,WHITE,12},
{1,0,0,0,
   1,0,0,0,
   1,0,0,0,
   1,0,0,0,RED,17},
{1,1,1,1,
   0,0,0,0,
   0,0,0,0,
   0,0,0,0,RED,16},
{1,1,0,0,
   1,1,0,0,
   0,0,0,0,
   0,0,0,0,BLUE,18}
};
 
int TimerCounter=0;
int snownum=0;
int size;
int change1=10;
int annal[4][2],score=0,level=0,color,Boxnumber;
void plot();
void operation();
void *save1,*save2;
void Copy();
void DrawSnow();
void Pr();
void   interrupt   (   *oldhandler)();
void   interrupt   newhandler( )
{
TimerCounter++;       
TimerCounter==36;
oldhandler();
}
void   SetTimer(void   interrupt   (*IntProc)())
{
oldhandler=getvect(TIMER);
disable();                /*   设置新的时钟中断处理过程时,禁止所有中断   */
setvect(TIMER,IntProc);
enable();               /*   开启中断   */
}
void   KillTimer()                                                          
{
disable();
setvect(TIMER,oldhandler);
enable();
}
void main()
{
int gdriver=DETECT,gmode;
SetTimer(newhandler);                      /*   修改时钟中断   */
registerbgidriver(EGAVGA_driver);
initgraph(&gdriver,&gmode,"c:\\turboc2");
Copy();
DrawSnow();
closegraph();
initgraph(&gdriver,&gmode,"E:\\TC20H\\INCLUDE\\GRAPHICS.H");        /*初始化图形*/
plot();
operation();
getch();
}
  void Copy()
{
setcolor(0);
setfillstyle(SOLID_FILL,15);
fillellipse(200,200,4,4);
size=imagesize(196,196,204,204);
save1=malloc(size);
save2=malloc(size);
getimage(196,196,204,204,save1);
getimage(96,96,104,104,save2);
}
void Pr()
{
int fr[]={392,392,440,294,262,262,220,294,392,392,
           440,532,440,392,262,262,220,294,392,294,
           262,247,220,196,392,294,330,294,262,262,
           220,294,330,294,262,294,22,247,220,196};
setcolor(change1/10);
settextstyle(0,0,3);
outtextxy(100,200,"Welcome to our Game!!!”);
sound(fr[change1/10]);
}
void DrawSnow()
{
int i;
int sx[62];
randomize();
for(i=0;i<62;i++)
sx[i]=(i+2)*10;
cleardevice();
while(!kbhit())
{
Pr();
if (snownum!=100)
{
    snow[snownum].speed=2+random(5);
    i=random(62);
    snow[snownum].x=sx[i];
    snow[snownum].y=10-random(100);
}
for(i=0;i<snownum;i++)
   putimage(snow[i].x,snow[i].y,save2,COPY_PUT);
  Pr();
if(snownum!=100)
   snownum++;
/*delay(300);*/
setfillstyle(SOLID_FILL,15);
for(i=0;i<snownum;i++)
   {
    snow[i].y+=snow[i].speed;
    putimage(snow[i].x,snow[i].y,save1,COPY_PUT);
    if(snow[i].y>500)
    snow[i].y=10-random(200);
   }
change1++;
if(change1==140)
change1=10;
}
nosound();
}
void plot()
{
int i,j;
char ch1[]={24,'-','R','o','l','l','\0'},
     ch2[]={25,'-','D','o','w','n','w','a','r','d','s','\0'},
     ch3[]={26,'-','T','u','r','n',' ','L','e','f','t','\0'},
     ch4[]={27,'-','T','u','r','n',' ','R','i','g','h','t','\0'};
setcolor(LIGHTGRAY);
rectangle(200,30,350,255);
i=0;
while((i++)<10)
  line(200+i*15,30,200+i*15,255);
i=0;
while((i++)<15)
  line(200,30+i*15,350,30+i*15);
setcolor(WHITE);
rectangle(170,270,390,330);
outtextxy(190,280,ch1);
outtextxy(270,280,ch2);
outtextxy(190,300,ch3);
outtextxy(290,300,ch4);
outtextxy(190,320,"Esc-Exit");
settextjustify(1,1);
outtextxy(390,50,"score");
outtextxy(390,100,"lexel");
outtextxy(390,150,"Next box");
outtextxy(390,65,"0");
outtextxy(390,115,"0");
}
void nextboxfun(SHAPE shapes[],int i)
{
int m,n;
for(m=0;m<4;m++)
   for(n=0;n<4;n++)
    if(shapes[i].box[m][n])
    {
     setfillstyle(1,shapes[i].color);
     bar(370+n*15+1,180+m*15+1,370+n*15+15-1,180+m*15+15-1);
     setcolor(LIGHTGRAY);
     rectangle(370+n*15,180+m*15,370+n*15+15,180+m*15+15);
    }
}
int Leftmobile()
{
int m,k=-1;         /*暂时存放annal中的坐标数据,用来判断左移是否成立*/
for(m=0;m<4;m++)
{
   if(annal[m][0]!=k)
   {
    if(form[annal[m][0]][annal[m][1]-1])
     return(0);
    k=annal[m][0];
   }
}
return(1);
}
int Rightmobile()
{
int m,k=-1;
for(m=3;m>=0;m--)
{
   if(annal[m][0]!=k)
   {
    if(form[annal[m][0]][annal[m][1]+1])
     return(0);
    k=annal[m][0];
   }
}
return(1);
}
int Downmobile()
{
int m;
for(m=0;m<4;m++)
   form[annal[m][0]][annal[m][1]]=2;     /*将方块此时的位置设置成2以方便后面的判断*/
for(m=0;m<4;m++)
{
   if(form[annal[m][0]+1][annal[m][1]]==1)          /*如果等于1说明此处已经有方块*/
   {
    for(m=0;m<4;m++)/*将原本数值还原*/
     form[annal[m][0]][annal[m][1]]=1;
    return(0);
   }
}
for(m=0;m<4;m++)                        /*将原本数值还原*/
   form[annal[m][0]][annal[m][1]]=1;
return(1);
}
void LeftRedraw()
{
int m;
y--;
for(m=0;m<4;m++)
{
   setfillstyle(1,BLACK);
bar(200+(annal[m][1]-1)*15+1,30+annal[m][0]*15+1,200+(annal[m][1]-1)*15+15-1,30+annal[m][0]*15+15-1);          /*将原图像位置设置为黑色框*/
}
for(m=0;m<4;m++)
   form[annal[m][0]][annal[m][1]]=0;
for(m=0;m<4;m++)
{
   annal[m][1]--;
   form[annal[m][0]][annal[m][1]]=1;
   setfillstyle(1,color);
bar(200+(annal[m][1]-1)*15+1,30+annal[m][0]*15+1,200+(annal[m][1]-1)*15+15-1,30+annal[m][0]*15+15-1);       /*在新的位置重绘图像*/
}
}
void RightRedraw()
{
int m;
y++;
for(m=0;m<4;m++)
{
   setfillstyle(1,BLACK);
bar(200+(annal[m][1]-1)*15+1,30+annal[m][0]*15+1,200+(annal[m][1]-1)*15+15-1,30+annal[m][0]*15+15-1);           /*将原图像位置设置为黑色框*/
}
for(m=0;m<4;m++)
   form[annal[m][0]][annal[m][1]]=0;
for(m=0;m<4;m++)
{
 annal[m][1]++;
  form[annal[m][0]][annal[m][1]]=1;
  setfillstyle(1,color);
bar(200+(annal[m][1]-1)*15+1,30+annal[m][0]*15+1,200+(annal[m][1]-1)*15+15-1,30+annal[m][0]*15+15-1); }          /*在新的位置重绘图像*/
}
void DownRedraw()
{
int m,n;
x++;
for(m=0;m<4;m++)
{
   setfillstyle(1,BLACK);
bar(200+(annal[m][1]-1)*15+1,30+annal[m][0]*15+1,200+(annal[m][1]-1)*15+15-1,30+annal[m][0]*15+15-1);            /*将原图像位置设置为黑色框*/
}
for(m=0;m<4;m++)
   form[annal[m][0]][annal[m][1]]=0;
for(m=0;m<4;m++)
{
   annal[m][0]++;
   form[annal[m][0]][annal[m][1]]=1;
   setfillstyle(1,color); bar(200+(annal[m][1]-1)*15+1,30+annal[m][0]*15+1,200+(annal[m][1]-1)*15+15-1,30+annal[m][0]*15+15-1);      /*在新的位置重绘图像*/
}
}
int UPmobile()
{
int m,n,k;
k=shapes[Boxnumber].next;/*k==此方块下一个方块的编号*/
for(m=0;m<4;m++)
   form[annal[m][0]][annal[m][1]]=2;         
for(m=0;m<4;m++)
   for(n=0;n<4;n++)
   {
    if(form[x+m][y+n]==1&&shapes[k].box[m][n])      
{
for(m=0;m<4;m++)
      form[annal[m][0]][annal[m][1]]=1;/*将2还原为1*/
     return(0);
}
}
 for(m=0;m<4;m++)
    form[annal[m][0]][annal[m][1]]=1;
   return(1);
}
void change()
{
int m,n,k,i=0;
k=Boxnumber=shapes[Boxnumber].next;/*等于要转变的方块编号*/
for(m=0;m<4;m++)
{
   setfillstyle(1,BLACK);
bar(200+(annal[m][1]-1)*15+1,30+annal[m][0]*15+1,200+(annal[m][1]-1)*15+15-1,30+annal[m][0]*15+15-1);        /*将原图像位置设置为黑色框*/
}
for(m=0;m<4;m++)               /*将原方块位置改为0*/
   form[annal[m][0]][annal[m][1]]=0;
for(m=0;m<4;m++)              /*重新记录新的形状坐标*/
   for(n=0;n<4;n++)
    if(shapes[k].box[m][n])
    {
     annal[i][0]=x+m;         /*annal更新新的坐标*/
     annal[i][1]=y+n;
     i++;
    }
    for(m=0;m<4;m++)              /*将新坐标设置为1并绘图*/
    {
     form[annal[m][0]][annal[m][1]]=1;
     setfillstyle(1,color);   bar(200+(annal[m][1]-1)*15+1,30+annal[m][0]*15+1,200+(annal[m][1]-1)*15+15-1,30+annal[m][0]*15+15-1)         ;/*在新的位置重绘图像*/
    }
}
void FulllineJudge()
{
void *p1;
int m,n,i,k,p,q;
char *ch;
if(!(p1=malloc(imagesize(200,30,350,255))))
{
   printf("开辟空间失败\n");
   getch();
   exit(1);
}
i=0;
for(m=14;m>=0;m--)    /*逐行判断是否有满行情况*/
{
   for(n=1;n<=10;n++)
   {
    if(!form[m][n])         /*如果有一个为0那么退出本行的循环*/
     break;
    else if(n==10)           /*1-10全为1*/
    {
     i++;            /*用来记录所消行数*/
     for(p=m;p>=1;p--)
      for(q=1;q<=10;q++)        /*所有行数信息下降1行*/
       form[p][q]=form[p-1][q];
      for(p=1;p<=10;p++)          /*最顶行清零*/
       form[0][p]=0;
      getimage(200,30,350,30+m*15,p1);
      putimage(200,45,p1,0);
      m++;
    }
   }
  if(i==4)    /*如果已经消掉4行则不用再进行判断*/
    break;
}
if(i==1)      /*一次所消行数进行不同分数奖励*/
   score+=10;
if(i==2)
   score+=30;
if(i==3)
   score+=60;
if(i==4)
   score+=100;
setcolor(WHITE);       /*绘图部分*/
sprintf(ch,"%d",score);
setfillstyle(1,BLACK);
bar(380,60,400,80);
outtextxy(390,65,ch);
level=score/500;
setfillstyle(1,BLACK);
bar(380,110,400,130);
sprintf(ch,"%d",level);
outtextxy(390,115,ch);
free(p1);
}
void operation()
{
int newbox,nextbox,m,n,k=1,i,KEY,l,o;
o=1;
srand((unsigned)time(NULL));
nextbox=rand()%19;
while(o)
{
   if(k)           /*产生新方块*/
   {
    x=0,y=4;           /*还原x,y*/
    Boxnumber=newbox=nextbox;
    nextbox=rand()%19;
    setfillstyle(1,BLACK);
    bar(360,160,430,250);
    nextboxfun(shapes,nextbox);
    i=0;
    color=shapes[newbox].color;
    for(m=0;m<4;m++)
     for(n=0;n<4;n++)
      if(shapes[newbox].box[m][n])  /*将新方块在俄罗斯方块界面的坐标记录在annal中*/
      {
       annal[i][0]=0+m;    /*记录坐标*/
       annal[i][1]=4+n;
       if(form[0+m][4+n])
       {
        setfillstyle(1,BLACK);
        bar(240,130,310,150);
        setcolor(RED);
        outtextxy(275,140,"GAME OVER");
        getch();
        o=0;
       }
       form[0+m][4+n]=1;
       setfillstyle(1,shapes[newbox].color);
       bar(200+n*15+1+45,30+m*15+1,200+n*15+15-1+45,30+m*15+15-1);/*将对应的位画上颜色*/
       setcolor(LIGHTGRAY);
       rectangle(200+n*15+45,30+m*15,200+n*15+15+45,30+m*15+15);
       i++;
      }
      k=0;
    }
  if(bioskey(1))      /*读取键盘*/
    KEY=bioskey(0);
   else
    KEY=0;
   switch(KEY)
   {
    case LEFT:/*左*/
    if(Leftmobile())
     LeftRedraw();break;
    case RIGHT:/*右*/
    if(Rightmobile())
     RightRedraw();break;
    case DOWN:/*下*/
    if(Downmobile())
     DownRedraw();
    else
    {FulllineJudge();k=1;}break;
   case UP:/*变形*/
    if(UPmobile())
     change();break;
   case ESC:o=0;break;
   }
   if(TimerCounter>(36-level*2))      /*TimerCounter每秒钟增加18*/
   {
    TimerCounter=0;
    if(Downmobile())        /*作下降处理*/
     DownRedraw();
    else
    {FulllineJudge();k=1;}
   }
}
KillTimer();
}

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

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部