⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maze--xw.cpp

📁 这是一个迷宫寻宝的课程设计。用BC实现
💻 CPP
字号:
#include<dos.h>
#include<time.h>
#include<bios.h>
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
#include<graphics.h>


void Seek();
void putmaze();
void showtime();
void endscreen();
void majorscreen();
void startscreen( void );
void DrawMan( int x, int y );
void DrawSpace( int x, int y );
void DrawBlackSpace( int x,int y );
void DrawBlack( int x, int y );
void DrawWall( int x, int y );
void DrawXLine( int x, int y );
void DrawYLine( int x, int y );
void DrawUpLeftcorner( int x, int y );
void DrawUpRightcorner( int x, int y );
void DrawDownLeftcorner( int x, int y );
void DrawDownRightcorner( int x, int y );


char maze[21][60]={ 
		    "744444444444444444444444444444444444444444444444444444444448",
		    "5##########################################################5",
		    "5##3#33333333#333##3#3#3333##3333####3#3#333#33#3#3#3##333#5",
		    "5#333#33###33#33#3#3#3332333#3#3333#3#333#33#333333333#3###5",
		    "5#33###3#3333#3####3###333###3#3###3###3###3333###3###33#3#5",
		    "5#33#33333#333#3#3#3#3#3#333#3#3333333#3#333##3#3#33333##3#5",
		    "5#33###333#33##3#####3###3#3###3#3###3###3#3#3333###33##33#5",
		    "5#3333333#33#3#3#333#33333#3#3#333333333###33#333333333333#5",
		    "5######33##33##3#3#3#3#3#####3#3#3#3###3#3333###3#33#3#33##5",
		    "5#33333333#3#3#3#3#3#3#3#3##333333333#3#33#3###3333####333#5",
		    "5#33#3#####3#3#3#3#####3#3#3#3#3####33#####3####3333#3#3###5",
		    "5#33#3#333#3#3#33333#33333#3#33#3#333##333#3#33#3#33##33#3#5",
		    "5#33##3#3####3#####3#3###3#3#3##3##3#####3#3#3##3#33#3#3#3#5",
		    "5#33#33#3#33#3#3333333#3#3#333333333#3#33#3#333##333##33#3#5",
		    "5#33#3333#33#3#3#######3#3#33######33##3#3#3##3##3#3#333#3#5",
		    "5#33###3##33#3333333#3#3333333#333#333333333#3#33#3#3#3##3#5",
		    "5#33#3#3#333##3#3#3333#3#######3333333#####333333333#33333#5",
		    "5#33#3#3#33333333#33###3#3#33#3333#3#33333#333#3#33#3###33#5",
                    "5#1333333333#3333#33333333333#333333333333#333333333333333#5",
                    "5##########################################################5",
                    "944444444444444444444444444444444444444444444444444444444440",
                   };

int main()
{
    char Key2;
    int flag1=1;
    while(flag1)
    {
	clrscr();
	showtime(); 
	majorscreen();
	Key2=getch();
	clrscr();

	if(Key2=='2'||Key2=='q') 
	    break; 
	if(Key2=='1')
	    { 
		startscreen();
		putmaze();
		showtime();
		textbackground(0);
		textcolor(10);
		gotoxy(38,2);
		cputs(" MAZE ");
		textcolor(12);
		gotoxy(11,24);
		cputs(" PS: ");
		textcolor(14);
		gotoxy(15,24);
		cputs(" w-up,a-left,d-right,s-down! ");
		gotoxy(15,25);
		cputs(" press q to quit the game anytime! ");
		Seek();
	    }
    }
    return 0;
}

void Seek()
{
    char key2='0';
    int manx=2,many=18,desx=24,desy=3;
    int flag[21][60],i,j;
    for(i=0;i<21;i++)
        for(j=0;j<60;j++) 
            flag[i][j]=0;  

    while (key2 !='q')
    {
        key2 =getch();
	sound(880);
	delay(100);
	nosound();
	switch (key2)
	    {
		case 'w':
		    if (maze[many - 1][manx] =='#')
		 	    break;
		    if(flag[many-1][manx]) 
		    {
			DrawSpace(manx,many);
			flag[many][manx]=0;
		    }
		    else 
			DrawBlackSpace(manx, many);
		    --many;
		    flag[many][manx]=1;
		    DrawMan(manx, many);
		    break;
	   	case 's':
		    if (maze[many + 1][manx] =='#')
			break;
	  	    if(flag[many+1][manx]) 
		    {
			DrawSpace(manx,many);
			flag[many][manx]=0;
		    }
		    else 
			DrawBlackSpace(manx, many);
		    ++many;
		    flag[many][manx]=1;
		    DrawMan(manx, many);
		    break;
		case 'a':
		    if (maze[many][manx - 1] =='#')
			break;
		    if(flag[many][manx-1]) 
		    {
			DrawSpace(manx,many);
     			flag[many][manx]=0;
		    }
	  	    else 
			DrawBlackSpace(manx, many);
    		    DrawBlackSpace(manx, many);
		    --manx;
		    flag[many][manx]=1;
		    DrawMan(manx, many);
		    break;
		case 'd':
		    if (maze[many][manx + 1] =='#')
			break;
		    if(flag[many][manx+1])
  		    {
			DrawSpace(manx,many);
			flag[many][manx]=0;
		    }
		    else 
			DrawBlackSpace(manx, many);
		    DrawBlackSpace(manx, many);
		    ++manx;
		    flag[many][manx]=1;
		    DrawMan(manx, many);
		    break;
		defualt:
		    break;
	    }

	    if (manx == desx && many == desy)
	    {   
		endscreen();
		showtime();
		getch();
		key2 ='q';
	    }
    }
}

