2016.7.10实训--数字迷宫

来源:互联网 发布:天天看盘软件怎么样 编辑:程序博客网 时间:2024/06/05 18:30

本案例要求制作一个《数字迷宫》的游戏,该游戏的玩法描述为:

1、 系统主要地图为固定模式

2、 地图内部包含墙体、走廊、阿拉伯数字(1~7)、玩家头像(1个)四种不同内容

3、 用户通过上、下、左、右按键来控制玩家头像在地图范围内的走动

4、 在玩家走动过程中,碰撞墙壁时,该步的走动无效,玩家头像原地不变

5、 玩家允许在设计走廊内部进行走动

6、 玩家头像遇到阿拉布数字时,数字消失,并且对玩家走动无影响

7、 玩家头像遭遇所有数字之后(地图上无阿拉伯数字),该局游戏结束,并自动开始下一局游戏

8、 在某一局游戏中,游戏走廊、墙体与其他游戏相同,但数字位置会随机产生

9、 阿拉伯数字产生在走廊内而非墙体上

10、  在玩家游戏过程中按下Esc键,游戏无条件退出。

11、  在玩家未使用Esc键时,程序正常运行



#include<windows.h>
#include <stdio.h> //输入输出函数
#include<time.h>
#include<conio.h>
#include <stdlib.h> //动态存储分配函数


int code[17][24]= {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    {0,9,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0},
    {0,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0,-1,0,0,0,0,0,-1,-1,-1,0,0},
    {0,-1,0,0,0,-1,0,0,0,0,0,0,0,-1,0,0,0,0,0,-1,0,0,0,0},
    {0,-1,-1,-1,-1,-1,0,-1,-1,-1,-1,-1,0,-1,0,-1,-1,-1,-1,-1,-1,-1,0,0},
    {0,-1,0,0,-1,0,-1,0,0,0,0,-1,0,-1,-1,-1,0,0,0,0,0,0,0,0},
    {0,-1,-1,-1,-1,-1,-1,0,0,0,0,-1,0,0,0,-1,0,0,0,0,0,0,0,0},
    {0,-1,0,0,0,0,0,0,0,0,0,-1,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,0},
    {0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,-1,0,0,-1,0,0,0,0},
    {0,0,0,0,0,0,-1,0,0,0,0,0,0,0,-1,0,-1,0,0,-1,0,0,0,0},
    {0,0,0,0,0,0,-1,0,0,0,0,0,0,0,-1,0,-1,-1,-1,-1,0,-1,-1,0},
    {0,0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,-1,0,0},
    {0,0,0,0,0,-1,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,-1,0,0},
    {0,0,-1,-1,-1,-1,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0},
    {0,-1,-1,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0},
    {0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
};


void SetCode(int num)  //打印出地图上的信息
{
    if(num==0)
        printf("■");
    else if(num==-1)
        printf("  ");
    else
        printf("%d ",num);
}


void SetWindow() //生成地图主体函数
{
    int i,j;
    for(i=0; i<17; i++)
    {
        for(j=0; j<24; j++)
        {


            if(code[i][j]==9)
                printf("\1 ");


            else
                SetCode(code[i][j]);
        }
        printf("\n");
    }
    SetNum();
}


void SetNum()   //在数组"走廊"位置生成1~7的阿拉伯数字
{
    int i,j,a,b;
    int x=7;
    srand((unsigned)time(NULL));
    while(x)
    {
        a=rand()%17;
        b=rand()%24;
        if(code[a][b]==-1)
        {
            code[a][b]=x;
            x--;
            Gotoxy(2*b,a);
            printf("%d ",code[a][b]);
        }
    }
}


void GoToXYSpace(int x, int y)  //恢复头像上一步的地图
{
    code[x][y] = -1;
    Gotoxy(2*y,x);
    printf("  ");
}


void Gotoxy(int x,int y) //光标移动到这个位置,输出用户头像
{
    COORD c= {x,y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}


void CheckLeftNum(int x,int y)  //检测剩余数字的数量
{
    int i,j;
    int leftnum=0;
    for(i=1; i<16; i++)
    {
        for(j=1; j<23; j++)
        {
            if(code[i][j] > 0 && code[i][j] < 8)
                leftnum++;
        }
    }
    if(leftnum == 0)
        Begin(x,y);
}


void Begin(int x,int y) //初始化地图
{
    GoToXYSpace(x,y);
    code[1][1]=9;
    Gotoxy(2,1);
    printf("\1 ");
    SetNum();
}


void LeftKey(int code[][24],int x,int y)  //向左走
{
    int x1,y1; //下个位置的坐标
    x1 = x;
    y1 = y - 1;
    if(code[x1][y1] != 0)
    {
        code[x1][y1] = 9;
        GoToXYSpace(x,y);
        Gotoxy(2*y1,x1);
        printf("\1 ");
    }
}


void UpKey(int code[][24],int x,int y)  //向右走
{
    int x1,y1; //下个位置的坐标
    x1 = x - 1;
    y1 = y;
    if(code[x1][y1] != 0)
    {
        code[x1][y1] = 9;
        Gotoxy(2*y1,x1);
        printf("\1 ");
    }
}


void DownKey(int code[][24],int x,int y) //向下走
{
    int x1,y1; //下个位置的坐标
    x1 = x + 1;
    y1 = y;
    if(code[x1][y1] != 0)
    {
        code[x1][y1] = 9;
        Gotoxy(2*y1,x1);
        printf("\1 ");
    }
}


void RightKey(int code[][24],int x,int y)  //向下走
{
    int x1,y1; //下个位置的坐标
    x1 = x;
    y1 = y + 1;
    if(code[x1][y1] != 0)
    {
        code[x1][y1] = 9;
        Gotoxy(2*y1,x1);
        printf("\1 ");
    }
}


void Hidden()//隐藏光标
{
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cci;
    GetConsoleCursorInfo(hOut,&cci);
    cci.bVisible=0;//赋1为显示,赋0为隐藏
    SetConsoleCursorInfo(hOut,&cci);
}


void main()
{
    int i,j;
    int x,y; //人的位置
    char key; //所按下的方向键
    SetWindow(); //输出初始界面
    Hidden();
    while(1)
    {
        for(i=0; i<17; i++)
        {
            for(j=0; j<24; j++)
                if(code[i][j] == 9)
                {
                    x = i;
                    y = j;
                    key=getch();
                    if(key == 27)
                    {
                        Gotoxy(0,24);
                        printf("退出...\n");
                        return 9;
                    }
                    key = getch();
                    switch(key)
                    {
                    case 72:    //上箭头 0x4800
                        if(code[x-1][y] != 0)
                        {
                            UpKey(code,x,y);
                            GoToXYSpace(x,y);
                            CheckLeftNum(x-1,y);
                        }
                        break;
                    case 75: //左箭头 0x4b00
                        if(code[x][y-1] != 0)
                        {
                            LeftKey(code,x,y);
                            GoToXYSpace(x,y);
                            CheckLeftNum(x,y-1);
                        }
                        break;
                    case 77: //右箭头 0x4d00
                        if(code[x][y+1] != 0)
                        {
                            RightKey(code,x,y);
                            GoToXYSpace(x,y);
                            CheckLeftNum(x,y+1);
                        }
                        break;
                    case 80: //下箭头 0x5000
                        if(code[x+1][y] != 0)
                        {
                            DownKey(code,x,y);
                            GoToXYSpace(x,y);
                            CheckLeftNum(x+1,y);
                        }
                        break;
                    default:
                        break;
                    }
                }
        }


    }
}

0 0