飞道的博客

C语言自主设计迷宫类游戏实例

519人阅读  评论(0)

附:

C语言实现计算器完整代码:https://blog.csdn.net/qq_41915690/article/details/80466865

(破败圣迹之逃出生天)使用设计说明

Design By 海上的雨

 

 

 

 

                                                                                                                                                                                                      

1 软件介绍(使用说明)

用w、a、s、d或者W、A、S、D分别控制角色的移动(上、左、下、右),使目标向○处逃生!触碰◎则死亡!逃生或死亡则游戏结束。

游戏开始界面↓

死亡界面↓

逃生界面↓

2 函数设计(设计思路)

图形构成

破败圣迹之逃出生天由主角、墙壁、道路、逃生标志、死亡标志、装饰符以及文字构成。自建一个txt文档储存数据,用数字1代替墙壁"▓",2代表角色"♀" ,3代表道路"  ",4代表文字"破败圣迹之逃出生天"、5代表装饰符"卐"、6代表装饰符"卍"、7代表逃生标志"○"、*代表死亡标志"◎"来构成图形化界面。

使用voidshuru(char shuju[29][26],int *a,int *b);以及      void shuchu(char shuju[29][26],int *a,int*b);这两个函数对txt文档进行提取和显示。其中voidshuru(char shuju[29][26],int *a,int *b)函数作用是从文件中读取信息,并将数据存放于二维数组shuju[29][26]中,*a与*b记录角色的位置。void shuchu(char shuju[29][26],int *a,int *b)函数的作用是从二维数组shuju[29][26]中读信息,并根据shuju[29][26]中的各个值输出相应的符号。

以下为实现代码:

void shuru(char shuju[29][26],int *a,int*b)

{

         inti=0,j=0;

         FILE*fp;

   char ch;

         if((fp=fopen("破败圣迹之逃出生天.txt","r"))==NULL)

         {

                   printf("文件路径不正确!");

                   exit(1);

         }

         ch=fgetc(fp);

         while(ch!=EOF)

         {  

                   if(ch=='1')

       {

                           

                            shuju[i][j]=ch;

                            j++;

                            if(j==26)

                            {

                                     i++;

                                     j=0;

                            }       

                   }

                   elseif(ch=='2')

                   {

                           

                            shuju[i][j]=ch;

                            (*a)=i;

                            (*b)=j;

           j++;

                            if(j==26)

                            {

                                     i++;

                                     j=0;

                            }

                   }

                   elseif(ch=='3')

                   {

                            shuju[i][j]=ch;

           j++;

                            if(j==26)

                            {

                                     i++;

                                     j=0;

                            }

                   }

                   else

                   {       

           shuju[i][j]=ch;

           j++;

                            if(j==26)

                            {

                                     i++;

                                     j=0;

                            }

                   }

       ch=fgetc(fp);

         }

         fclose(fp);

}

void shuchu(char shuju[29][26],int *a,int*b)

{

         inti,j;

         system("cls");

         for(i=0;i<29;i++)

                   for(j=0;j<26;j++)

                   {

                            if(shuju[i][j]=='1')

                                     printf("▓");

                            elseif(shuju[i][j]=='2')

                            {  

                                     printf("♀");

                                     (*a)=i;

                (*b)=j;

                            }

                            elseif(shuju[i][j]=='3')

                            {

                                     printf("  ");

                            }

                            elseif(shuju[i][j]=='4')

                            {

                                     printf("破败圣迹之逃出生天");

                            }

                            elseif(shuju[i][j]=='5')

                            {

                                     printf("卐");

                            }

                            elseif(shuju[i][j]=='6')

                            {

                                     printf("卍");

                            }

                            elseif(shuju[i][j]=='7')

                            {

                                     printf("○");

                            }

                            elseif(shuju[i][j]=='*')

                            {

                                     printf("◎");

                            }

                            elseif(shuju[i][j]=='8')

                            {

                                     printf("W:向上移动\tS:向下移动");

                            }

                            elseif(shuju[i][j]=='9')

                            {

                                     printf("A:向左移动\tD:向右移动");

                            }

                            /*elseif(shuju[i][j]=='*')

                            {

                                     printf("A:向左移动");

                            }*/

                            /*elseif(shuju[i][j]=='/')

                            {

                                     printf("D:向右移动");

                            }*/

                            elseif(shuju[i][j]=='+')

                            {

                                     printf("向○处逃生!触碰◎则死亡!");

                            }

                            else

                            {

                                     putchar(shuju[i][j]);

                            }

                   }

                   printf("\n");

}

