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

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

服务器之家 - 编程语言 - C/C++ - C语言实现打飞机游戏

C语言实现打飞机游戏

2021-08-21 16:4442577465 C/C++

这篇文章主要为大家详细介绍了C语言实现打飞机游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
 
#define U 1
 #define D 2
 #define L 3
 #define R 4
 
typedef struct node  //己方飞机的节点
 {
   int x;
   int y;
   struct node *next;
 }node;
 
typedef struct Bullet
 {
   int x;
   int y;
   int status;  //0 1 表示此处是否有子弹
   struct Bullet *next;
 }Bullet;   //子弹模型节点
 
typedef struct Enemy
 {
   int x;
   int y;
   int status;   //0 1 表示此节点是否存在飞机
 }Enemy;   //敌机节点
 
// 全局变量//
 node *head,*p;
 Enemy Ehead[30];
 Bullet *HBullet,*q,*H;
 int Status,score=0;
 int BulletStatus,s=1;
 int add_1=1,add_2=0;
 
//函数声明//
 void Pos();     //固定光标
 void CreateMap();    //创建游戏界面
 void CreateAircraft();    //创建自己的飞机
 void printAircraft();    //打印自己的飞机
 void Aircraftmove();   //自己飞机的移动
 void CreateEnemyAircraftModel();  //创建敌机的模型 结构体数组
 void CreateEnemyAircraft();   //创建一架敌方飞机
 void EnemyAircraftMove();  //敌机的移动
 void CreateBulletModel();   //创建子弹的模型
 void Shoot();   //发射子弹
 void Bulletmove();   //子弹的移动
 int GetBulletStatus();    //获取是否有子弹存在
 void JudgeCollide();     //判定是否击中敌机
 void JudgeGameOver();   //判定是否与敌机相撞
 //为了防止在运行别的函数时这个函数无法运行,所以讲起嵌入每一个循环的函数中,保证不遗漏任何情况.
 void Pause();    //暂停
 void ControlGame();  //控制游戏主程序
 void GameStart();   //游戏开的的预先运行函数集合
 void EndGame();  //结束游戏
 /*
     ■    飞机的基本模型
        ■■■   第一行为机头,   敌机的设定只需用到机头,机身仅仅只是打印出来,并没有实际内容
         ■■■   下两行为机身
 
*/
 void Pos(int x,int y)//设置光标位置
 {
   COORD pos;
   pos.X=x;
   pos.Y=y;
   HANDLE hOutPut;
   hOutPut=GetStdHandle(STD_OUTPUT_HANDLE);
   SetConsoleCursorPosition(hOutPut,pos);
 }
 
void CreateMap()
 {
   system("mode con cols=100 lines=55");
   int i;
   for(i=0;i<=67;i+=2)//上下边
   {
     Pos(i,0);
     printf("■");
     Pos(i,50);
     printf("■");
   }
   for(i=0;i<=50;i++)//左右边
   {
     Pos(0,i);
     printf("■");
     Pos(66,i);
     printf("■");
 
  }
   Pos(80,8);
   printf("您的得分%d",score);
   Pos(70,20);
   printf("Space:发射子弹");
   Pos(70,22);
   printf("ESC:退出游戏");
   Pos(70,24);
   printf("Enter:暂停");
   Pos(70,26);
   printf("用↑.↓.←.→控制飞机");
   Pos(70,28);
   printf("F1:加速敌机的产生");
   Pos(70,30);
   printf("F2:减缓敌机的产生");
   Pos(80,40);
   printf("特别说明:");
   Pos(70,42);
   printf("敌机从地图下飞出不结束游戏");
 }
 
void CreateAircraft()
 {
   node *q;
   int i;
   head=(node*)malloc(sizeof(node));//链表形式创建飞机,x,y分别为显示的坐标
   head->x=32;
   head->y=47;
   q=head;
   for(i=0;i<=4;i+=2)
   {
     p=(node*)malloc(sizeof(node));
     head->next=p;
     p->x=30+i;
     p->y=48;
     head=p;
   }
   for(i=0;i<=4;i+=2)
   {
     p=(node*)malloc(sizeof(node));
     head->next=p;
     p->x=30+i;
     p->y=49;
     head=p;
   }
   p->next=NULL;
   head=q;
   p=head;
 }
 