void majorscreen()
{
    textbackground(0);
    textcolor(14);
    gotoxy(32,10);
    cputs( " \x1 1. Game  \x1 " );
    gotoxy(32,15);
    cputs( " \x1 2. Quit! \x1 " );
    gotoxy(22,19);
    cputs( " \x1 Press 1 or 2 to play the game! \x1 " );
    gotoxy(22,22);
    cputs( " \x1 And Press Q to quit the game!  \x1 " );
}
void startscreen( void )
{
    showtime();
    textbackground(0);
    textcolor(10);
    gotoxy(8,8);
    cputs( " \x1 This is a game for you to find the shinning thing!            \x1 " );
    gotoxy(8,10);
    cputs( " \x1 You are an explorer ,if you find the Diamond, you are a hero. \x1 " );
    gotoxy(8,12);
    cputs( " \x1 Please press any key to begin the game!                       \x1 " ) ;
    gotoxy(8,14);
    cputs( " \x1 And press q whenever to quit the game!                        \x1 " );
    gotoxy(8,16);
    cputs( " \x1 Good luck! And have a good time!                              \x1 " );
    gotoxy(30,18);
    cputs( " \x1 --- Hope.XW --- \x1 " );
    getch();
    clrscr();
}

void putmaze()
{
    int x=0,y=0;
    for(;y<21;y++)
    for(x=0;x<60;x++)
    {
        if(maze[y][x]==' ') 
            DrawSpace(x,y);
	else if(maze[y][x]=='1') 
	    DrawMan(x,y);
	else if(maze[y][x]=='#') 
	    DrawWall(x,y);
	else if(maze[y][x]=='3') 
	    DrawBlack(x,y);
	else if(maze[y][x]=='4') 
	    DrawXLine(x,y);
	else if(maze[y][x]=='5') 
	    DrawYLine(x,y);
	else if(maze[y][x]=='7') 
	    DrawUpLeftcorner(x,y);
	else if(maze[y][x]=='8') 
	    DrawUpRightcorner(x,y);
	else if(maze[y][x]=='9') 
	    DrawDownLeftcorner(x,y);
	else if(maze[y][x]=='0') 
	    DrawDownRightcorner(x,y);	
	else if(maze[y][x]=='2')
 	{
	    textbackground(12|128);
	    textcolor(12);
	    gotoxy(x+12,y+3);
	    putch('*');
	}

    }

}

void DrawSpace( int x, int y )
{
    gotoxy(x+12,y+3);
    textbackground(0);
    textcolor(0);
    putch(' ');
}

void DrawBlackSpace( int x,int y )
{
    gotoxy(x+12,y+3);
    textbackground(0);
    textcolor(0);
    putch(219);
}

void DrawMan( int x, int y )
{   
    gotoxy(x+12,y+3);
    textbackground(0);
    textcolor(14);
    putch(1);
    gotoxy(x+12,y+3);
}

void DrawWall( int x, int y )
{
    int i;
    gotoxy(x+12,y+3);
    i=rand()%15;
    textbackground(15);
    putch(176);
    gotoxy(wherex()-1,wherey());
    textcolor(i);
    putch(4);
}

void DrawBlack( int x, int y )
{
    gotoxy(x+12,y+3);
    textbackground(0);
    textcolor(0);
    putch(0);
}

void DrawXLine( int x, int y )
{
    gotoxy(x+12,y+3);
    textbackground(0);
    textcolor(10);
    putch(205);
}

void DrawYLine( int x, int y )
{
    gotoxy(x+12,y+3);
    textbackground(0);
    textcolor(10);
    putch(186);
}

void DrawUpLeftcorner( int x, int y )
{
    gotoxy(x+12,y+3);
    textbackground(0);
    textcolor(10);
    putch(201);
}

void DrawUpRightcorner( int x, int y )
{
    gotoxy(x+12,y+3);
    textbackground(0);
    textcolor(10);
    putch(187);
}

void DrawDownLeftcorner( int x, int y )
{
    gotoxy(x+12,y+3);
    textbackground(0);
    textcolor(10);
    putch(200);
}

void DrawDownRightcorner( int x, int y )
{
    gotoxy(x+12,y+3);
    textbackground(0);
    textcolor(10);
    putch(188);
}

void showtime()
{
    gotoxy(1,1);
    textbackground(12);
    textcolor(10);
    time_t now;
    struct tm * timenow;
    time(&now);
    timenow=localtime(&now);
    printf(" Local time is %s\n", asctime(timenow));
}

void endscreen()
{
    clrscr();
    cputs("\a\a\a\a");
    gotoxy(18,15);
    textbackground(12);
    textcolor(14|128);
    cputs(" Congratulations! You have found the Diamond! ");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -