#include<stdio.h> #include<stdlib.h> #include<windows.h> #include<conio.h> int main() { char a[1000][1000] = { "##########", "#o # # ", "# ## ## ##", "# # ##", "## ## ##", "##########" }; int i, x = 1, y = 1; int count = 0; printf("\n\n\t请使用英文输入法!\n\t操作方式:上下左右=WSAD\n\t o 代表你操作的人物, # 代表墙体。\n\t请开始你的游戏吧!\n\n\n"); for (i = 0; i < 11; i++) puts(a[i]); char ch; while (x != 1 || y != 9) { ch = _getch(); count++; if (ch == 's') { if (a[x + 1][y] != '#') { a[x][y] = ' '; x++; a[x][y] = 'o'; } } if (ch == 'w') { if (a[x - 1][y] != '#') { a[x][y] = ' '; x--; a[x][y] = 'o'; } } if (ch == 'a') { if (a[x][y - 1] != '#') { a[x][y] = ' '; y--; a[x][y] = 'o'; } } if (ch == 'd') { if (a[x][y + 1] != '#') { a[x][y] = ' '; y++; a[x][y] = 'o'; } } system("cls"); if (x == 1 && y == 9) printf("成功过关\n"); for (i = 0; i < 6; i++) puts(a[i]); } printf("你一共走了%d步\n", count); Sleep(5000); return 0; }
时间: 2024-04-15 19:24:52 浏览: 21
这是一个使用C语言编写的迷宫游戏代码。代码中使用了二维数组来表示迷宫地图,其中'#'代表墙体,'o'代表你操作的人物。你可以通过上下左右键(WSAD)来操作人物移动。游戏的目标是从起点(x=1,y=1)走到终点(x=1,y=9)。当人物到达终点时,会打印出"成功过关"并统计你一共走了多少步。
请注意,在运行此代码之前,请确保你的编译环境中已经包含了所需的头文件(stdio.h、stdlib.h、windows.h、conio.h)。
如果你有任何关于这段代码的问题或者需要进一步的帮助,请随时告诉我。
相关问题
C语言中#include <stdlib.h> #include <conio.h> #include <stdio.h> double fun(int m) {double t=1.0; int i; for(i=2;i<=m;i++) t+=1.0/i; return i; } void main() ( int m; system("CLS"); printf("\nPlease enter linteger number:"); scanf("%d",&m); printf("\nThe resultis %1f\n",fun(m)));
这段代码中有一个语法错误,函数fun的返回值类型应该是double,但是代码中返回的是整型变量i,应该改为返回变量t。另外,代码中的“;”符号也需要修改,应该改为英文的分号。
正确的代码如下所示:
```c
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
double fun(int m) {
double t = 1.0;
int i;
for(i = 2; i <= m; i++) {
t += 1.0 / i;
}
return t;
}
void main() {
int m;
system("CLS");
printf("\nPlease enter integer number:");
scanf("%d", &m);
printf("\nThe result is %1f\n", fun(m));
}
```
这段代码的功能是计算从2到m的分数和,其中m是用户输入的整数。函数fun的实现中使用了循环语句for来计算分数和,最后返回分数和的值。主函数main中调用函数fun,并输出计算结果。
C语言程序填空:从键盘输入10个字符,将其全部输出到一个磁盘文件“data.dat"中保存起来#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { FILE *fp; int num; int i=0; if((fp=fopen("data.dat","w"))==NULL) { printf("打不开文件\n"); exit(0); } while(i<=9) { fprintf(fp,"%d",num); i++; } }
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main() {
FILE *fp;
char ch[10];
int i=0;
if((fp=fopen("data.dat","w"))==NULL) {
printf("打不开文件\n");
exit(0);
}
printf("请输入10个字符:\n");
while(i<=9) {
ch[i] = getch();
fprintf(fp,"%c",ch[i]);
i++;
}
fclose(fp);
printf("已将输入的字符保存到文件data.dat中!");
}
注意:程序中使用了 getch() 函数,需要包含 conio.h 头文件。另外,文件操作完成后需要关闭文件,使用 fclose() 函数。
相关推荐
![application/x-gzip](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)