void printAircraft()
 {
   while(p!=NULL)
   {
     Pos(p->x,p->y);
     printf("■");
     p=p->next;
   }
   p=head;
 
}
 
void Aircraftmove()
 {
   JudgeGameOver();
   JudgeCollide();
   Sleep(50);
   if(Status==U)    //向上飞行
   {
     while (p!=NULL)
     {
       p->y=p->y-1;
       Pos(p->x,p->y);
       printf("■");
       p=p->next;
     }
     p=head;
     Pos(p->x-2,p->y+3);
     printf(" ");
     Pos(p->x,p->y+3);
     printf(" ");
     Pos(p->x+2,p->y+3);
     printf(" ");
   }
   if(Status==D)    //向下飞行
   {
     while (p!=NULL)
     {
       p->y=p->y+1;
       Pos(p->x,p->y);
       printf("■");
       p=p->next;
     }
     p=head;
     Pos(p->x,p->y-1);
     printf(" ");
     Pos(p->x-2,p->y);
     printf(" ");
     Pos(p->x+2,p->y);
     printf(" ");
   }
   if(Status==L)    //向左飞行
   {
     while (p!=NULL)
     {
       p->x=p->x-2;
       Pos(p->x,p->y);
       printf("■");
       p=p->next;
     }
     p=head;
     Pos(p->x+2,p->y);
     printf(" ");
     Pos(p->x+4,p->y+1);
     printf(" ");
     Pos(p->x+4,p->y+2);
     printf(" ");
   }
   if(Status==R)     //向右飞行
   {
     while (p!=NULL)
     {
       p->x=p->x+2;
       Pos(p->x,p->y);
       printf("■");
       p=p->next;
     }
     p=head;
     Pos(p->x-2,p->y);
     printf(" ");
     Pos(p->x-4,p->y+1);
     printf(" ");
     Pos(p->x-4,p->y+2);
     printf(" ");
   }
 
}
 
void CreateEnemyAircraftModel()
 {       
  int j;
   for(j=1;j<=30;j++)    //结构体数组表示敌军飞机最多同时存在30架,也许不可能有那么多,数字用的稍大.
   {        //每一个值表示一架飞机,敌机只需知道其机头坐标
     Ehead[j].x=-1;
     Ehead[j].y=1;
     Ehead[j].status=0;
   }
 }
 
void CreateEnemyAircraft()//创建出一架新的飞机
 {
   JudgeCollide();
   JudgeGameOver();
   srand((unsigned)time(NULL));
   int j;
   for(j=1;j<=20;j++)
   {
     if(Ehead[j].status==0)
     {
       JudgeGameOver();
       while(Ehead[j].x%2!=0) //保证机头x为偶数,对其格子.
       {
         Ehead[j].x=rand()%62+3;
       }
       Ehead[j].y=1;
       Ehead[j].status=1;
       Pos((Ehead[j].x),(Ehead[j].y));
       printf("■");
       break;
     }
   }
 }
 
