玩转c语言——c语言小游戏 迷宫小游戏(附源码)_c语言迷宫游戏代码

在游戏中我们需要输入W,S,A,D中的一个,来控制小球的移动

以W为例我们来看看小球上移时程序该怎么运行

我们想让小球向上移动,基本条件是小球上方没有‘#’

然后小球才可以向上移动;

小球上移后行数X+1,列数Y不变

即小球下一个的位置在a[x+1][y]

这就是我们点击W后小球上移的程序原理

注:为了游戏的体验感,我们输入WSAD是使用getch()

各位如果有兴趣也可以试一下使用getchar和getche是什么效果(吐血小游戏)

ch = _getch();
if (ch == 'a')
		{
			if (a[x][y - 1] != '#')
			{
				a[x][y] = ' ';
				y--;
				a[x][y] = 'o';
			}
		}

其他的 **S,A,D,**也是类似的

S原理

ch = _getch();
		if (ch == 's')
		{
			if (a[x + 1][y] != '#')
			{
				a[x][y] = ' ';
				x++;
				a[x][y] = 'o';
			}
		}

A原理

if (ch == 'a')
		{
			if (a[x][y - 1] != '#')
			{
				a[x][y] = ' ';
				y--;
				a[x][y] = 'o';
			}
        }

D原理

if (ch == 'd')
		{
			if (a[x][y + 1] != '#')
			{
				a[x][y] = ' ';
				y++;
				a[x][y] = 'o';
			}
		}

我们在利用WSAD移动时,不希望小球出现在原来的位置

我们需要在每一次输入WSAD时用system(“cls”)进行一次清零;


我们需要利用while循环是程序在未到达出口时持续运行

	while (x != 1 || y != 5)
	{
		ch = _getch();
		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';
			}
		}

这个小游戏的器官大致就是这样

以下就是整个小游戏的源码

#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
int main()
{
	char a[100][100] = { "######",
						"#o #  ",
						"# ## #",
						"#  # #",
						"##   #",
						"######" };
	int i, x = 1, y = 1;//p,q存储迷宫出口的位置
	for (i = 0; i < 6; i++)
		puts(a[i]);
	char ch;
	while (x != 1 || y != 5)
	{
		ch = _getch();
		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==5)
		printf("成功过关\n");
		for (i = 0; i < 6; i++)
			puts(a[i]);
	}
		return 0;
}

这就是最后的运行结果,走出迷宫后会出现成功过关四个字

我们也可以对走过的步数进行计数;

定义一个count;每移动一次;count++

#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;
	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] != '#')
文末有福利领取哦~
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

👉**一、Python所有方向的学习路线**

Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。![img](https://img-blog.csdnimg.cn/c67c0f87cf9343879a1278dfb067f802.png)

👉**二、Python必备开发工具**

![img](https://img-blog.csdnimg.cn/757ca3f717df4825b7d90a11cad93bc7.png)  
👉**三、Python视频合集**

观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。  
![img](https://img-blog.csdnimg.cn/31066dd7f1d245159f21623d9efafa68.png)

👉 **四、实战案例**

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。**(文末领读者福利)**  
![img](https://img-blog.csdnimg.cn/e78afb3dcb8e4da3bae5b6ffb9c07ec7.png)

👉**五、Python练习题**

检查学习结果。  
![img](https://img-blog.csdnimg.cn/280da06969e54cf180f4904270636b8e.png)

👉**六、面试资料**

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。  
![img](https://img-blog.csdnimg.cn/a9d7c35e6919437a988883d84dcc5e58.png)

![img](https://img-blog.csdnimg.cn/5db8141418d544d3a8e9da4805b1a3f9.png)

👉因篇幅有限,仅展示部分资料,这份完整版的Python全套学习资料已经上传




**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化学习资料的朋友,可以戳这里无偿获取](https://bbs.csdn.net/topics/618317507)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的C语言迷宫游戏代码示例,仅供参考: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_ROWS 10 #define MAX_COLS 10 char maze[MAX_ROWS][MAX_COLS] = { {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}, {'#', '.', '.', '.', '#', '.', '.', '.', '.', '#'}, {'#', '.', '#', '.', '#', '.', '#', '#', '.', '#'}, {'#', '.', '#', '.', '.', '.', '.', '.', '.', '#'}, {'#', '.', '.', '#', '#', '.', '#', '#', '.', '#'}, {'#', '#', '.', '.', '#', '.', '.', '.', '.', '#'}, {'#', '.', '#', '.', '#', '#', '#', '.', '#', '#'}, {'#', '.', '.', '.', '.', '.', '.', '.', '.', '#'}, {'#', '#', '#', '#', '#', '#', '#', '#', '.', '#'}, {'#', '.', '.', '.', '.', '.', '.', '#', '#', '#'} }; int start_row = 1; int start_col = 1; int end_row = 8; int end_col = 8; void print_maze() { int row, col; for (row = 0; row < MAX_ROWS; row++) { for (col = 0; col < MAX_COLS; col++) { printf("%c", maze[row][col]); } printf("\n"); } } int solve_maze(int row, int col) { if (row == end_row && col == end_col) { return 1; } if (maze[row][col] == '#' || maze[row][col] == '+') { return 0; } maze[row][col] = '+'; if (solve_maze(row - 1, col) || solve_maze(row + 1, col) || solve_maze(row, col - 1) || solve_maze(row, col + 1)) { return 1; } maze[row][col] = '.'; return 0; } int main() { printf("Welcome to the maze game!\n"); printf("Press enter to start..."); getchar(); system("clear"); print_maze(); printf("Press enter to start solving the maze..."); getchar(); system("clear"); if (solve_maze(start_row, start_col)) { printf("Congratulations! You have solved the maze!\n"); print_maze(); } else { printf("Sorry, you have failed to solve the maze.\n"); } return 0; } ``` 这个迷宫游戏使用二维数组来存储迷宫的地图,'#'表示墙壁,'.'表示通路,'+'表示已经探索过的位置。使用递归算法来搜索迷宫解决方案。在开始游戏前,需要指定起点和终点的位置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值