彩色世界之寻找老婆【迷宫游戏二】
2014-12-09 20:06
239 查看
//比上一个就多了色彩,那个颜色索引模式是我百度得到的。写给像我一样的菜鸟们。
//那个设置颜色的函数和它的设置,可以很好的移植到其它诸如此类的程序中。给你的程序加点颜色吧!
//迷宫游戏
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define Height 31 //迷宫的高度,必须为奇数
#define Width 31 //迷宫的宽度,必须为奇数
#define Wall 1
#define Road 0
#define Start 2
#define End 3
#define Esc 5
#define Up 1
#define Down 2
#define Left 3
#define Right 4
//颜色索引函数
void set_console_color(unsigned short color_index)
{点击打开链接
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_index);
}
int map[Height + 2][Width + 2];
void gotoxy(int x, int y) //移动坐标
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void hidden()//隐藏光标
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut, &cci);
cci.bVisible = 0;//赋1为显示,赋0为隐藏
SetConsoleCursorInfo(hOut, &cci);
}
void create(int x, int y) //随机生成迷宫
{
int c[4][2] = { 0, 1, 1, 0, 0, -1, -1, 0 }; //四个方向
int i, j, t;
//将方向打乱
for (i = 0; i<4; i++)
{
j = rand() % 4;
t = c[i][0];
c[i][0] = c[j][0];
c[j][0] = t;
t = c[i][1];
c[i][1] = c[j][1];
c[j][1] = t;
}
map[x][y] = Road;
for (i = 0; i<4; i++)
if (map[x + 2 * c[i][0]][y + 2 * c[i][1]] == Wall)
{
map[x + c[i][0]][y + c[i][1]] = Road;
create(x + 2 * c[i][0], y + 2 * c[i][1]);
}
}
int get_key() //接收按键
{
char c;
while (c = getch())
{
if (c == 27) return Esc; //Esc
if (c != -32)continue;
c = getch();
if (c == 72) return Up; //上
if (c == 80) return Down; //下
if (c == 75) return Left; //左
if (c == 77) return Right; //右
}
return 0;
}
void paint(int x, int y) //画迷宫
{
gotoxy(2 * y - 2, x - 1);
switch (map[x][y])
{
case Start:
set_console_color(3);
printf("¤"); break; //画入口
case End:
set_console_color(13);
printf("♀"); break; //画出口
case Wall:
set_console_color(2);
printf("■"); break; //画墙
case Road:
set_console_color(9);
printf("□"); break; //画路
}
}
void game()
{
set_console_color(11);
int x = 2, y = 1; //玩家当前位置,刚开始在入口处
int c; //用来接收按键
while (1)
{
gotoxy(2 * y - 2, x - 1);
set_console_color(11);
printf("♂"); //画出玩家当前位置
if (map[x][y] == End) //判断是否到达出口
{
gotoxy(30, 24);
printf("恭喜您已成功找到姻缘!按任意键结束");
getch();
break;
}
c = get_key();
if (c == Esc)
{
gotoxy(0, 24);
break;
}
switch (c)
{
case Up: //向上走
if (map[x - 1][y] != Wall)
{
paint(x, y);
x--;
}
break;
case Down:
if (map[x + 1][y] != Wall)
{
paint(x, y);
x++;
}
break;
case Left: //向左走
if (map[x][y - 1] != Wall)
{
paint(x, y);
y--;
}
break;
case Right: //向右走
if (map[x][y + 1] != Wall)
{
paint(x, y);
y++;
}
break;
}
}
}
int main()
{
int i, j;
srand((unsigned)time(NULL)); //初始化随即种子
hidden(); //隐藏光标
for (i = 0; i <= Height + 1; i++)
for (j = 0; j <= Width + 1; j++)
if (i == 0 || i == Height + 1 || j == 0 || j == Width + 1) //初始化迷宫
map[i][j] = Road;
else map[i][j] = Wall;
create(2 * (rand() % (Height / 2) + 1), 2 * (rand() % (Width / 2) + 1)); //从随机一个点开始生成迷宫,该点行列都为偶数
for (i = 0; i <= Height + 1; i++) //边界处理
{
map[i][0] = Wall;
map[i][Width + 1] = Wall;
}
for (j = 0; j <= Width + 1; j++) //边界处理
{
map[0][j] = Wall;
map[Height + 1][j] = Wall;
}
map[2][1] = Start; //给定入口
map[Height - 1][Width] = End; //给定出口
for (i = 1; i <= Height; i++)
for (j = 1; j <= Width; j++) //画出迷宫
paint(i, j);
game(); //开始游戏
getch();
return 0;
}
//比上一个就多了色彩,那个颜色索引模式是我百度得到的。写给像我一样的菜鸟们。
//那个设置颜色的函数和它的设置,可以很好的移植到其它诸如此类的程序中。给你的程序加点颜色吧!
//迷宫游戏
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define Height 31 //迷宫的高度,必须为奇数
#define Width 31 //迷宫的宽度,必须为奇数
#define Wall 1
#define Road 0
#define Start 2
#define End 3
#define Esc 5
#define Up 1
#define Down 2
#define Left 3
#define Right 4
//颜色索引函数
void set_console_color(unsigned short color_index)
{点击打开链接
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_index);
}
int map[Height + 2][Width + 2];
void gotoxy(int x, int y) //移动坐标
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void hidden()//隐藏光标
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut, &cci);
cci.bVisible = 0;//赋1为显示,赋0为隐藏
SetConsoleCursorInfo(hOut, &cci);
}
void create(int x, int y) //随机生成迷宫
{
int c[4][2] = { 0, 1, 1, 0, 0, -1, -1, 0 }; //四个方向
int i, j, t;
//将方向打乱
for (i = 0; i<4; i++)
{
j = rand() % 4;
t = c[i][0];
c[i][0] = c[j][0];
c[j][0] = t;
t = c[i][1];
c[i][1] = c[j][1];
c[j][1] = t;
}
map[x][y] = Road;
for (i = 0; i<4; i++)
if (map[x + 2 * c[i][0]][y + 2 * c[i][1]] == Wall)
{
map[x + c[i][0]][y + c[i][1]] = Road;
create(x + 2 * c[i][0], y + 2 * c[i][1]);
}
}
int get_key() //接收按键
{
char c;
while (c = getch())
{
if (c == 27) return Esc; //Esc
if (c != -32)continue;
c = getch();
if (c == 72) return Up; //上
if (c == 80) return Down; //下
if (c == 75) return Left; //左
if (c == 77) return Right; //右
}
return 0;
}
void paint(int x, int y) //画迷宫
{
gotoxy(2 * y - 2, x - 1);
switch (map[x][y])
{
case Start:
set_console_color(3);
printf("¤"); break; //画入口
case End:
set_console_color(13);
printf("♀"); break; //画出口
case Wall:
set_console_color(2);
printf("■"); break; //画墙
case Road:
set_console_color(9);
printf("□"); break; //画路
}
}
void game()
{
set_console_color(11);
int x = 2, y = 1; //玩家当前位置,刚开始在入口处
int c; //用来接收按键
while (1)
{
gotoxy(2 * y - 2, x - 1);
set_console_color(11);
printf("♂"); //画出玩家当前位置
if (map[x][y] == End) //判断是否到达出口
{
gotoxy(30, 24);
printf("恭喜您已成功找到姻缘!按任意键结束");
getch();
break;
}
c = get_key();
if (c == Esc)
{
gotoxy(0, 24);
break;
}
switch (c)
{
case Up: //向上走
if (map[x - 1][y] != Wall)
{
paint(x, y);
x--;
}
break;
case Down:
if (map[x + 1][y] != Wall)
{
paint(x, y);
x++;
}
break;
case Left: //向左走
if (map[x][y - 1] != Wall)
{
paint(x, y);
y--;
}
break;
case Right: //向右走
if (map[x][y + 1] != Wall)
{
paint(x, y);
y++;
}
break;
}
}
}
int main()
{
int i, j;
srand((unsigned)time(NULL)); //初始化随即种子
hidden(); //隐藏光标
for (i = 0; i <= Height + 1; i++)
for (j = 0; j <= Width + 1; j++)
if (i == 0 || i == Height + 1 || j == 0 || j == Width + 1) //初始化迷宫
map[i][j] = Road;
else map[i][j] = Wall;
create(2 * (rand() % (Height / 2) + 1), 2 * (rand() % (Width / 2) + 1)); //从随机一个点开始生成迷宫,该点行列都为偶数
for (i = 0; i <= Height + 1; i++) //边界处理
{
map[i][0] = Wall;
map[i][Width + 1] = Wall;
}
for (j = 0; j <= Width + 1; j++) //边界处理
{
map[0][j] = Wall;
map[Height + 1][j] = Wall;
}
map[2][1] = Start; //给定入口
map[Height - 1][Width] = End; //给定出口
for (i = 1; i <= Height; i++)
for (j = 1; j <= Width; j++) //画出迷宫
paint(i, j);
game(); //开始游戏
getch();
return 0;
}
相关文章推荐
- 黑白世界之寻找老婆【迷宫一】
- XNA 4.0 3D 游戏开发之世界漫游
- 51 nod: 1459 迷宫游戏
- 51nod 1459 迷宫游戏
- 创建游戏中的人类世界:数据驱动的开放世界AI框架
- 【C语言】迷宫游戏
- 新网站的BETA版:XNA游戏世界beta上线
- python实现的生成随机迷宫算法核心代码分享(含游戏完整代码)
- 迷宫游戏代码OpenGL
- 真实世界的Windows Azure:使用Windows Azure社交游戏开发商享有更低的成本和改进的扩展性
- 世界顶级游戏发行商TOP20
- 【Visual C++】游戏开发笔记四十七 浅墨DirectX教程十五 翱翔于三维世界:摄像机的实现
- 【Visual C++】游戏开发笔记四十一 浅墨DirectX教程之九 为三维世界添彩:纹理映射技术(一)
- 用栈实现迷宫游戏寻路
- 数据结构应用:利用栈破解迷宫游戏
- 走出 JNDI 迷宫学习从容地寻找 bean 的Home接口
- cocos2d-x快乐的做让人快乐的游戏3:cocos-2d 3.x中的物理世界
- Shieldstar:一款改变世界的STG游戏!震撼发布!
- HTML5游戏开发进阶 2 :创建基本的游戏世界
- 迷宫游戏