当前位置:   article > 正文

飞机大战小游戏 C语言(课设任务)_c语言链表游戏

c语言链表游戏

 大一课设做的飞机大战,可以进行登入和注册,这个是利用单链表做的,源代码已经给出,这个是最基本的飞机大战模式,我设置了几个功能,比如排行榜之类的。排行榜是用结构体数组做的,已及冒泡排序,并且在文件里保存信息。比较简单。

搞清楚链表注册和登入问题的话,恭喜你单链表你已经掌握差不多了。我的另一篇文章专门介绍了链表的基本知识。https://blog.csdn.net/m0_62387059/article/details/122731940

这个是注册页面规范:

这个是登入页面:

游戏菜单: 

 

飞机大战页面: 

 

 话不多说,直接上代码

以下是源代码 

  1. #include"stdio.h"
  2. #include"windows.h"    //用于获取窗口的句柄与屏幕控制
  3. #include"conio.h"      //用于获取键盘输入的内容
  4. #include"string.h"  
  5. #include"stdlib.h"   
  6. /*两个结构体*/
  7. /*用于存用户信息*/ 
  8. typedef struct usepeople
  9. {
  10.     char name[20];//用户名 
  11.     char mm[20];//密码 
  12.     usepeople *next;
  13. }U;
  14. /*输出历史记录*/ 
  15. struct history
  16. {
  17.     char NAME[20];
  18.     int SCORE;
  19. }s[10];
  20. //定义全局变量
  21. int o;    //保存排行榜分数
  22. char j[20];     //保存排行榜用户名
  23. int h,w;   //定义画面的高和宽 
  24. int p_x,p_y;       // 定义我方飞机的位置
  25. int e_x,e_y;       // 定义敌方飞机的位置 
  26. int b_x,b_y;       //定义子弹的位置 
  27. int score;         //得分
  28. int f;              //定义飞机的状态
  29. int num=1;          //控制排名的数量  (我设置的是8个)     
  30. void gotoxy(int x,int y);              //光标移动到(x,y)位置 
  31. void menu1(U *h,int num);                       //菜单功能   
  32. int color(int c);                       //更改文字颜色
  33. void explation();                    //游戏右侧显示 
  34. void tip();                         //游戏说明
  35. void seescore();                      //分数显示 
  36. void startup();                      //游戏初始化 
  37. void show();                        //显示游戏画面 
  38. void Fly();                         //定义函数来控制子弹和敌人的移动 
  39. void Planefly() ;                   //定义函数来控制飞机的移动和子弹的发射
  40. void gameover(U *h);                  //设计游戏结束界面
  41. U * createpeople(U *h);           //用于注册用户的信息
  42. void create(U *h);                //建立第一个注册信息 
  43. void History(struct history s[],int num); //用于遍历输出挑战者信息
  44. void insert(U *h);            //在结尾加入注册后的玩家账号 
  45. U* search(U *h,char *name);  //用于判断用户的用户名是否正确 
  46. void deletenum(U *h,char name[]);//注销用户信息 
  47. void iregister(U *h);        //注册页面 
  48. void dengru(U *h,int num);   //登入页面 
  49. void log_in(U *h);               //保存用户信息 
  50. void msort(history *a,int n);    //记录冒泡排序 
  51. int color(int c)
  52. {
  53.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);        //更改文字颜色
  54.     return 0;
  55. }
  56. //注销用户信息
  57. void deletenum(U *h,char name[])
  58. {
  59.     U *p=h->next;
  60.     while(p&&strcmp(p->name,name)!=0)
  61.     {
  62.         h=p;
  63.         p=p->next;
  64.     }
  65.     if(p)
  66.     {
  67.         h->next=p->next;
  68.         free(p);
  69.     }
  70. }
  71. /*设置光标的位置*/
  72. void gotoxy(int x,int y)
  73. {
  74.     COORD c;
  75.     c.X=x;
  76.     c.Y=y;
  77.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
  78. }
  79. /*光标的隐藏*/
  80. void HideCursor()
  81. {
  82.     CONSOLE_CURSOR_INFO cursor_info = {1,0};//第二个值为0表示隐藏光标
  83.     SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
  84. }
  85. /*进行冒泡排序*/ 
  86. void msort(history *a,int n)
  87. {
  88.     
  89.     for(int i=1;i<num;i++)
  90.     {
  91.         for(int f=num;f>i;f--)
  92.         {
  93.             if(s[f].SCORE>s[f-1].SCORE)
  94.             {
  95.                 o=a[f].SCORE;
  96.                 a[f].SCORE=a[f-1].SCORE;
  97.                 a[f-1].SCORE=o;
  98.                 strcpy(j,a[f].NAME);
  99.                 strcpy(a[f].NAME,a[f-1].NAME);
  100.                 strcpy(a[f-1].NAME,j);
  101.             }
  102.         }
  103.     }
  104. }
  105. //用于遍历输出挑战者信息
  106. void History(struct history s[],int num,U* h)
  107. {
  108.     system("cls");
  109.     if(num==1)
  110.     {
  111.         gotoxy(1,1);
  112.         printf("你还没有记录");
  113.     }
  114.     
  115.     for(int i=1;i<num;i++)   //这里的for是用来逐一输出每一个用户的数据 
  116.     {
  117.         gotoxy(15,3+2*i);
  118.         printf("%d:用户名:%s   ",i,s[i].NAME);
  119.         printf("得分:%d   ",s[i].SCORE);
  120.     
  121.     }
  122.         gotoxy(1,28);
  123.         printf("请注意最多保存8次记录");
  124.         getch();
  125.         system("cls");
  126.         menu1(h,num);  //看完以后返回开始界面 
  127. //建立第一个注册信息 
  128. void create(U *h)
  129. {
  130.     U *p,*q=h;
  131.     char name[20];
  132.     printf("请输入用户名:\n");
  133.     gotoxy(39,12);
  134.     scanf("%s",name);
  135.     while(strcmp(name,"OVER"))
  136.     {
  137.         p=(U*)malloc(sizeof(U));
  138.         strcpy(p->name,name);
  139.         char mm[20];
  140.         gotoxy(39,13);
  141.         printf("请输入密码:(换行输入OVER结束)\n"); 
  142.         gotoxy(39,14);
  143.         scanf("%s",mm);
  144.         strcpy(p->mm,mm);
  145.         q->next=p;
  146.         q=p;
  147.         gotoxy(39,15);
  148.         scanf("%s",name);
  149.     }
  150.     q->next=NULL;
  151. }
  152. U* search(U *h,char *name)
  153. {
  154.     while(h=h->next,h)
  155.     {if(!strcmp(h->name,name))  return h;}
  156.     return NULL;
  157. }
  158. U * createpeople(U *h)
  159. {
  160.     U *q;
  161.     q=(U*)malloc(sizeof(U));
  162. W:    printf("输入你要注册的用户名:\n");
  163.     scanf("%s",q->name);
  164.     if(search(h,q->name)) {system("cls");printf("该用户名已存在!!!\n");_getch();system("cls");goto W;}
  165.     printf("输入你的密码:\n");
  166.     scanf("%s",q->mm);
  167.     q->next=NULL;
  168.     return q;
  169. }
  170. //在结尾加入注册后的玩家账号 
  171. void insert(U *h)  
  172. {
  173.     U *q,*temp;
  174.     q=createpeople(h);
  175.     while(h=h->next,h){if(h->next==NULL)break;}temp=h;
  176.     q->next=temp->next;
  177.     temp->next=q;
  178. }
  179. /*游戏说明*/
  180. void tip()    
  181. {
  182.     gotoxy(30,5);printf("w向上,s向下,a向左,d向右,按空格射击");
  183.     gotoxy(30,10) ;printf("你击落一架敌机可得1分"); 
  184.     gotoxy(30,15);    printf("如果你被敌机撞上就会死亡");
  185. }                     
  186. void menu1(U *h,int num)                       //菜单功能   
  187. {
  188.     int n;
  189.     gotoxy(37,10);
  190.     printf("飞 机 大 战"); 
  191.     gotoxy(15,20);
  192.     printf("输入数字1:开始游戏") ;
  193.     gotoxy(55,20);
  194.     printf("输入数字2:查看规则");
  195.     gotoxy(15,22);
  196.     printf("输入数字3:查看挑战榜"); 
  197.     gotoxy(55,22);
  198.     printf("输入数字4:退出游戏"); 
  199.     gotoxy(15,24);
  200.     printf("输入数字5:注销账号");
  201.     gotoxy(55,24);
  202.     printf("输入数字6:请你签到"); 
  203.     gotoxy(37,25);
  204.     printf("请输入你的选择:"); 
  205.      scanf("%d",&n);
  206.      switch(n)
  207.      {
  208.          case 1:
  209.              {
  210.                  system("cls"); 
  211.              }break;
  212.         case 2:
  213.             {
  214.                 system("cls");
  215.                 tip(); 
  216.                 printf("\n");
  217.                 gotoxy(30,17);
  218.                 printf("输入1 ,返回主页");
  219.                 int a=0
  220.                 scanf("%d",&a);
  221.                 if(a==1) {system("cls");menu1(h,num);}
  222.             }break;
  223.         case 3:
  224.             {
  225.                  History(s,num,h);    
  226.             }break;
  227.         case 4:
  228.             {
  229.                 system("cls");
  230.                 printf("你确定要退出游戏吗?\n");
  231.                 printf("确定输入1,不确定输入2\n");
  232.                 int a=0;
  233.                 scanf("%d",&a);
  234.                 if(a==1)    exit(0);
  235.                 else {system("cls");menu1(h,num);}
  236.             }break;
  237.         case 5:
  238.             {
  239.                 char name[20];
  240.                     system("cls");
  241.                     printf("输入要删除在帐号:");
  242.                     scanf("%s",name);
  243.                     deletenum(h,name);
  244.                     printf("输入1 ,返回主页");
  245.                 int a=0
  246.                 scanf("%d",&a);
  247.                 if(a==1)
  248.                  {
  249.                    system("cls");
  250.                    if(h->next==NULL)
  251.                     {iregister(h);dengru(h,num);}
  252.                     else dengru(h,num);menu1(h,num);
  253.                  }    
  254.             }break;
  255.         case 6:
  256.             {
  257.                 system("cls");log_in(h);
  258.                 gotoxy(42,12);printf("请输入任意键继续!!!\n");
  259.                 _getch();
  260.                 system("cls");
  261.                 menu1(h,num);
  262.             }break;
  263.             default:
  264.             {
  265.                 gotoxy(37,25);
  266.                 printf("请看清楚条件\n");
  267.                 _getch();
  268.                 system("cls");
  269.                 menu1(h,num);
  270.             }break;
  271.      }
  272. void startup()      //游戏初始化 
  273. {
  274.     h=20; //高为20 
  275.     w=50; //宽为50
  276.     score=0; //得分初始化 
  277.     f=1;  //飞机的状态
  278.     p_x=w/2; //飞机x的位置 
  279.     p_y=h-4; //飞机y的位置
  280.     e_x=2+rand()%w-2;
  281.     e_y=0
  282.     b_x=p_x;
  283.     b_y=0;
  284.     HideCursor();   //隐藏光标 
  285. }
  286. /*显示游戏画面*/
  287. void show()
  288. {
  289.     int i,j;
  290.     for(i=0;i<h;i++)
  291.     {
  292.         for(j=0;j<w;j++)
  293.         {
  294.             if(f==0)     break;
  295.             else
  296.             {
  297.             if((i==0)||(j==0)||(i==h-1)||(j==w-1))   printf("+");
  298.             else if((i==p_y)&&(j==p_x))     printf("A");
  299.             else if((i==b_y)&&(j==b_x))       printf("!");
  300.             else if((i==e_y)&&(j==e_x))       printf("@");
  301.             else printf(" ");
  302.             }    
  303.         }
  304.     printf("\n");
  305.     }
  306.     if((p_x==e_x)&&(p_y==e_y))    f--;
  307. }               
  308. /*分数显示  */ 
  309. void seescore(int num)
  310. {
  311.     gotoxy(56,5);printf("w向上,s向下,a向左,d向右,按空格射击");
  312.     gotoxy(56,7);printf("A为您的战机"); 
  313.     gotoxy(56,8);printf("@为敌机");
  314.     gotoxy(2,21);printf("你的得分为%d",score);
  315.     s[num].SCORE=score;
  316.  /*用循环控制敌机的飞行速度*/ 
  317. void Fly()
  318. {
  319.      static int speed=0;
  320.      if(speed<10) speed++;
  321.      if(speed==10)   
  322.      {
  323.          if(e_y<h)   e_y++;
  324.          else { e_y=0;e_x=2+rand()%w-2;}
  325.         speed=0;
  326.      }
  327. }
  328. void b_Fly()
  329. {
  330.      if(b_y>0)     b_y--;//控制子弹的飞行效果 
  331.      if((b_y==e_y)&&(b_x==e_x))      {score++;e_y=0;e_x=2+rand()%w-2;b_y=0;}
  332. }
  333. /*控制飞机的移动*/ 
  334. void Planefly() 
  335. {
  336.     char input;
  337.     if(kbhit()) //kbhit函数是判断是否有输入 
  338.     {
  339.         input=getch(); //将输入的值传入input里面 
  340.         if((input=='w')&&(p_y>1)) //如果按下wsad则相应移动飞机的位置 
  341.         {
  342.             p_y--;
  343.         }
  344.         if((input=='s')&&(p_y<h-2))
  345.         {
  346.             p_y++;
  347.         }
  348.         if((input=='a')&&(p_x>1))
  349.         {
  350.             p_x--;
  351.         }
  352.         if((input=='d')&&(p_x<w-2))
  353.         {
  354.             p_x++;
  355.         }
  356.         if((input==' ')&&(b_y==0))
  357.         {
  358.             b_x=p_x;
  359.             b_y=p_y;
  360.         }
  361.     }
  362. }          
  363. void gameover(U *h)//游戏结束提示 
  364. {
  365.     system("cls");int a;
  366.     gotoxy(39,14);printf("输入1继续打飞机,输入2结束游戏");
  367.     scanf("%d",&a);
  368.     switch(a)
  369.     {
  370.         case 1:{system("cls");}break;
  371.         case 2:{system("cls");exit(0);}break;
  372.         default:{system("cls");printf("请看清楚要求!");_getch();gameover(h);}break;
  373.     }
  374. /*登入与注册*/ 
  375. void iregister(U *h)
  376. {
  377.     char name[20];
  378.     char mm[20];
  379.     gotoxy(51,6);    printf("注册") ;
  380.     gotoxy(39,11);     create(h);//注册
  381.     gotoxy(39,16);
  382.     printf("注册成功!!!(按任意键登入)\n");
  383.     HideCursor();
  384.     _getch();system("cls");
  385. void dengru(U *h,int num)
  386. {
  387.     char name[20];char mm[20];
  388. o:  gotoxy(39,16);
  389.     printf("请输入用户名:\n");
  390.     gotoxy(39,17);
  391.     scanf("%s",name);
  392.     strcpy(s[num].NAME,name);//将用户名存入结构体数组 
  393.     gotoxy(39,18);
  394.     printf("请输入密码:\n");
  395.     gotoxy(39,19);
  396.     scanf("%s",mm);
  397.     if(!search(h,name))  
  398.     {
  399.      system("cls");gotoxy(39,16);printf("没有该用户信息,请先注册!!!(按任意键继续)");
  400.      _getch();system("cls") ;insert(h);system("cls"); goto o; 
  401.     }
  402.     U *temp=search(h,name);//判断密码 
  403.     if(strcmp(temp->mm,mm)) 
  404.     {system("cls");gotoxy(39,17);printf("密码错误,请重新输入!(按任意键继续)");_getch();system("cls") ;dengru(h,num);} 
  405.     else{gotoxy(39,20);printf("登入成功!!!(按任意键开始游戏)");_getch();system("cls");} 
  406. }   
  407. void log_in(U *h) {
  408.     FILE*fp;
  409.     char name[20];char time[20];
  410.     if ((fp = fopen("d:\\informeation storage.txt", "a+")) == NULL) {
  411.         printf("文件不存在,创建成功");
  412.     }
  413. M:    gotoxy(36,9);
  414.     printf("为了为您的账号签到,请再次输入账号\n");
  415.     gotoxy(42,10);
  416.     scanf("%s", name);
  417.     if(!search(h,name))  {gotoxy(42,11);printf("输入错误,请重新输入");_getch();system("cls");goto M;    }
  418.     fputs(name, fp);
  419.     gotoxy(36,11);
  420.     printf("请输入登入的时间\n");
  421.     gotoxy(42,12);
  422.     scanf("%s", time);
  423.     fputs(time, fp);
  424.     fclose(fp);
  425. }
  426. int main ()
  427. {
  428.     system("mode con cols=100 lines=30");  //创建宽10030的程序界面大小
  429.     U *head=(U*)malloc(sizeof(U)); //建立一个带头的链表 
  430.     iregister(head); L:dengru(head,num);menu1(head,num);startup();//初始化
  431.     while(1)
  432.     {
  433.         gotoxy(0,0);show();seescore(num);
  434.         if(f==0)//判断飞机的状态 
  435.         {
  436.             gameover(head);s[num].SCORE=score;num++;if(num==10) num=1;
  437.             msort(s,num+1);
  438.             goto L;
  439.         }
  440.         b_Fly();Fly();Planefly(); 
  441.     }
  442.     return 0;
  443.  } 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/358943
推荐阅读
相关标签
  

闽ICP备14008679号