void EnemyAircraftMove()       //模拟敌机向下飞行的函数
 {
   Sleep(100);
   if(s>=15)     //控制敌机飞机的出现频率
   {
     CreateEnemyAircraft();
    s=1;
   }
   s=s+add_1;
   JudgeCollide();
   JudgeGameOver();
   int j;
   for(j=1;j<=30;j++)
   {
     if(Ehead[j].status==1 && Ehead[j].y==1)  //特殊情况
     {
       Pos(Ehead[j].x,Ehead[j].y);
       printf(" ");
     }
     if(Ehead[j].status==1 && Ehead[j].y==2) //特殊情况
     {
       Pos(Ehead[j].x,Ehead[j].y);
       printf(" ");
       Pos(Ehead[j].x-2,Ehead[j].y-1);
       printf("  ");
     }
     if(Ehead[j].status==1 && 3<=Ehead[j].y && Ehead[j].y<=49)
     {
       Pos(Ehead[j].x,Ehead[j].y);
       printf(" ");
       Pos(Ehead[j].x-2,Ehead[j].y-1);
       printf("  ");
       Pos(Ehead[j].x-2,Ehead[j].y-2);
       printf("  ");
     }
     if(Ehead[j].status==1 && Ehead[j].y==50)//特殊情况
     {
       Pos(Ehead[j].x-2,Ehead[j].y-1);
       printf("  ");
       Pos(Ehead[j].x-2,Ehead[j].y-2);
       printf("  ");
     }
     if(Ehead[j].status==1 && Ehead[j].y==51)//特殊情况
     {
       Pos(Ehead[j].x-2,Ehead[j].y-2);
       printf("  ");
     }
   }
   for(j=1;j<=20;j++)      //将每一个敌机机头向下移动一位
   {
     if(Ehead[j].status==1)
     {
       if(Ehead[j].y<=52)
       {
         Ehead[j].y=Ehead[j].y+1;
       }
       if(Ehead[j].y>=53)
       {
         Ehead[j].status=0;
       }
     }
   }
   for(j=1;j<=20;j++)
   {
     if(Ehead[j].status==1 && Ehead[j].y==2)//特殊情况
     {
       Pos(Ehead[j].x,Ehead[j].y);
       printf("■");
       Pos(Ehead[j].x-2,Ehead[j].y-1);
       printf("■■■");
     }
     if(Ehead[j].status==1 && 3<=Ehead[j].y && Ehead[j].y<=50)
     {
       Pos(Ehead[j].x,Ehead[j].y);
       printf("■");
       Pos(Ehead[j].x-2,Ehead[j].y-1);
       printf("■■■");
       Pos(Ehead[j].x-2,Ehead[j].y-2);
       printf("■■■");
     }
     if(Ehead[j].status==1 && Ehead[j].y==51)//特殊情况
     {
       Pos(Ehead[j].x-2,Ehead[j].y-1);
       printf("■■■");
       Pos(Ehead[j].x-2,Ehead[j].y-2);
       printf("■■■");
     }
     if(Ehead[j].status==1 && Ehead[j].y==52)//特殊情况
     {
       Pos(Ehead[j].x-2,Ehead[j].y-2);
       printf("■■■");
     }
   }
   JudgeGameOver();
 }
 
void CreateBulletModel()      //创建子弹的模型
 {
   int i;
   q=(Bullet*)malloc(sizeof(Bullet));   
  q->x=0;
   q->y=1;
   q->status=0;
   H=q;
   for(i=2;i<=46;i++)   //每行最多存在一个子弹,子弹的最大数值为地图上下边框所确定的值.所以一上下边框确定一个最大的链表.
   {
     HBullet=(Bullet*)malloc(sizeof(Bullet));
     HBullet->y=i;
     HBullet->x=-1;
     HBullet->status=0;
     q->next=HBullet;
     q=HBullet;
   }
   q->next=H;
   HBullet=H;
 }
 
void Shoot()    //发射子弹
 {
   JudgeGameOver();
   JudgeCollide();
   while(HBullet->next->y!=head->y)   
   {
     HBullet=HBullet->next;
   }
   HBullet->x=head->x;
   HBullet->status=1;    //1表示此处有子弹
   Pos(HBullet->x,HBullet->y);
   printf("■");
   HBullet=H;
 }
 
void BulletMove()     //子弹的飞行
 {         //与敌军飞机飞行同理
   JudgeGameOver();
   JudgeCollide();
   while(HBullet!=q)
   {
     if(HBullet->status==1)
     {
       Pos(HBullet->x,HBullet->y);
       printf(" ");
     }
     HBullet=HBullet->next;
   }
   HBullet=H->next;
   while(HBullet!=q)
   {
     H->x=HBullet->x;
     H->status=HBullet->status;
     H=H->next;
     HBullet=HBullet->next;
   }
 
  HBullet=HBullet->next;
   H=HBullet;
   while(HBullet!=q)
   {
     if(HBullet->status==1)
     {
       Pos(HBullet->x,HBullet->y);
       printf("■");
     }
     HBullet=HBullet->next;
   }
   HBullet=H;
   JudgeGameOver();
 }
 
int GetBulletStatus()  //获取地图中是否存在子弹,不存在子弹的时候BulletMove()将不运行
 {
   JudgeGameOver();
   JudgeCollide();
   while(HBullet!=q)
   {
     if(HBullet->status==1)
     {
       BulletStatus=1;
       break;
     }
     HBullet=HBullet->next;
   }
   HBullet=H;
   return 0;
 }
 
void JudgeCollide()   //判断是否集中敌机
 
  int j;
   while(HBullet!=q)
   {
     for(j=1;j<=30;j++)
     {
       JudgeGameOver();
       if(HBullet->status==1 && Ehead[j].status==1)
       {
         if((HBullet->x==Ehead[j].x && HBullet->y==Ehead[j].y))
         {
           HBullet->status=0;
           Ehead[j].status=0;
           score=score+10+add_2;
           Pos(80,8);
           printf("您的得分%d",score);
           Pos(HBullet->x,HBullet->y);
           printf(" ");
           Pos(HBullet->x-2,HBullet->y-1);
           printf("  ");
           Pos(HBullet->x-2,HBullet->y-2);
           printf("  ");
         }
         if(HBullet->x+2==Ehead[j].x && HBullet->y==Ehead[j].y)
         {
           HBullet->status=0;
           Ehead[j].status=0;
           score=score+10+add_2;
           Pos(80,8);
           printf("您的得分%d",score);
           Pos(HBullet->x,HBullet->y);
           printf(" ");
           Pos(HBullet->x+2,HBullet->y);
           printf(" ");
           Pos(HBullet->x,HBullet->y-1);
           printf("  ");
           Pos(HBullet->x,HBullet->y-2);
           printf("  ");
         }
         if(HBullet->x-2==Ehead[j].x && HBullet->y==Ehead[j].y)
        {
           HBullet->status=0;
           Ehead[j].status=0;
           score=score+10+add_2;
           Pos(80,8);
           printf("您的得分%d",score);
           Pos(HBullet->x,HBullet->y);
           printf(" ");
           Pos(HBullet->x-2,HBullet->y);
           printf(" ");
           Pos(HBullet->x-4,HBullet->y-1);
           printf("  ");
           Pos(HBullet->x-4,HBullet->y-2);
           printf("  ");
         }
       }
 
    }
     HBullet=HBullet->next;
   }
   HBullet=H;
 }
 
void JudgeGameOver() //判断是否撞机
 {
   int j;
   for(j=1;j<=30;j++)
   {
     while(p!=NULL)
     {
       if(Ehead[j].x==p->x && Ehead[j].y==p->y)
       {
         EndGame();
       }
       p=p->next;
     }
   }
   p=head;
 }
 
void Pause()//暂停
 {
   while(1)
   {
     Sleep(300);
     if(GetAsyncKeyState(VK_RETURN))
     {
      break;
     }
   }
 }
 
void ControlGame()    // 控制主程序
 {
   while(1)
   {   
    JudgeGameOver();
     JudgeCollide();
     if(GetAsyncKeyState(VK_UP) && head->y!=1)
     {
       Status=U;
       Aircraftmove();
     }
     else if(GetAsyncKeyState(VK_DOWN) && head->y+2!=49)
     {
 
      Status=D;
       Aircraftmove();
     }
     else if(GetAsyncKeyState(VK_LEFT) && head->x-2!=2)
     {
       Status=L;
       Aircraftmove();
     }
     else if(GetAsyncKeyState(VK_RIGHT) && head->x+2!=64)
     {
       Status=R;
       Aircraftmove();
     }
     if(GetAsyncKeyState(VK_F1) && head->y!=1)
     {
       if(add_1<5)
      {
       add_1=add_1+1;
       add_2=add_2+2;
       }
     }
     if(GetAsyncKeyState(VK_F2) && head->y!=1)
     {
       if(add_1>2 &&add_1 <=5)
      {
       add_1=add_1-1;
       add_1=add_2-2;
       }
     }
     else if(GetAsyncKeyState(VK_RETURN))
     {
       Pause();
     }
     else if(GetAsyncKeyState(VK_SPACE))
     {
       Shoot();
     }
     else if(GetAsyncKeyState(VK_ESCAPE))
     {
       EndGame();
     }
     GetBulletStatus();
     if(BulletStatus==1)
     {
       BulletMove();
     }
     EnemyAircraftMove();
     JudgeGameOver();
   }
 }
 
void GameStart()
{
   CreateMap();
   CreateAircraft();
   printAircraft();
   CreateEnemyAircraftModel();
   CreateBulletModel();
 }
 
void EndGame()
 {
   system("cls");
   Pos(35,25);
   printf("Game Over.");
   Pos(1,50);
   exit(0);
 }
 
void main()
 {
   GameStart();
   ControlGame();
 }

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

原文链接:https://blog.csdn.net/qq_42577465/article/details/90716603

延伸 · 阅读

精彩推荐