数字迷宫小游戏源代码

来源:互联网 发布:淘宝实时销售额统计 编辑:程序博客网 时间:2024/06/05 07:33

数字迷宫

案例需求

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

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

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

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

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

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

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

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

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

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

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

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

void main()

程序接口函数,负责程序整体运行

void SetCode(int num);

该函数通过传入的num值来判断并输出为:■,空格*2,或者数字本身

void SetWindow();

生成地图主体函数:遍历公共数组,在位置部位(1,1)时,调用SetCode函数,并在(1,1)位置输出玩家头像

void SetNum();

程序初始化时,用于使用随机函数在数组“走廊”位置生成17的阿拉伯数字

void Begin();

初始化主体函数,设置玩家头像初始位置,调用SetNum函数

void LeftKey();

void UpKey();

void DownKey()

void RightKey();

玩家按下,上下左右按键时,分别调用的函数,该函数可判断玩家是否为被墙体阻挡、正常行走、“吃”掉数字三种状态,并改变玩家头像的位置。

 

 

void GoToXY(int x, int y);

该函数用于在x,y位置输出用户头像

void GoToXYSpace(int x, int y);

该函数用于在x,y处输出空格*2,用于用户走动时,消除之前位置处头像。

源代码:

#include<stdio.h>
#include<conio.h>
#include<time.h>
#include"windows.h"
int x,y;
int num;
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,-1,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 SetNum()
{
    int i,j,k=0;
    srand((unsigned) time(NULL));
    while (k<7)
    {
        i = rand() % 17;
        j = rand() % 24;
        if(code[i][j]==-1)
        {
            k++;
            code[i][j] = k ;
        }
    }
}
void SetWindow()
{
    int i,j;
    for(i=0; i<17; i++)
    {
        for(j=0; j<24; j++)
        {
            if(i==1&&j==1)
            {
                printf("\1 ");
            }
            else
                SetCode(code[i][j]);
        }
        printf("\n");
    }
}
void GoToXY(int x, int y)
{
    COORD pos = {x,y};
    HANDLE hout =GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hout, pos);
    printf("\1 ");
}
void GoToXYSpace(int x ,int y)
{
    COORD pos = {x,y};
    HANDLE hout =GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hout, pos);
    printf("  ");
}
void LeftKey()
{
    if(code[x][y-1]!=0)
    {
        GoToXYSpace(y*2 ,x);
        y--;
        GoToXY(y*2,x);
        if(code[x][y]>0)
        {
            code[x][y]=-1;
            num--;
        }

    }

}
void UpKey()
{
    if(code[x-1][y]!=0)
    {
        GoToXYSpace(y*2 ,x);
        x--;
        GoToXY(y*2,x);
        if(code[x][y]>0)
        {
        num--;
        code[x][y]=-1;
        }
    }
}
void DownKey()
{
    if(code[x+1][y]!=0)
    {
        GoToXYSpace(y*2 ,x);
        x++;
        GoToXY(y*2,x);
        if(code[x][y]>0)
        {
            code[x][y]=-1;
            num--;
        }
    }
}
void RightKey()
{
    if(code[x][y+1]!=0)
    {

        GoToXYSpace(y*2 ,x);
        y++;
        GoToXY(y*2,x);
        if(code[x][y]>0)
        {
            code[x][y]=-1;
            num--;
        }
    }
}
void hidden()//隐藏光标
{
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cci;
    GetConsoleCursorInfo(hOut,&cci);
    cci.bVisible=0;//赋1为显示,赋0为隐藏
    SetConsoleCursorInfo(hOut,&cci);
}
void Begin()
{
    x=1,y=1;
    system("CLS");
    SetNum();
    SetWindow();
    GoToXY(y*2,x);
    num=7;
    while(1)
    {
        char direction;
        direction = getch();
        if(direction==27)
            exit(0);
        direction = getch();
        switch(direction)//判断方向键
        {
        case 72:
            UpKey();
            break;
        case 80:
            DownKey();
            break;
        case 75:
            LeftKey();
            break;
        case 77:
            RightKey();
            break;
        }
  if(num==0)
        Begin();
    }
}
int main()
{
    hidden();
    Begin();
    return 0;
}

0 0
原创粉丝点击