简易版推箱子游戏

关于文件的使用,在这个代码里你要先创建一个D:\\data.txt,,否则不行,你也可以改变一下文件的打开方式。

这里有一些新的功能函数,比如gotoxy()这个自定义函数,里面的一些代码,你可能没学过,在网上可以搜到,其实

这也是定位光标的固定代码,在这个函数里电脑屏幕是一个坐标系,原点在左上角。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
int rx,ry;  //小人在每一步的坐标
int count=0;
FILE *fp; //文件指针
//w--墙 ‘ ’--空地 i--有箱子的目的地 m--目的地 r--小人 s--有小人的目的地
char status[22][22]; //状态数组
void gotoxy(int x,int y);
void initstatus();
void putoutChar(int x,int y,char ch);
void color(int x);
void showMain();
int judge1();
int judge2();
int judge3();
int judge4();
void putoutDestination(int x,int y);
void putoutMan(int x,int y);
void putoutBox(int x,int y);
void putoutDbox(int x,int y);
void putoutBlank(int x,int y);
void firstfreshenDestination();
void secondfreshenDestination();
void thirdfreshenDestination();
void fourfreshenDestination();
void moveUp(int num);
void moveDown(int num);
void moveLeft(int num);
void moveRight(int num);
void reset(int num);
int move(int num);
void firstprintWall();
void firstprintBox();
int first();
int second();
int third();
int four();
void inFile();
void outFile();
void step();
/*
**写入文件
*/
void inFile()
{
    int s=0;
	if((fp= fopen("D:\\data.txt","r"))==NULL)
	{
	    printf("Can not open file!\n");
		exit(1);
	}
    fscanf(fp,"%d",&s);
    fclose(fp);
    if((fp= fopen("D:\\data.txt","w"))==NULL)
	{
	    printf("Can not open file!\n");
		exit(1);
	}
    if(s>count)
    {
        fprintf(fp,"%d",count);
    }
    else
    {
        fprintf(fp,"%d",s);
    }
    fclose(fp);
}
/*
**读取文件
*/
void outFile()
{
    int s=0;
    if((fp= fopen("D:\\data.txt","r"))==NULL)
	{
        printf("Can not open file!\n");
		exit(1);
	}
	fscanf(fp,"%d",&s);
	color(13);
    gotoxy(7,13);
    printf("玩家最少步数为\n");
    gotoxy(7,15);
	printf(": %d",s);
	fclose(fp);
}
/*
**第一关步数
*/
void step()
{
    color(13);
    gotoxy(7,10);
    printf("步数:%d",count);
}
/*
**将光标移到指定位置
*/
void gotoxy(int x,int y)
{
    HANDLE hout;
    COORD  pos={x,y};
    hout=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hout,pos);
}
/*
**初始化状态数组
*/
void initStatus()
{
    int i,j;
    for(i=0;i<=80;i++)
    {
        for(j=0;j<=80;j++)
        {
            status[i][j]=' ';
        }
    }
}
/*
**指定位置输出指定字符
*/
void putoutChar(int x,int y,char ch)
{
    gotoxy(x,y);
    printf("%c",ch);
}
/*
**
*/
void color(int x) //自定义函根据参数改变颜色
{
    if(x>=0 && x<=15)//参数在0-15的范围颜色
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
    else
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);//默认的颜色白色
}
/*
**主界面
*/
void showMain()
{
    int x,y;
    for(x=1,y=0;x<=60;x++)
    {
        color(4);
        putoutChar(x,y,'_');
    }
    for(x=1,y=20;x<=60;x++)
    {
        color(4);
        putoutChar(x,y,'_');
    }
    for(x=1,y=1;y<=20;y++)
    {
        color(4);
        putoutChar(x,y,'|');
    }
    for(x=60,y=1;y<=20;y++)
    {
        color(4);
        putoutChar(x,y,'|');
    }
    gotoxy(22,15);
    color(4);
    printf("游戏基本操作:\n");
    gotoxy(22,16);
    color(4);
    printf("w--上\ts--下\n");
    gotoxy(22,17);
    color(4);
    printf("a--左\td--右\n");
    gotoxy(5,3);
    color(4);
    printf("ESC键退出游戏\n");
    gotoxy(39,3);
    color(4);
    printf("按b重置当前关卡\n");
    gotoxy(39,10);
    color(4);
    printf("按字母n可直接跳入\n");
    gotoxy(39,11);
    color(4);
    printf("下一关卡\n");
    gotoxy(46,16);
    color(4);
    printf("w\n");
    gotoxy(45,17);
    color(4);
    printf("asd\n");
    gotoxy(39,16);
    color(4);
    printf("--->\n");
    gotoxy(39,17);
    color(4);
    printf("--->\n");
}
/*
**判断第一关是否完成
*/
int judge1()
{
    int flag=0;
    if(status[24][7]=='i'&&status[24][8]=='i'&&status[24][9]=='i')
    {
        flag=1;
    }
    return flag;
}
/*
**判断第二关是否完成
*/
int judge2()
{
    int flag=0;
    if(status[24][7]=='i'&&status[24][8]=='i')
    {
        flag=1;
    }
    return flag;
}
/*
**判断第三关是否完成
*/
int judge3()
{
    int flag=0;
    if(status[24][7]=='i')
    {
        flag=1;
    }
    return flag;
}
/*
**判断第四关是否完成
*/
int judge4()
{
    int flag=0;
    if(status[24][7]=='i'&&status[24][8]=='i'&&status[24][9]=='i'&&status[29][11]=='i')
    {
        flag=1;
    }
    return flag;
}
/*
**指定位置打印目的地
*/
void putoutDestination(int x,int y)
{
    putoutChar(x,y,3);
}
/*
**指定位置打印小人
*/
void putoutMan(int x,int y)
{
    color(8);
    putoutChar(x,y,2);
}
/*
**指定位置打印箱子
*/
void putoutBox(int x,int y)
{
    color(7);
    putoutChar(x,y,4);
}
/*
**指定位置打印在目的地的箱子
*/
void putoutDbox(int x,int y)
{
    color(13);
    putoutChar(x,y,22);
}
/*
**指定位置打印空格
*/
void putoutBlank(int x,int y)
{
    putoutChar(x,y,' ');
}
/*
**刷新第一关的目的地
*/
void firstfreshenDestination()
{
    color(6);
    if(status[24][7]!='s'&&status[24][7]!='i')
    {
        putoutDestination(24,7);
        status[24][7]='m';
    }
    if(status[24][8]!='s'&&status[24][8]!='i')
    {
        putoutDestination(24,8);
        status[24][8]='m';
    }
    if(status[24][9]!='s'&&status[24][9]!='i')
    {
        putoutDestination(24,9);
        status[24][9]='m';
    }
}
/*
**刷新第二关的目的地
*/
void secondfreshenDestination()
{
    color(6);
    if(status[24][7]!='s'&&status[24][7]!='i')
    {
        putoutDestination(24,7);
        status[24][7]='m';
    }
    if(status[24][8]!='s'&&status[24][8]!='i')
    {
        putoutDestination(24,8);
        status[24][8]='m';
    }
}
/*
**刷新第三关的目的地
*/
void thirdfreshenDestination()
{
    color(6);
    if(status[24][7]!='s'&&status[24][7]!='i')
    {
        putoutDestination(24,7);
        status[24][7]='m';
    }
}
/*
**刷新第四关的目的地
*/
void fourfreshenDestination()
{
    color(6);
    if(status[24][7]!='s'&&status[24][7]!='i')
    {
        putoutDestination(24,7);
        status[24][7]='m';
    }
    if(status[24][8]!='s'&&status[24][8]!='i')
    {
        putoutDestination(24,8);
        status[24][8]='m';
    }
    if(status[24][9]!='s'&&status[24][9]!='i')
    {
        putoutDestination(24,9);
        status[24][9]='m';
    }
    if(status[29][11]!='s'&&status[29][11]!='i')
    {
        putoutDestination(29,11);
        status[29][11]='m';
    }
}
/*
**向上走
*/
void moveUp(int num)
{
    int flag=1;
    int f=1;
    if(f==1&&status[rx][ry-1]=='w')
    {
        flag=0;
        f=0;
    }
    else if(f==1&&status[rx][ry-1]=='b')
    {
        if(f==1&&status[rx][ry-2]=='w'||status[rx][ry-2]=='b'||status[rx][ry-2]=='i')
        {
            flag=0;
            f=0;
        }
        else if(f==1&&status[rx][ry-2]=='m')
        {
            f=0;
            //在位置的上上方打印目的地箱子的标志
            putoutDbox(rx,ry-2);
            //在位置的上方打印小人的标志,并把原位置标志消除
            putoutMan(rx,ry-1);
            putoutBlank(rx,ry);
            //改变状态
            status[rx][ry-2]='i';
            status[rx][ry-1]='r';
            status[rx][ry]=' ';
        }
        else if(f==1&&status[rx][ry-2]==' ')
        {
            f=0;
            //在位置的上上方打印箱子的标志
            putoutBox(rx,ry-2);
            //在位置的上方打印小人的标志,并把原位置标志消除
            putoutMan(rx,ry-1);
            putoutBlank(rx,ry);
            //改变状态
            status[rx][ry-2]='b';
            status[rx][ry-1]='r';
            status[rx][ry]=' ';
        }
    }
    else if(f==1&&status[rx][ry-1]=='i')
    {
        if(f==1&&status[rx][ry-2]=='w'||status[rx][ry-2]=='b'||status[rx][ry-2]=='i')
        {
            flag=0;
            f=0;
        }
        else if(f==1&&status[rx][ry-2]=='m')
        {
            f=0;
            //在位置的上上方打印目的地箱子的标志
            putoutDbox(rx,ry-2);
            //在位置的上方打印目的地小人的标志,并把原位置标志消除
            putoutMan(rx,ry-1);
            putoutBlank(rx,ry);
            //改变状态
            status[rx][ry-2]='i';
            status[rx][ry-1]='s';
            status[rx][ry]=' ';
        }
    }
    else if(f==1&&status[rx][ry-1]==' ')
    {
        f=0;
        //在位置的上方打印小人的标志,并把原位置标志消除
        putoutBlank(rx,ry);
        putoutMan(rx,ry-1);
        //改变状态
        status[rx][ry-1]='r';
        status[rx][ry]=' ';
    }
    else if(f==1&&status[rx][ry-1]=='m')
    {
        f=0;
        //在位置的上方打印目的地小人的标志,并把原位置标志消除
        putoutBlank(rx,ry);
        putoutMan(rx,ry-1);
        //改变状态
        status[rx][ry-1]='s';
        status[rx][ry]=' ';
    }
    if(flag)
    {
        ry--;
    }

    if(num==1)
    {
        firstfreshenDestination();
    }
    else if(num==2)
    {
        secondfreshenDestination();
    }
    else if(num==3)
    {
        thirdfreshenDestination();
    }
    else if(num==4)
    {
        fourfreshenDestination();
    }

    gotoxy(rx,ry);
}
/*
**向下走
*/
void moveDown(int num)
{
    int flag=1;
    int f=1;
    if(f==1&&status[rx][ry+1]=='w')
    {
        flag=0;
        f=0;
    }
    else if(f==1&&status[rx][ry+1]=='b')
    {
        if(f==1&&status[rx][ry+2]=='w'||status[rx][ry+2]=='b'||status[rx][ry+2]=='i')
        {
            flag=0;
            f=0;
        }
        else if(f==1&&status[rx][ry+2]=='m')
        {
            f=0;
            //在位置的下下方打印目的地箱子的标志
            putoutDbox(rx,ry+2);
            //在位置的下方打印小人的标志,并把原位置标志消除
            putoutMan(rx,ry+1);
            putoutBlank(rx,ry);
            //改变状态
            status[rx][ry+2]='i';
            status[rx][ry+1]='r';
            status[rx][ry]=' ';
        }
        else if(f==1&&status[rx][ry+2]==' ')
        {
            f=0;
            //在位置的下下方打印箱子的标志
            putoutBox(rx,ry+2);
            //在位置的下方打印小人的标志,并把原位置标志消除
            putoutMan(rx,ry+1);
            putoutBlank(rx,ry);
            //改变状态
            status[rx][ry+2]='b';
            status[rx][ry+1]='r';
            status[rx][ry]=' ';
        }
    }
    else if(f==1&&status[rx][ry+1]=='i')
    {
        if(f==1&&status[rx][ry+2]=='w'||status[rx][ry+2]=='b'||status[rx][ry+2]=='i')
        {
            flag=0;
            f=0;
        }
        else if(f==1&&status[rx][ry+2]=='m')
        {
            f=0;
            //在位置的下下方打印目的地箱子的标志
            putoutDbox(rx,ry+2);
            //在位置的下方打印目的地小人的标志,并把原位置标志消除
            putoutMan(rx,ry+1);
            putoutBlank(rx,ry);
            //改变状态
            status[rx][ry+2]='i';
            status[rx][ry+1]='s';
            status[rx][ry]=' ';
        }
    }
    else if(f==1&&status[rx][ry+1]==' ')
    {
        f=0;
        //在位置的下方打印小人的标志,并把原位置标志消除
        putoutBlank(rx,ry);
        putoutMan(rx,ry+1);
        //改变状态
        status[rx][ry+1]='r';
        status[rx][ry]=' ';
    }
    else if(f==1&&status[rx][ry+1]=='m')
    {
        f=0;
        //在位置的下方打印小人的标志,并把原位置标志消除
        putoutBlank(rx,ry);
        putoutMan(rx,ry+1);
        //改变状态
        status[rx][ry+1]='s';
        status[rx][ry]=' ';
    }
    if(flag)
    {
        ry++;
    }

    if(num==1)
    {
        firstfreshenDestination();
    }
    else if(num==2)
    {
        secondfreshenDestination();
    }
    else if(num==3)
    {
        thirdfreshenDestination();
    }
    else if(num==4)
    {
        fourfreshenDestination();
    }

    gotoxy(rx,ry);
}
/*
**向左走
*/
void moveLeft(int num)
{
    int flag=1;
    int f=1;
    if(f==1&&status[rx-1][ry]=='w')
    {
        flag=0;
        f=0;
    }
    else if(f==1&&status[rx-1][ry]=='b')
    {
        if(f==1&&status[rx-2][ry]=='w'||status[rx-2][ry]=='b'||status[rx-2][ry]=='i')
        {
            flag=0;
            f=0;
        }
        else if(f==1&&status[rx-2][ry]=='m')
        {
            f=0;
            //在位置的左左方打印目的地箱子的标志
            putoutDbox(rx-2,ry);
            //在位置的左方打印小人的标志,并把原位置标志消除
            putoutMan(rx-1,ry);
            putoutBlank(rx,ry);
            //改变状态
            status[rx-2][ry]='i';
            status[rx-1][ry]='r';
            status[rx][ry]=' ';
        }
        else if(f==1&&status[rx-2][ry]==' ')
        {
            f=0;
            //在位置的左左方打印箱子的标志
            putoutBox(rx-2,ry);
            //在位置的左方打印小人的标志,并把原位置标志消除
            putoutMan(rx-1,ry);
            putoutBlank(rx,ry);
            //改变状态
            status[rx-2][ry]='b';
            status[rx-1][ry]='r';
            status[rx][ry]=' ';
        }
    }
    else if(f==1&&status[rx-1][ry]=='i')
    {
        if(f==1&&status[rx-2][ry]=='w'||status[rx-2][ry]=='b'||status[rx-2][ry]=='i')
        {
            flag=0;
            f=0;
        }
        else if(f==1&&status[rx-2][ry]=='m')
        {
            f=0;
            //在位置的左左方打印目的地箱子的标志
            putoutDbox(rx-2,ry);
            //在位置的左方打印目的地小人的标志,并把原位置标志消除
            putoutMan(rx-1,ry);
            putoutBlank(rx,ry);
            //改变状态
            status[rx-2][ry]='i';
            status[rx-1][ry]='s';
            status[rx][ry]=' ';
        }
    }
    else if(f==1&&status[rx-1][ry]==' ')
    {
        f=0;
        //在位置的左方打印小人的标志,并把原位置标志消除
        putoutBlank(rx,ry);
        putoutMan(rx-1,ry);
        //改变状态
        status[rx-1][ry]='r';
        status[rx][ry]=' ';
    }
    else if(f==1&&status[rx-1][ry]=='m')
    {
        f=0;
        //在位置的左方打印目的地小人的标志,并把原位置标志消除
        putoutBlank(rx,ry);
        putoutMan(rx-1,ry);
        //改变状态
        status[rx-1][ry]='s';
        status[rx][ry]=' ';
    }
    if(flag)
    {
        rx--;
    }

    if(num==1)
    {
        firstfreshenDestination();
    }
    else if(num==2)
    {
        secondfreshenDestination();
    }
    else if(num==3)
    {
        thirdfreshenDestination();
    }
    else if(num==4)
    {
        fourfreshenDestination();
    }

    gotoxy(rx,ry);
}
/*
**向右走
*/
void moveRight(int num)
{
    int flag=1;
    int f=1;
    if(f==1&&status[rx+1][ry]=='w')
    {
        flag=0;
        f=0;
    }
    else if(f==1&&status[rx+1][ry]=='b')
    {
        if(f==1&&status[rx+2][ry]=='w'||status[rx+2][ry]=='b'||status[rx+2][ry]=='i')
        {
            flag=0;
            f=0;
        }
        else if(f==1&&status[rx+2][ry]=='m')
        {
            f=0;
            //在位置的右右方打印目的地箱子的标志
            putoutDbox(rx+2,ry);
            //在位置的右方打印小人的标志,并把原位置标志消除
            putoutMan(rx+1,ry);
            putoutBlank(rx,ry);
            //改变状态
            status[rx+2][ry]='i';
            status[rx+1][ry]='r';
            status[rx][ry]=' ';
        }
        else if(f==1&&status[rx+2][ry]==' ')
        {
            f=0;
            //在位置的右方打印箱子的标志,并把原位置标志消除
            putoutBox(rx+2,ry);
            //在位置的右方打印小人的标志,并把原位置标志消除
            putoutMan(rx+1,ry);
            putoutBlank(rx,ry);
            //改变状态
            status[rx+2][ry]='b';
            status[rx+1][ry]='r';
            status[rx][ry]=' ';
        }
    }
    else if(f==1&&status[rx+1][ry]=='i')
    {
        if(f==1&&status[rx+2][ry]=='w'||status[rx+2][ry]=='b'||status[rx+2][ry]=='i')
        {
            flag=0;
            f=0;
        }
        else if(f==1&&status[rx+2][ry]=='m')
        {
            f=0;
            //在位置的右右方打印目的地箱子的标志
            putoutDbox(rx+2,ry);
            //在位置的右方打印目的地小人的标志,并把原位置标志消除
            putoutMan(rx+1,ry);
            putoutBlank(rx,ry);
            //改变状态
            status[rx+2][ry]='i';
            status[rx+1][ry]='s';
            status[rx][ry]=' ';
        }
    }
    else if(f==1&&status[rx+1][ry]==' ')
    {
        f=0;
        //在位置的右方打印小人的标志,并把原位置标志消除
        putoutBlank(rx,ry);
        putoutMan(rx+1,ry);
        //改变状态
        status[rx+1][ry]='r';
        status[rx][ry]=' ';
    }
    else if(f==1&&status[rx+1][ry]=='m')
    {
        f=0;
        //在位置的右方打印小人的标志,并把原位置标志消除
        putoutBlank(rx,ry);
        putoutMan(rx+1,ry);
        //改变状态
        status[rx+1][ry]='s';
        status[rx][ry]=' ';
    }
    if(flag)
    {
        rx++;
    }

    if(num==1)
    {
        firstfreshenDestination();
    }
    else if(num==2)
    {
        secondfreshenDestination();
    }
    else if(num==3)
    {
        thirdfreshenDestination();
    }
    else if(num==4)
    {
        fourfreshenDestination();
    }

    gotoxy(rx,ry);
}
/*
**重置当前关
*/
void reset(int num)
{
    system("cls");
    if(num==1)
    {
        first();
    }
    else if(num==2)
    {
        second();
    }
    else if(num==3)
    {
        third();
    }
    else if(num==4)
    {
        four();
    }
}
/*
**输入移动字符
**分配对应的移动函数
*/
int move(int num)
{
    char ch;
    int a=1;
    char choose=1;
    int choo;
    int flag=1;
    while(a)
    {
        ch=getch();
        switch(ch)
        {
            case 'w': moveUp(num);count++;step();break;
            case 's': moveDown(num);count++;step();break;
            case 'a': moveLeft(num);count++;step();break;
            case 'd': moveRight(num);count++;step();break;
            case 'b': reset(num);break;
            case 'n': a=0;break;
            default :break;
        }
        if(ch==27)
        {
            flag=0;
            break;
        }
        if(num==1)
        {
            if(judge1())
            {
                frame();
                inFile();
                gotoxy(22,8);
                color(13);
                printf("恭喜您通过第一关成功\n");
                gotoxy(22,9);
                printf("是否进行第二关?\n");
                gotoxy(22,10);
                printf("1---是  2---否\n");
                gotoxy(22,11);
                while(1)
                {
                    choose=getch();
                    choose-=48;
                    choo=choose;
                    if(choo==1)
                    {
                        flag=1;
                        break;
                    }
                    else if(choo==2)
                    {
                        flag=0;
                        break;
                    }
                    else
                    {
                        gotoxy(22,13);
                        printf("输入有误,请重新输入\n");
                    }
                }
                break;
            }
        }
        else if(num==2)
        {
            if(judge2())
            {
                frame();
                inFile();
                gotoxy(22,8);
                color(13);
                printf("恭喜您通过第二关成功\n");
                gotoxy(22,9);
                printf("是否进行第三关?\n");
                gotoxy(22,10);
                printf("1---是  2---否\n");
                gotoxy(22,11);
                while(1)
                {
                    choose=getch();
                    choose-=48;
                    choo=choose;
                    if(choo==1)
                    {
                        flag=1;
                        break;
                    }
                    else if(choo==2)
                    {
                        flag=0;
                        break;
                    }
                    else
                    {
                        gotoxy(22,13);
                        printf("输入有误,请重新输入\n");
                    }
                }
                break;
            }
        }
        else if(num==3)
        {
            if(judge3())
            {
                frame();
                inFile();
                gotoxy(22,8);
                color(13);
                printf("恭喜您通过第三关成功\n");
                gotoxy(22,9);
                printf("是否进行第四关?\n");
                gotoxy(22,10);
                printf("1---是  2---否\n");
                gotoxy(22,11);
                while(1)
                {
                    choose=getch();
                    choose-=48;
                    choo=choose;
                    if(choo==1)
                    {
                        flag=1;
                        break;
                    }
                    else if(choo==2)
                    {
                        flag=0;
                        break;
                    }
                    else
                    {
                        gotoxy(22,13);
                        printf("输入有误,请重新输入\n");
                    }
                }
                break;
            }
        }
        else if(num==4)
        {
            if(judge4())
            {
                frame();
                inFile();
                gotoxy(22,8);
                color(13);
                printf("恭喜您通过第四关成功\n");
                gotoxy(22,9);
                printf("恭喜你已经通过所有关卡\n");
                gotoxy(22,10);
                system("pause");
                break;
            }
        }
    }
    return flag;
}
/*
**第一关初始化墙壁
*/
void firstprintWall()
{
    int x,y;
    color(2);
    for(x=3,y=4;y<=9;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=5,y=4;y<=6;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=7,y=2;y<=6;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=11,y=2;y<=6;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=6,y=8;y<=10;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=10,y=6;y<=10;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
        putoutChar(24,6,1);
        status[24][6]='w';
    for(x=8,y=2;x<=10;x++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
        putoutChar(26,8,1);
        status[26][8]='w';
    for(x=9,y=6;x<=10;x++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=4,y=9;x<=5;x++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=7,y=10;x<=9;x++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
}
/*
**第一关初始化箱子
*/
void firstprintBox()
{
    color(7);
    putoutChar(28,6,4);
    status[28][6]='b';
    putoutChar(29,6,4);
    status[29][6]='b';
    putoutChar(29,7,4);
    status[29][7]='b';
}
/*
**第一关初始化目的地
*/
void firstprintDestination()
{
    color(6);
    putoutChar(24,7,3);
    status[24][7]='m';
    putoutChar(24,8,3);
    status[24][8]='m';
    putoutChar(24,9,3);
    status[24][9]='m';
}
/*
**第一关初始化小人
*/
void firstprintMan()
{
    color(7);
    putoutChar(30,5,2);
    status[30][5]='r';
}
/*
**第一关初始化屏幕
*/
void firstinit()
{
    firstprintWall();//初始化墙壁
    firstprintBox();//初始化箱子
    firstprintDestination();//初始化目的地
    firstprintMan();//初始化小人
}
/*
**第一关
*/
int first()
{
    int flag=1;
    count=0;
    system("cls");
    initStatus();
    showMain();
    gotoxy(26,2);
    color(4);
    printf("第一关\n");
    firstinit();
    gotoxy(30,5);
    rx=30;ry=5;
    flag=move(1);
    gotoxy(1,20);
    return flag;
}

/*
**第二关初始化墙壁
*/
void secondprintWall()
{
    int x,y;
    color(2);
    for(x=3,y=4;y<=9;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=5,y=4;y<=6;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=7,y=2;y<=6;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=11,y=2;y<=6;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=6,y=8;y<=10;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=10,y=6;y<=10;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
        putoutChar(24,6,1);
        status[24][6]='w';
    for(x=8,y=2;x<=10;x++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
        putoutChar(26,8,1);
        status[26][8]='w';
    for(x=9,y=6;x<=10;x++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=4,y=9;x<=5;x++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=7,y=10;x<=9;x++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
}
/*
**第二关初始化箱子
*/
void secondprintBox()
{
    color(7);
    putoutChar(28,6,4);
    status[28][6]='b';
    putoutChar(29,6,4);
    status[29][6]='b';
}
/*
**第二关初始化目的地
*/
void secondprintDestination()
{
    color(6);
    putoutChar(24,7,3);
    status[24][7]='m';
    putoutChar(24,8,3);
    status[24][8]='m';
}
/*
**第二关初始化小人
*/
void secondprintMan()
{
    color(7);
    putoutChar(29,5,2);
    status[29][5]='r';
}
/*
**第二关初始化屏幕
*/
void secondinit()
{
    secondprintWall();//初始化墙壁
    secondprintBox();//初始化箱子
    secondprintDestination();//初始化目的地
    secondprintMan();//初始化小人
}
/*
**第二关
*/
int second()
{
    int flag=1;
    count=0;
    system("cls");
    initStatus();
    gotoxy(26,2);
    color(4);
    printf("第二关\n");
    showMain();
    secondinit();
    gotoxy(29,5);
    rx=29;ry=5;
    flag=move(2);
    gotoxy(1,20);
    return flag;
}
/*
**第三关初始化墙壁
*/
void thirdprintWall()
{
    int x,y;
    color(2);
    for(x=3,y=4;y<=9;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=5,y=4;y<=6;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=7,y=2;y<=6;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=11,y=2;y<=6;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=6,y=8;y<=10;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=10,y=6;y<=10;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
        putoutChar(24,6,1);
        status[24][6]='w';
    for(x=8,y=2;x<=10;x++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
        putoutChar(26,8,1);
        status[26][8]='w';
    for(x=9,y=6;x<=10;x++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=4,y=9;x<=5;x++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=7,y=10;x<=9;x++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
}
/*
**第三关初始化箱子
*/
void thirdprintBox()
{
    color(7);
    putoutChar(28,6,4);
    status[28][6]='b';
}
/*
**第三关初始化目的地
*/
void thirdprintDestination()
{
    color(6);
    putoutChar(24,7,3);
    status[24][7]='m';
}
/*
**第三关初始化小人
*/
void thirdprintMan()
{
    color(8);
    putoutChar(28,5,2);
    status[28][5]='r';
}
/*
**第三关初始化屏幕
*/
void thirdinit()
{
    thirdprintWall();//初始化墙壁
    thirdprintBox();//初始化箱子
    thirdprintDestination();//初始化目的地
    thirdprintMan();//初始化小人
}
/*
**第三关
*/
int third()
{
    int flag=1;
    count=0;
    system("cls");
    initStatus();
    gotoxy(26,2);
    color(4);
    printf("第三关\n");
    showMain();
    thirdinit();
    gotoxy(28,5);
    rx=28;ry=5;
    flag=move(3);
    gotoxy(1,20);
    return flag;
}
/*
**第四关初始化墙壁
*/
void fourprintWall()
{
    int x,y;
    color(2);
    for(x=3,y=4;y<=9;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=5,y=4;y<=6;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=7,y=1;y<=6;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=12,y=1;y<=6;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=6,y=8;y<=10;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=10,y=6;y<=10;y++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
        putoutChar(24,6,1);
        status[24][6]='w';
    for(x=8,y=1;x<=11;x++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
        putoutChar(26,8,1);
        status[26][8]='w';

        putoutChar(31,8,1);
        status[31][8]='w';
    for(x=9,y=6;x<=10;x++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=4,y=9;x<=5;x++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
    for(x=7,y=10;x<=9;x++)
    {
        putoutChar(x+20,y+2,1);
        status[x+20][y+2]='w';
    }
}
/*
**第四关初始化箱子
*/
void fourprintBox()
{
    color(7);
    putoutChar(28,6,4);
    status[28][6]='b';
    putoutChar(29,6,4);
    status[29][6]='b';
    putoutChar(29,7,4);
    status[29][7]='b';
    putoutChar(28,10,4);
    status[28][10]='b';
}
/*
**第四关初始化目的地
*/
void fourprintDestination()
{
    color(6);
    putoutChar(24,7,3);
    status[24][7]='m';
    putoutChar(24,8,3);
    status[24][8]='m';
    putoutChar(24,9,3);
    status[24][9]='m';
    putoutChar(29,11,3);
    status[29][11]='m';
}
/*
**第四关初始化小人
*/
void fourprintMan()
{
    color(8);
    putoutChar(26,9,2);
    status[26][9]='r';
}
/*
**第四关初始化屏幕
*/
void fourinit()
{
    fourprintWall();//初始化墙壁
    fourprintBox();//初始化箱子
    fourprintDestination();//初始化目的地
    fourprintMan();//初始化小人
}
/*
**第四关
*/
int four()
{
    int flag=1;
    count=0;
    system("cls");
    initStatus();
    gotoxy(26,2);
    color(4);
    printf("第四关\n");
    showMain();
    fourinit();
    gotoxy(26,9);
    rx=26;ry=9;
    flag=move(4);
    gotoxy(1,20);
    return flag;
}
/*
**打印边框
*/
void frame()
{
    system("cls");
    int x,y;
    color(4);
    for(x=1,y=0;x<=60;x++)
    {
        printf("1");
        putoutChar(x,y,'_');
    }
    for(x=1,y=20;x<=60;x++)
    {
        putoutChar(x,y,'_');
    }
    for(x=1,y=1;y<=20;y++)
    {
        putoutChar(x,y,'|');
    }
    for(x=60,y=1;y<=20;y++)
    {
        putoutChar(x,y,'|');
    }
    color(1);
    for(x=2,y=1;x<=59;x++)
    {
        putoutChar(x,y,'_');
    }
    for(x=2,y=19;x<=59;x++)
    {
        putoutChar(x,y,'_');
    }
    for(x=2,y=2;y<=19;y++)
    {
        putoutChar(x,y,'|');
    }
    for(x=59,y=2;y<=19;y++)
    {
        putoutChar(x,y,'|');
    }
}
/*
**说明页面
*/
void explain()
{
    int choose;
    frame();
    color(13);
    gotoxy(10,3);
    printf("游戏背景:\n");
    gotoxy(10,4);
    printf("      经典的推箱子是来自日本的古老游戏,\n");
    gotoxy(10,5);
    printf("在一个狭小的仓库中,要求把木箱从开始位置推放到\n");
    gotoxy(10,6);
    printf("指定的位置,而且箱子只能推,不能拉。本游戏一共4关\n");
    gotoxy(10,8);
    printf("游戏操作:\n");
    gotoxy(10,9);
    printf("      w---上 d---下 a---左 d---右\n");
    while(1)
    {
        gotoxy(19,11);
        printf("1---返回上一个页面");
        choose=getch();
        choose-=48;
        if(choose==1)
        {
            break;
        }
        else
        {
            gotoxy(18,15);
            printf("输入有误,请重新输入");
        }
    }
}
/*
**开始页面
*/
int gameStart()
{
    char choose;
    frame();
    outFile();
    color(13);
    gotoxy(27,5);
    printf("推箱子\n");
    gotoxy(25,7);
    printf("1===开始游戏\n");
    gotoxy(25,9);
    printf("0===退出游戏\n");
    gotoxy(25,11);
    printf("2===游戏说明\n");
    gotoxy(37,11);
    while(1)
    {
        choose=getch();
        choose-=48;
        if(choose==1||choose==2||choose==0)
        {
            break;
        }
        else
        {
            gotoxy(22,14);
            printf("输入有误,请重新输入\n");
        }
    }
    return choose;
}
/*
**结束页面
*/
void gameEnd()
{
    char choose;
    frame();
    color(13);
    gotoxy(25,9);
    printf("GAME OVER\n");
    gotoxy(1,21);
}
int main()
{
    int flag=1;
    int f;
    while(1)
    {
        f=gameStart();
        if(f==1)
        {
            while(1)
            {
                flag=first();//第一关
                if(flag==0)
                {
                    break;
                }
                flag=second();//第二关
                if(flag==0)
                {
                    break;
                }
                flag=third();//第三关
                if(flag==0)
                {
                    break;
                }
                flag=four();//第四关
                break;
            }
            gameEnd();
            break;
        }
        else if(f==2)
        {
            explain();
        }
        else if(f==0)
        {
            gameEnd();
            break;
        }
    }
    return 0;
}









  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值