c++三人/双人贪吃蛇源码,双缓冲,多线程,AI蛇,吃啥长啥


游戏介绍:
在游戏时间结束之前吃尽可能多的食物
尽可能的长(zhang)长(chang)
然后干掉其他的蛇!
只有 1 个幸存者!

作者の坦白:
我只做了游戏的主体部分,,,没做菜单界面,,,,,,,
因为,,我对菜单界面有太多的想法,,,没个把月做不出来"
而且我现在又必须要复习数学和物理,,,,,,,,,,,
而且我感觉用继承和多态来做菜单界面会更简单,,,,,,
但我现在还不太懂继承和多态,,所以我打算把继承和多态学一学之后再来做菜单界面
不过我已经把接口做好了,可以直接在接口里设置玩家数据:
(共三个玩家槽位)

特色功能(相比于传统的控制台贪吃蛇游戏)说明:
1:采用多线程实现流畅的 双/三 人同时操作。在源码的moduleControl()函数里
2:采用了双缓冲,防止眼睛被闪瞎。(双缓冲的缺点就是不能设置其他的输出颜色啊啊啊啊啊!!!所以你看到的只有白色的符号。)
3:在吃了食物,吃掉比自己小的蛇之后有加分的实时显示。在源码的showRTInfo(string whichScreen) //real time info //这个whichScreen指的是缓冲区的意思,因为我用的是双缓冲。
4:AI蛇:AI的难度设计的刚刚好,不那么笨,也不那么聪明。。。。(我不会做那种毫发无损吃全图的贪吃蛇)在源码robotMovingJudge()函数里
5:摒弃了传统的 “头插尾删” 移动方法,而采用了 “遍历操作” 法,传统的 “头插尾删” 不能做到 ‘吃啥长啥’ (看上面的演示你就懂了,以前写过贪吃蛇的朋友肯定懂我在说什么),而现在采用了“遍历”法以后,对每个蛇的身体进行单独的移动,就可以做到‘吃啥长啥’。在源码snakeMoving()函数里
6:加入了多个部分进行游戏数据的显示,上面,左下角,右边,以及右下角的‘成长值’(在成长值达到100时长 1 个长度)。

下面贴代码!!!呜呜呜,肝了15天肝完的,居然免费就放出来了,,一定要点赞啊!!评论也行!!!这是我第一次发博客!!

哦对!我希望大家在看我代码的时候牢记这两句话:
1:you are noy here for code, you are here for ship product ;Jamie Zawinski
2:When you inherit code form someone alse, sometimes it’s faster to write your own than to reuse theirs. because it’s going to take certain amount of time to understand their code and learn how to use it and understand it well enough to be able to debug it;Jamie Zawinski

