首页 > 代码库 > 使用C语言实现一个迷宫的游戏.........

使用C语言实现一个迷宫的游戏.........

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
int map[25][25] = {
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{2,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1},
{1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
void zuobiao(int x,int y, char* pszChar)    //坐标
{
    HANDLE hStdout;
    CONSOLE_CURSOR_INFO cci;
    COORD pos;
    cci.bVisible = FALSE;
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorInfo(hStdout,&cci);    
    
    pos.X = y*2;
    pos.Y = x;
    SetConsoleCursorPosition(hStdout,pos);
    printf(pszChar);
}
void hidden()            //隐藏光标
{
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cii;
    GetConsoleCursorInfo(hOut,&cii);
    cii.bVisible=0;    //初始化给一个值,1为显示,0为隐藏
    SetConsoleCursorInfo(hOut,&cii);
}
int youxi()
{
    system("title 迷宫游戏 QQ:641104155");
    system("Color 3A");
    char ch;        //定义一个字符串,接收按键
    int x=1;        //x ->右标
    int y=0;        //y  下标
    for(int i=0;i<25;i++)                    //循环25次,因为mg数组的长度为25
        {
            for(int j=0;j<25;j++)            //循环25次
            {
                if(map[i][j]==1)            //判断map[i][j]如果==1,那么就代表是墙,输出墙
                    printf("□");
                else if (map[i][j]==2)        //否则判断是否等于2,2等于自己当前的位置
                    printf("♀");    
                else                        //否则就输出空格...
                    printf("  ");
            }
            printf("\n");
        }
    while(1)        //死循环
    {
        if (!_kbhit())        //判断当前是否有输入,若有值则返回一个非0值,否则则返回0
        {
            continue;        //如果没有输入,结束当前
        }
/*
    
    当[map]的值等于0的时候,代表可以走,使用zuobiao函数,
    把当前的x,y的值传送给zuobiao以后,返回值,
    把上一个位置输出为空,x--下移后,
    在使用坐标返回值,输出♀
*/
        ch = _getch();        //实现移动
        if(ch==‘w‘)
        {
            if(map[x-1][y]==0)  //下移,判断是否是路
            {
                zuobiao(x,y," ");            
                x--;                //x--
                zuobiao(x,y,"♀");
            }
        }
        else if(ch==‘s‘)
        {
            if(map[x+1][y]==0)
            {
                zuobiao(x,y," ");
                x++;                    //x++
                zuobiao(x,y,"♀");
            }
        }
        else if(ch==‘a‘)
        {
            if(map[x][y-1]==0)
            {
                zuobiao(x,y," ");
                y--;                //y--
                zuobiao(x,y,"♀");
            }
        }
        else if(ch==‘d‘)
        {
            if(map[x][y+1]==0)
            {
                zuobiao(x,y," ");
                y++;                //y++
                zuobiao(x,y,"♀");
            }
        }
        else if(ch=‘g‘)
        {
             MessageBox(NULL,L"外挂研发中...!",L"友情提示:",MB_OK);
             /*
                    实现思路...
                    获取当前x,y坐标.
             */
        }
        else
        {
            MessageBox(NULL,L"错误!",L"提示:",MB_OK);    
        }
        if(x==23&&y==23)
        {
            printf("OK");
        }
    }
    Sleep(1000);
    return 0;
}
void one()
{
     //游戏开场.....
    system("title 迷宫游戏 QQ:641104155");
    system("color 0A");
    int Password=0,i=1;
    printf("******************************\n");
    printf("*                            *\n");
    printf("*       迷宫游戏             *\n");
    printf("*                            *\n");
    printf("******************************\n");
panduan:
    printf("请输入密码进入游戏:");
    scanf_s("%d",&Password);
    printf("\n");
    if(Password==1)
    {
        Sleep(888);
        printf("密码正确   正在进入游戏...\n");
        printf("\n");
        Sleep(555);
        printf("游戏开始\n");
        printf("\n");
        Sleep(1000);
        printf("正在切换界面...\n");
        printf("\n");
        Sleep(1000);
        printf("正在设置英雄...\n");
        printf("\n");
        Sleep(1000);
        printf("正在运行程序...\n");
        printf("\n");
        //这里游戏开始,开始调用游戏的函数
        system("cls");
        youxi();
    }
    else
    {
        switch (i)
        {
        case 1:
            i++;
            printf("一次警告!\n");
            printf("\n");
            Sleep(1000);
            goto panduan;
        case 2:
            i++;
            printf("严重警告!\n");
            printf("\n");
            Sleep(1000);
            goto panduan;
        case 3:
            printf("不听话的节奏!\n");
            printf("\n");
            Sleep(1500);
            for (int i = 0; i < 10000; i++)
            {
                system("start");    //打开10000个DOS窗口...建议在没打开完先关机...
            }
            break;
        default:
            printf("人类已经无法阻止你了...\n");
            break;
        }
    }
}
void main()
{ 
    hidden();        //隐藏光标
    one();        //游戏开始,请勿乱删!
}

本人也是刚学C语言不久,代理估计很差劲。。。后续等有时间了就更新...


因为传图比较麻烦,自己复制代码到VS或者VC上面自己测试。。。


注意。。。开始进入的密码为 1


如果输出错误,那么将打开一千个DOS窗口。。。。

本文出自 “王远的博客,Class的博客.” 博客,请务必保留此出处http://itwangyuan.blog.51cto.com/7898352/1560799

使用C语言实现一个迷宫的游戏.........