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

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 robot
		string 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 ranning
	bool 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) {
   
	//foods
	smallFood.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;
	//snake
	gamePlayer 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 bytes
	player1.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 bytes
	player2.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 bytes
	player3.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);

	//otherSetting
	m_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 map
		if (whichScreen == "A")printfSXY_A("▁", i, 2);
		if (whichScreen == "B")printfSXY_B("▁", i, 2);
	}
	for (int j = 3; j < InfoCornerHigh; ++j) {
   //left column
		if (whichScreen == "A")printfSXY_A("▕", 2, j);
		if (whichScreen == "B")printfSXY_B("▕", 2, j);
	}
	for (int j = 3; j < playScreenHigh; ++j) {
   //right column
		if (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 map
		if (whichScreen == "A")printfSXY_A("▔", i, playScreenHigh);
		if (whichScreen == "B")printfSXY_B("▔", i, playScreenHigh);
	}
	//左下角信息框
	for (int i = 4; i < InfoCornerWidth; i += 2) {
   //up of the LowerLeftConner
		if (whichScreen == "A")printfSXY_A("▔", i, InfoCornerHigh);
		if (whichScreen == "B")printfSXY_B("▔", i, InfoCornerHigh);
	}
	for (int j = InfoCornerHigh; j < playScreenHigh; ++j) {
   //right of the LowerLeftConner
		if (whichScreen == "A")printfSXY_A("▕", InfoCornerWidth, j);
		if (whichScreen == "B")printfSXY_B("▕", InfoCornerWidth, j);
	}
	if (whichScreen == "A")printfSXY_A("█", InfoCornerWidth, InfoCornerHigh);//upper right of the LowerLeftConner
	if (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
  • 61
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 18
    评论
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值