• 控制台贪吃蛇


    (不完善,只是实现了基本的操作以及分数的统计)
    方法:用一个二维数组保存蛇的坐标,之后依次输出蛇。
    蛇的位置每次只移动一下,蛇头坐标改变,则第一个蛇身坐标等于蛇头,依次下去,第二个蛇身等于第一个蛇身 ,··· 。
    蛇每次移动的过程中之前的蛇尾用空格来擦除。
    以下是代码:代码写得丑,求轻喷!

    #include<iostream>
    #include<Windows.h>
    #include<conio.h>
    #include<time.h>
    using namespace std;
    using std::cout;
    
    typedef enum { UP = 1, DOWN, RIGHT, LEFT } Direct;
    Direct Dir = LEFT;  //蛇初始方向
    int boundX = 40;int boundY = 20; // 界面大小
    int Score = 0;  //分数
    short ScoX = 42;short ScoY = 10; //分数输出位置
    short FoodX, FoodY;
    bool Alive = true;bool NoFood = true;
    int LastX;int LastY; //记录要擦除的蛇坐标
    int len = 3;    //蛇初始长度
    int Snake[200][2];
    
    void Goto_xy(short X, short Y);
    void HideCursor();
    void paint();
    bool logic();
    void Getkey();
    void GameOver();
    void PrintScore();
    void init();
    
    void Paint_Map() //画边框
    {
        for (int y = 0, x = 0; y < boundY; y++) {
            for (x = 0; x < boundX; x++) {
                if (y == 0 || y == boundY - 1)
                    cout << "=";
                else if (x == 0 || x == boundX - 1)
                    cout << "#";
                else cout << " ";
            }
            cout << endl;
        }
        return;
    }
    
    void Goto_xy(short X,short Y)  //移动光标位置
    {
        HANDLE hOut;
        hOut = GetStdHandle(STD_OUTPUT_HANDLE);
        COORD pos = { X,Y};
        SetConsoleCursorPosition(hOut, pos);
    }
    
    void HideCursor() //隐藏光标
    {
        CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
        SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
    }
    
    bool logic()  //游戏大体逻辑
    {
        Sleep(150);
        LastX = Snake[len-2][0]; //更新数组中的蛇各个位置坐标
        LastY = Snake[len - 2][1];
        for (int i = len -2; i >0; --i) {
            Snake[i][0] = Snake[i - 1][0];
            Snake[i][1] = Snake[i - 1][1];
        }
        Snake[0][0] = Snake[len - 1][0];
        Snake[0][1] = Snake[len - 1][1];
        switch (Dir) {      //由蛇的方向来改变蛇头的坐标
        case UP:
            Snake[len-1][1]--;
            break;
        case DOWN:
            Snake[len-1][1]++;
            break;
        case RIGHT:
            Snake[len-1][0]++;
            break;
        case LEFT:
            Snake[len-1][0]--;
            break;
        }
        for (int i = 0; i < len - 1; ++i) { //判断蛇头是否撞到自己
            if (Snake[len - 1][0] == Snake[i][0] && Snake[len - 1][1] == Snake[i][1]) {
                Alive = false;
                break;
            }
        }
        if (Snake[len - 1][0] == FoodX && Snake[len - 1][1] == FoodY) { //蛇头坐标等于食物坐标
            ++len;
            Snake[len - 1][0] = FoodX;
            Snake[len - 1][1] = FoodY;
            Score += 10;
            NoFood = true;
        }
        if (NoFood) {   //如果吃到食物,更新坐标
            int tmpX = Snake[len - 2][0];
            int tmpY = Snake[len - 2][1];
            for (int i = len - 2; i > 0; --i) {
                Snake[i][0] = Snake[i - 1][0];
                Snake[i][1] = Snake[i - 1][1];
            }
            Snake[0][0] = tmpX;
            Snake[0][1] = tmpY;
        }
        if (Snake[len - 1][0] < 1 || Snake[len - 1][0] > boundX - 2 || Snake[len - 1][1] < 1 || Snake[len - 1][1]> boundY - 2) //判断蛇撞墙
            Alive = false;
        return Alive;
    }
    
    void paint()
    {
        Goto_xy(42, 2);
        cout << "W上,A左,S下,D右";  //输出操作方法
        Goto_xy(ScoX,ScoY);
        cout << "您的分数是:" << Score; //输出分数
        Goto_xy(LastX, LastY);  
        cout << " ";    //用空格擦除蛇尾
        for (int i = 0; i < len; ++i) { //遍历数组将蛇打印出来
            Goto_xy(Snake[i][0], Snake[i][1]);
            if (i == len - 1)
                switch (Dir) {
                case UP: cout << "W"; break;
                case DOWN: cout << "S"; break;
                case RIGHT: cout << "D"; break;
                case LEFT: cout << "A"; break;
                }
            else 
                cout << "o";
        }
        if (NoFood) {   //如果食物被吃,则输出新的事物
            srand((unsigned int)time(NULL));
            FoodX = rand() % 38 + 1;
            FoodY = rand() % 18 + 1;
            Goto_xy(FoodX, FoodY);
            cout << "*";
            NoFood = false;
        }
        return;
    }
    
    void GameOver() //游戏结束
    {
        system("cls");
        for (int i = 0; i < 5;++i)
            cout << endl;
        for (int i = 0; i < 29; ++i)
            cout << " ";
        cout << "Gameover and press any key to quit";
        while (1)
            if (_kbhit())
                break;
        return;
    }
    
    void Getkey()
    {
        char key;
        if (_kbhit()){ //获得键盘消息更改蛇位置
            key = _getch();
            switch (key) {
            case 'w':
                Dir = UP;
                break;
            case 'a':
                Dir = LEFT;
                break;
            case 's':
                Dir = DOWN;
                break;      
            case 'd':
                Dir = RIGHT;
                break;
            }
        }
    }
    
    void init()
    {
        len = 3;
        Snake[0][0] = 19;
        Snake[0][1] = 9;
        Snake[1][0] = 20;
        Snake[1][1] = 9;
        Snake[2][0] = 18;
        Snake[2][1] = 9;
    }
    
    int main()
    {
        init();
        HideCursor();
        Paint_Map();
        while(Alive){
            paint();
            Getkey();
            Alive = logic();
        }
        GameOver();
        return 0;
    }
  • 相关阅读:
    从属性文件中读取配置
    Page Object Manager
    在Selenium中使用JavaScriptExecutor处理Ajax调用?
    wait
    常用操作
    Selenium收藏官方网址
    PageObject样例
    解决办法-错误:Access denied for user 'root'@'localhost'
    Struts2中的OGNL详解
    用C++,调用浏览器打开一个网页
  • 原文地址:https://www.cnblogs.com/yinqs/p/12588997.html
Copyright © 2020-2023  润新知