建议不要直接复制!!!!
最好自己打出来,文本书写可能有中英文格式的错误!!!
C语言课程设计里面常有的游戏设计,我这里有一个已经做过了修改和注释的代码,自己修改的所以保证程序的可执行性。
迷宫可以随机生成路径,并有唯一出口!
代码中有几个地方的引用音效文件的地址可能需要自己修改一下,颜色变换等我就不多赘述了,很简单的程序,下面是代码:
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#include <mmsystem.h>
#include <stdlib.h>
#include<graphics.h>
#pragma comment(lib, "winmm.lib")
/*
*迷宫的高度
*/
#define Height 29
/*
*迷宫的宽度
*/
#define Width 57
#define Esc 5
#define Up 1
#define Down 2
#define Left 3
#define Right 4
#define Wall 1
#define Road 0
#define Start 2
#define End 3
#define K 6
int map[Height+2][Width+2];
/*
*随机生成迷宫
*/
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]);
}
}
/*
*移动坐标
*/
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 paint(int x,int y)
{
gotoxy(2*y-2,x-1);
switch(map[x][y])
{
case Start:
printf("进");break;
case End:
printf("出");break;
case Wall:
printf("�ˆ");break;
case Road:
printf(" ");break;
}
}
/*
*接收按键
*/
int get_key()
{
char c;
while(c=getch())
{
if(c==27) return Esc; /*Esc*/
if(c==32)return K;
// 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 game()
{
SHOWTIME: int z;
int x=2,y=1; /*玩家当前位置,刚开始在入口处*/
int c; /*用来接收按键*/
while(1)
{
gotoxy(2*y-2,x-1);
printf("◎"); /*画出玩家当前位置*/
if(map[x][y]==End) /*判断是否到达出口*/
{
PlaySound (TEXT("Z3BROTHER - Tokyo Hot.wav"), NULL, SND_ASYNC | SND_NODEFAULT);
gotoxy(30,24);
printf("\n\n\t\t\t\t\t!!!!!CONGRETULATIONS!!!!!!");
getch();
break;
}
c=get_key();
if(c==Esc)
{
gotoxy(0,24);
break;
}
if(c==K)
{
goto SHOWTIME;
}
/*按空格回到初始位置!并在你走过的地方留下标记*/
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()
{
system("color 1b");
IMAGE img;
loadimage(&img,"timg1.jpg");
initgraph(img.getwidth(),img.getheight());
PlaySound (TEXT("Various Artists - 苏维埃进行曲.wav"), NULL, SND_ASYNC | SND_NODEFAULT);
putimage(0,0,&img);
getch();
closegraph();
printf("\n\n\n\t\t★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★\n");
printf("\t ★ \t\t游戏规则 ①↑↓←→键移动图标\t\t\t ★\n\t ★\t\t\t\t\t②esc退出游戏\t\t\t\t\t★\n\t ★\t\t\t\t\t③空格键回到起点\t\t\t\t★\n");
printf("\t\t★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★\n");
printf("\n\n\t\t\t\t\t\t !按任意键继续!\n\t\t\t\t\t\t !祝游戏愉快!");
printf("\n\n\n\n\t\t\t\t\t\t★★!老师新年快乐!★★\n\n\n\n\n\n\n\n\n");
getch();
FUCK:int i,j;
system("color 0f");
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);
PlaySound (TEXT("Rob Simonsen - Red.wav"), NULL, SND_ASYNC | SND_NODEFAULT);
game(); /*开始游戏*/
system("cls");
PlaySound (TEXT("邓超 - 无敌.wav"), NULL, SND_ASYNC | SND_NODEFAULT);
printf("\n\n\n\t\t\t\t\t\t (^-^)V\n\n\n\t\t\t\t\t\t亲,再战一回合?\n\n\n\t\t\t\t ★★★★★按空格键重来,Esc键退出★★★★★");
int n;
n=get_key();
if (n==K) goto FUCK;
if (n==Esc)
system("cls");
return 0;
}
下面是效果图