主角移动、死亡、逃生的实现

定义二维数组用来记录各个位置。可以理解为用坐标表示位置,当键入w/a/s/d通过getch()对玩家输入的控制键进行不显示的读取,然后对shuju [i][j]进行相应的改变,之后运行shuchu(shuju,&a,&b)进行输出绘图,从而完成了角色"♀"的移动。为了保证角色不向墙壁移动,在调换shuju [i][j]的内容时调用if语句进行相关设定。同理,当主角走入死亡标记"◎"或逃生标记"○"时通过调制if语句对游戏进行死亡或逃生的展现。

以下为对应代码:

ch=getch();

    while(1)

       {

              switch(ch)

              {

              case 'w':

              case 'W':

                     {

                            if(shuju[a-1][b]=='3'||shuju[a-1][b]=='*')

                            { 

                                   c=shuju[a-1][b];

                                   shuju[a-1][b]=shuju[a][b];

                                   shuju[a][b]=c;

                                   shuchu(shuju,&a,&b);

                            }

                            break;

                     }

              case 'a':

              case 'A':

                     {

                            if(shuju[a][b-1]=='3'||shuju[a][b-1]=='*')

                            {

                                   c=shuju[a][b-1];

                                   shuju[a][b-1]=shuju[a][b];

                                   shuju[a][b]=c;

                                   shuchu(shuju,&a,&b);

                            }

                            break;

                     }

              case 's':

              case 'S':

                     {

                            if(shuju[a+1][b]=='3'||shuju[a+1][b]=='*')

                            {

                                   c=shuju[a+1][b];

                                   shuju[a+1][b]=shuju[a][b];

                                   shuju[a][b]=c;

                                   shuchu(shuju,&a,&b);

                            }

                            break;

                     }

              case 'd':

              case 'D':

                     {

                            if(shuju[a][b+1]=='3'||shuju[a][b+1]=='*')

                            {

                                   c=shuju[a][b+1];

                                   shuju[a][b+1]=shuju[a][b];

                                   shuju[a][b]=c;

                                   shuchu(shuju,&a,&b);

                            }

                            break;

                     }

              }

              if(shuju[22][23]=='2')

              {

                     printf("你已逃出生天!\n");

                     break;

              }

       if(shuju[3][13]=='2'||shuju[3][19]=='2'||shuju[9][5]=='2'||shuju[7][20]=='2'||shuju[12][19]=='2'||shuju[17][13]=='2'||shuju[23][19]=='2')

              {

                     printf("你已死亡!\n");

                     break;

              }

              ch=getch();

       }

}       

数据的读取

由”图形构成”部分可知,我们已经将有代表意义的1.2.3···等数据排列在一个txt文档里。然后通过(fp=fopen("破败圣迹之逃出生天.txt","r"))==NULL确认文件是否存在,并读取数据存入fp中。然后对fp进行操作,实现数据的读取。

以下为实现代码:

void shuru(char shuju[29][26],int *a,int*b)

{

         inti=0,j=0;

         FILE*fp;

   char ch;

         if((fp=fopen("破败圣迹之逃出生天.txt","r"))==NULL)

         {

                   printf("文件路径不正确!");

                   exit(1);

         }

         ch=fgetc(fp);

         while(ch!=EOF)

         {  

                   if(ch=='1')

       {

                           

                            shuju[i][j]=ch;

                            j++;

                            if(j==26)

                            {

                                     i++;

                                     j=0;

                            }       

                   }

                   elseif(ch=='2')

                   {

                           

                            shuju[i][j]=ch;

                            (*a)=i;

                            (*b)=j;

           j++;

                            if(j==26)

                            {

                                     i++;

                                     j=0;

                            }

                   }

                   elseif(ch=='3')

                   {

                            shuju[i][j]=ch;

           j++;

                            if(j==26)

                            {

                                     i++;

                                     j=0;

                            }

                   }

                   else

                   {       

           shuju[i][j]=ch;

           j++;

                            if(j==26)

                            {

                                     i++;

                                     j=0;

                            }

                   }

       ch=fgetc(fp);

         }

         fclose(fp);

游戏结束

通过while(1)语句当满足设定的if语句并break出时则游戏接触。

以下为实现代码:

while(1)

{

if(shuju[22][23]=='2')

                   {

                            printf("你已逃出生天!\n");

                            break;

                   }

         if(shuju[3][13]=='2'||shuju[3][19]=='2'||shuju[9][5]=='2'||shuju[7][20]=='2'||shuju[12][19]=='2'||shuju[17][13]=='2'||shuju[23][19]=='2')

                   {

                            printf("你已死亡!\n");

                            break;

                   }

                   ch=getch();

         }

}

