zoukankan      html  css  js  c++  java
  • C++实现的控制台-贪吃蛇

    周六终于可以抽出一整段时间了

    想了想就写个贪吃蛇吧    第一次写 差不多下了140行  也不算太多吧

    以后ACM比赛是在做不来就自己打个贪吃蛇玩

    ps:本来想写个项目的 但是为了方便你们阅读 就写在一个文件里面了

    测试了下  无重大BUG     但是 功德圆满 没有测试

    知识点:

    1:刷新窗口 system("cls");

    2: time.h 用来编写时钟系统;

    3:SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);  重置光标位置,即你想在那里输出就把光标移到那个地方输出就可以了

      开始还有一个思路就是 不用数组存地图 只用定位光标来  更新蛇  后面发现有点困难

    4:其他的就是一些C++的基本语法了 

    感兴趣的可以拿下去玩玩

    贴代码:

    //基础知识clock_t clock(void)  返回从程序开始到现在为止的时间单位毫秒 CLOCKS_PER_SEC 转换为秒的单位 相当于1000.0 
    /* 打开exe 后输出普通游戏画面并在地图中间提示 按任意键开始游戏
    */
    #include<iostream>
    #include<windows.h>
    #include<conio.h>
    #include<time.h>
    #include<list>
    #include<string>
    using namespace std;
    class  retro_snake
    {
    private:
        COORD zz;//缓存前进的点用; 
        list<COORD>snake;//保存蛇 
        string ma[15]; //保存游戏画面 
        int direction; //保存现在蛇的前进方向 
        int grates;
        int level;     //游戏等级 
        int rate;      //游戏速率 
    public:
        retro_snake() //初始化游戏 
        {
            level = 0; rate = 800;  grates = 0; direction = 1;
            for (int i = 0; i<15; i++)
            {
                if (i == 0 || i == 14)
                    ma[i] = "###################################";//35
                else
                    ma[i] = "#                                 #";
            }
            zz.X = 17; zz.Y = 7;
            snake.push_front(zz);
        }
        void draw(void) const//绘画游戏界面
        {
            for (int i = 0; i<15; i++)
                cout << ma[i] << endl;
            cout << "得分:" << grates << "等级:" << level << endl;
            cout << "w: 上 s:下 a:左 d:右  空格:暂停 " << endl;
        }
        void creat_food()//生成食物
        {
            int x, y;
            do
            {
                x = rand() % 14 + 1;
                y = rand() % 34 + 1;
            } while (ma[x][y] != ' ');
            ma[x][y] = '$';
        }
        void clock_system(void)//时钟系统 控制游戏进行
        {
            double Start = clock();
            char ch = 'w'; bool flag;//初始化前进方向
            creat_food();
            while (1)
            {
                while ((clock() - Start)<rate && !(flag=_kbhit()));//一秒后自动读取 或者直接有键入 
                Start = clock();
                if (flag)
                {
                    ch = _getch();
                    int direction_;
                    if (ch == ' ')//暂停的功能实现
                    {
                        while (!_kbhit());
                        ch = _getch();
                        Start = clock();
                    }
                    else if (ch == 'r')
                    {
    
                    }
                    switch (ch)
                    {
                    case 'w':direction_ = 1; break;
                    case 'a':direction_ = 3; break;
                    case 's':direction_ = 4; break;
                    case 'd':direction_ = 2; break;
                    default:direction_ = 0;
                    }
                    if (direction_ + direction != 5&&direction_!=0) direction = direction_;//如果输入的方向不和现在前进的方向冲突 就改变
                }
                switch (direction)
                {
                case 1:zz.X = snake.front().X; zz.Y = snake.front().Y - 1; break;
                case 2:zz.X = snake.front().X + 1; zz.Y = snake.front().Y; break;
                case 3:zz.X = snake.front().X - 1; zz.Y = snake.front().Y; break;
                case 4:zz.X = snake.front().X; zz.Y = snake.front().Y + 1; break;
                }
                if (ma[zz.Y][zz.X] == '#' || ma[zz.Y][zz.X] == '.')
                {
                    COORD coord; coord.X = 3; coord.Y = 8;
                    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
                    cout << "游戏结束"; 
                    Sleep(300);
                    break;
                }
                else if (ma[zz.Y][zz.X] == '$')
                {
                    grates++; level = grates / 10; 
                    level = min(5, level);
                    rate = 800 - level * 100;
                    ma[snake.front().Y][snake.front().X] = '.';
                    snake.push_front(zz);
                    ma[snake.front().Y][snake.front().X] = '@';
                    if (snake.size() == 429)
                    {
                        COORD coord; coord.X = 3; coord.Y = 8;
                        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
                        cout << "功德圆满";
                        Sleep(300);
                        break;
                    }
                    creat_food();
                }
                else
                {
                    ma[snake.front().Y][snake.front().X] = '.';
                    snake.push_front(zz);
                    ma[snake.front().Y][snake.front().X] = '@';
                    ma[snake.back().Y][snake.back().X] = ' ';
                    snake.pop_back();
                }
                system("cls");
                draw();
            }
        }
        void start(void)
        {
            draw();
            COORD coord; coord.X = 13; coord.Y = 8;
            SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
            cout << "按任意键开始";
            while (!_kbhit());
                char ch =_getch();
            system("cls");
            draw();
            clock_system();
        }
    };
    int main()
    {
        retro_snake Game;
        Game.start();
        return 0;
    }
  • 相关阅读:
    管理员技术(三): 配置静态网络地址、 使用yum软件源 、 升级Linux内核、查找并处理文件、查找并提取文件内容
    管理员技术(二): 访问练习用虚拟机、 命令行基础技巧 、 挂载并访问光盘设备、ls列表及文档创建、复制删除移动
    管理员技术(一):装机预备技能、安装一台RHEL7虚拟机、使用RHEL7图形桌面、Linux命令行基本操作
    基础(三):yum(RedHat系列)和apt-get(Debian系列 )用法及区别
    基础(二):Linux系统/etc/init.d目录和/etc/rc.local脚本
    基础(一):SCSI硬盘与IDE硬盘有什么区别
    高级运维(六):源码安装Redis缓存服务、常用Redis数据库操作指令、配置Redis主从服务器
    错题
    count 【mysql】
    自连接和子查询
  • 原文地址:https://www.cnblogs.com/Swust-lyon/p/6714025.html
Copyright © 2011-2022 走看看