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; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」