c++ 100行代码写cmd贪吃蛇

今天比较无聊,写了一个贪吃蛇玩玩,只能运行在windows平台下

参考博客:

rand生成随机数: https://blog.csdn.net/qq_37050329/article/details/90478174

不用按下enter获得输入:https://blog.csdn.net/weixin_47212736/article/details/115770857

减少刷新屏幕的闪烁感(或许可以继续基于这个做优化): https://blog.csdn.net/wangxun20081008/article/details/115594058 

 

理解:想写贪吃蛇必须要利用好stl里deque这个容器,这样写起来更方便。

 

#include <iostream>
#include <vector>
#include <deque>
#include <windows.h>
#include <thread>
#include <stdlib.h>
#include"conio.h"
using namespace std;

deque<pair<int, int> > snakePositions;
char input;
pair<int, int> starPosition;
bool exitGame;

void gotoxy(short x, short y) {                    
    COORD pos = {x,y};
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOut, pos);
}

void createARandomStar() {
    starPosition.first = rand() % 18 + 1;
    starPosition.second = rand() % 48 + 1;
}

void printAll(vector<vector<char> > & vvi) {
    gotoxy(0,0);
    for (int i = 0; i < vvi.size(); ++i) {
        for (int j = 0; j < vvi[i].size(); ++j) {
            cout << vvi[i][j];
        }
        cout << endl;
    }
    
} 

void addStarIntoMap(vector<vector<char> > & map) {
    map[starPosition.first][starPosition.second] = 'Q';
}

//draw snake into map
void drawSnakeStarIntoMapAndPrint(vector<vector<char> >  map, deque<pair<int, int> > & snakePositions) {
    int row = snakePositions[0].first;
    int column = snakePositions[0].second;
    map[row][column] = 'O';
    
    for (int i = 1; i < snakePositions.size(); ++i) {
        row = snakePositions[i].first;
        column = snakePositions[i].second;
            map[row][column] = 'o';
    }
    // add star
    addStarIntoMap(map);
    printAll(map);
}     

void drawBorder(vector<vector<char> > & vvi) {
    for (int i = 0; i < vvi[0].size(); ++i) {
        vvi[0][i] = '#';
        vvi[vvi.size() - 1][i] = '#';
    }
    
    for (int i = 0; i < vvi.size(); ++i) {
        vvi[i][0] = '#';
        vvi[i][vvi[0].size() - 1] = '#';
    }
}

void snakePositionsUpdate(deque<pair<int, int> > & snakePositions, char direction) {
    pair<int, int> head(snakePositions[0]);
    
    switch(direction) {
        case 'w' :
            head.first -= 1;
            break;
        case 's' :
            head.first += 1;
            break;
        case 'a' :
            head.second -= 1;
            break;
        case 'd' :
            head.second += 1;
    }
    
    snakePositions.push_front(head);
    
    
    
    if (head != starPosition) {
        snakePositions.pop_back();
    } else {
        createARandomStar();
    } 
    
    for (int i = 1; i < snakePositions.size(); ++i) {
        if (head == snakePositions[i]) exitGame = true;
    }
}

void updateGraph() {
    vector<vector<char> > map(20, vector<char>(50, ' '));
    drawBorder(map);

    while (1) {
        if (exitGame) return;
        snakePositionsUpdate(snakePositions, input);
        drawSnakeStarIntoMapAndPrint(map, snakePositions);
        Sleep(100);
    }
}

int main() {
    exitGame = false;
    pair<int, int> head(1, 1);
    snakePositions.push_back(head);    
    createARandomStar();
    
    //no need to click "enter" after click direction 
    input=getch();
    thread draw_thread(updateGraph);
    while (1) {
        input=getch();
    }
    
    return 0;
} 

 

原文链接: https://www.cnblogs.com/woodx/p/17142075.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    c++ 100行代码写cmd贪吃蛇

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/319431

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月24日 下午3:18
下一篇 2023年2月24日 下午3:18

相关推荐