3 数据结构

Ø  void shuru(char shuju[29][26],int *a,int *b);函数用与对储存数字以及字符的txt文档进行读取、转化、存储。

Ø  void shuchu(char shuju[29][26],int *a,int *b);函数用于通过设定不用的数字及字符代表相应的意义,对shuju[i][j]进行绘制。

Ø  getch();函数用于对输入的数据不进行显示而是直接保存,使游戏界面清洁。

Ø  while(1)

         {

         switch(ch)

                   {

If()

{

}

If()

{

}

Ch=getch();

}

}  函数用于设定通过键入的案件对shuju[a][b]内的数据进行相应的改变并进行游戏胜利与失败的设定。

Ø           FILE *fp;

    char ch;

         if((fp=fopen("破败圣迹之逃出生天.txt","r"))==NULL)

{

}  函数对存放数据的txt文档进行读取并保存于fp。

Ø  while(ch!=EOF)

       { 

If()

{

}

}   函数表示当ch不是标准输入的结尾时运行if语句。

Ø           system("cls");函数用于清屏,使用户在操作后能够使用一个新的界面。

4 最后

设计感悟

²  C语言程序图形化界面的行程可以先用数字或字符替代,然后进行读取操作。方便快捷,可修改性强。

²  把文件中的数据读到数组中,可以减少文本的读取次数。

C语言感悟

²  设计程序时多运行测试,多以用户的角度进行思考,对程序源代码进行不断地完善。

²  程序工程量大时在源代码中做好注释,可以在浏览时能够更直观、快速的浏览代码。

²  程序工程量较大时需要考虑先编写整体结构,然后对源代码进行改善。

²  编程中有许多类似的代码行,善于运用复制粘贴可以加快编程速度。