#include <iostream>
#include <string>
#include <time.h>
#include <Windows.h>
#include <thread>
#include <functional>
#include <tchar.h>
#include <vector>
#include <list>
#include <deque>
#include <map>
#include <mutex>
#include <future>
#include <queue>
#include <conio.h>
#include <assert.h>using namespace std;
enum snake_speed{slow=1,middle=2,fast=4};
enum map_size {mapWidth = 140, mapHigh = 43, playScreenWidth = 120,playScreenHigh = 40, InfoCornerHigh = 34, InfoCornerWidth = 50
};using ifDied = bool;
using ifShowed = bool;
using direction = string;
using diedWay = string;
using gameOverWay = string; //"all snakes died" "time run out"//控制台屏幕缓冲区句柄,创建新的控制台缓冲区
HANDLE hScreenA = CreateConsoleScreenBuffer(GENERIC_WRITE,//定义进程可以往缓冲区写数据FILE_SHARE_WRITE,//定义缓冲区可共享写权限NULL,CONSOLE_TEXTMODE_BUFFER,NULL
);
HANDLE hScreenB = CreateConsoleScreenBuffer(GENERIC_WRITE,//定义进程可以往缓冲区写数据FILE_SHARE_WRITE,//定义缓冲区可共享写权限NULL,CONSOLE_TEXTMODE_BUFFER,NULL
);
void setHScreenSize(HANDLE& hscreen, int width, int high) {//设置缓冲区窗口大小//HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);COORD size = { static_cast<SHORT>(width), static_cast<SHORT>(high) };SetConsoleScreenBufferSize(hscreen, size);//修改屏幕缓冲区SMALL_RECT rc = { 1,1, static_cast<SHORT>(width), static_cast<SHORT>(high) };SetConsoleWindowInfo(hscreen, true, &rc);//修改窗口大小 注意:窗口大小不能超过缓冲区大小,不然修改会失败!system("cls");return;
}//用bind函数把hScreen参数绑定之后在后面操作双缓冲区的时候就可以轻松许多
//用法:printfSXY_A( 要输出的string , X坐标 , Y坐标 )
void printfSXY(string content, SHORT x, SHORT y, HANDLE hscreen) {//我搞了一个下午都没有搞清楚双缓冲怎么改输出颜色,fxxk,//一般情况下用SetConsoleTextAttribute就可以改输出颜色了,但是这个双缓冲fxxk是真蛋疼COORD coord = { x,y };WriteConsoleOutputCharacterA(hscreen, content.c_str(), content.size(), coord, NULL);
}
auto printfSXY_A = bind(&printfSXY, placeholders::_1, placeholders::_2, placeholders::_3, hScreenA);//向缓冲区A输入,用API实现
auto printfSXY_B = bind(&printfSXY, placeholders::_1, placeholders::_2, placeholders::_3, hScreenB);//向缓冲区B输入,用API实现
void setScreen(HANDLE hscreen) {SetConsoleActiveScreenBuffer(hscreen);
}void consolePrintf(string content, int x, int y, int color) {//普通输出函数,用goto实现COORD pos = { static_cast<short>(x),static_cast<short>(y) };HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 获取标准输出设备句柄SetConsoleCursorPosition(hOut, pos);//两个参数分别是指定哪个窗体,具体位置SetConsoleTextAttribute(hOut, color);cout << content << endl;SetConsoleTextAttribute(hOut, 7);
}//这个类作为游戏与用户数据和菜单数据的接口
struct gameIniSet {struct player {//个人数据bool m_ifOnline;string m_playerName;int m_preMaxScore;int m_money;//游戏数据string m_race;//human or robotstring m_headSymbol;string m_bodySymbol;int m_snakeLongth;string m_direction;int m_snakeSpeed;};struct otherSet {int m_gameTimeLeft;//游戏倒计时int m_foodsCoinsides;int m_totalSpeed;string m_language;};player user1;player user2;player user3;otherSet otherSet;
};//kitfunctions,顾名思义,就是函数工具箱的意思,里面用来装常用的函数
//我一般都是把这个kitfunction放头文件里的,但是这里为了省去多传文件的麻烦,就把它放这了
class kitFunctions {private:static int getLocalTime_year();static int getLocalTime_month();static int getLocalTime_day();static int getLocalTime_hour();static int getLocalTime_min();static int getLocalTime_sec();
public://加上static全局可调用static string get_hourMinSec_string();static string get_yearMonthDay_string();
};
int kitFunctions::getLocalTime_year()
{time_t timep;time(&timep);struct tm* time_pointer = localtime(&timep);return 1900 + time_pointer->tm_year;
}
int kitFunctions::getLocalTime_month()
{time_t timep;time(&timep);struct tm* time_pointer = localtime(&timep);return 1 + time_pointer->tm_mon;
}
int kitFunctions::getLocalTime_day()
{time_t timep;time(&timep);struct tm* time_pointer = localtime(&timep);return time_pointer->tm_mday;
}
int kitFunctions::getLocalTime_hour()
{time_t timep;time(&timep);struct tm* time_pointer = localtime(&timep);return time_pointer->tm_hour;
}
int kitFunctions::getLocalTime_min()
{time_t timep;time(&timep);struct tm* time_pointer = localtime(&timep);return time_pointer->tm_min;
}
int kitFunctions::getLocalTime_sec()
{time_t timep;time(&timep);struct tm* time_pointer = localtime(&timep);return time_pointer->tm_sec;
}
string kitFunctions::get_yearMonthDay_string()
{string year, month, day;year = to_string(getLocalTime_year());month = to_string(getLocalTime_month());day = to_string(getLocalTime_day());string temp = "0";if (month.size() < 2)month.insert(0, temp);if (day.size() < 2)day.insert(0, temp);string MixedTime = year + month + day;return MixedTime;
}
string kitFunctions::get_hourMinSec_string()
{string hour, min, sec;hour = to_string(getLocalTime_hour());min = to_string(getLocalTime_min());sec = to_string(getLocalTime_sec());string temp = "0";if (hour.size() < 2)hour.insert(0, temp);if (min.size() < 2)min.insert(0, temp);if (sec.size() < 2)sec.insert(0, temp);string MixedTime = hour + ":" + min + ":" + sec;return MixedTime;
}class snakeGame
{private://其实这个atomic操作完全没有必要,相对于这些偏门的函数,mutex更为简单,,但是为了装逼,我选择用它atomic<bool> m_ifCloseSubtitleLock;atomic<bool> m_ifOkForShowRanking;atomic<bool> m_ifSkipAfterGameScoreCounting;atomic<bool> m_ifShowedAfterGameScoreCounting;mutex m_subtitleLock;mutex m_foodsLock;mutex m_playersLock;mutex m_gameOverLock;bool m_ifCloseSubtitle;bool m_ifShowedLastSurvivalOneInfo;int m_totalSpeed;string m_nowLocalTime;int m_gameTimePast;int m_gameTimeLeft;//游戏倒计时pair<bool, gameOverWay> m_gameOver;//作为多个线程结束的信号class gamePlayer {public:class snake {public:struct snakeBit {string m_symbol;int m_x;int m_y;snakeBit() {}snakeBit(string symbol, int x, int y) :m_symbol(symbol), m_x(x), m_y(y) {}};//pair<>只能带俩参数,,,tuple可以带多个参数tuple<ifDied, ifShowed, diedWay> m_died;//(ifDied:是否死亡,ifShowed:是否已在屏幕上显示过死亡信息,diedway:死亡方式,)deque<snakeBit> m_snakeDeque;direction m_direction;int m_snakeSpeed;//用来控制速度,分int m_speedSum;//又是双缓冲又是多线程又要做这个速度模块只能用这种很猥琐的方法了.bool m_moving;//用来搞不同的蛇不同速度的功能bool m_ifOkForsnakeMoving;//防止因按键过快导致的 hit himself 问题int m_snakeLongth;int m_snakeGrowthValue;string m_headSymbol;string m_bodySymbol;bool operator> (snake anotherSnake) {if (this->m_snakeLongth > anotherSnake.m_snakeLongth)return true;else return false;}snake(){}};bool m_ifOnline;string m_race;snake m_snake;string m_playerName;int m_preMaxScoreOfSnake;int m_nowScore;int m_money;gamePlayer() {}};vector<gamePlayer> gamePlayers;struct food {int m_x;int m_y;string m_foodType;string m_symbol;int m_growValue;int m_scoreAdding;int m_moneyAdding;string m_feature;food() {}food(int x, int y) :m_x(x), m_y(y) {}};list<unique_ptr<food>> m_foods;food smallFood;food middleFood;food moneyFood;food treasureFood;food squareFood;food circleFood;food pentacleFood;//pentacle:五角星int m_foodCoinsides;int m_theWattingAfterAllFoodsIsEatten;//这只是一个用来计数的int m_speakingCounting;//这也是一个用来计数的bool m_ifSaidTheLastSentence;//这也是一个用来计数的int m_endUpDrawCounting;//这也是一个用来计数的bool m_ifEndUpDraw;//这也是一个用来计数的struct growthValueBlock {string m_symbol;int m_x;int m_y;growthValueBlock(string symbol, int x, int y) :m_symbol(symbol), m_x(x), m_y(y) {}};using myGVB = growthValueBlock;vector<myGVB> m_userA_GUB_vector;deque<string> m_lowerLeftConnerInfos;int m_centerScreenInfo;//用来表示屏幕中间的信息显示位置 velue//fxxk!!!!右上角玩家姓名下面的PMScore是PreMaxScore的意思,但是因为这段字符串太tm长了//,如果让它显示出来就爆出控制台了,所以我只好加上了字幕功能, 真是太操蛋了, ╮(╯▽╰)╭struct subtitle {string m_content;string m_sentence;//sentenc才是真正show出来的内容int m_y;//在第几行显示int m_x;//在第几列开始显示,默认为 playScreenWidth - 4//secondsAfterBegin这个功能还没做出来20200703,因为做出来了意义也不大。。。。int m_secondsAfterBegin;//下面的 m_startPos,m_strLongth,m_strSize都只是一些操蛋的计算参数//,除非你是无聊到爆炸的数学做题家,否则我还是建议你不要管这个subtitle怎么实现的//如果有想加的subtitle直接去iniAttributes()里面设置就行了,int m_startPos;int m_strLongth;int m_strSize;bool m_haveShowed;subtitle(string content, int y, int secondsAfterBegin) {//传入的content必须要注意“ 2 格一个字” 这个原则m_content = content;m_y = y;m_secondsAfterBegin = secondsAfterBegin;//下面这些默认值自然有它的道理,都是我慢慢调的,,深究真没什么意义m_x = playScreenWidth - 4;m_startPos = 0;m_strLongth = 2;m_strSize = content.size();m_haveShowed = false;}};vector<subtitle> m_subtitles;vector<pair<int,string>> m_ranking;//屏幕上面的实时排名struct coordinate {int m_x;int m_y;};vector<tuple<int,int, string, int>>m_RTInfos;//(int x,int y,string massage,int showedtimes)map<int,string>m_theLastLivingRobotIsGoingToSay;//以逗号为单位"ohhhhh,only me???,oh,I BEGGING you,  DO NOT close the window,you know...bool m_ifOkToSayTheLastLivingRobotIsGoingToSay;    //,since i'm merely just , a process ,....,and all my life span is , the time when the , program is ranningbool m_ifSaidTheLastLivingRobotIsGoingToSay;    //,I just want to live longer...,......,"struct rankingData {string m_champion;string m_rannerUp;string m_secondRannerUp;};rankingData m_rankingData;
public:snakeGame(gameIniSet & gameIniset);void moduleShow();void moduleControl();void afterGameScoreCounting();//结束游戏后的游戏计分void press_TAB_to_skip();void iniAttributes(const gameIniSet& gameIniset);void iniSystem();void iniMap(string whichScreen);void iniSnakes();void clearScreen(string whichScreen);//像这种whichScreen就用不着加&了void showFoods(string whichScreen);void showSnakes(string whichScreen);void showScore(string whichScreen);void showTimePast(string whichScreen);void showSubtitle(string whichScreen);void showRanking(string whichScreen);void showDeadInfo();void showLowerLeftConnerInfo(string massage);void addRTInfo(gamePlayer::snake& snake, string& massage);void showRTInfo(string whichScreen);//show real time information 实时信息显示void showGameOver(gameOverWay gameOverWay);//包括所有食物被吃完后的成功void foodsCreating();void robotMovingJudge();void snakeMoving();void subtitleThread();//因为字幕是延迟显示的,不与主线程同步,所以这里再开一条线程void RankingThread();void afterMovingJudge();void user1StrockJudge();void user2StrockJudge();void user3StrockJudge();
};
void snakeGame::iniAttributes(const gameIniSet& gameIniset) {//foodssmallFood.m_foodType = "smallFood";smallFood.m_symbol = "o";//horizontal size:1;smallFood.m_growValue = 10;smallFood.m_scoreAdding = 10;smallFood.m_moneyAdding = 0;middleFood.m_foodType = "middleFood";middleFood.m_symbol = "O";//horizontal size:1;middleFood.m_growValue = 20;middleFood.m_scoreAdding = 20;middleFood.m_moneyAdding = 0;moneyFood.m_foodType = "moneyFood";moneyFood.m_symbol = "#";//horizontal size:1;moneyFood.m_growValue = 0;moneyFood.m_scoreAdding = 0;moneyFood.m_moneyAdding = 10;treasureFood.m_foodType = "treasureFood";treasureFood.m_symbol = "$";//horizontal size:2;treasureFood.m_growValue = 1;treasureFood.m_scoreAdding = 0;treasureFood.m_moneyAdding = 20;squareFood.m_foodType = "squareFood";squareFood.m_symbol = "■";//horizontal size:2;squareFood.m_growValue = 0;squareFood.m_scoreAdding = 50;squareFood.m_moneyAdding = 0;circleFood.m_foodType = "circleFood";circleFood.m_symbol = "●";//horizontal size:2;circleFood.m_growValue = 0;circleFood.m_scoreAdding = 50;circleFood.m_moneyAdding = 0;pentacleFood.m_foodType = "pentacleFood";pentacleFood.m_symbol = "★";//horizontal size:2;pentacleFood.m_growValue = 0;pentacleFood.m_scoreAdding = 50;pentacleFood.m_moneyAdding = 0;//snakegamePlayer player1;gamePlayer player2;gamePlayer player3;//关于这个get<>是用于读取tuple类型的列表的,和pair类似。get<0>(player1.m_snake.m_died) = false;get<1>(player1.m_snake.m_died) = false;player1.m_ifOnline = gameIniset.user1.m_ifOnline;player1.m_race = gameIniset.user1.m_race;player1.m_snake.m_headSymbol = gameIniset.user1.m_headSymbol;//the size of symbol must be 2 bytesplayer1.m_snake.m_bodySymbol = gameIniset.user1.m_bodySymbol;player1.m_snake.m_snakeLongth = gameIniset.user1.m_snakeLongth;player1.m_snake.m_direction = gameIniset.user1.m_direction;player1.m_playerName = gameIniset.user1.m_playerName;player1.m_preMaxScoreOfSnake = gameIniset.user1.m_preMaxScore;player1.m_money = gameIniset.user1.m_money;player1.m_snake.m_snakeSpeed = gameIniset.user1.m_snakeSpeed;player1.m_nowScore = 0;player1.m_snake.m_moving = false;player1.m_snake.m_ifOkForsnakeMoving = true;player1.m_snake.m_speedSum = 0;get<0>(player2.m_snake.m_died) = false;get<1>(player2.m_snake.m_died) = false;player2.m_ifOnline = gameIniset.user2.m_ifOnline;player2.m_race = gameIniset.user2.m_race;player2.m_snake.m_headSymbol = gameIniset.user2.m_headSymbol;//the size of symbol must be 2 bytesplayer2.m_snake.m_bodySymbol = gameIniset.user2.m_bodySymbol;player2.m_snake.m_snakeLongth = gameIniset.user2.m_snakeLongth;player2.m_snake.m_direction = gameIniset.user2.m_direction;player2.m_playerName = gameIniset.user2.m_playerName;player2.m_preMaxScoreOfSnake = gameIniset.user2.m_preMaxScore;player2.m_money = gameIniset.user2.m_money;player2.m_snake.m_snakeSpeed = gameIniset.user2.m_snakeSpeed;player2.m_nowScore = 0;player2.m_snake.m_moving = false;player2.m_snake.m_ifOkForsnakeMoving = true;player2.m_snake.m_speedSum = 0;get<0>(player2.m_snake.m_died) = false;get<1>(player2.m_snake.m_died) = false;player3.m_ifOnline = gameIniset.user3.m_ifOnline;player3.m_race = gameIniset.user3.m_race;player3.m_snake.m_headSymbol = gameIniset.user3.m_headSymbol;//the size of symbol must be 2 bytesplayer3.m_snake.m_bodySymbol = gameIniset.user3.m_bodySymbol;player3.m_snake.m_snakeLongth = gameIniset.user3.m_snakeLongth;player3.m_snake.m_direction = gameIniset.user3.m_direction;player3.m_playerName = gameIniset.user3.m_playerName;player3.m_preMaxScoreOfSnake = gameIniset.user3.m_preMaxScore;player3.m_money = gameIniset.user3.m_money;player3.m_snake.m_snakeSpeed = gameIniset.user3.m_snakeSpeed;player3.m_nowScore = 0;player3.m_snake.m_moving = false;player3.m_snake.m_ifOkForsnakeMoving = true;player3.m_snake.m_speedSum = 0;gamePlayers.emplace_back(player1);gamePlayers.emplace_back(player2);gamePlayers.emplace_back(player3);//这种写法也可以//gamePlayers.push_back(move(player1));//gamePlayers.push_back(move(player2));//gamePlayers.push_back(move(player3));//subtitle//可以在这里加预先的字幕string explainA1 = "同志们好↖(^ω^)↗,这段废话,没错,就是你现在看到的这段废话,,,额,我是想解释一下,这个";//这段话有点长,必须分开写string explainA2 = ",,右上角在玩家姓名下面的 “PMScore” 其实是 ‘PreMaxScore’ 的意思,,,,";string explainA3 = ",,,哎,没办法≡(▔﹏▔)≡,因为 PreMaxScore 这个字符串 ♂ 太长了, ";string explainA4 = "如果让它显示出来就爆出控制台了,所以我只好加上了这句说明。。。。";string explainA5 = "我的QQ号是  1668568309  ,,,我很期待大家加我呢o(≥ω≤)o !!!";string myExplainA = explainA1 + explainA2 + explainA3 + explainA4 + explainA5;string myExplainB = "(你可以按' tab ' 键立刻终止这段话的显示)";subtitle subtitle1(myExplainA, playScreenHigh - 1,5);//(string content,int y ,int secondsAfterBegin)subtitle subtitle2(myExplainB, playScreenHigh - 2,6);m_subtitles.emplace_back(subtitle1);m_subtitles.emplace_back(subtitle2);//otherSettingm_gameTimeLeft = gameIniset.otherSet.m_gameTimeLeft;//游戏倒计时//其他m_ifCloseSubtitle = false;m_ifOkForShowRanking = true;m_ifShowedLastSurvivalOneInfo = false;m_theWattingAfterAllFoodsIsEatten = 0;m_speakingCounting = 0;m_ifSaidTheLastSentence = false;m_endUpDrawCounting = 0;m_ifEndUpDraw = false;m_ifOkToSayTheLastLivingRobotIsGoingToSay = false;m_ifSaidTheLastLivingRobotIsGoingToSay = false;m_ifSkipAfterGameScoreCounting = false;m_ifShowedAfterGameScoreCounting = false;m_theLastLivingRobotIsGoingToSay.emplace(pair<int, string>(1, "ohhhhh"));m_theLastLivingRobotIsGoingToSay.emplace(pair<int, string>(2, "only me???"));m_theLastLivingRobotIsGoingToSay.emplace(pair<int, string>(3, "oh"));m_theLastLivingRobotIsGoingToSay.emplace(pair<int, string>(4, "I BEGGING you"));m_theLastLivingRobotIsGoingToSay.emplace(pair<int, string>(5, "DO NOT close the window"));m_theLastLivingRobotIsGoingToSay.emplace(pair<int, string>(6, "you know..."));m_theLastLivingRobotIsGoingToSay.emplace(pair<int, string>(7, "since i'm merely just "));m_theLastLivingRobotIsGoingToSay.emplace(pair<int, string>(8, " a process"));m_theLastLivingRobotIsGoingToSay.emplace(pair<int, string>(9, "...."));m_theLastLivingRobotIsGoingToSay.emplace(pair<int, string>(10, "and all my life span is "));m_theLastLivingRobotIsGoingToSay.emplace(pair<int, string>(11, "the time when the "));m_theLastLivingRobotIsGoingToSay.emplace(pair<int, string>(12, "program's ranning"));m_theLastLivingRobotIsGoingToSay.emplace(pair<int, string>(13, "I just want to live longer..."));m_theLastLivingRobotIsGoingToSay.emplace(pair<int, string>(14, "......"));for (int i = 0, j = 4; i < 6; ++i, j += 2) {//预先装好右下角成长值的符号m_userA_GUB_vector.emplace_back("■", playScreenWidth + j, playScreenHigh - 7);}
}
void snakeGame::iniSystem() {//随机种子srand(static_cast<unsigned int>(time(NULL)));//设置两个缓冲区的屏幕大小setHScreenSize(hScreenA, mapWidth, mapHigh);setHScreenSize(hScreenB, mapWidth, mapHigh);隐藏两个缓冲区的光标CONSOLE_CURSOR_INFO cci;cci.bVisible = 0;cci.dwSize = 1;SetConsoleCursorInfo(hScreenA, &cci);SetConsoleCursorInfo(hScreenB, &cci);
}
void snakeGame::iniMap(string whichScreen) {//贪吃蛇游戏边框//本来这段代码挺好读的,但自从加了左下角信息框的功能之后,,,就变得特操蛋for (int i = 4; i < playScreenWidth - 2; i += 2) {//top of the mapif (whichScreen == "A")printfSXY_A("▁", i, 2);if (whichScreen == "B")printfSXY_B("▁", i, 2);}for (int j = 3; j < InfoCornerHigh; ++j) {//left columnif (whichScreen == "A")printfSXY_A("▕", 2, j);if (whichScreen == "B")printfSXY_B("▕", 2, j);}for (int j = 3; j < playScreenHigh; ++j) {//right columnif (whichScreen == "A")printfSXY_A("▏", playScreenWidth - 2, j);if (whichScreen == "B")printfSXY_B("▏", playScreenWidth - 2, j);}for (int i = InfoCornerWidth + 2; i < playScreenWidth - 2; i += 2) {//bottom of the mapif (whichScreen == "A")printfSXY_A("▔", i, playScreenHigh);if (whichScreen == "B")printfSXY_B("▔", i, playScreenHigh);}//左下角信息框for (int i = 4; i < InfoCornerWidth; i += 2) {//up of the LowerLeftConnerif (whichScreen == "A")printfSXY_A("▔", i, InfoCornerHigh);if (whichScreen == "B")printfSXY_B("▔", i, InfoCornerHigh);}for (int j = InfoCornerHigh; j < playScreenHigh; ++j) {//right of the LowerLeftConnerif (whichScreen == "A")printfSXY_A("▕", InfoCornerWidth, j);if (whichScreen == "B")printfSXY_B("▕", InfoCornerWidth, j);}if (whichScreen == "A")printfSXY_A("█", InfoCornerWidth, InfoCornerHigh);//upper right of the LowerLeftConnerif (whichScreen == "B")printfSXY_B("█", InfoCornerWidth, InfoCornerHigh);//计分板边框for (int j = 3; j < playScreenHigh; ++j) {if (whichScreen == "A")printfSXY_A("|", playScreenWidth + 1, j);if (whichScreen == "B")printfSXY_B("|", playScreenWidth + 1, j);}for (int j = 3; j < playScreenHigh; ++j) {if (whichScreen == "A")printfSXY_A("|", mapWidth - 2, j);if (whichScreen == "B")printfSXY_B("|", mapWidth - 2, j);}for (int i = playScreenWidth + 2; i < mapWidth - 2; i += 2) {if (whichScreen == "A")printfSXY_A("▂", i, 2);if (whichScreen == "B")printfSXY_B("▂", i, 2);}for (int i = playScreenWidth + 2; i < mapWidth - 2; i += 2) {if (whichScreen == "A")printfSXY_A("═", i, playScreenHigh - 16);if (whichScreen == "B")printfSXY_B("═", i, playScreenHigh - 16);}for (int i = playScreenWidth + 2; i < mapWidth - 2; i += 2) {if (whichScreen == "A")printfSXY_A("﹊", i, playScreenHigh);if (whichScreen == "B")printfSXY_B("﹊", i, playScreenHigh);}//显示计分板不变的内容(最上面的SCORE COUNTS 和玩家姓名)string scoreBroad = "SCORE COUNTS";if (whichScreen == "A")printfSXY_A(scoreBroad, playScreenWidth + 4, 2);if (whichScreen == "B")printfSXY_B(scoreBroad, playScreenWidth + 4, 2);int y = 3;//player名字的y轴坐标for (auto& player : gamePlayers) {if (player.m_ifOnline == true) {string playerName = player.m_playerName;if (whichScreen == "A")printfSXY_A(playerName, playScreenWidth + 6, y);if (whichScreen == "B")printfSXY_B(playerName, playScreenWidth + 6, y);//fxxkstring preMaxScore = "  PMScore: " + to_string(player.m_preMaxScoreOfSnake);if (whichScreen == "A")printfSXY_A(preMaxScore, playScreenWidth + 2, y + 1);if (whichScreen == "B")printfSXY_B(preMaxScore, playScreenWidth + 2, y + 1);}y += 7;//players的名字之间要隔这么几行来写信息}//右下角成长值边框(共三个)//我懒得用算法优化。。。这样可以多水几十行代码(逃... //主要是因为这么散着写开了之后确实更方便阅读(如果用for写的话那就太操蛋了)if (gamePlayers[0].m_ifOnline == true) {int x = playScreenWidth + ((mapWidth - playScreenWidth) / 2 - gamePlayers[2].m_playerName.size() / 2);if (whichScreen == "A")printfSXY_A(gamePlayers[0].m_playerName, x, playScreenHigh - 15);if (whichScreen == "B")printfSXY_B(gamePlayers[0].m_playerName, x, playScreenHigh - 15);}for (int i = playScreenWidth + 4; i < mapWidth - 4; i += 2) {if (whichScreen == "A")printfSXY_A("▁", i, playScreenHigh - 13);if (whichScreen == "B")printfSXY_B("▁", i, playScreenHigh - 13);}for (int i = playScreenWidth + 4; i < mapWidth - 4; i += 2) {if (whichScreen == "A")printfSXY_A("▔", i, playScreenHigh - 11);if (whichScreen == "B")printfSXY_B("▔", i, playScreenHigh - 11);}if (whichScreen == "A")printfSXY_A("▕", playScreenWidth + 2, playScreenHigh - 12);if (whichScreen == "B")printfSXY_B("▕", playScreenWidth + 2, playScreenHigh - 12);if (whichScreen == "A")printfSXY_A("▏", mapWidth - 4, playScreenHigh - 12);if (whichScreen == "B")printfSXY_B("▏", mapWidth - 4, playScreenHigh - 12);if (gamePlayers[1].m_ifOnline == true) {int x = playScreenWidth + ((mapWidth - playScreenWidth) / 2 - gamePlayers[2].m_playerName.size() / 2);if (whichScreen == "A")printfSXY_A(gamePlayers[1].m_playerName, x, playScreenHigh - 10);if (whichScreen == "B")printfSXY_B(gamePlayers[1].m_playerName, x, playScreenHigh - 10);}for (int i = playScreenWidth + 4; i < mapWidth - 4; i += 2) {if (whichScreen == "A")printfSXY_A("▁", i, playScreenHigh - 8);if (whichScreen == "B")printfSXY_B("▁", i, playScreenHigh - 8);}for (int i = playScreenWidth + 4; i < mapWidth - 4; i += 2) {if (whichScreen == "A")printfSXY_A("▔", i, playScreenHigh - 6);if (whichScreen == "B")printfSXY_B("▔", i, playScreenHigh - 6);}if (whichScreen == "A")printfSXY_A("▕", playScreenWidth + 2, playScreenHigh - 7);if (whichScreen == "B")printfSXY_B("▕", playScreenWidth + 2, playScreenHigh - 7);if (whichScreen == "A")printfSXY_A("▏", mapWidth - 4, playScreenHigh - 7);if (whichScreen == "B")printfSXY_B("▏", mapWidth - 4, playScreenHigh - 7);if (gamePlayers[2].m_ifOnline == true) {int x = playScreenWidth + ((mapWidth - playScreenWidth) / 2 - gamePlayers[2].m_playerName.size() / 2);if (whichScreen == "A")printfSXY_A(gamePlayers[2].m_playerName, x, playScreenHigh - 5);if (whichScreen == "B")printfSXY_B(gamePlayers[2].m_playerName, x, playScreenHigh - 5);}for (int i = playScreenWidth + 4; i < mapWidth - 4; i += 2) {if (whichScreen == "A")printfSXY_A("▁", i, playScreenHigh - 3);if (whichScreen == "B")printfSXY_B("▁", i, playScreenHigh - 3);}for (int i = playScreenWidth + 4; i < mapWidth - 4; i += 2) {if (whichScreen == "A")printfSXY_A("▔", i, playScreenHigh - 1);if (whichScreen == "B")printfSXY_B("▔", i, playScreenHigh - 1);}if (whichScreen == "A")printfSXY_A("▕", playScreenWidth + 2, playScreenHigh - 2);if (whichScreen == "B")printfSXY_B("▕", playScreenWidth + 2, playScreenHigh - 2);if (whichScreen == "A")printfSXY_A("▏", mapWidth - 4, playScreenHigh - 2);if (whichScreen == "B")printfSXY_B("▏", mapWidth - 4, playScreenHigh - 2);}
void snakeGame::iniSnakes() {//player1's snake is located at 30 for x(when playScreenWidth is 120)if (gamePlayers[0].m_ifOnline == true) {gamePlayers[0].m_snake.m_snakeDeque.emplace_front(gamePlayers[0].m_snake.m_headSymbol, playScreenWidth / 4, playScreenHigh / 2);for (int i = 0; i < gamePlayers[0].m_snake.m_snakeLongth; ++i) {gamePlayers[0].m_snake.m_snakeDeque.emplace_back(gamePlayers[0].m_snake.m_bodySymbol, playScreenWidth / 4, playScreenHigh / 2);}}//player2's snake is located at 30 for x(when playScreenWidth is 120)if (gamePlayers[1].m_ifOnline == true) {gamePlayers[1].m_snake.m_snakeDeque.emplace_front(gamePlayers[1].m_snake.m_headSymbol, playScreenWidth / 2, playScreenHigh / 2);for (int i = 0; i < gamePlayers[1].m_snake.m_snakeLongth; ++i) {gamePlayers[1].m_snake.m_snakeDeque.emplace_back(gamePlayers[1].m_snake.m_bodySymbol, playScreenWidth / 2, playScreenHigh / 2);}}//player3's snake is located at 30 for x(when playScreenWidth is 120)if (gamePlayers[2].m_ifOnline == true) {gamePlayers[2].m_snake.m_snakeDeque.emplace_front(gamePlayers[2].m_snake.m_headSymbol, playScreenWidth - playScreenWidth / 4, playScreenHigh / 2);for (int i = 0; i < gamePlayers[2].m_snake.m_snakeLongth; ++i) {gamePlayers[2].m_snake.m_snakeDeque.emplace_back(gamePlayers[2].m_snake.m_bodySymbol, playScreenWidth - playScreenWidth / 4, playScreenHigh / 2);}}
}
void snakeGame::clearScreen(string whichScreen){//主屏幕清屏函数,因使用system("cls");再打印框架有一定几率造成框架上移一行的错误,所以单独编写清屏函数//清屏的时候要小心不能清到左下角的信息框string voidScreenLineA(playScreenWidth - 6, ' ');string voidScreenLineB(playScreenWidth - 6 - (InfoCornerWidth - 2), ' ');for (int i = 3; i < InfoCornerHigh; i++){if (whichScreen == "A")printfSXY_A(voidScreenLineA, 4, i);if (whichScreen == "B")printfSXY_B(voidScreenLineA, 4, i);}for (int i = InfoCornerHigh; i < playScreenHigh; i++){if (whichScreen == "A")printfSXY_A(voidScreenLineB, InfoCornerWidth + 2, i);if (whichScreen == "B")printfSXY_B(voidScreenLineB, InfoCornerWidth + 2, i);}//clean gorwthBlocks at the lower right cornerstring voidScreenLine(12, ' ');if (whichScreen == "A")printfSXY_A(voidScreenLine, playScreenWidth + 4, playScreenHigh - 2);if (whichScreen == "B")printfSXY_B(voidScreenLine, playScreenWidth + 4, playScreenHigh - 2);if (whichScreen == "A")printfSXY_A(voidScreenLine, playScreenWidth + 4, playScreenHigh - 4);if (whichScreen == "B")printfSXY_B(voidScreenLine, playScreenWidth + 4, playScreenHigh - 4);if (whichScreen == "A")printfSXY_A(voidScreenLine, playScreenWidth + 4, playScreenHigh - 7);if (whichScreen == "B")printfSXY_B(voidScreenLine, playScreenWidth + 4, playScreenHigh - 7);if (whichScreen == "A")printfSXY_A(voidScreenLine, playScreenWidth + 4, playScreenHigh - 9);if (whichScreen == "B")printfSXY_B(voidScreenLine, playScreenWidth + 4, playScreenHigh - 9);if (whichScreen == "A")printfSXY_A(voidScreenLine, playScreenWidth + 4, playScreenHigh - 12);if (whichScreen == "B")printfSXY_B(voidScreenLine, playScreenWidth + 4, playScreenHigh - 12);if (whichScreen == "A")printfSXY_A(voidScreenLine, playScreenWidth + 4, playScreenHigh - 14);if (whichScreen == "B")printfSXY_B(voidScreenLine, playScreenWidth + 4, playScreenHigh - 14);//清理左上角的游戏时间if (whichScreen == "A")printfSXY_A(voidScreenLine, 3, 1);if (whichScreen == "B")printfSXY_B(voidScreenLine, 3, 1);
}
void snakeGame::showFoods(string whichScreen) {lock_guard<mutex> foodsLock(m_foodsLock);if (m_foods.empty())return;for (auto& food : m_foods) {if (whichScreen == "A")printfSXY_A(food->m_symbol, food->m_x, food->m_y);if (whichScreen == "B")printfSXY_B(food->m_symbol, food->m_x, food->m_y);}
}
void snakeGame::showSnakes(string whichScreen) {lock_guard<mutex> playersLock(m_playersLock);for (auto & player : gamePlayers) {if (player.m_ifOnline == false) {continue;}for (auto & snakebit : player.m_snake.m_snakeDeque) {if (whichScreen == "A") printfSXY_A(snakebit.m_symbol, snakebit.m_x, snakebit.m_y);if (whichScreen == "B") printfSXY_B(snakebit.m_symbol, snakebit.m_x, snakebit.m_y);}}
}
void snakeGame::showScore(string whichScreen) {int y = 3;//player名字的y轴坐标lock_guard<mutex> playersLock(m_playersLock);for (auto& player : gamePlayers) {if (player.m_ifOnline == false) continue;string nowScore = "NowScore: " + to_string(player.m_nowScore);if (whichScreen == "A")printfSXY_A(nowScore, playScreenWidth + 4, y + 2);if (whichScreen == "B")printfSXY_B(nowScore, playScreenWidth + 4, y + 2);string money = "Money: " + to_string(player.m_money);if (whichScreen == "A")printfSXY_A(money, playScreenWidth + 4, y + 3);if (whichScreen == "B")printfSXY_B(money, playScreenWidth + 4, y + 3);string longth = "Longth: " + to_string(player.m_snake.m_snakeLongth);if (whichScreen == "A")printfSXY_A(longth, playScreenWidth + 4, y + 4);if (whichScreen == "B")printfSXY_B(longth, playScreenWidth + 4, y + 4);y += 7;}//右下角成长值的实现y = playScreenHigh - 12;//最上面的成长值for (auto& player : gamePlayers) {if (player.m_ifOnline == false ) continue;string growthValueA = to_string(player.m_snake.m_snakeGrowthValue) + "/100";if (whichScreen == "A")printfSXY_A(growthValueA, playScreenWidth + 7, y-2);if (whichScreen == "B")printfSXY_B(growthValueA, playScreenWidth + 7, y-2);//这些计算方式真的很操蛋,你真的读不懂的,这些计算的东西都是慢慢调试出来的,不是读出来的//,Jamie Zawinski 说过,遇到这种恶心的代码时因该做的事不是重读,而是自己再实现一次这种功能//when you inherit code from someone else ,sometimes it's faster to write//your own than to reuse theirs.int nomber = player.m_snake.m_snakeGrowthValue;if (player.m_snake.m_snakeGrowthValue >= 98) {nomber = 97;//因为98/14==7,但是m_userA_GUB_vector只有6个数据,你看,我就说很操蛋吧}int gorwthBlock_nom = nomber / 14;int i = 0;auto itBegin = m_userA_GUB_vector.begin();while (i < gorwthBlock_nom) {if (whichScreen == "A")printfSXY_A(itBegin->m_symbol, itBegin->m_x, y);if (whichScreen == "B")printfSXY_B(itBegin->m_symbol, itBegin->m_x, y);++i;++itBegin;}y += 5;}
}
void snakeGame::showTimePast(string whichScreen) {int timeLeft = m_gameTimeLeft - m_gameTimePast;if (timeLeft < 1) {m_gameOver.first = true;m_gameOver.second = "time run out";showLowerLeftConnerInfo("time's running out");}int minites = timeLeft / 60;int seconds = timeLeft % 60;string minites_str = to_string(minites);string seconds_str = to_string(seconds);string temp = "0";if (minites_str.size() == 1) {minites_str.insert(0, temp);}if (seconds_str.size() == 1) {seconds_str.insert(0, temp);}string clock = minites_str + ":" + seconds_str;string TheRestTime = " The rest time : ";if (whichScreen == "A")printfSXY_A(TheRestTime + clock, 3, 1);if (whichScreen == "B")printfSXY_B(TheRestTime + clock, 3, 1);
}
void snakeGame::showSubtitle(string whichScreen) {lock_guard<mutex> subtitleLock(m_subtitleLock);if (m_ifCloseSubtitle == true)return;for (auto& subtitle : m_subtitles) {if (subtitle.m_haveShowed == true) continue;if (whichScreen == "A")printfSXY_A(subtitle.m_sentence, subtitle.m_x, subtitle.m_y);if (whichScreen == "B")printfSXY_B(subtitle.m_sentence, subtitle.m_x, subtitle.m_y);}
}
void snakeGame::showRanking(string whichScreen) {//其实这里不用加string whichScreen,但是为了好看我还是加了if (m_ifOkForShowRanking == true) {pair<int, string> championOfScore;pair<int, string> rannerUpOfScore;pair<int, string> secondRannerUpOfScore;pair<int, string> championOfMoney;pair<int, string> rannerUpOfMoney;pair<int, string> secondRannerUpOfMoney;pair<int, string> championOfLongth;pair<int, string> rannerUpOfLongth;pair<int, string> secondRannerUpOfLongth;championOfScore.second = "现在分数最高的人是 : ";rannerUpOfScore.second = "现在分数排第二的人是 : ";secondRannerUpOfScore.second = "现在分数排第三的人是 : ";championOfMoney.second = "现在钱最多的人是 : ";rannerUpOfMoney.second = "现在第二有钱的人是 : ";secondRannerUpOfMoney.second = "现在第三有钱的人是 : ";championOfLongth.second = "现在最长 ♂ 的蛇是 : ";rannerUpOfLongth.second = "现在第二长 ♂ 的蛇是 : ";secondRannerUpOfLongth.second = "现在第三长 ♂ 的蛇是 : ";vector<pair<int, string>> arrayForRanking;lock_guard<mutex> playersLock(m_playersLock);//依score排序for (auto& player : gamePlayers) {if (player.m_ifOnline == false) continue;arrayForRanking.emplace_back(make_pair(player.m_nowScore, player.m_playerName));}sort(arrayForRanking.begin(), arrayForRanking.end(), [](pair<int, string> a, pair<int, string> b) {if (a.first > b.first)return true;//从大到小排序else return false;});switch (arrayForRanking.size()) {case 1: {championOfScore.first = arrayForRanking[0].first;championOfScore.second += arrayForRanking[0].second;break;}case 2: {championOfScore.first = arrayForRanking[0].first;championOfScore.second += arrayForRanking[0].second;rannerUpOfScore.first = arrayForRanking[1].first;rannerUpOfScore.second += arrayForRanking[1].second;break;}case 3: {championOfScore.first = arrayForRanking[0].first;championOfScore.second += arrayForRanking[0].second;rannerUpOfScore.first = arrayForRanking[1].first;rannerUpOfScore.second += arrayForRanking[1].second;secondRannerUpOfScore.first = arrayForRanking[2].first;secondRannerUpOfScore.second += arrayForRanking[2].second;break;}}arrayForRanking.clear();//依money排序for (auto& player : gamePlayers) {if (player.m_ifOnline == false) continue;arrayForRanking.emplace_back(make_pair(player.m_money, player.m_playerName));}sort(arrayForRanking.begin(), arrayForRanking.end(), [](pair<int, string> a, pair<int, string> b) {if (a.first > b.first)return true;//从大到小排序else return false;});switch (arrayForRanking.size()) {case 1: {championOfMoney.first = arrayForRanking[0].first;championOfMoney.second += arrayForRanking[0].second;break;}case 2: {championOfMoney.first = arrayForRanking[0].first;championOfMoney.second += arrayForRanking[0].second;rannerUpOfMoney.first = arrayForRanking[1].first;rannerUpOfMoney.second += arrayForRanking[1].second;break;}case 3: {championOfMoney.first = arrayForRanking[0].first;championOfMoney.second += arrayForRanking[0].second;rannerUpOfMoney.first = arrayForRanking[1].first;rannerUpOfMoney.second += arrayForRanking[1].second;secondRannerUpOfMoney.first = arrayForRanking[2].first;secondRannerUpOfMoney.second += arrayForRanking[2].second;break;}}arrayForRanking.clear();//依longth排序for (auto& player : gamePlayers) {if (player.m_ifOnline == false) continue;arrayForRanking.emplace_back(make_pair(player.m_snake.m_snakeLongth, player.m_playerName));}sort(arrayForRanking.begin(), arrayForRanking.end(), [](pair<int, string> a, pair<int, string> b) {if (a.first > b.first)return true;//从大到小排序else return false;});switch (arrayForRanking.size()) {case 1: {championOfLongth.first = arrayForRanking[0].first;championOfLongth.second += arrayForRanking[0].second;break;}case 2: {championOfLongth.first = arrayForRanking[0].first;championOfLongth.second += arrayForRanking[0].second;rannerUpOfLongth.first = arrayForRanking[1].first;rannerUpOfLongth.second += arrayForRanking[1].second;break;}case 3: {championOfLongth.first = arrayForRanking[0].first;championOfLongth.second += arrayForRanking[0].second;rannerUpOfLongth.first = arrayForRanking[1].first;rannerUpOfLongth.second += arrayForRanking[1].second;secondRannerUpOfLongth.first = arrayForRanking[2].first;secondRannerUpOfLongth.second += arrayForRanking[2].second;break;}}arrayForRanking.clear();m_ranking.clear();m_ranking.emplace_back(championOfScore);m_ranking.emplace_back(championOfScore);m_ranking.emplace_back(championOfScore);m_ranking.emplace_back(championOfMoney);m_ranking.emplace_back(championOfMoney);m_ranking.emplace_back(championOfMoney);m_ranking.emplace_back(championOfLongth);m_ranking.emplace_back(championOfLongth);m_ranking.emplace_back(championOfLongth);int random = (rand() % ((m_ranking.size() - 1) - 0 + 1)) + 0;string voidScreenLine(60, ' ');//居中显示pair<int,string> goingToShow = m_ranking[random];string theValue = "    数值是 : ";theValue += to_string(goingToShow.first);int X = playScreenWidth / 2 - goingToShow.second.size() / 2;printfSXY_A(voidScreenLine, X, 1);printfSXY_B(voidScreenLine, X, 1);printfSXY_A(goingToShow.second + theValue, X, 1);printfSXY_B(goingToShow.second + theValue, X, 1);m_ifOkForShowRanking = false;}
}
void snakeGame::addRTInfo(gamePlayer::snake& snake, string& massage) {//有可能编译器要在这一行报错,反正我是可以正常运行的m_RTInfos.emplace_back(snake.m_snakeDeque.front().m_x, snake.m_snakeDeque.front().m_y, massage, NULL);
}
void snakeGame::showRTInfo(string whichScreen) {//最多3条实时信息if (!m_RTInfos.empty()) {int oldX = 0;int oldY = 0;int displacement = 1;for (auto itBegin = m_RTInfos.begin(); itBegin != m_RTInfos.end(); ++itBegin) {if (oldX == get<0>(*itBegin) && oldY == get<1>(*itBegin)) displacement += 1; //同一位置有多条信息的处理else displacement = 1;oldX = get<0>(*itBegin);oldY = get<1>(*itBegin);if (get<0>(*itBegin) >= playScreenWidth / 2 && get<1>(*itBegin) <= playScreenHigh / 2) {//第一象限的RTInfo显示//居中显示int X = playScreenWidth - (playScreenWidth - get<0>(*itBegin)) - get<2>(*itBegin).size();if (whichScreen == "A") printfSXY_A(get<2>(*itBegin), X, get<1>(*itBegin) + displacement);if (whichScreen == "B") printfSXY_B(get<2>(*itBegin), X, get<1>(*itBegin) + displacement);}if (get<0>(*itBegin) < playScreenWidth / 2 && get<1>(*itBegin) <= playScreenHigh / 2) {//第二象限int X = 2 + get<0>(*itBegin);if (whichScreen == "A") printfSXY_A(get<2>(*itBegin), X, get<1>(*itBegin) + displacement);if (whichScreen == "B") printfSXY_B(get<2>(*itBegin), X, get<1>(*itBegin) + displacement);}if (get<0>(*itBegin) < playScreenWidth / 2 && get<1>(*itBegin) > playScreenHigh / 2) {//第三象限int X = 2 + get<0>(*itBegin);if (whichScreen == "A") printfSXY_A(get<2>(*itBegin), X, get<1>(*itBegin) - displacement);if (whichScreen == "B") printfSXY_B(get<2>(*itBegin), X, get<1>(*itBegin) - displacement);}if (get<0>(*itBegin) >= playScreenWidth / 2 && get<1>(*itBegin) > playScreenHigh / 2) {//第四象限int X = playScreenWidth - (playScreenWidth - get<0>(*itBegin)) - get<2>(*itBegin).size();if (whichScreen == "A") printfSXY_A(get<2>(*itBegin), X, get<1>(*itBegin) - displacement);if (whichScreen == "B") printfSXY_B(get<2>(*itBegin), X, get<1>(*itBegin) - displacement);}get<3>(*itBegin) += 1;}//删除迭代器得分离出来while (1) {auto needToErase = find_if(m_RTInfos.begin(), m_RTInfos.end(), [](tuple<int, int, string, int> value) {if (get<3>(value) > 11)return true;//删除显示11次以上(12次)的massageelse return false;});if (needToErase == m_RTInfos.end())break;//遍历到删除完所以显示7次以上(8次)的massageelse m_RTInfos.erase(needToErase);}}
}
void snakeGame::showDeadInfo() {lock_guard<mutex> playersLock(m_playersLock);int nowSurvival = 0;string lastSurvivalOne;for (auto& player : gamePlayers) {if (player.m_ifOnline == false || get<1>(player.m_snake.m_died) == true) continue;++nowSurvival;//若能走到这一行说明蛇存活lastSurvivalOne = player.m_playerName;if (get<0>(player.m_snake.m_died) == true) {string deadInfoUnder = player.m_playerName + " " + get<2>(player.m_snake.m_died);//显示在左下角的死亡信息showLowerLeftConnerInfo(deadInfoUnder);get<1>(player.m_snake.m_died) = true;}}if (nowSurvival == 0) {m_gameOver.first = true;m_gameOver.second = "all snakes died";showLowerLeftConnerInfo("ohh all of you are dead now");}else if (nowSurvival == 1) {if (m_ifShowedLastSurvivalOneInfo == false) {string theInfoWaitingToShow = "The last servival one is " + lastSurvivalOne + " !";showLowerLeftConnerInfo(theInfoWaitingToShow);m_ifShowedLastSurvivalOneInfo = true;}}else if (nowSurvival > 1 && m_foods.empty()) {//当所有食物都吃完了,此时蛇还不止1条,而且幸存的蛇长度还都相等时,达成平局条件bool allSurvivalSnakeAreEquelOnLongth = true ;int snakeLongth = 0;int i = 0;for (auto& player : gamePlayers) {if (player.m_ifOnline == false || get<0>(player.m_snake.m_died) == true) continue;if (i == 0) { snakeLongth = player.m_snake.m_snakeLongth; ++i;}else if (player.m_snake.m_snakeLongth != snakeLongth){snakeLongth = player.m_snake.m_snakeLongth;allSurvivalSnakeAreEquelOnLongth = false;}}if (allSurvivalSnakeAreEquelOnLongth == true) {++m_endUpDrawCounting;if (m_endUpDrawCounting > 60) {m_ifEndUpDraw = true;m_gameOver.first = true;m_gameOver.second = "a dead heat deuce";showLowerLeftConnerInfo("The game end up in a draw");}}}//全部食物吃完,并且只剩一条蛇//停留一小会再结束if (m_foods.size() == 0 && nowSurvival == 1) {//让蛇在胜利之后再飘一会,然后再结束本局m_theWattingAfterAllFoodsIsEatten += 1;if (m_theWattingAfterAllFoodsIsEatten == 50) {m_gameOver.first = true;m_gameOver.second = "vectory";showLowerLeftConnerInfo("Let's call it all day !!!");showLowerLeftConnerInfo("Thanks for playing");}else if (m_theWattingAfterAllFoodsIsEatten == 20) {for (auto& player : gamePlayers) {if (player.m_ifOnline == false || get<0>(player.m_snake.m_died) == true) continue;string goingToShow = "hahahaha!!!";addRTInfo(player.m_snake, goingToShow);showLowerLeftConnerInfo(player.m_playerName + " : " + goingToShow);break;}}else if (m_theWattingAfterAllFoodsIsEatten == 40) {for (auto& player : gamePlayers) {if (player.m_ifOnline == false || get<0>(player.m_snake.m_died) == true) continue;string goingToShow;if (player.m_race == "human") goingToShow = "I AM REAL WINNER!!!";else if (player.m_race == "robot") goingToShow = "I'M READY TO DIE";addRTInfo(player.m_snake, goingToShow);showLowerLeftConnerInfo(player.m_playerName + " : " + goingToShow);break;}}}else if (m_foods.size() == 0 && nowSurvival != 1 && m_ifSaidTheLastSentence == false) {//根据目前的状况发一些RTInfostring goingToShow1 = "Now all the foods are eatten ,";string goingToShow2 = "But still there are snakes!!!!";string goingToShow3 = "Who is the real winner ?";showLowerLeftConnerInfo(goingToShow1);showLowerLeftConnerInfo(goingToShow2);showLowerLeftConnerInfo(goingToShow3);int longest = 0;for (auto& player : gamePlayers) {if (player.m_ifOnline == false || get<0>(player.m_snake.m_died) == true) continue;if (player.m_snake.m_snakeLongth > longest)longest = player.m_snake.m_snakeLongth;}for (auto& player : gamePlayers) {if (player.m_ifOnline == false || get<0>(player.m_snake.m_died) == true) continue;if (player.m_snake.m_snakeLongth == longest) {string goingToShow = "I'm all but the winner!!!";addRTInfo(player.m_snake, goingToShow);showLowerLeftConnerInfo(player.m_playerName +" : "+ goingToShow);goingToShow = "I'll slaughter you else";addRTInfo(player.m_snake, goingToShow);showLowerLeftConnerInfo(player.m_playerName + " : " + goingToShow);}else {string goingToShow = "Shit!!! I had chance!!!";addRTInfo(player.m_snake, goingToShow);showLowerLeftConnerInfo(player.m_playerName + " : " + goingToShow);}}m_ifSaidTheLastSentence = true;}
}
void snakeGame::showLowerLeftConnerInfo(string massage) {string nowTime = kitFunctions::get_hourMinSec_string();nowTime += " ";massage.insert(0, nowTime);m_lowerLeftConnerInfos.emplace_back(massage);if (m_lowerLeftConnerInfos.size() > 8) {m_lowerLeftConnerInfos.pop_front();}string voidScreenLine(InfoCornerWidth - 2, ' ');int y = 42;//反向遍历得用迭代器for (auto itbegin = m_lowerLeftConnerInfos.crbegin(); itbegin != m_lowerLeftConnerInfos.crend(); ++itbegin) {//先清空再输出printfSXY_A(voidScreenLine, 2, y);printfSXY_B(voidScreenLine, 2, y);printfSXY_A(*itbegin, 2, y);printfSXY_B(*itbegin, 2, y);y -= 1;}
}
void snakeGame::showGameOver(gameOverWay gameOverWay) {//居中显示 gameOverWaystring gameOverInfo;int coordinatX;if (gameOverWay == "time run out") {gameOverInfo = "Time's Running Out";}else if (gameOverWay == "all snakes died") {gameOverInfo = "You Guys All Died";}else if (gameOverWay == "vectory") {lock_guard<mutex> playersLock(m_playersLock);for (auto& player : gamePlayers) {if (player.m_ifOnline == false || get<0>(player.m_snake.m_died) == true) continue;gameOverInfo = player.m_playerName+" IS VECTORIOUS";break;}}else if (gameOverWay == "a dead heat deuce") {gameOverInfo = "A DEAD HEAT DEUCE";}coordinatX = playScreenWidth / 2 - gameOverInfo.size() / 2;printfSXY_A(gameOverInfo, coordinatX, playScreenHigh / 2 - 1);printfSXY_B(gameOverInfo, coordinatX, playScreenHigh / 2 - 1);string gameOver_str = "GAME OVER";if(gameOverWay == "all foods eatten")gameOver_str = "VECTORY";int coordinatX_gameover = playScreenWidth / 2 - gameOver_str.size() / 2;printfSXY_A(gameOver_str, coordinatX_gameover, playScreenHigh / 2);printfSXY_B(gameOver_str, coordinatX_gameover, playScreenHigh / 2);getch();
}
void snakeGame::foodsCreating() {lock_guard<mutex> playersLock(m_playersLock);lock_guard<mutex> foodsLock(m_foodsLock);int nowOverallfoods = m_foods.size();while (nowOverallfoods < m_foodCoinsides) {auto newFood = make_unique<food>();int random_X, random_Y;do {//判断新食物坐标是否与蛇和已经存在的食物冲突random_X = (rand() % ((playScreenWidth - 4) - 5 + 1)) + 5;random_Y = (rand() % ((playScreenHigh - 1) - 4 + 1)) + 4;} while ([&]()->bool {//“md,下面这段lambda表达式实在是太操了”,你一定会这么想,,其实我也是这么想的//,但,操蛋不正是lambda表达式的作用吗?//是否与蛇头冲突//这里我只遍历了蛇头,因为觉得遍历完整个蛇身是浪费时间,反正不影响游戏体验for (auto& player : gamePlayers) {if (player.m_ifOnline == false) { continue; }if (random_X == player.m_snake.m_snakeDeque.front().m_x && random_Y == player.m_snake.m_snakeDeque.front().m_y) {return true;}}return false;}() || [&]()->bool {//是否与已存在的食物冲突for (auto& food : m_foods) {if (random_X == food->m_x && random_Y == food->m_y) {return true;}}return false;}() || [&]()->bool {//是否将食物刷新到了左下角的信息显示区if (random_X <= InfoCornerWidth + 2 && random_Y >= InfoCornerHigh) {return true;}return false;}());//下面这几行用来设置各种食物出现的概率// 1~100 的随机,// 结果(1~60)是smallFood// 结果(61~75)是middleFood// 结果(76~85)是moneyFood// 结果(86~91)是treasureFood// 结果(92~94)是squareFood// 结果(95~97)是circleFood// 结果(98~100)是pentacleFoodint random_nom = (rand() % (100 - 1 + 1)) + 1;if (random_nom <= 60) *newFood = smallFood;//有可能编译器会在这几行报错,反正我的是可以正常运行的if (60 < random_nom && random_nom <= 75) *newFood = middleFood;if (75 < random_nom && random_nom <= 85) *newFood = moneyFood;if (85 < random_nom && random_nom <= 91) *newFood = treasureFood;if (91 < random_nom && random_nom <= 94) *newFood = squareFood;if (94 < random_nom && random_nom <= 97) *newFood = circleFood;if (97 < random_nom && random_nom <= 100) *newFood = pentacleFood;(*newFood).m_x = random_X;(*newFood).m_y = random_Y;m_foods.emplace_back(move(newFood));++nowOverallfoods;}
}
void snakeGame::robotMovingJudge() {lock_guard<mutex> playersLock(m_playersLock);lock_guard<mutex> foodsLock(m_foodsLock);for (auto& player : gamePlayers) {if (player.m_ifOnline == false || get<0>(player.m_snake.m_died) == true) continue;if (player.m_race == "robot") {player.m_snake.m_speedSum += player.m_snake.m_snakeSpeed;int equalsMeToMove = 4;//这数值只能是4的倍数if (player.m_snake.m_speedSum < equalsMeToMove)continue;player.m_snake.m_speedSum = 0;player.m_snake.m_moving = true;//robot行动机理://1:不能向自己的反方向移动(那样就撞到自己了)//2:避免撞任何一条蛇//3:避免撞到边框//4:向着 m_foods.front()那个food前进。//5:若所有食物都吃完了而此时还有不止一条蛇,就向着对方的脑袋冲过去。//2020708:fxxk这个功能太难了,我搞了2天最后都只做成了智障蛇bool ifTurnLeft = true;//turn left在这里更为准确的理解方式是change direction to leftbool ifTurnRight = true;bool ifTurnUp = true;bool ifTurnDown = true;int snakeFrontX = player.m_snake.m_snakeDeque.front().m_x;int snakeFrontY = player.m_snake.m_snakeDeque.front().m_y;//不能向自己的反方向移动if (player.m_snake.m_direction == "up") { ifTurnDown = false; }else if (player.m_snake.m_direction == "down") { ifTurnUp = false; }else if (player.m_snake.m_direction == "left") { ifTurnRight = false; }else if (player.m_snake.m_direction == "right") { ifTurnLeft = false; }//是否要撞到任何一条蛇了for (auto otherSnakeBegin = gamePlayers.begin(); otherSnakeBegin != gamePlayers.end(); ++otherSnakeBegin) {if (otherSnakeBegin->m_ifOnline == false || get<0>(otherSnakeBegin->m_snake.m_died) == true) continue;if (player.m_snake > otherSnakeBegin->m_snake)goto skipSnakeImpattingJudge;for (auto snakeBodyBegin = otherSnakeBegin->m_snake.m_snakeDeque.begin(); snakeBodyBegin != otherSnakeBegin->m_snake.m_snakeDeque.end(); ++snakeBodyBegin) {if (snakeBodyBegin->m_x == player.m_snake.m_snakeDeque.front().m_x + 2&&snakeBodyBegin->m_y == player.m_snake.m_snakeDeque.front().m_y) {ifTurnRight = false; }else if (snakeBodyBegin->m_x == player.m_snake.m_snakeDeque.front().m_x - 2&&snakeBodyBegin->m_y == player.m_snake.m_snakeDeque.front().m_y) {ifTurnLeft = false; }else if (snakeBodyBegin->m_y == player.m_snake.m_snakeDeque.front().m_y + 1&&snakeBodyBegin->m_x == player.m_snake.m_snakeDeque.front().m_x) {ifTurnDown = false; }else if (snakeBodyBegin->m_y == player.m_snake.m_snakeDeque.front().m_y - 1&&snakeBodyBegin->m_x == player.m_snake.m_snakeDeque.front().m_x) {ifTurnUp = false; }}skipSnakeImpattingJudge:{}}//是否要撞到边框了//是否要撞到左下角信息框了if (player.m_snake.m_snakeDeque.front().m_x < InfoCornerWidth + 2 && player.m_snake.m_snakeDeque.front().m_y > InfoCornerHigh - 2) {ifTurnDown = false;}if (player.m_snake.m_snakeDeque.front().m_x < InfoCornerWidth + 3 && player.m_snake.m_snakeDeque.front().m_y >(InfoCornerHigh - 1)) {ifTurnLeft = false;}//是不是要撞到地图了if (player.m_snake.m_snakeDeque.front().m_x < 5) {ifTurnLeft = false;if (player.m_snake.m_snakeDeque.front().m_y < 4) {ifTurnUp = false;}else if (player.m_snake.m_snakeDeque.front().m_y >= InfoCornerHigh - 2) {ifTurnDown = false;}}if (player.m_snake.m_snakeDeque.front().m_x > playScreenWidth - 5) {ifTurnRight = false;if (player.m_snake.m_snakeDeque.front().m_y < 4) {ifTurnUp = false;}else if (player.m_snake.m_snakeDeque.front().m_y >= playScreenHigh - 2) {ifTurnDown = false;}}if (player.m_snake.m_snakeDeque.front().m_y < 4) {ifTurnUp = false;if (player.m_snake.m_snakeDeque.front().m_x < 5) {ifTurnLeft = false;}else if (player.m_snake.m_snakeDeque.front().m_x >= playScreenWidth - 5) {ifTurnRight = false;}}if (player.m_snake.m_snakeDeque.front().m_y > playScreenHigh - 2) {ifTurnDown = false;if (player.m_snake.m_snakeDeque.front().m_x < InfoCornerWidth + 3) {ifTurnLeft = false;}else if (player.m_snake.m_snakeDeque.front().m_x >= playScreenWidth - 5) {ifTurnRight = false;}}//向着食物前进//只有在没有其他顾虑的情况下才会向着食物前进if ([&]()->bool {if (player.m_snake.m_direction == "up") {if(ifTurnRight==true&& ifTurnLeft==true&& ifTurnUp==true&& ifTurnDown==false)return true;else return false;}else if (player.m_snake.m_direction == "down") {if (ifTurnRight == true && ifTurnLeft == true && ifTurnUp == false && ifTurnDown == true)return true;else return false;}else if (player.m_snake.m_direction == "left") {if (ifTurnRight == false && ifTurnLeft == true && ifTurnUp == true && ifTurnDown == true)return true;else return false;}else if (player.m_snake.m_direction == "right") {if (ifTurnRight == true && ifTurnLeft == false && ifTurnUp == true && ifTurnDown == true)return true;else return false;}else {string goingToThrow = "wrong direction of " + player.m_playerName;throw runtime_error(goingToThrow);}}()) {ifTurnRight = false;ifTurnLeft = false;ifTurnUp = false;ifTurnDown = false;if (!m_foods.empty()) {int targetFoodX = m_foods.front().get()->m_x;int targetFoodY = m_foods.front().get()->m_y;if (targetFoodX > snakeFrontX && targetFoodY == snakeFrontY) {//以蛇头为O点,食物在X轴正半轴上if (player.m_snake.m_direction == "left") {ifTurnUp = true;ifTurnDown = true;}else ifTurnRight = true;}else if (targetFoodX < snakeFrontX && targetFoodY == snakeFrontY) {//以蛇头为O点,食物在X轴负半轴上if (player.m_snake.m_direction == "right") {ifTurnUp = true;ifTurnDown = true;}else ifTurnLeft = true;}else if (targetFoodX == snakeFrontX && targetFoodY < snakeFrontY) {//以蛇头为O点,食物在Y轴正半轴上if (player.m_snake.m_direction == "down") {ifTurnRight = true;ifTurnLeft = true;}else ifTurnUp = true;}else if (targetFoodX == snakeFrontX && targetFoodY > snakeFrontY) {//以蛇头为O点,食物在Y轴负半轴上if (player.m_snake.m_direction == "down") {ifTurnRight = true;ifTurnLeft = true;}else ifTurnDown = true;}else if (targetFoodX > snakeFrontX && targetFoodY < snakeFrontY) {//以蛇头为O点,食物在第一象限if (player.m_snake.m_direction == "down") {ifTurnRight = true;}else if (player.m_snake.m_direction == "left") {ifTurnRight = false;ifTurnUp = true;}else {ifTurnRight = true;ifTurnUp = true;}}else if (targetFoodX < snakeFrontX && targetFoodY < snakeFrontY) {//以蛇头为O点,食物在第二象限if (player.m_snake.m_direction == "down") {ifTurnLeft = true;}else if (player.m_snake.m_direction == "right") {ifTurnUp = true;}else {ifTurnLeft = true;ifTurnUp = true;}}else if (targetFoodX < snakeFrontX && targetFoodY > snakeFrontY) {//以蛇头为O点,食物在第三象限if (player.m_snake.m_direction == "up") {ifTurnLeft = true;}else if (player.m_snake.m_direction == "right") {ifTurnDown = true;}else {ifTurnLeft = true;ifTurnDown = true;}}else if (targetFoodX > snakeFrontX && targetFoodY > snakeFrontY) {//以蛇头为O点,食物在第四象限if (player.m_snake.m_direction == "up") {ifTurnRight = true;}else if (player.m_snake.m_direction == "left") {ifTurnDown = true;}else {ifTurnRight = true;ifTurnDown = true;}}}else if(m_foods.empty()) {//若所有食物都吃完了而此时还有不止一条蛇,就向着对方的脑袋冲过去。bool anyOneElseSurviving = false;vector<gamePlayer>::iterator anotherSurvivalSnake;for (auto itbegin = gamePlayers.begin(); itbegin != gamePlayers.end(); ++itbegin) {if (itbegin->m_ifOnline == false || get<0>(itbegin->m_snake.m_died) == true) continue;if (itbegin->m_playerName == player.m_playerName) continue;anyOneElseSurviving = true;anotherSurvivalSnake = itbegin;//有可能编译器要在这一行报错,反正我是可以正常运行的}if (anyOneElseSurviving == true) {int targetSnakeX = anotherSurvivalSnake->m_snake.m_snakeDeque.front().m_x;int targetSnakeY = anotherSurvivalSnake->m_snake.m_snakeDeque.front().m_y;if (targetSnakeX > snakeFrontX && targetSnakeY == snakeFrontY) {//以蛇头为O点,食物在X轴正半轴上if (player.m_snake.m_direction == "left") {ifTurnUp = true;ifTurnDown = true;}else ifTurnRight = true;}else if (targetSnakeX < snakeFrontX && targetSnakeY == snakeFrontY) {//以蛇头为O点,食物在X轴负半轴上if (player.m_snake.m_direction == "right") {ifTurnUp = true;ifTurnDown = true;}else ifTurnLeft = true;}else if (targetSnakeX == snakeFrontX && targetSnakeY < snakeFrontY) {//以蛇头为O点,食物在Y轴正半轴上if (player.m_snake.m_direction == "down") {ifTurnRight = true;ifTurnLeft = true;}else ifTurnUp = true;}else if (targetSnakeX == snakeFrontX && targetSnakeY > snakeFrontY) {//以蛇头为O点,食物在Y轴负半轴上if (player.m_snake.m_direction == "down") {ifTurnRight = true;ifTurnLeft = true;}else ifTurnDown = true;}else if (targetSnakeX > snakeFrontX && targetSnakeY < snakeFrontY) {//以蛇头为O点,食物在第一象限if (player.m_snake.m_direction == "down") {ifTurnRight = true;}else if (player.m_snake.m_direction == "left") {ifTurnRight = false;ifTurnUp = true;}else {ifTurnRight = true;ifTurnUp = true;}}else if (targetSnakeX < snakeFrontX && targetSnakeY < snakeFrontY) {//以蛇头为O点,食物在第二象限if (player.m_snake.m_direction == "down") {ifTurnLeft = true;}else if (player.m_snake.m_direction == "right") {ifTurnUp = true;}else {ifTurnLeft = true;ifTurnUp = true;}}else if (targetSnakeX < snakeFrontX && targetSnakeY > snakeFrontY) {//以蛇头为O点,食物在第三象限if (player.m_snake.m_direction == "up") {ifTurnLeft = true;}else if (player.m_snake.m_direction == "right") {ifTurnDown = true;}else {ifTurnLeft = true;ifTurnDown = true;}}else if (targetSnakeX > snakeFrontX && targetSnakeY > snakeFrontY) {//以蛇头为O点,食物在第四象限if (player.m_snake.m_direction == "up") {ifTurnRight = true;}else if (player.m_snake.m_direction == "left") {ifTurnDown = true;}else {ifTurnRight = true;ifTurnDown = true;}}}}}vector<string> myBeActing;if (ifTurnLeft == true) myBeActing.emplace_back("left");if (ifTurnRight == true) myBeActing.emplace_back("right");if (ifTurnUp == true) myBeActing.emplace_back("up");if (ifTurnDown == true) myBeActing.emplace_back("down");if (!myBeActing.empty()) {int temp = (rand() % (myBeActing.size() - 1 - 0 + 1)) + 0; //随机决定可行的方向player.m_snake.m_direction = myBeActing[temp];}myBeActing.clear();}}
}
void snakeGame::snakeMoving() {下面注释掉的这段代码是传统的贪吃蛇行动代码(头插尾删),缺点是不能做“吃啥长啥”这个功能,你可以把它释放掉来试试就懂我的意思了//lock_guard<mutex> playersLock(m_playersLock);//for (auto& player : gamePlayers) {// if (player.m_ifOnline == false || get<0>(player.m_snake.m_died) == true) continue;//有可能编译器会在这几行报错,反正我的是可以正常运行的//   gamePlayer::snake::snakeBit oldHead = player.m_snake.m_snakeDeque.front();//   player.m_snake.m_snakeDeque.front().m_symbol = player.m_snake.m_bodySymbol;//  if (player.m_snake.m_direction == "up") { --oldHead.m_y; }//    else if (player.m_snake.m_direction == "down") { ++oldHead.m_y; }//   else if (player.m_snake.m_direction == "left") { oldHead.m_x -= 2; }// else if (player.m_snake.m_direction == "right") { oldHead.m_x += 2; }//   player.m_snake.m_snakeDeque.emplace_front(oldHead.m_symbol, oldHead.m_x, oldHead.m_y);//    player.m_snake.m_snakeDeque.pop_back();//}//下面是可以做到吃啥长啥的代码lock_guard<mutex> playersLock(m_playersLock);lock_guard<mutex> foodsLock(m_foodsLock);for (auto& player : gamePlayers) {if (player.m_ifOnline == false || get<0>(player.m_snake.m_died) == true) continue;//有可能编译器会在这几行报错,反正我的是可以正常运行的//不同的蛇不同的速度,哎,又是双缓冲又是多线程又要做这个速度模块只能用这种很猥琐的方法了.//这4行代码是控制多个蛇的速度的,与吃啥长啥无关的,可以去掉.int equalsMeToMove = 4;if (player.m_race == "human") {player.m_snake.m_speedSum += player.m_snake.m_snakeSpeed;if (player.m_snake.m_speedSum < equalsMeToMove)continue;player.m_snake.m_speedSum = 0;player.m_snake.m_moving = true;}else if (player.m_race == "robot") {if (player.m_snake.m_moving == false)continue;}for (auto itbegin = player.m_snake.m_snakeDeque.rbegin(); itbegin != --player.m_snake.m_snakeDeque.rend(); ++itbegin) {//反向遍历到(--rend())足以,因为我们要对头进行单独操作itbegin->m_x = (itbegin + 1)->m_x;itbegin->m_y = (itbegin + 1)->m_y;}if (player.m_snake.m_direction == "up") { --player.m_snake.m_snakeDeque.front().m_y; }else if (player.m_snake.m_direction == "down") { ++player.m_snake.m_snakeDeque.front().m_y; }else if (player.m_snake.m_direction == "left") { player.m_snake.m_snakeDeque.front().m_x -= 2; }else if (player.m_snake.m_direction == "right") { player.m_snake.m_snakeDeque.front().m_x += 2; }player.m_snake.m_ifOkForsnakeMoving = true;}
}
void snakeGame::afterMovingJudge() {lock_guard<mutex> playersLock(m_playersLock);lock_guard<mutex> foodsLock(m_foodsLock);//if hitting the wallfor (auto& player : gamePlayers) {if (player.m_ifOnline == false || get<0>(player.m_snake.m_died) == true) continue;//这个continue很重要!!md不说了,都是泪!!if (player.m_snake.m_moving == false)continue;if (player.m_snake.m_snakeDeque.front().m_x <= 2 || player.m_snake.m_snakeDeque.front().m_x >= playScreenWidth - 2 ||player.m_snake.m_snakeDeque.front().m_y <= 2 || player.m_snake.m_snakeDeque.front().m_y >= playScreenHigh ||(player.m_snake.m_snakeDeque.front().m_x < InfoCornerWidth + 2 && player.m_snake.m_snakeDeque.front().m_y >= InfoCornerHigh)) {get<0>(player.m_snake.m_died) = true;get<2>(player.m_snake.m_died) = "hitting the wall";string goingToShow = "OHHHHH SHITTTT !";addRTInfo(player.m_snake, goingToShow);}}//if hitting himselffor (auto& player : gamePlayers) {if (player.m_ifOnline == false || get<0>(player.m_snake.m_died) == true) continue;if (player.m_snake.m_moving == false)continue;for (auto snakeBitBigin = ++player.m_snake.m_snakeDeque.begin(); snakeBitBigin != player.m_snake.m_snakeDeque.end(); ++snakeBitBigin) {if (player.m_snake.m_snakeDeque.front().m_x == snakeBitBigin->m_x && player.m_snake.m_snakeDeque.front().m_y == snakeBitBigin->m_y) {get<0>(player.m_snake.m_died) = true;get<2>(player.m_snake.m_died) = "hitting himself";string goingToShow = "OHHHHH NOOOO !";addRTInfo(player.m_snake, goingToShow);}}}//if eating another snakefor (auto itbegin = gamePlayers.begin(); itbegin != gamePlayers.end();++itbegin) {//遍历每条蛇if (itbegin->m_ifOnline == false || get<0>(itbegin->m_snake.m_died) == true) continue;if (itbegin->m_snake.m_moving == false)continue;for (auto& playerhhh : gamePlayers) {//遍历除正在判断的蛇以外的其他蛇(包括已死亡的)if (playerhhh.m_ifOnline == false || playerhhh.m_playerName == itbegin->m_playerName) continue;for (auto& snakebit : playerhhh.m_snake.m_snakeDeque) {if (itbegin->m_snake.m_snakeDeque.front().m_x == snakebit.m_x && itbegin->m_snake.m_snakeDeque.front().m_y == snakebit.m_y) {if (itbegin->m_snake > playerhhh.m_snake) {for (int i = 0; i < playerhhh.m_snake.m_snakeLongth; ++i) {itbegin->m_snake.m_snakeDeque.emplace_back(itbegin->m_snake.m_bodySymbol,itbegin->m_snake.m_snakeDeque.back().m_x, itbegin->m_snake.m_snakeDeque.back().m_y);}if (get<0>(playerhhh.m_snake.m_died) == true) {string goingToShow = playerhhh.m_playerName + "'s ramain is eatten by " + itbegin->m_playerName;showLowerLeftConnerInfo(goingToShow);}else {get<0>(playerhhh.m_snake.m_died) = true;get<2>(playerhhh.m_snake.m_died) = "is eatten by " + itbegin->m_playerName;}string goingToShow = "It's tasty !";itbegin->m_snake.m_snakeLongth = itbegin->m_snake.m_snakeLongth + playerhhh.m_snake.m_snakeLongth;addRTInfo(itbegin->m_snake, goingToShow);goingToShow = "Longth + " + to_string(playerhhh.m_snake.m_snakeLongth);addRTInfo(itbegin->m_snake, goingToShow);playerhhh.m_snake.m_snakeDeque.clear();goto nextEattingAnotherJudge;}}//md这嵌套太多了我还以为我在写C#  20200708}}nextEattingAnotherJudge:if (1 > 2) {};//用来站位,不然有语法错误}//if eating foodfor (auto& player : gamePlayers) {if (player.m_ifOnline == false || get<0>(player.m_snake.m_died) == true) continue;if (player.m_snake.m_moving == false)continue;for (auto itBegin = m_foods.begin(); itBegin != m_foods.end(); ++itBegin) {//食物是1格或2格宽的,而蛇是2格宽的//食物生成的x坐标为1,2,3,4递加的,而蛇头的x坐标为4,6,8,10双倍递加的if ((itBegin->get()->m_y == player.m_snake.m_snakeDeque.front().m_y && //对 Y 轴的判断要放在X轴之前,这样会计算得更快一些//对于squareFood,circleFood,pentacleFood这种占两行的food要单独判断 X 轴坐标[&]()->bool {if (itBegin->get()->m_symbol.size() == 1) {if (itBegin->get()->m_x == player.m_snake.m_snakeDeque.front().m_x ||itBegin->get()->m_x == player.m_snake.m_snakeDeque.front().m_x + 1)  //之所以x的判断还要加一是因为食物是一格的,蛇是两格的return true;else return false;}else if (itBegin->get()->m_symbol.size() == 2) {if (itBegin->get()->m_x == player.m_snake.m_snakeDeque.front().m_x ||itBegin->get()->m_x == player.m_snake.m_snakeDeque.front().m_x + 1 ||itBegin->get()->m_x + 1 == player.m_snake.m_snakeDeque.front().m_x)return true;else return false;}else {printfSXY_A("kkk", 60, 41);printfSXY_B("kkk", 60, 41);throw runtime_error("there's some food that boust size over 3");return false;}}())) { //不瞒你说,我自己都觉得lambda写法好操蛋 ( ̄▽ ̄)~*//一直到这里才开始执行吃到食物后的判断 =^_^=player.m_money += itBegin->get()->m_moneyAdding;player.m_nowScore += itBegin->get()->m_scoreAdding;player.m_snake.m_snakeGrowthValue += itBegin->get()->m_growValue;if (itBegin->get()->m_foodType == "smallFood" || itBegin->get()->m_foodType == "middleFood") {string goingToShow = "Score + " + to_string(itBegin->get()->m_scoreAdding);addRTInfo(player.m_snake, goingToShow);}else if (itBegin->get()->m_foodType == "moneyFood" || itBegin->get()->m_foodType == "treasureFood") {string goingToShow = "Money + " + to_string(itBegin->get()->m_moneyAdding);addRTInfo(player.m_snake, goingToShow);}int addingLongth = 0;//squareFood,circleFood,pentacleFood,这三种,吃啥长啥if (itBegin->get()->m_foodType == "squareFood") {addingLongth += 1;player.m_snake.m_snakeLongth += 1;player.m_snake.m_snakeDeque.emplace_back(squareFood.m_symbol,player.m_snake.m_snakeDeque.back().m_x, player.m_snake.m_snakeDeque.back().m_y);}else if (itBegin->get()->m_foodType == "circleFood") {addingLongth += 1;player.m_snake.m_snakeLongth += 1;player.m_snake.m_snakeDeque.emplace_back(circleFood.m_symbol,player.m_snake.m_snakeDeque.back().m_x, player.m_snake.m_snakeDeque.back().m_y);}else if (itBegin->get()->m_foodType == "pentacleFood") {addingLongth += 1;player.m_snake.m_snakeLongth += 1;player.m_snake.m_snakeDeque.emplace_back(pentacleFood.m_symbol,player.m_snake.m_snakeDeque.back().m_x, player.m_snake.m_snakeDeque.back().m_y);}//右下角成长值满一百长一格if (player.m_snake.m_snakeGrowthValue >= 100) {addingLongth += 1;player.m_snake.m_snakeLongth += 1;player.m_snake.m_snakeDeque.emplace_back(player.m_snake.m_bodySymbol,player.m_snake.m_snakeDeque.back().m_x, player.m_snake.m_snakeDeque.back().m_y);player.m_snake.m_snakeGrowthValue -= 100;}if (addingLongth != 0) {string goingToShow = " Longth + " + to_string(addingLongth);addRTInfo(player.m_snake, goingToShow);}m_foods.erase(itBegin);//显示目前剩余食物进度(在剩余3/4,2/4,1/4,最后5个)时显示if (m_foods.size() == (m_foodCoinsides / 4) * 3) {showLowerLeftConnerInfo("Foods are 3/4 left");showLowerLeftConnerInfo("Speed up");m_totalSpeed -= m_totalSpeed / 5;}else if (m_foods.size() == (m_foodCoinsides / 4) * 2) {showLowerLeftConnerInfo("Foods are 1/2 left");showLowerLeftConnerInfo("Speed up !");m_totalSpeed -= m_totalSpeed / 5;}else if (m_foods.size() == (m_foodCoinsides / 4) * 1) {showLowerLeftConnerInfo("Foods are 1/4 left");showLowerLeftConnerInfo("Speed up !!!");m_totalSpeed -= m_totalSpeed / 4;}else if (m_foods.size() == 5) {showLowerLeftConnerInfo("Foods are only 5 left!");showLowerLeftConnerInfo("Speeding up !!!!!");m_totalSpeed -= m_totalSpeed / 4;}else if (m_foods.size() == 4) {showLowerLeftConnerInfo("Foods are only 4 left!!");}else if (m_foods.size() == 3) {showLowerLeftConnerInfo("Foods are only 3 left!!!");}else if (m_foods.size() == 2) {showLowerLeftConnerInfo("Only 2 foods !!!!");showLowerLeftConnerInfo("Speeding up !!!!!!!!!!");m_totalSpeed -= m_totalSpeed / 3;}else if (m_foods.size() == 1) {showLowerLeftConnerInfo("The last ONE food!!!!!");showLowerLeftConnerInfo("Faster !!!!!!!!!!!!!!!");string goingToShow = "I'm ready to end the gamble!!!";addRTInfo(player.m_snake, goingToShow);showLowerLeftConnerInfo(player.m_playerName + ":" + goingToShow);m_totalSpeed -= m_totalSpeed / 2;}else if (m_foods.size() == 0) {showLowerLeftConnerInfo("ALL FOODS HAVE EATTEN");showLowerLeftConnerInfo("Last one food eatten by " + player.m_playerName);}break;}}//md这么多 } 我差点以为我在写 C#player.m_snake.m_moving = false;}//根据目前各个蛇的情况发一些 Real Time Infoint nowSurvivalQuantity = 0;for (auto itbegin = gamePlayers.begin(); itbegin != gamePlayers.end(); ++itbegin) {if (itbegin->m_ifOnline == false || get<0>(itbegin->m_snake.m_died) == true) continue;++nowSurvivalQuantity;}if (nowSurvivalQuantity == 1 ) {vector<snakeGame::gamePlayer>::iterator theLastSurvivalRobot;for (auto itbegin = gamePlayers.begin(); itbegin != gamePlayers.end(); ++itbegin) {if (itbegin->m_ifOnline == false || get<0>(itbegin->m_snake.m_died) == true) continue;theLastSurvivalRobot = itbegin;//我的编译器会在这儿报错,但程序依然可以正常运行,而且我明明就是对的好吗!break;}if (theLastSurvivalRobot->m_race == "human")goto skipSayTheLastLivingRobotIsGoingToSay;if (m_speakingCounting == 1)m_ifOkToSayTheLastLivingRobotIsGoingToSay = true;if (m_ifOkToSayTheLastLivingRobotIsGoingToSay == true&& m_ifSaidTheLastLivingRobotIsGoingToSay==false) {double dividend = static_cast<double>(m_speakingCounting);//被除数double divisor = 15;//除数double quotient = dividend / divisor;if (quotient == 1 || quotient == 2 || quotient == 3 || quotient == 4 ||quotient == 5 || quotient == 6 || quotient == 7 || quotient == 8 ||quotient == 9 || quotient == 10 || quotient == 11 || quotient == 12 ||quotient == 13 || quotient == 14) {int whichSentence = static_cast<int>(quotient);string goingToShow = m_theLastLivingRobotIsGoingToSay[whichSentence];addRTInfo(theLastSurvivalRobot->m_snake, goingToShow);showLowerLeftConnerInfo(theLastSurvivalRobot->m_playerName + " : " + goingToShow);}if (quotient == 14) {m_ifOkToSayTheLastLivingRobotIsGoingToSay = false;m_ifSaidTheLastLivingRobotIsGoingToSay = true;}}skipSayTheLastLivingRobotIsGoingToSay: {}}if (m_speakingCounting == 250) {int longest = 0;int shortest = 10000;for (auto& player : gamePlayers) {if (player.m_ifOnline == false || get<0>(player.m_snake.m_died) == true) continue;if (player.m_snake.m_snakeLongth > longest)longest = player.m_snake.m_snakeLongth;if (player.m_snake.m_snakeLongth < shortest)shortest = player.m_snake.m_snakeLongth;}for (auto& player : gamePlayers) {if (player.m_ifOnline == false || get<0>(player.m_snake.m_died) == true) continue;if (player.m_snake.m_snakeLongth == longest) {string goingToShow = "I'm the strongest now !!!";addRTInfo(player.m_snake, goingToShow);showLowerLeftConnerInfo(player.m_playerName + " : " + goingToShow);}else if (player.m_snake.m_snakeLongth == shortest) {string goingToShow = "Oh i'm the most weak one now";addRTInfo(player.m_snake, goingToShow);showLowerLeftConnerInfo(player.m_playerName + " : " + goingToShow);}}m_speakingCounting = 0;}m_speakingCounting += 1;
}
void snakeGame::user1StrockJudge() {//做个留恋:之前我是像下面这几行这样来写的,,结果就会导致return后忘记unlock的问题//if (GetAsyncKeyState(0x57) & 0x8000) {//    m_playersLock.lock();// if (gamePlayers[0].m_snake.m_direction == "down") return;// gamePlayers[0].m_snake.m_direction = "up";// m_playersLock.unlock();//}if (GetAsyncKeyState(0x57) & 0x8000) {lock_guard<mutex> playersLock(m_playersLock);if (gamePlayers[0].m_snake.m_direction == "down" ||gamePlayers[0].m_snake.m_ifOkForsnakeMoving == false) return;gamePlayers[0].m_snake.m_direction = "up";gamePlayers[0].m_snake.m_ifOkForsnakeMoving = false;//防止因按键过快导致的 hit himself 问题//这里我详细解释一下这个因按键过快导致的 hit himself问题//假设蛇此时向右走,user此时快速连按了 up 和 left 两个键,你猜此时计算机认为蛇往哪走?//没错,此时计算机就会认为蛇在往左走了!然后蛇头就向左拐,于是就会被判定 hit himself}else if (GetAsyncKeyState(0x53) & 0x8000) {lock_guard<mutex> playersLock(m_playersLock);if (gamePlayers[0].m_snake.m_direction == "up" ||gamePlayers[0].m_snake.m_ifOkForsnakeMoving == false) return;gamePlayers[0].m_snake.m_direction = "down";gamePlayers[0].m_snake.m_ifOkForsnakeMoving = false;}else if (GetAsyncKeyState(0x41) & 0x8000) {lock_guard<mutex> playersLock(m_playersLock);if (gamePlayers[0].m_snake.m_direction == "right" ||gamePlayers[0].m_snake.m_ifOkForsnakeMoving == false) return;gamePlayers[0].m_snake.m_direction = "left";gamePlayers[0].m_snake.m_ifOkForsnakeMoving = false;}else if (GetAsyncKeyState(0x44) & 0x8000) {lock_guard<mutex> playersLock(m_playersLock);if (gamePlayers[0].m_snake.m_direction == "left" ||gamePlayers[0].m_snake.m_ifOkForsnakeMoving == false) return;gamePlayers[0].m_snake.m_direction = "right";gamePlayers[0].m_snake.m_ifOkForsnakeMoving = false;}else if (GetAsyncKeyState(0x09) & 0x8000) {//lock_guard<mutex> ifCloseSubtitleLock(m_ifCloseSubtitleLock);//因为这里m_ifCloseSubtitleLock已经是atomic的了,所以不用mutex了m_ifCloseSubtitle = true;}
}
void snakeGame::user2StrockJudge() {if (GetAsyncKeyState(VK_UP) & 0x8000) {lock_guard<mutex> playersLock(m_playersLock);if (gamePlayers[1].m_snake.m_direction == "down" ||gamePlayers[1].m_snake.m_ifOkForsnakeMoving == false) return;gamePlayers[1].m_snake.m_direction = "up";gamePlayers[1].m_snake.m_ifOkForsnakeMoving = false;}else if (GetAsyncKeyState(VK_DOWN) & 0x8000) {lock_guard<mutex> playersLock(m_playersLock);if (gamePlayers[1].m_snake.m_direction == "up" ||gamePlayers[1].m_snake.m_ifOkForsnakeMoving == false) return;gamePlayers[1].m_snake.m_direction = "down";gamePlayers[1].m_snake.m_ifOkForsnakeMoving = false;}else if (GetAsyncKeyState(VK_LEFT) & 0x8000) {lock_guard<mutex> playersLock(m_playersLock);if (gamePlayers[1].m_snake.m_direction == "right" ||gamePlayers[1].m_snake.m_ifOkForsnakeMoving == false) return;gamePlayers[1].m_snake.m_direction = "left";gamePlayers[1].m_snake.m_ifOkForsnakeMoving = false;}else if (GetAsyncKeyState(VK_RIGHT) & 0x8000) {lock_guard<mutex> playersLock(m_playersLock);if (gamePlayers[1].m_snake.m_direction == "left" ||gamePlayers[1].m_snake.m_ifOkForsnakeMoving == false) return;gamePlayers[1].m_snake.m_direction = "right";gamePlayers[1].m_snake.m_ifOkForsnakeMoving = false;}else if (GetAsyncKeyState(0x09) & 0x8000) {//lock_guard<mutex> ifCloseSubtitleLock(m_ifCloseSubtitleLock);//因为这里m_ifCloseSubtitleLock已经是atomic的了,所以不用mutex了m_ifCloseSubtitle = true;}
}
void snakeGame::user3StrockJudge() {if (GetAsyncKeyState(0x55) & 0x8000) {lock_guard<mutex> playersLock(m_playersLock);if (gamePlayers[2].m_snake.m_direction == "down" ||gamePlayers[2].m_snake.m_ifOkForsnakeMoving == false) return;gamePlayers[2].m_snake.m_direction = "up";gamePlayers[2].m_snake.m_ifOkForsnakeMoving = false;}else if (GetAsyncKeyState(0x4a) & 0x8000) {lock_guard<mutex> playersLock(m_playersLock);if (gamePlayers[2].m_snake.m_direction == "up" ||gamePlayers[2].m_snake.m_ifOkForsnakeMoving == false) return;gamePlayers[2].m_snake.m_direction = "down";gamePlayers[2].m_snake.m_ifOkForsnakeMoving = false;}else if (GetAsyncKeyState(0x48) & 0x8000) {lock_guard<mutex> playersLock(m_playersLock);if (gamePlayers[2].m_snake.m_direction == "right" ||gamePlayers[2].m_snake.m_ifOkForsnakeMoving == false) return;gamePlayers[2].m_snake.m_direction = "left";gamePlayers[2].m_snake.m_ifOkForsnakeMoving = false;}else if (GetAsyncKeyState(0x4b) & 0x8000) {lock_guard<mutex> playersLock(m_playersLock);if (gamePlayers[2].m_snake.m_direction == "left" ||gamePlayers[2].m_snake.m_ifOkForsnakeMoving == false) return;gamePlayers[2].m_snake.m_direction = "right";gamePlayers[2].m_snake.m_ifOkForsnakeMoving = false;}else if (GetAsyncKeyState(0x09) & 0x8000) {//lock_guard<mutex> ifCloseSubtitleLock(m_ifCloseSubtitleLock);//因为这里m_ifCloseSubtitleLock已经是atomic的了,所以不用mutex了m_ifCloseSubtitle = true;}
}
void snakeGame::subtitleThread() {this_thread::sleep_for(chrono::seconds(3));while (1) {unique_lock<mutex> gameOverLock(m_gameOverLock);if (m_gameOver.first == true)break;gameOverLock.unlock();//unique_lock<mutex> closeSubtitleLock(m_ifCloseSubtitleLock);//因为这里m_ifCloseSubtitleLock已经是atomic的了,所以不用mutex了if (m_ifCloseSubtitle == true)break;//closeSubtitleLock.unlock();unique_lock<mutex> subtitleLock(m_subtitleLock);bool allShowedJudge = true;for (auto& subtitle : m_subtitles) {if (subtitle.m_haveShowed == true) continue;allShowedJudge = false;//如果能走到这行,说明字幕肯定没有显示完//string substr(int pos = 0 , int n = npos)const; 获取(返回)字符串s中从第npos位开始的长度为5的字符串subtitle.m_sentence = subtitle.m_content.substr(subtitle.m_startPos, subtitle.m_strLongth);if (subtitle.m_x <= InfoCornerWidth + 3) {//当字幕走到左边框时subtitle.m_startPos += 2;if (subtitle.m_startPos > subtitle.m_strSize) {subtitle.m_haveShowed = true;}}else {//当字幕在中间走时subtitle.m_x -= 2;if (subtitle.m_strLongth <= subtitle.m_strSize) {subtitle.m_strLongth += 2;}}}if (allShowedJudge == true)break;subtitleLock.unlock();this_thread::sleep_for(chrono::milliseconds(300));}m_ifCloseSubtitle = true;
}
void snakeGame::RankingThread() {while (1) {for (int i = 0; i < 50; ++i) {unique_lock<mutex> gameOverLock(m_gameOverLock);if (m_gameOver.first == true)break;gameOverLock.unlock();this_thread::sleep_for(chrono::milliseconds(100));}unique_lock<mutex> gameOverLock(m_gameOverLock);if (m_gameOver.first == true)break;gameOverLock.unlock();m_ifOkForShowRanking = true;}
}
void snakeGame::moduleShow() {int startTime = static_cast<int>(time(NULL));int endTime;while (1) {unique_lock<mutex> gameOverLock(m_gameOverLock);if (m_gameOver.first == true)break;gameOverLock.unlock();endTime = static_cast<int>(time(NULL));m_gameTimePast = endTime - startTime;robotMovingJudge();snakeMoving();//moveing若放到其他线程蛇就会一卡一卡的afterMovingJudge();//moveing线程蛇就会出现吃一个食物加两次分的情况(因为show线程还为开始下一次的moving)clearScreen("A");showSubtitle("A");showRanking("A");showScore("A");showTimePast("A");showRTInfo("A");showFoods("A");showSnakes("A");setScreen(hScreenA);showDeadInfo();this_thread::sleep_for(chrono::milliseconds(m_totalSpeed));gameOverLock.lock();if (m_gameOver.first == true)break;gameOverLock.unlock();endTime = static_cast<int>(time(NULL));m_gameTimePast = endTime - startTime;robotMovingJudge();snakeMoving();afterMovingJudge();clearScreen("B");showSubtitle("B");showRanking("B");showScore("B");showTimePast("B");showRTInfo("B");showFoods("B");showSnakes("B");setScreen(hScreenB);showDeadInfo();this_thread::sleep_for(chrono::milliseconds(m_totalSpeed));}showGameOver(m_gameOver.second);setScreen(hScreenA);
}
void snakeGame::moduleControl() {while (1) {unique_lock<mutex> gameOverLock(m_gameOverLock);if (m_gameOver.first == true)break;gameOverLock.unlock();if (gamePlayers[0].m_ifOnline == true && gamePlayers[0].m_race == "human")user1StrockJudge();if (gamePlayers[1].m_ifOnline == true && gamePlayers[1].m_race == "human")user2StrockJudge();if (gamePlayers[2].m_ifOnline == true && gamePlayers[2].m_race == "human")user3StrockJudge();}
}
void snakeGame::afterGameScoreCounting() {for (int x = mapWidth / 2 - 29; x < mapWidth / 2 + 30; x += 2) {//top of the mapconsolePrintf("▁", x, mapHigh / 2 - 10, 7);}for (int y = mapHigh / 2 - 9; y < mapHigh / 2 + 10; ++y) {//left columnconsolePrintf("▕", mapWidth / 2 - 31, y, 7);}for (int y = mapHigh / 2 - 9; y < mapHigh / 2 + 10; ++y) {//right columnconsolePrintf("▏", mapWidth / 2 + 31, y, 7);}for (int x = mapWidth / 2 - 29; x < mapWidth / 2 + 30; x += 2) {//bottom of the mapconsolePrintf("▔", x, mapHigh / 2 + 10, 7);}int x = mapWidth / 2 - 24;//player名字的x轴坐标for (auto& player : gamePlayers) {if (player.m_ifOnline == true) {string playerName = player.m_playerName;consolePrintf(playerName, x, mapHigh / 2 - 8, 7);string preMaxScore = "PMScore: " + to_string(player.m_preMaxScoreOfSnake);consolePrintf(preMaxScore, x, mapHigh / 2 - 7, 7);string nowScore = "Score: ";consolePrintf(nowScore, x, mapHigh / 2 - 6, 7);string money = "Money: ";consolePrintf(money, x, mapHigh / 2 - 5, 7);string longth = "Longth: ";consolePrintf(longth, x, mapHigh / 2 - 4, 7);}x += 20;//players的名字之间要隔这么几行来写信息}string pressTABToSkip = "Press 'tab' to skip";x = mapWidth / 2 - pressTABToSkip.size() / 2;consolePrintf(pressTABToSkip, x, mapHigh / 2 + 2, 1);//实现分数快速递加的效果x = mapWidth / 2 - 24;for (auto& player : gamePlayers) {if (player.m_ifOnline == true) {int scoreToShow = 0;int colorToShow = 7;while (scoreToShow < player.m_nowScore) {string nowScore = "Score: " + to_string(scoreToShow);consolePrintf(nowScore, x, mapHigh / 2 - 6, colorToShow);scoreToShow += 1;if (scoreToShow > player.m_preMaxScoreOfSnake)colorToShow = 4;if(m_ifSkipAfterGameScoreCounting == false)Sleep(1);}int moneyToShow = 0;while (moneyToShow < player.m_money) {string money = "Money: " + to_string(moneyToShow);consolePrintf(money, x, mapHigh / 2 - 5, 7);moneyToShow += 1;if (m_ifSkipAfterGameScoreCounting == false)Sleep(1);}int longthToShow = 0;while (longthToShow < player.m_snake.m_snakeLongth) {string Longth = "Longth: " + to_string(longthToShow);consolePrintf(Longth, x, mapHigh / 2 - 4, 7);longthToShow += 1;if (m_ifSkipAfterGameScoreCounting == false)Sleep(1);}if (player.m_nowScore > player.m_preMaxScoreOfSnake) {if (m_ifSkipAfterGameScoreCounting == false)Sleep(1000);string breakRecord = "breakRecord";consolePrintf(breakRecord, x, mapHigh / 2 - 2, 4);}if (m_ifSkipAfterGameScoreCounting == false)Sleep(1500);x += 20;//players的名字之间要隔这么几行来写信息}}//判断最终存活者x = mapWidth / 2 - 24;//player名字的x轴坐标if (m_ifSkipAfterGameScoreCounting == false)Sleep(2000);bool allDied = true;for (auto& player : gamePlayers) {if (player.m_ifOnline == true) {if (get<0>(player.m_snake.m_died) == false) {string surviver = "SURVIVER";consolePrintf(surviver, x, mapHigh / 2, 2);allDied = false;}}x += 20;//players的名字之间要隔这么几行来写信息}if (allDied == true) {string surviver = "YOU GUYS ALL DEAD";x = mapWidth / 2 - surviver.size() / 2;consolePrintf(surviver, x, mapHigh / 2, 4);}m_ifShowedAfterGameScoreCounting = true;
}
void snakeGame::press_TAB_to_skip() {while (1) {if (m_ifShowedAfterGameScoreCounting == true)break;if (GetAsyncKeyState(0x09) & 0x8000) {m_ifSkipAfterGameScoreCounting = true;}}
}
snakeGame::snakeGame(gameIniSet & gameIniset) {m_totalSpeed = gameIniset.otherSet.m_totalSpeed;m_foodCoinsides = gameIniset.otherSet.m_foodsCoinsides;iniSystem();iniAttributes(gameIniset);iniMap("A");iniMap("B");iniSnakes();foodsCreating();//这是屏幕上显示的那些话thread subTitles(&snakeGame::subtitleThread, this);thread RankingThread(&snakeGame::RankingThread, this);//游戏分为 图像显示(show) 用户控制(userEnter) 2个模块//20200706补充:md本来想分三个模块写的结果现在moduleShow成了主模块了!thread moduleShow(&snakeGame::moduleShow, this);thread moduleControl(&snakeGame::moduleControl, this);moduleShow.join();moduleControl.join();subTitles.join();RankingThread.join();getch();CloseHandle(hScreenA);CloseHandle(hScreenB);setScreen(GetStdHandle(STD_OUTPUT_HANDLE));//回到原来的控制台thread pressTABToSkip(&snakeGame::press_TAB_to_skip, this);thread afterGameScoreCounting(&snakeGame::afterGameScoreCounting, this);afterGameScoreCounting.join();pressTABToSkip.join();
}int main()
{//备注1 : getch()函数error:c4996解决方法://在 项目,属性,c/c++,预处理器,预处理器定义 里添加(英文:Project -> Properties -> c/c++ -> Preprocessor -> Preprocessor definition)//_CRT_SECURE_NO_DEPRECATE //_CRT_NONSTDC_NO_DEPRECATE//备注2 : 若打开程序后一直闪屏,请右击窗口,属性,布局,在窗口大小那儿把宽度改为 140,高度改为 43(每个在闪的窗口都要改)//这是因为用了双缓冲之后程序在两个窗口切的原因!!!!!双缓冲太恶心了!!!!//下次写程序再也不用双缓冲了!!!//备注3:无故在第65行WriteConsoleOutputCharacterA(hscreen, content.c_str(), length, coord, NULL)函数卡退的问题解决//把上面的 Debug 改成 x86 的 !!!!fxxk!!!我也不知道x64和x86是什么区别,,,反正改就对了。。。。//备注4:如果还是打不开,win10系统bug石锤了//控制台设置system("color 07");string cols = to_string(mapWidth);string lines = to_string(mapHigh);string setSize = "mode con cols=" + cols + " lines=" + lines;system(setSize.c_str());SetConsoleTitle(_T("snake"));gameIniSet myiniSet;//这个类作为游戏与用户数据和菜单数据的接口myiniSet.user1.m_ifOnline = true;//是否在线myiniSet.user1.m_race = "human"; //可以填 human 或 robot,注意不要填错了myiniSet.user1.m_playerName = "流影之王";//玩家名称myiniSet.user1.m_preMaxScore = 200;//该玩家历史最高分(现在没用)myiniSet.user1.m_money = 300;//金钱(现在没用)myiniSet.user1.m_headSymbol = "◆";//蛇头的符号,必须要有2个字节的大小,比如‘a’是一字节,‘aa’就是两字节myiniSet.user1.m_bodySymbol = "■";//蛇身的符号,必须要有2个字节的大小,比如‘a’是一字节,‘aa’就是两字节myiniSet.user1.m_snakeLongth = 7;//蛇的长度myiniSet.user1.m_direction = "up";//蛇的初始方向myiniSet.user1.m_snakeSpeed = fast;//速度,可以填 slow,middle,fastmyiniSet.user2.m_ifOnline = true;myiniSet.user2.m_race = "robot";myiniSet.user2.m_playerName = "老八";myiniSet.user2.m_preMaxScore = 200;myiniSet.user2.m_money = 400;myiniSet.user2.m_headSymbol = "◆";//the size of symbol must be 2 bytesmyiniSet.user2.m_bodySymbol = "●";myiniSet.user2.m_snakeLongth = 5;myiniSet.user2.m_direction = "up";myiniSet.user2.m_snakeSpeed = fast;myiniSet.user3.m_ifOnline = true;myiniSet.user3.m_race = "robot";myiniSet.user3.m_playerName = "000dlv000";myiniSet.user3.m_preMaxScore = 300;myiniSet.user3.m_money = 300;myiniSet.user3.m_headSymbol = "◆";//the size of symbol must be 2 bytesmyiniSet.user3.m_bodySymbol = "★";myiniSet.user3.m_snakeLongth = 5;myiniSet.user3.m_direction = "up";myiniSet.user3.m_snakeSpeed = fast;myiniSet.otherSet.m_gameTimeLeft = 300;//游戏时间限制,不能超过3599(一小时)myiniSet.otherSet.m_foodsCoinsides = 100;//食物总数,食物总数不能少于10要不然要出bug,假设m_foodsCoinsides = 2,那当余下食物剩余1时是应该判断食物被吃3/4还是食物只剩下2个呢?myiniSet.otherSet.m_totalSpeed = 50; //游戏速度,数值越小越快,,,不能太小,要不然可能出bugassert((myiniSet.user1.m_race == "human" || myiniSet.user1.m_race == "robot") &&(myiniSet.user2.m_race == "human" || myiniSet.user2.m_race == "robot") &&(myiniSet.user3.m_race == "human" || myiniSet.user3.m_race == "robot"));//防止输错字cout << "作者の坦白" << endl;getch();cout << "\n我只做了游戏的主体部分,,,没做菜单界面,,,,,,," << endl;cout << "因为,,我对菜单界面有太多的想法,,,没个把月做不出来" << endl;cout << "而且我现在又必须要复习数学和物理,,,,,,,,,,," << endl;cout << "而且我感觉用继承和多态来做菜单界面会更简单,,,,,," << endl;cout << "但我现在还不太懂继承和多态,,所以我打算把继承和多态再学一学之后再来做菜单界面" << endl;getch();cout << "\n游戏操作:user1 :上下左右wsad," << endl;cout << "游戏操作:user2 :上下左右箭头操作," << endl;cout << "游戏操作:user3 :上下左右ujhl," << endl;getch();cout << "\n若打开程序后一直闪屏,请右击窗口,属性,布局,在窗口大小那儿把宽度改为 140,高度改为 43(每个在闪的窗口都要改)" << endl;cout << "这是因为用了双缓冲之后程序在两个窗口切的原因!!!!!双缓冲太恶心了!!!!(╯^╰〉" << endl;cout << "下次写程序再也不用双缓冲了!!!" << endl;getch();cout << "\n现在也请大家手动右击窗口,属性,布局,检查一下窗口大小是不是宽 140,高43,因为有时候改不了,,," << endl;cout << "待会如果打开游戏之后屏幕在闪就得改这个(~ ̄▽ ̄)~[]" << endl;getch();cout << "游戏说明:" << endl;cout << "\n在游戏时间结束之前吃尽可能多的食物" << endl;cout << "尽可能的长(zhang)长(chang)" << endl;cout << "然后干掉其他的蛇!" << endl;getch();cout << "\n只有 1 个幸存者!" << endl;getch();cout << "\n\n作者:DLV  " << endl;cout << "QQ:1668568309" << endl;getch();cout << "请确保输入法为英文" << endl;getch();cout << "\n\n准备开始游戏!" << endl;getch();try {snakeGame snakeGameStart(myiniSet);}catch (runtime_error err) {cout << err.what() << endl;}cout << "Y(^_^)Y游戏结束Y(^_^)Y" << endl;getch();return 0;
}

2300多行啊!!!就算去掉注释和水代码的部分都至少有1200行!

可能代码有一点杂,因为写到后面加了一些很杂的功能,而且要debug!!!于是代码就有点杂乱了。。。这并不是什么问题!

最后还是用这句对我触动很大的话来结尾吧!
you are noy here for code, you are here for ship product ;Jamie Zawinski

2020.7.13补充:
关于snakeGame::showRTInfo(string whichScreen)函数中对迭代器的处理的详细解释:删除迭代器后如何继续遍历容器

c语言c++三人/双人贪吃蛇源码,双缓冲,多线程,AI蛇,吃啥长啥相关推荐

  1. 贪吃蛇c语言经典程序下载,c语言贪吃蛇源码

    c语言贪吃蛇源码是一款非常经典的休闲益智游戏.c语言贪吃蛇源码游戏画面采用了像素风格,并添加了5中游戏模式,为玩家带来更有趣的游戏体验,有兴趣的玩家可以下载c语言贪吃蛇源码玩玩,回忆下童年的乐趣. 游 ...

  2. 贪吃蛇php源代码,GitHub - fusijie/Snake: CocoaChina Cocos2d-x编程大战:贪吃蛇源码汇总...

    ##CocoaChina 编程大战 贪吃蛇源码汇总 ###活动地址: ###获奖地址: ###感谢他们的分享(以下排名不分先后) 1.fireblackill First blood!第一个参与的活动 ...

  3. C++实现贪吃蛇源码

    C++实现贪吃蛇源码 大一刚入学,算是自学的小测验,虽然代码有点丑,但是自我感觉还是蛮好的哈哈哈,毕竟全是自己一个人从无到有写出来的. #include<iostream> #includ ...

  4. 易语言从html中获取验证码,易语言过腾讯滑块验证码模块源码和调用例程

    易语言过腾讯滑块验证码模块源码和调用例程.版本 2 .支持库 BmpOperate .支持库 spec .程序集 程序集1 .子程序 _启动子程序, 整数型, , 请在本子程序中放置易模块初始化代码 ...

  5. 微信小程序:独家微信社群人脉小程序源码带后端控制源码完整版端控带简单教程

    这是一款小编自营的一款人脉小程序系统 小编自营大概有三个多月了吧一直没有给大家公开 本款小程序群二维码自动采集推送的,所以大家不用担心没有群难运营 小编运营几个月不靠一丝一毫的推广每天自然流量都是一千 ...

  6. Opencv多语言自然场景文本识别系统(源码&教程)

    1. 研究背景 人类在自然场景中可以快速定位并识别看到的文字信息,但是想要计算机做到和人类一样是比较困难的.开发人员一直想要让机器也能识别图像中的文字信息.当然,对于自然场景来说,图像中的信息复杂甚至 ...

  7. c语言实现udp 聊天程序代码,C语言用UDP 实现局域网聊天程序源码.doc

    您所在位置:网站首页 > 海量文档 &nbsp>&nbsp计算机&nbsp>&nbspC/C++资料 C语言用UDP 实现局域网聊天程序源码.doc1 ...

  8. mysql 线程池源码模块_易语言Mysql线程池2.0模块源码

    易语言Mysql线程池2.0模块源码 易语言Mysql线程池2.0模块源码 系统结构:GetThis,初始化,关闭类线程,线程_测试,其他_附加文本,连接池初始化,取mysql句柄,释放mysql句柄 ...

  9. C语言实现哈希表(附完整源码)

    C语言实现哈希表 C语言实现哈希表附完整源码 C语言实现哈希表附完整源码 #include<stdio.h> #include<stdlib.h> #define SUCCES ...

  10. Java的三种代理模式完整源码分析

    Java的三种代理模式&完整源码分析 Java的三种代理模式&完整源码分析 参考资料: 博客园-Java的三种代理模式 简书-JDK动态代理-超详细源码分析 [博客园-WeakCach ...

最新文章

  1. aml linux 环境,各位辛苦编译DSDT.aml的兄弟,linux是个好工具啊,简单提取
  2. flask 与layui冲突
  3. 【PM模块】技术对象管理
  4. input-text
  5. linux 查看cpu
  6. 理查德·汉明和他的汉明码
  7. tensorflow.python.framework.errors_impl.NotFoundError: libnvinfer.so.5: cannot open shared object fi
  8. 【noip模拟赛3】编码
  9. [XSY] 简单的博弈题(博弈+dp+组合数+容斥)
  10. Delphi XE2 之 FireMonkey 入门(18) - TLang(多语言切换的实现)
  11. Java ArrayList removeRange()方法与示例
  12. Android开发继承webview,WebView如何从当前的Android主题继承颜色?
  13. matlab2c使用c++实现matlab函数系列教程- polyint函数
  14. Android Multimedia框架总结(十七)音频开发基础知识
  15. These dependencies were not found:问题
  16. C语言的行指针和列指针
  17. 你还在纠结英语的发音问题吗,你还老记不住单词吗?
  18. XCTF|PWN-string-WP
  19. 盘点百度开放云编程马拉松八大亮点
  20. 校园招聘-2017携程秋招后台开发笔试编程题

热门文章

  1. idea导入导出 settings 设置文件
  2. 类似818tu.co微信小说分销系统设计之多公众号网页授权自动登录源码
  3. 实时公交api接口-车来了公交接口-API公交接口 -如何接入
  4. 神经网络用英文怎么说,神经网络算法的英文
  5. Msql自定义函数和存储过程
  6. 声卡测试音频100Hz~1KHz
  7. ios游戏开发 Sprite Kit教程:初学者 1
  8. http请求中关于SSL server certificate验证的trace细节
  9. QT中关于书签的操作
  10. 从图书馆进入网络刷题练习与考试平台