²  遇到无法实现的地方多运用网络资源或向先导求知,能够快速丰富自己的C语言知识库。

 

 

 

 

 

 


  
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<conio.h>
  4. void main()
  5. {
  6. void shuru(char shuju[29][26],int *a,int *b);
  7. void shuchu(char shuju[29][26],int *a,int *b);
  8. int a,b;
  9. char ch,c;
  10. char shuju[ 29][ 26];
  11. shuru(shuju,&a,&b);
  12. shuchu(shuju,&a,&b);
  13. ch=getch();
  14. while( 1)
  15. {
  16. switch(ch)
  17. {
  18. case 'w':
  19. case 'W':
  20. {
  21. if(shuju[a -1][b]== '3'||shuju[a -1][b]== '*')
  22. {
  23. c=shuju[a -1][b];
  24. shuju[a -1][b]=shuju[a][b];
  25. shuju[a][b]=c;
  26. shuchu(shuju,&a,&b);
  27. }
  28. break;
  29. }
  30. case 'a':
  31. case 'A':
  32. {
  33. if(shuju[a][b -1]== '3'||shuju[a][b -1]== '*')
  34. {
  35. c=shuju[a][b -1];
  36. shuju[a][b -1]=shuju[a][b];
  37. shuju[a][b]=c;
  38. shuchu(shuju,&a,&b);
  39. }
  40. break;
  41. }
  42. case 's':
  43. case 'S':
  44. {
  45. if(shuju[a+ 1][b]== '3'||shuju[a+ 1][b]== '*')
  46. {
  47. c=shuju[a+ 1][b];
  48. shuju[a+ 1][b]=shuju[a][b];
  49. shuju[a][b]=c;
  50. shuchu(shuju,&a,&b);
  51. }
  52. break;
  53. }
  54. case 'd':
  55. case 'D':
  56. {
  57. if(shuju[a][b+ 1]== '3'||shuju[a][b+ 1]== '*')
  58. {
  59. c=shuju[a][b+ 1];
  60. shuju[a][b+ 1]=shuju[a][b];
  61. shuju[a][b]=c;
  62. shuchu(shuju,&a,&b);
  63. }
  64. break;
  65. }
  66. }
  67. if(shuju[ 22][ 23]== '2')
  68. {
  69. printf( "你已逃出生天!\n");
  70. break;
  71. }
  72. if(shuju[ 3][ 13]== '2'||shuju[ 3][ 19]== '2'||shuju[ 9][ 5]== '2'||shuju[ 7][ 20]== '2'||shuju[ 12][ 19]== '2'||shuju[ 17][ 13]== '2'||shuju[ 23][ 19]== '2')
  73. {
  74. printf( "你已死亡!\n");
  75. break;
  76. }
  77. ch=getch();
  78. }
  79. }
  80. void shuru(char shuju[29][26],int *a,int *b)
  81. {
  82. int i= 0,j= 0;
  83. FILE *fp;
  84. char ch;
  85. if((fp=fopen( "破败圣迹之逃出生天.txt", "r"))== NULL)
  86. {
  87. printf( "文件路径不正确!");
  88. exit( 1);
  89. }
  90. ch=fgetc(fp);
  91. while(ch!=EOF)
  92. {
  93. if(ch== '1')
  94. {
  95. shuju[i][j]=ch;
  96. j++;
  97. if(j== 26)
  98. {
  99. i++;
  100. j= 0;
  101. }
  102. }
  103. else if(ch== '2')
  104. {
  105. shuju[i][j]=ch;
  106. (*a)=i;
  107. (*b)=j;
  108. j++;
  109. if(j== 26)
  110. {
  111. i++;
  112. j= 0;
  113. }
  114. }
  115. else if(ch== '3')
  116. {
  117. shuju[i][j]=ch;
  118. j++;
  119. if(j== 26)
  120. {
  121. i++;
  122. j= 0;
  123. }
  124. }
  125. else
  126. {
  127. shuju[i][j]=ch;
  128. j++;
  129. if(j== 26)
  130. {
  131. i++;
  132. j= 0;
  133. }
  134. }
  135. ch=fgetc(fp);
  136. }
  137. fclose(fp);
  138. }
  139. void shuchu(char shuju[29][26],int *a,int *b)
  140. {
  141. int i,j;
  142. system( "cls");
  143. for(i= 0;i< 29;i++)
  144. for(j= 0;j< 26;j++)
  145. {
  146. if(shuju[i][j]== '1')
  147. printf( "▓");
  148. else if(shuju[i][j]== '2')
  149. {
  150. printf( "♀");
  151. (*a)=i;
  152. (*b)=j;
  153. }
  154. else if(shuju[i][j]== '3')
  155. {
  156. printf( " ");
  157. }
  158. else if(shuju[i][j]== '4')
  159. {
  160. printf( "破败圣迹之逃出生天");
  161. }
  162. else if(shuju[i][j]== '5')
  163. {
  164. printf( "卐");
  165. }
  166. else if(shuju[i][j]== '6')
  167. {
  168. printf( "卍");
  169. }
  170. else if(shuju[i][j]== '7')
  171. {
  172. printf( "○");
  173. }
  174. else if(shuju[i][j]== '*')
  175. {
  176. printf( "◎");
  177. }
  178. else if(shuju[i][j]== '8')
  179. {
  180. printf( "W:向上移动\tS:向下移动");
  181. }
  182. else if(shuju[i][j]== '9')
  183. {
  184. printf( "A:向左移动\tD:向右移动");
  185. }
  186. /*else if(shuju[i][j]=='*')
  187. {
  188. printf("A:向左移动");
  189. }*/
  190. /*else if(shuju[i][j]=='/')
  191. {
  192. printf("D:向右移动");
  193. }*/
  194. else if(shuju[i][j]== '+')
  195. {
  196. printf( "向○处逃生!触碰◎则死亡!");
  197. }
  198. else
  199. {
  200. putchar(shuju[i][j]);
  201. }
  202. }
  203. printf( "\n");
  204. }

 

 


转载:https://blog.csdn.net/qq_41915690/article/details/80463892
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场