前些日子(几年前)在桌游店玩了一种很好玩的桌游:古玩大富翁
于是文哥把它做成了可以远程多人联机的游戏
这里是服务端代码
#define WIN32_LEAN_AND_MEAN #include#include #include #include #include #include #include #include #pragma comment(lib, "ws2_32.lib") #pragma warning(disable:4996) int m; // 最大玩家数量 const int specialNum = 2001102900; std::string specialstring = "2001102900"; char* ip = new char[16]; void getIP() { //1.初始化wsa WSADATA wsaData; int ret = WSAStartup(MAKEWORd(2, 2), &wsaData); if (ret == 0) { //2.获取主机名 char hostname[256]; ret = gethostname(hostname, sizeof(hostname)); if (ret != SOCKET_ERROR) { //3.获取主机ip HOSTENT* host = gethostbyname(hostname); if (host != NULL) { //4.转化为char*并拷贝返回 strcpy(ip, inet_ntoa(*(in_addr*)*host->h_addr_list)); } } } } int main() { std::cin >> m; getIP(); std::cout << "sever" << std::endl; // 启动Windows socket2 环境 //初始化套节字 WORD ver = MAKEWORd(2, 2); WSADATA dat; WSAStartup(ver, &dat); // 校验版本 if (2 != HIBYTE(dat.wVersion) || 2 != LOBYTE(dat.wVersion)) { // 版本不对 WSACleanup(); return 0; } //1 建立一个socket SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (INVALID_SOCKET == sock) { std::cout << "错误,建立socket失败" << std::endl; } else { std::cout << "建立socket成功.." << std::endl; } sockaddr_in sin = {}; sin.sin_family = AF_INET; sin.sin_port = htons(0000);//服务器端口号 sin.sin_addr.S_un.S_addr = inet_addr("0.0.0.0");//服务器私网 //2 bind 绑定用于接受客户端链接的网络端口 if (SOCKET_ERROR == bind(sock, (sockaddr*)&sin, sizeof(sin))) { std::cout << "绑定网络端口失败" << std::endl; } else { std::cout << "绑定网络端口成功" << std::endl; } //3. listen监听网络端口 if (SOCKET_ERROR == listen(sock, 4)) // 5表示最多监听5个用户 { std::cout << "监听网络端口失败" << std::endl; } //初始化随机函数 time_t t = 0; t = time(0); srand((unsigned int)t); int num = 0; sockaddr_in* clientAddr = new sockaddr_in[4]; int* nAddrLen = new int[4]; SOCKET* cSock = new SOCKET[4]; std::string msg = "Hello, I'm Server"; char recvBuf[256] = {}; std::string s; while (true) { for (int i = 0; i < 4; i++) { clientAddr[i] = {}; nAddrLen[i] = sizeof(sockaddr_in); cSock[i] = INVALID_SOCKET; } int* rr = new int[m]; for (int i = 0; i < m; i++) { rr[i] = -1; } int r; for (int i = 0; i < m; i++) { do { r = rand() % m; } while (rr[r] != -1); rr[r] = i; } while (true) { cSock[rr[num]] = accept(sock, (sockaddr*)&clientAddr[rr[num]], &nAddrLen[rr[num]]); if (INVALID_SOCKET != cSock[rr[num]]) { std::cout << "新客户端加入:" << inet_ntoa(clientAddr[rr[num]].sin_addr) << std::endl; std::cout << cSock[rr[num]] << std::endl; s = std::to_string(rr[num]); send(cSock[rr[num]], s.c_str(), 1, 0); std::cout << "num" << rr[num] << std::endl; num++; if (num == m) { break; } } } int listen = 0; s = "yes" + std::to_string(m); for (int i = 0; i < m; i++) { send(cSock[i], s.c_str(), 4, 0); } //开始游戏 int nlen; nlen = recv(cSock[listen], recvBuf, 15, 0); if (nlen <= 0) { break; } s = recvBuf; while (s[s.length() - 1] == 'a') { s = s.substr(0, s.length() - 1); } std::cout << "接收到玩家" << listen << "的信息:" << s << std::endl; if (s.length() > 8 && s.compare(0, 8, specialstring, 0, 8) == 0) { //特殊信息 if (s[9] == '0') { num = rand(); s = std::to_string(num); while (s.length() < 15) { s = s + "a"; } for (int i = 0; i < m; i++) { send(cSock[i], s.c_str(), 15, 0); } } } else if (s.length() > 4 && s.compare(0, 4, "next", 0, 4) == 0) { //切换玩家 listen = s[5] - '0'; } else if (s.length() > 3 && s.compare(0, 3, "win", 0, 3) == 0) { //获胜 break; } else { //转发信息给其他玩家 for (int i = 0; i < m; i++) { if (i == listen) { continue; } while (s.length() < 15) { s = s + "a"; } send(cSock[i], s.c_str(), 15, 0); } } while (true) { } } //6 关闭套接字closesocket closesocket(sock); //清楚Windows socket环境 WSACleanup(); std::cin >> s; }
这里是客户端代码
#define WIN32_LEAN_AND_MEAN #include#include #include #include #include #include #include #include #include #include #include #include #pragma comment(lib, "ws2_32.lib") #pragma warning(disable:4996) //定义屏幕尺寸常量 const int SCREEN_WIDTH = 1000; const int SCREEN_HEIGHT = 640; std::string qqNum = ""; SDL_Surface** qqSurface = new SDL_Surface * [4]; SDL_Texture** qqTexture = new SDL_Texture * [4]; std::string* name = new std::string[4]; int n; // 玩家数量 int* money; // 玩家金币 int* pos; // 玩家位置 bool* ifstop; // 玩家是否被暂停 bool** friends; // 玩家的伙计 int** antique; // 玩家的古董 int* antiqueNum;//玩家古董总数 int* city = new int[39]; // 地区所属 int* bank = new int[8]; // 银行的古董 int* village = new int[8]; // 古董村的古董 int villageHave; int* fri = new int[4]; // 剩余伙计,村长1,冒险家4,卦师2(骰两次),村民1(二选一) int* an = new int[8]; // 古董价值 int dice; // 骰子 int displayHelp = -1; int me; bool quit = false; int winer = -1; int now = 0; const int specialNum = 2001102900; SDL_Window* window = NULL; SDL_Surface* screenSurface = NULL; SDL_Renderer* renderer = NULL; TTF_Font* font = NULL; SDL_Color coloryellow = { 255, 255, 0 ,255 }; SDL_Color colorred = { 255, 0,0,255 }; SDL_Color colorgreen = { 0, 255,0,255 }; SDL_Color colorblue = { 0, 0,255,255 }; SDL_Color colorpurple = { 255, 0,255,255 }; SDL_Color colorlittleblue = { 0, 255,255,255 }; SDL_Color colorblack = { 0, 0, 0, 255 }; SDL_Surface* text1 = NULL; SDL_Texture* text1tex = NULL; SDL_Rect rect1; SDL_Rect rect2; //SDL_Rect rectFri; //SDL_Rect rectAntiqueMin; //SDL_Rect rectPlayer; //SDL_Rect rectStar; SDL_Rect rectDice; SDL_Rect rectDice1; SDL_Rect rectDice2; SDL_Rect rectAntique1; SDL_Rect rectAntique2; SDL_Rect rectHelp; int lastRand = 6; int antique1 = 0;//古董村抽取的古董 int antique2 = 0; std::string s1; SDL_Surface* beginBackground = NULL;//原版背景 SDL_Surface* nowBackground = NULL;//当前背景 SDL_Surface** friendsPhoto = new SDL_Surface * [4];//伙计图案 SDL_Surface** playerFlag = new SDL_Surface * [4]; SDL_Surface** antiqueminSur = new SDL_Surface * [8]; SDL_Surface* starPhoto;//星星图片 SDL_Texture** playerPhoto = new SDL_Texture * [4];//玩家头像 SDL_Texture** dicePhoto = new SDL_Texture * [9];//骰子图片 SDL_Texture** antiqueminPhoto = new SDL_Texture * [8];//古董小标志图片 SDL_Texture** help = new SDL_Texture * [9]; // 提示 SDL_Texture** helpFri = new SDL_Texture * [4]; // 伙伴提示 SDL_Texture** helpAn = new SDL_Texture * [8]; int* specialHelp = new int[9]; int questionNum = -1; SDL_Texture** texQuestion = new SDL_Texture * [30]; SDL_Rect rectQuestion; SDL_Rect rectQuestion1; int* posx = new int[4];//玩家信息坐标 int* posy = new int[4]; int* cityposx = new int[39];//城市坐标 int* cityposy = new int[39]; int* cityRank = new int[39];//城市等级 int** cityValue = new int* [39];//城市赚钱数量 int* cityNeed = new int[39];//城市购买价值 SOCKET sock; sockaddr_in sin0 = {}; char* ip = new char[16]; bool ifListeningMe = false; char recvBuf[256] = {}; //玩家鼠标输入决定 bool needJudge = false; int judgeNum = -1; bool needRand = false; bool needFly = false; bool needRob = false; bool needBuy = false; bool needFri = false; bool needSell = false; bool needPledge = false; void getIP() { //1.初始化wsa WSADATA wsaData; int ret = WSAStartup(MAKEWORd(2, 2), &wsaData); if (ret == 0) { //2.获取主机名 char hostname[256]; ret = gethostname(hostname, sizeof(hostname)); if (ret != SOCKET_ERROR) { //3.获取主机ip HOSTENT* host = gethostbyname(hostname); if (host != NULL) { //4.转化为char*并拷贝返回 strcpy(ip, inet_ntoa(*(in_addr*)*host->h_addr_list)); } } } } //从服务器获取数字,若是随机数则listen为2001102900 int acquire(int listen = now) { if (ifListeningMe) { if (listen == specialNum) { std::string s = std::to_string(listen); while (s.length() < 15) { s = s + "a"; } std::cout << "send: " << s << std::endl; send(sock, s.c_str(), 15, 0); } else { std::string s = "next " + std::to_string(listen); while (s.length() < 15) { s = s + "a"; } send(sock, s.c_str(), 15, 0); ifListeningMe = false; std::cout << "listen: " << listen << std::endl; } } while (true) { int nlen = recv(sock, recvBuf, 15, 0); std::string s = recvBuf; if (nlen > 0) { while (s[s.length() - 1] == 'a') { s = s.substr(0, s.length() - 1); } std::cout << "receive: " << recvBuf << std::endl; return atoi(recvBuf); } } } //向服务器发送信息 void send0(std::string s) { ifListeningMe = true; while (s.length() < 15) { s = s + "a"; } std::cout << "listen me!" << std::endl; std::cout << "send: " << s << std::endl; send(sock, s.c_str(), 15, 0); } void send0(int o) { send0(std::to_string(o)); } //显示文字 void display(std::string s, int x, int y, double d, SDL_Color c) { SDL_Rect rect3; text1 = TTF_RenderText_Blended(font, s.data(), c); if (text1 == NULL) { std::cout << SDL_GetError() << std::endl; std::cout << TTF_GetError() << std::endl; std::cout << s << std::endl; } text1tex = SDL_CreateTextureFromSurface(renderer, text1); rect3.x = x; rect3.y = y; rect3.w = text1->w * d; rect3.h = text1->h * d; SDL_RenderCopy(renderer, text1tex, NULL, &rect3); SDL_FreeSurface(text1); SDL_DestroyTexture(text1tex); } void displayFlag(int c, int p) { rect1.x = cityposx[c]; rect1.y = cityposy[c] + 60; rect1.w = 20; rect1.h = 20; if (p == -1) { SDL_BlitSurface(beginBackground, &rect1, nowBackground, &rect1); } else { SDL_BlitSurface(playerFlag[p], NULL, nowBackground, &rect1); } } void displayAntique(int p) { rect1.x = posx[p]; rect1.y = posy[p] + 160; rect1.w = 180; rect1.h = 120; SDL_BlitSurface(beginBackground, &rect1, nowBackground, &rect1); rect1.w = 30; rect1.h = 30; int i = 0; for (int j = 2; j < 8; j++) { for (int k = 0; k < antique[p][j]; k++) { SDL_BlitSurface(antiqueminSur[j], NULL, nowBackground, &rect1); rect1.x += 30; i++; if (i == 6) { rect1.x -= 180; rect1.y += 30; i = 0; } } } } class message { public: message(); ~message(); void newmes(int, int); int* num = new int[4]; uint32_t* time = new uint32_t[4]; void displayMes(); private: }; message::message() { for (int i = 0; i < 4; i++) { time[i] = 0; } } message::~message() { } void message::newmes(int n, int p) { time[p] = SDL_GetTicks() + 3000; num[p] = n; } void message::displayMes() { uint32_t now = SDL_GetTicks(); std::string s; for (int i = 0; i < n; i++) { if (time[i] > now) { s = std::to_string(num[i]); if (num[i] > 0) { s = "+" + s; } display(s.c_str(), posx[i] + 30, posy[i], 2, colorblue); } } } message* mes = new message; //a给b钱 void give(int a, int b, int m) { if (a == -1) // 银行 { money[b] += m; mes->newmes(m, b); return; } int mm = money[a]; int mm1; int mm2; money[a] -= m; while (money[a] < 0) { //abb //a 1:伙计 2:古董 3:建筑物 //bb 序号 if (now == me) { questionNum = 13; needPledge = true; while (needPledge) { SDL_Delay(100); } send0(judgeNum); } else { judgeNum = acquire(); } mm1 = judgeNum / 100; mm2 = judgeNum % 100; if (b == -1) { switch (mm1) { case 1: fri[mm2]++; friends[a][mm2] = false; rect1.x = posx[a] + mm2 * 40; rect1.y = posy[a] + 280; rect1.h = 40; rect1.w = 40; SDL_BlitSurface(beginBackground, &rect1, nowBackground, &rect1); money[a] += 300; break; case 2: antique[a][mm2]--; bank[mm2]++; antiqueNum[a]--; displayAntique(a); money[a] += an[mm2]; break; case 3: city[mm2] = b; displayFlag(mm2, b); if (mm2 == 7) { displayFlag(33, b); } else if (mm2 == 20) { displayFlag(29, b); } money[a] += cityNeed[mm2]; break; default: break; } } else { switch (mm1) { case 1: if (!friends[b][mm2]) { friends[b][mm2] = true; friends[a][mm2] = false; rect1.x = posx[a] + mm2 * 40; rect1.y = posy[a] + 280; rect1.h = 40; rect1.w = 40; SDL_BlitSurface(beginBackground, &rect1, nowBackground, &rect1); rect1.x = posx[b] + mm2 * 40; rect1.y = posy[b] + 280; rect1.h = 40; rect1.w = 40; SDL_BlitSurface(friendsPhoto[mm2], NULL, nowBackground, &rect1); money[a] += 300; } break; case 2: antique[a][mm2]--; antique[b][mm2]++; antiqueNum[a]--; antiqueNum[b]++; displayAntique(a); displayAntique(b); money[a] += an[mm2]; break; case 3: city[mm2] = b; displayFlag(mm2, b); if (mm2 == 7) { displayFlag(33, b); } else if (mm2 == 20) { displayFlag(29, b); } money[a] += cityNeed[mm2]; break; default: break; } } } questionNum = -1; if (money[a] > mm) { money[a] = mm; } if (b != -1) { money[b] += mm - money[a]; mes->newmes(mm - money[a], b); } mes->newmes(money[a] - mm, a); } //初始化数据 void numsInit() { money = new int[n]; pos = new int[n]; ifstop = new bool[n]; friends = new bool* [n]; antique = new int* [n]; antiqueNum = new int[n]; for (int i = 0; i < n; i++) { money[i] = 10000; pos[i] = 0; ifstop[i] = false; friends[i] = new bool[4]; antiqueNum[i] = 1; for (int j = 0; j < 4; j++) { friends[i][j] = false; } antique[i] = new int[8]; for (int j = 2; j < 8; j++) { antique[i][j] = 0; } antique[i][2] = 1; } for (int i = 0; i < 39; i++) { city[i] = -1; cityRank[i] = 0; cityValue[i] = new int[4]; } for (int i = 2; i < 8; i++) { bank[i] = 0; } an[2] = 200; an[3] = 500; an[4] = 1000; an[5] = 1500; an[6] = 2000; an[7] = 3000; fri[0] = 1; fri[1] = 4; fri[2] = 2; fri[3] = 1; village[2] = 7 - n; village[3] = 6; village[4] = 5; village[5] = 3; village[6] = 2; village[7] = 1; villageHave = 24 - n; posx[0] = 640; posx[1] = 820; posx[2] = 640; posx[3] = 820; posy[0] = 0; posy[1] = 0; posy[2] = 320; posy[3] = 320; for (int i = 0; i < 8; i++) { cityposx[i] = 0; cityposy[i] = 560 - i * 80; } cityposx[8] = 80; cityposy[8] = 0; for (int i = 9; i < 13; i++) { cityposx[i] = 160; cityposy[i] = (i - 9) * 80; } for (int i = 13; i < 18; i++) { cityposx[i] = 240 + (i - 13) * 80; cityposy[i] = 240; } cityposx[18] = 560; cityposy[18] = 160; cityposx[19] = 560; cityposy[19] = 80; for (int i = 20; i < 24; i++) { cityposx[i] = 560 - (i - 20) * 80; cityposy[i] = 0; } for (int i = 24; i < 27; i++) { cityposx[i] = 320; } cityposy[24] = 80; cityposy[25] = 160; cityposy[26] = 320; for (int i = 27; i < 31; i++) { cityposy[i] = 400; cityposx[i] = 320 + (i - 27) * 80; } cityposx[31] = 560; cityposy[31] = 480; for (int i = 32; i < 39; i++) { cityposx[i] = 560 - (i - 32) * 80; cityposy[i] = 560; } //rectAntiqueMin.x = 0; //rectAntiqueMin.y = 0; //rectAntiqueMin.w = 30; //rectAntiqueMin.h = 30; //rectFri.x = 0; //rectFri.y = 0; //rectFri.w = 40; //rectFri.h = 40; //rectPlayer.x = 0; //rectPlayer.y = 0; //rectPlayer.w = 20; //rectPlayer.h = 20; //rectStar.x = 0; //rectStar.y = 0; //rectStar.w = 17; //rectStar.h = 16; rectDice.x = 408; rectDice.y = 327; rectDice.w = 64; rectDice.h = 66; rectDice1.x = 408; rectDice1.y = 325; rectDice1.w = 64; rectDice1.h = 69; rectDice2.x = 408; rectDice2.y = 328; rectDice2.w = 64; rectDice2.h = 63; rectAntique1.x = 105; rectAntique1.y = 425; rectAntique1.w = 30; rectAntique1.h = 30; rectAntique2.x = 265; rectAntique2.y = 425; rectAntique2.w = 30; rectAntique2.h = 30; rectHelp.x = 80; rectHelp.y = 320; rectHelp.w = 240; rectHelp.h = 240; cityValue[1][0] = 100; cityValue[1][1] = 500; cityValue[1][2] = 1000; cityValue[1][3] = 3000; cityNeed[1] = 1800; cityValue[2][0] = 200; cityValue[2][1] = 700; cityValue[2][2] = 1500; cityValue[2][3] = 4000; cityNeed[2] = 2400; cityValue[4][0] = 200; cityValue[4][1] = 700; cityValue[4][2] = 1500; cityValue[4][3] = 4000; cityNeed[4] = 2400; cityValue[5][0] = 200; cityValue[5][1] = 600; cityValue[5][2] = 1500; cityValue[5][3] = 3500; cityNeed[5] = 2200; cityValue[8][0] = 400; cityValue[8][1] = 1000; cityValue[8][2] = 2000; cityValue[8][3] = 4500; cityNeed[8] = 5000; cityValue[9][0] = 400; cityValue[9][1] = 1000; cityValue[9][2] = 2000; cityValue[9][3] = 4500; cityNeed[9] = 5000; cityValue[10][0] = 300; cityValue[10][1] = 900; cityValue[10][2] = 2000; cityValue[10][3] = 5000; cityNeed[10] = 3000; cityValue[12][0] = 300; cityValue[12][1] = 900; cityValue[12][2] = 2000; cityValue[12][3] = 5000; cityNeed[12] = 3000; cityValue[13][0] = 300; cityValue[13][1] = 1000; cityValue[13][2] = 2500; cityValue[13][3] = 5500; cityNeed[13] = 3400; cityValue[26][0] = 400; cityValue[26][1] = 1200; cityValue[26][2] = 3000; cityValue[26][3] = 7000; cityNeed[26] = 4000; cityValue[27][0] = 300; cityValue[27][1] = 1100; cityValue[27][2] = 2500; cityValue[27][3] = 6500; cityNeed[27] = 3800; cityValue[28][0] = 300; cityValue[28][1] = 1100; cityValue[28][2] = 2500; cityValue[28][3] = 6500; cityNeed[28] = 3800; cityValue[30][0] = 400; cityValue[30][1] = 1200; cityValue[30][2] = 2500; cityValue[30][3] = 5500; cityNeed[30] = 5800; cityValue[31][0] = 400; cityValue[31][1] = 1200; cityValue[31][2] = 2500; cityValue[31][3] = 5500; cityNeed[31] = 5800; cityValue[34][0] = 200; cityValue[34][1] = 800; cityValue[34][2] = 2000; cityValue[34][3] = 4500; cityNeed[34] = 2700; cityValue[35][0] = 200; cityValue[35][1] = 800; cityValue[35][2] = 2000; cityValue[35][3] = 4500; cityNeed[35] = 2700; cityValue[37][0] = 200; cityValue[37][1] = 600; cityValue[37][2] = 1500; cityValue[37][3] = 3500; cityNeed[37] = 2300; cityValue[6][0] = 2; cityValue[6][1] = 3; cityValue[6][2] = 4; cityValue[6][3] = 5; cityNeed[6] = 3000; cityValue[17][0] = 2; cityValue[17][1] = 3; cityValue[17][2] = 4; cityValue[17][3] = 5; cityNeed[17] = 3000; cityValue[32][0] = 2; cityValue[32][1] = 3; cityValue[32][2] = 4; cityValue[32][3] = 5; cityNeed[32] = 3000; cityValue[38][0] = 2; cityValue[38][1] = 3; cityValue[38][2] = 4; cityValue[38][3] = 5; cityNeed[38] = 3000; cityNeed[3] = 3000; cityNeed[7] = 3000; cityNeed[20] = 3000; } void thread1() { bool b = true; int o = 0; int o1 = 0; //游戏逻辑 while (!quit) { if (winer != -1) { break; } while (ifstop[now]) { ifstop[now] = false; now++; if (now == n) { now = 0; } } b = false; std::cout << std::endl; std::cout << "now: " << now << std::endl; if (pos[now] == 3 && (city[3] == now || money[now] >= 1000)) // 机场 { if (now == me) { questionNum = 2; needJudge = true; while (needJudge) { SDL_Delay(100); } questionNum = -1; send0(judgeNum); } else { judgeNum = acquire(); } if (judgeNum) { std::cout << "fly!" << std::endl; //飞 if (now != city[3]) { money[now] -= 1000; mes->newmes(-1000, now); if (city[3] != -1) { money[city[3]] += 1000; mes->newmes(1000, city[3]); } } if (now == me) { questionNum = 7; needFly = true; while (needFly) { SDL_Delay(100); } send0(judgeNum); questionNum = -1; } else { judgeNum = acquire(); } pos[now] = judgeNum; b = true; if (pos[now] == 0) { money += 2000; mes->newmes(2000, now); if (money[now] >= 20000) { winer = now; break; //获胜 } } } } if (!b) { //获取输入 if (now == me) { questionNum = 12; needRand = true; while (needRand) { SDL_Delay(100); } questionNum = -1; send0(123); } else { judgeNum = acquire(); } dice = acquire(specialNum) % 6 + 1; // 骰子 lastRand = dice; b = true; if (pos[now] == 14) // 交叉路口 { //获取输入 if (now == me) { questionNum = 5; needJudge = true; while (needJudge) { SDL_Delay(100); } send0(judgeNum); questionNum = -1; } else { judgeNum = acquire(); } if (judgeNum) { b = false; pos[now] = 25; } } pos[now] += dice; // 前进 if (pos[now] - dice <= 25 && pos[now] > 25 && b) { if (pos[now] == 26) { pos[now] = 14; } else { pos[now]--; } } } if (pos[now] > 38) { pos[now] -= 39; money[now] += 2000; mes->newmes(2000, now); if (money[now] >= 20000) { winer = now; break; //获胜 } } std::cout << "goto: " << pos[now] << std::endl; std::cout << std::endl; switch (pos[now]) // 触发地盘事件 { case 0: case 14: o = 0; for (int i = 0; i < 4; i++) { if (!friends[now][i]) { o += fri[i]; } } if (o == 0) { break; } if (money[now] >= 300) { //获取输入 if (now == me) { questionNum = 1; needJudge = true; while (needJudge) { SDL_Delay(100); } send0(judgeNum); questionNum = -1; } else { judgeNum = acquire(); } if (judgeNum) { //获得伙计 money[now] -= 300; mes->newmes(-300, now); o = acquire(specialNum) % o + 1; for (int i = 0; i < 4; i++) { if (!friends[now][i]) { o -= fri[i]; } if (o <= 0) { std::cout << "acquire friend: " << i << std::endl; fri[i]--; friends[now][i] = true; rect1.x = posx[now] + i * 40; rect1.y = posy[now] + 280; rect1.w = 40; rect1.h = 40; SDL_BlitScaled(friendsPhoto[i], NULL, nowBackground, &rect1); break; } } } } break; case 3: //飞机场 if (city[3] == -1 && money[now] >= 3000) { //获取输入 if (now == me) { questionNum = 0; needJudge = true; while (needJudge) { SDL_Delay(100); } questionNum = -1; send0(judgeNum); } else { judgeNum = acquire(); } if (judgeNum) { std::cout << "player" << now << " buy airport" << std::endl; money[now] -= 3000; mes->newmes(-3000, now); city[3] = now; displayFlag(3, now); } } break; case 7: case 33: //市场 b = false; for (int i = 2; i < 8; i++) { if (bank[i] != 0 && money[now] > an[i]) { b = true; break; } } if (b) { //获取输入 while (true) { if (now == me) { questionNum = 11; rect1.y = 400; rect1.w = 30; rect1.h = 30; for (int i = 2; i < 8; i++) { rect1.x = 20 + i * 30; if (bank[i] != 0) { SDL_BlitSurface(antiqueminSur[i], NULL, nowBackground, &rect1); } } needBuy = true; while (needBuy) { SDL_Delay(100); } send0(judgeNum); } else { judgeNum = acquire(); } if (judgeNum == 0) { if (questionNum != -1) { questionNum = -1; rect1.y = 400; rect1.x = 80; rect1.w = 180; rect1.h = 30; SDL_BlitSurface(beginBackground, &rect1, nowBackground, &rect1); } break; } else if (an[judgeNum] <= money[now]) { bank[judgeNum]--; antique[now][judgeNum]++; antiqueNum[now]++; money[now] -= an[judgeNum]; mes->newmes(-an[judgeNum], now); if (city[7] != now && city[7] != -1) { money[city[7]] += an[judgeNum]; mes->newmes(an[judgeNum], city[7]); } displayAntique(now); std::cout << "player" << now << " buy antique" << judgeNum << std::endl; if (questionNum != -1) { questionNum = -1; rect1.y = 400; rect1.x = 80; rect1.w = 180; rect1.h = 30; SDL_BlitSurface(beginBackground, &rect1, nowBackground, &rect1); } break; } } } if (city[7] == -1 && money[now] >= 3000) { //获得输入 if (now == me) { questionNum = 0; needJudge = true; while (needJudge) { SDL_Delay(100); } questionNum = -1; send0(judgeNum); } else { judgeNum = acquire(); } if (judgeNum) { std::cout << "player" << now << " buy market" << std::endl; money[now] -= 3000; mes->newmes(-3000, now); displayFlag(33, now); displayFlag(7, now); city[7] = now; } } break; case 11: case 36: //盘口 //获取输入 if (now == me) { questionNum = 12; needRand = true; while (needRand) { SDL_Delay(100); } send0(123); questionNum = -1; } else { judgeNum = acquire(); } dice = acquire(specialNum) % 6 + 1; lastRand = dice; if (friends[now][1]) { dice++; } else if (dice == 1) { break; } b = false; for (int i = 2; i <= dice; i++) { for (int j = 0; j < n; j++) { if (j != now && antique[j][i] != 0) { b = true; break; } } if (b) { break; } } if (!b) { break; } //获取输入,抢夺 if (now == me) { questionNum = 8; needRob = true; while (needRob) { SDL_Delay(100); } send0(judgeNum); questionNum = -1; } else { judgeNum = acquire(); } std::cout << "player" << now << " dice" << dice << " rob" << judgeNum << std::endl; //抢夺 antique[judgeNum / 10][judgeNum % 10]--; antiqueNum[judgeNum / 10]--; displayAntique(judgeNum / 10); antique[now][judgeNum % 10]++; antiqueNum[now]++; displayAntique(now); break; case 15: case 16: case 18: case 19: case 21: case 22: case 24: case 25: //古董村 if (villageHave == 0) { std::cout << "village doesn't have antique" << std::endl; break; } //抽古董 o = acquire(specialNum) % villageHave + 1; if (friends[now][3] && villageHave != 1) { do { std::cout << "double" << std::endl; o1 = acquire(specialNum) % villageHave + 1; } while (o == o1); for (int i = 2; i < 8; i++) { o -= village[i]; if (o <= 0) { o = i; break; } } for (int i = 2; i < 8; i++) { o1 -= village[i]; if (o1 <= 0) { o1 = i; SDL_BlitSurface(antiqueminSur[o], NULL, nowBackground, &rectAntique1); SDL_BlitSurface(antiqueminSur[o1], NULL, nowBackground, &rectAntique2); break; } } //获取输入 if (now == me) { questionNum = 4; needJudge = true; while (needJudge) { SDL_Delay(100); } questionNum = -1; send0(judgeNum); } else { judgeNum = acquire(); } SDL_BlitSurface(beginBackground, &rectAntique2, nowBackground, &rectAntique2); SDL_BlitSurface(beginBackground, &rectAntique1, nowBackground, &rectAntique1); if (judgeNum) { o = o1; } } else { for (int i = 2; i < 8; i++) { o -= village[i]; if (o <= 0) { o = i; break; } } } SDL_BlitSurface(antiqueminSur[o], NULL, nowBackground, &rectAntique1); //骰骰子 //获取输入 if (now == me) { questionNum = 12; needRand = true; while (needRand) { SDL_Delay(100); } send0(123); questionNum = -1; } else { std::cout << "dice------------" << std::endl; judgeNum = acquire(); } judgeNum = acquire(specialNum) % 6 + 1; lastRand = judgeNum; if (friends[now][1]) { judgeNum++; } if (judgeNum < o && friends[now][2]) { //获取输入 if (now == me) { questionNum = 12; needRand = true; while (needRand) { SDL_Delay(100); } send0(123); questionNum = -1; } else { judgeNum = acquire(); } judgeNum = acquire(specialNum) % 6 + 1; lastRand = judgeNum; if (friends[now][1]) { judgeNum++; } } villageHave--; village[o]--; std::cout << "player" << now << " dice" << judgeNum << std::endl; if (judgeNum >= o) { std::cout << "player" << now << " acquire antique" << o << std::endl; antique[now][o]++; antiqueNum[now]++; displayAntique(now); //给村长钱 for (int i = 0; i < n; i++) { if (friends[i][0] && i != now) { if (money[now] > 400) { money[now] -= 400; money[i] += 400; mes->newmes(-400, now); mes->newmes(400, i); } else { give(now, i, 400); } } } } else { bank[o]++; } SDL_BlitSurface(beginBackground, &rectAntique1, nowBackground, &rectAntique1); break; case 23: //栈道 b = true; for (int i = 0; i < 4; i++) { if (friends[now][i]) { b = false; break; } } if (b) { ifstop[now] = true; } else { //获取输入 if (now == me) { needFri = true; questionNum = 10; while (needFri) { SDL_Delay(100); } questionNum = -1; send0(judgeNum); } else { judgeNum = acquire(); } friends[now][judgeNum] = false; fri[judgeNum]++; rect1.x = posx[now] + judgeNum * 40; rect1.y = posy[now] + 280; rect1.h = 40; rect1.w = 40; SDL_BlitSurface(beginBackground, &rect1, nowBackground, &rect1); } break; case 20: case 29: //酒馆 if (city[20] == now) { break; } o = 0; for (int i = 0; i < 4; i++) { o += friends[now][i]; } o *= 500; if (o > money[now]) { give(now, city[20], o); } else { if (o != 0) { money[now] -= o; mes->newmes(-o, now); } if (city[20] != -1) { if (o != 0) { money[city[20]] += o; mes->newmes(o, city[20]); } } else if (money[now] >= 3000) { //获取输入 if (now == me) { questionNum = 0; needJudge = true; while (needJudge) { SDL_Delay(100); } questionNum = -1; send0(judgeNum); } else { judgeNum = acquire(); } if (judgeNum) { money[now] -= 3000; mes->newmes(-3000, now); displayFlag(20, now); displayFlag(29, now); city[20] = now; } } } break; default: //城市 bool b2 = false; if (pos[now] == 9 || pos[now] == 31) { b2 = true; } if (city[pos[now] - b2] == -1) { //无人地盘 if (money[now] >= cityNeed[pos[now]]) { if (now == me) { needJudge = true; questionNum = 0; while (needJudge) { SDL_Delay(100); } questionNum = -1; send0(judgeNum); } else { judgeNum = acquire(); } if (judgeNum) { std::cout << "player" << now << " buy city" << std::endl; money[now] -= cityNeed[pos[now]]; mes->newmes(-cityNeed[pos[now]], now); city[pos[now] - b2] = now; displayFlag(pos[now] - b2, now); } } } else { //玩家地盘 o1 = city[pos[now] - b2];//地盘拥有者 if (antiqueNum[o1] > 0) { //决定要不要卖古董 if (o1 == me) { needJudge = true; questionNum = 3; while (needJudge) { SDL_Delay(100); } questionNum = -1; send0(judgeNum); } else { judgeNum = acquire(o1); } if (judgeNum) { //决定卖哪个古董 if (o1 == me) { questionNum = 9; needSell = true; while (needSell) { SDL_Delay(100); } questionNum = -1; send0(judgeNum); } else { judgeNum = acquire(o1); } int u = judgeNum; //决定是否加价 if (cityRank[pos[now] - b2] < 3) { if (o1 == me) { questionNum = 6; needJudge = true; while (needJudge) { SDL_Delay(100); } questionNum = -1; send0(judgeNum); } else { judgeNum = acquire(o1); } } else { judgeNum = 1; } //计算价格 o = an[u]; if (judgeNum) { int u1 = cityValue[pos[now] - b2][cityRank[pos[now] - b2]]; if (u1 < 10) { o *= (u1 + 1); } else { o += u1; } } else { rect1.x = cityposx[pos[now] - b2] + 17 * cityRank[pos[now] - b2] + 20; rect1.y = cityposy[pos[now] - b2] + 64; rect1.w = 17; rect1.h = 16; SDL_BlitSurface(starPhoto, NULL, nowBackground, &rect1); cityRank[pos[now] - b2]++; } //开始交易 antique[o1][u]--; antiqueNum[o1]--; displayAntique(o1); std::cout << "player" << o1 << "sell antique worth " << o << std::endl; if (o1 == now) { //和银行交易 bank[u]++; money[o1] += o; mes->newmes(o, o1); } else { //和玩家交易 if (money[now] >= o) { money[now] -= o; mes->newmes(-o, now); money[o1] += o; mes->newmes(o, o1); } else { give(now, o1, o); } antique[now][u]++; antiqueNum[now]++; displayAntique(now); } } } } break; } if (money[now] >= 20000) { std::cout << "player" << now << " win!" << std::endl; winer = now; break; //获胜 } now++; if (now == n) { now = 0; } } } void playGame() { numsInit(); if (me == 0) { ifListeningMe = true; } for (int i = 0; i < n; i++) { displayAntique(i); } std::thread t1(thread1); t1.detach(); uint32_t time = SDL_GetTicks(); uint32_t nowTime = SDL_GetTicks(); int mousex; int mousey; int mousex0; int mousey0; SDL_Event e; //获取玩家输入以及画面制作 while (!quit) { nowTime = SDL_GetTicks(); SDL_Delay(time + 33 > nowTime ? time + 33 - nowTime : 0);//控制为30帧每秒 time += 33; if (winer != -1) { break; } //获取输入 while (!quit) { if (SDL_PollEvent(&e) != 0) { //如果用户点击了关闭 if (e.type == SDL_QUIT) { std::cout << "Quit!" << std::endl; quit = true; winer = -2; break; } else if (e.type == SDL_MOUSEBUTTONDOWN && e.button.button == SDL_BUTTON_LEFT) { mousex = e.button.x; mousey = e.button.y; std::cout << "buttondown: " << mousex << ", " << mousey << std::endl; mousex0 = mousex / 80; mousey0 = mousey / 80; if (needJudge && mousey0 == 6) { if (mousex0 == 1) { //按下了T std::cout << "choose: T" << std::endl; judgeNum = 1; needJudge = false; } else if (mousex0 == 3) { //按下了F std::cout << "choose: F" << std::endl; judgeNum = 0; needJudge = false; } } else if (needRand && mousex0 == 5 && mousey0 == 4) { std::cout << "choose: dice" << std::endl; //按下骰子c needRand = false; } else if (needFly) { for (int i = 0; i < 39; i++) { if (mousex >= cityposx[i] && mousex < cityposx[i] + 80 && mousey >= cityposy[i] && mousey < cityposy[i] + 80) { judgeNum = i; needFly = false; break; } } } else if (needRob) { for (int i = 0; i < n; i++) { if (mousex >= posx[i] && mousex < posx[i] + 180 && mousey >= posy[i] + 160 && mousey < posy[i] + 280) { if (i != now) { judgeNum = 0; judgeNum += (mousex - posx[i]) / 30 + (mousey - posy[i] - 160) / 30 * 6; for (int j = 2; j <= dice; j++) { judgeNum -= antique[i][j]; if (judgeNum < 0) { judgeNum = j; judgeNum += i * 10; needRob = false; break; } } } break; } } } else if (needBuy) { if (mousex >= 80 && mousex < 260 && mousey >= 400 && mousey < 430) { judgeNum = 2 + (mousex - 80) / 30; if (bank[judgeNum] > 0) { needBuy = false; } } else if (mousex0 == 3 && mousey0 == 6) { std::cout << "choose: F" << std::endl; judgeNum = 0; needBuy = false; } } else if (needFri && mousex >= posx[me] && mousex < posx[me] + 160 && mousey >= posy[me] + 280 && mousey < posy[me] + 320) { judgeNum = (mousex - posx[me]) / 40; if (friends[me][judgeNum]) { needFri = false; } } else if (needSell && mousex >= posx[me] && mousex < posx[me] + 180 && mousey >= posy[me] + 160 && mousey < posy[me] + 280) { judgeNum = (mousex - posx[me]) / 30 + (mousey - posy[me] - 160) / 30 * 6; if (judgeNum < antiqueNum[me]) { for (int i = 2; i < 8; i++) { judgeNum -= antique[me][i]; if (judgeNum < 0) { judgeNum = i; break; } } needSell = false; } } else if (needPledge) { //abb //a 1:伙计 2:古董 3:建筑物 //bb 序号 judgeNum = 0; if (mousex >= posx[me] && mousex < posx[me] + 160 && mousey >= posy[me] + 280 && mousey < posy[me] + 320) { judgeNum = (mousex - posx[me]) / 40; if (friends[me][judgeNum]) { judgeNum += 100; needPledge = false; } } else if (mousex >= posx[me] && mousex < posx[me] + 180 && mousey >= posy[me] + 160 && mousey < posy[me] + 280) { judgeNum = (mousex - posx[me]) / 30 + (mousey - posy[me] - 160) / 30 * 6; if (judgeNum < antiqueNum[me]) { for (int i = 2; i < 8; i++) { judgeNum -= antique[me][i]; if (judgeNum < 0) { judgeNum = i + 200; break; } } needPledge = false; } } else { for (int i = 0; i < 39; i++) { if (city[i] == me && mousex >= cityposx[i] && mousex < cityposx[i] + 80 && mousey >= cityposy[i] && mousey < cityposy[i] + 80) { judgeNum = i + 300; needPledge = false; break; } } } } } else if (e.type == SDL_MOUSEBUTTONDOWN && e.button.button == SDL_BUTTON_RIGHT) { mousex = e.button.x; mousey = e.button.y; std::cout << "buttondown: " << mousex << ", " << mousey << std::endl; if (mousex < 640) { for (int i = 0; i < 39; i++) { if (mousex >= cityposx[i] && mousex < cityposx[i] + 80 && mousey >= cityposy[i] && mousey < cityposy[i] + 80) { displayHelp = i; switch (displayHelp) { case 9: case 31: displayHelp--; break; case 16: case 18: case 19: case 21: case 22: case 24: case 25: displayHelp = 15; break; case 29: displayHelp = 20; break; case 33: displayHelp = 7; break; case 36: displayHelp = 11; break; default: break; } break; } } if (needBuy && displayHelp == -1) { if (mousex >= 80 && mousex < 260 && mousey >= 400 && mousey < 430) { displayHelp = 2 + (mousex - 80) / 30; if (bank[displayHelp] > 0) { displayHelp += 200; } else { displayHelp = -1; } } } } else { for (int i = 0; i < n; i++) { if (mousex >= posx[i] && mousex < posx[i] + 180 && mousey >= posy[i] + 160 && mousey < posy[i] + 280) { displayHelp = 0; displayHelp += (mousex - posx[i]) / 30 + (mousey - posy[i] - 160) / 30 * 6; for (int j = 2; j < 8; j++) { displayHelp -= antique[i][j]; if (displayHelp < 0) { displayHelp = j + 200; break; } } if (displayHelp < 200) { displayHelp = -1; } break; } } for (int i = 0; i < n; i++) { if (mousex >= posx[i] && mousex < posx[i] + 160 && mousey >= posy[i] + 280 && mousey < posy[i] + 320) { displayHelp = (mousex - posx[i]) / 40 + 100; break; } } } } else if (e.type == SDL_MOUSEBUTTONUP) { displayHelp = -1; } } else { break; } } //制作画面 SDL_Texture* nowTex = NULL; nowTex = SDL_CreateTextureFromSurface(renderer, nowBackground); SDL_RenderCopy(renderer, nowTex, NULL, NULL); SDL_DestroyTexture(nowTex); //玩家金币显示 for (int i = 0; i < n; i++) { display(std::to_string(money[i]), posx[i] + 30, posy[i] + 30, 2, colorpurple); } //古董村古董数量显示 display(std::to_string(villageHave), 490, 170, 2, colorpurple); //玩家位置显示 for (int i = 0; i < n; i++) { int o = 0; for (int j = 0; j < i; j++) { if (pos[j] == pos[i]) { o++; } } rect2.x = cityposx[pos[i]] + o * 20; rect2.y = cityposy[pos[i]]; rect2.w = 40; rect2.h = 40; SDL_RenderCopy(renderer, playerPhoto[i], NULL, &rect2); } if (needJudge) { display("T", 90, 490, 4, colorred); display("F", 250, 490, 4, colorred); } if (needBuy) { display("F", 250, 490, 4, colorred); } SDL_RenderCopy(renderer, dicePhoto[lastRand], NULL, &rectDice); if (questionNum != -1) { SDL_RenderCopy(renderer, texQuestion[questionNum], NULL, &rectQuestion); } if (displayHelp != -1) { if (displayHelp < 100) { bool b = true; for (int i = 1; i < 9; i++) { if (displayHelp == specialHelp[i]) { SDL_RenderCopy(renderer, help[i], NULL, &rectHelp); b = false; } } if (b) { SDL_RenderCopy(renderer, help[0], NULL, &rectHelp); if (displayHelp == 6 || displayHelp == 32 || displayHelp == 38 || displayHelp == 17) { display(std::to_string(cityNeed[displayHelp]), 210, 327, 2, colorred); display("x" + std::to_string(cityValue[displayHelp][0]), 210, 375, 2, colorred); display("x" + std::to_string(cityValue[displayHelp][1]), 210, 423, 2, colorred); display("x" + std::to_string(cityValue[displayHelp][2]), 210, 471, 2, colorred); display("x" + std::to_string(cityValue[displayHelp][3]), 210, 519, 2, colorred); } else { display(std::to_string(cityNeed[displayHelp]), 210, 327, 2, colorred); display(std::to_string(cityValue[displayHelp][0]), 210, 375, 2, colorred); display(std::to_string(cityValue[displayHelp][1]), 210, 423, 2, colorred); display(std::to_string(cityValue[displayHelp][2]), 210, 471, 2, colorred); display(std::to_string(cityValue[displayHelp][3]), 210, 519, 2, colorred); } } } else if (displayHelp < 200) { SDL_RenderCopy(renderer, helpFri[displayHelp - 100], NULL, &rectHelp); } else { SDL_RenderCopy(renderer, helpAn[displayHelp - 200], NULL, &rectHelp); } } mes->displayMes(); SDL_RenderPresent(renderer); } } void winPrepare() { getIP(); //启动Windows socket2 环境 //初始化套节字 WORD ver = MAKEWORd(2, 2); WSADATA dat; WSAStartup(ver, &dat); //创建一个socket sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (INVALID_SOCKET == sock) { std::cout << "错误,建立socket失败" << std::endl; } else { std::cout << "建立socket成功.." << std::endl; } //2 连接服务器 sin0.sin_family = AF_INET; sin0.sin_port = htons(0000); sin0.sin_addr.S_un.S_addr = inet_addr("0.0.0.0");//服务器公网 if (SOCKET_ERROR == connect(sock, (sockaddr*)&sin0, sizeof(sockaddr_in))) { std::cout << "建立连接失败..." << std::endl; } else { std::cout << "建立连接成功..." << std::endl; } //3 接收服务器信息 char recvBuf[256] = {}; std::string s = "yes"; while (true) { int nlen = recv(sock, recvBuf, 1, 0); if (nlen > 0) { std::cout << "receive: " << recvBuf << std::endl; break; } } me = atoi(recvBuf); std::cout << "me:" << me << std::endl; while (true) { //接收到yes则开始游戏 int nlen = recv(sock, recvBuf, 4, 0); if (nlen > 0) { if (s.compare(0, 3, recvBuf, 0, 3) == 0) { n = recvBuf[3] - '0'; std::cout << "Play game!" << std::endl; break; } else { std::cout << "Receive error!" << std::endl; break; } } } //加载玩家信息 //服务器从第一位玩家的客户端开始监听,并将接收到的全部信息转发给其他玩家 //每位玩家发送完信息后,需发送next i(i为下一位服务器需要监听的客户端序号)来告知服务器 } void SDLPrepare() { SDL_Init(SDL_INIT_VIDEO); window = SDL_CreateWindow("SDL Tutorial", 50, 50, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); screenSurface = SDL_GetWindowSurface(window); //用显卡加速 renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); TTF_Init(); font = TTF_OpenFont("font\STXINWEI.TTF", 16); //载入 splash image nowBackground = IMG_Load("picture/rich4.png"); beginBackground = IMG_Load("picture/rich4.png"); if (nowBackground == NULL) { //失败 printf(SDL_GetError()); } else { //成功 //应用这个图像 SDL_Texture* tex = NULL; tex = SDL_CreateTextureFromSurface(renderer, nowBackground); SDL_RenderCopy(renderer, tex, NULL, NULL); SDL_RenderPresent(renderer); printf(SDL_GetError()); } SDL_Surface* firstphoto = NULL; std::string s; for (int i = 0; i < 4; i++) { s = "picture//NPC" + std::to_string(i + 5) + ".png"; friendsPhoto[i] = IMG_Load(s.data()); s = "picture//flag" + std::to_string(i + 1) + ".png"; playerFlag[i] = IMG_Load(s.data()); s = "picture//cir" + std::to_string(i + 1) + ".png"; firstphoto = IMG_Load(s.data()); if (firstphoto == NULL) { printf(SDL_GetError()); } playerPhoto[i] = SDL_CreateTextureFromSurface(renderer, firstphoto); SDL_FreeSurface(firstphoto); } for (int i = 1; i < 9; i++) { s = "picture//" + std::to_string(i) + ".png"; firstphoto = IMG_Load(s.data()); if (firstphoto == NULL) { printf(SDL_GetError()); } dicePhoto[i] = SDL_CreateTextureFromSurface(renderer, firstphoto); SDL_FreeSurface(firstphoto); } for (int i = 2; i < 8; i++) { s = "picture//antiquemin" + std::to_string(i) + ".png"; antiqueminSur[i] = IMG_Load(s.data()); if (antiqueminSur[i] == NULL) { printf(SDL_GetError()); } antiqueminPhoto[i] = SDL_CreateTextureFromSurface(renderer, firstphoto); } wchar_t msg0[] = L"是否购买地盘"; text1 = TTF_RenderUNICODE_Solid(font, (Uint16*)msg0, colorblue); if (text1 == NULL) { std::cout << SDL_GetError() << std::endl; } texQuestion[0] = SDL_CreateTextureFromSurface(renderer, text1); SDL_FreeSurface(text1); wchar_t msg1[] = L"是否购买伙计"; text1 = TTF_RenderUNICODE_Solid(font, (Uint16*)msg1, colorblue); texQuestion[1] = SDL_CreateTextureFromSurface(renderer, text1); SDL_FreeSurface(text1); wchar_t msg2[] = L"是否起飞 "; text1 = TTF_RenderUNICODE_Solid(font, (Uint16*)msg2, colorblue); texQuestion[2] = SDL_CreateTextureFromSurface(renderer, text1); SDL_FreeSurface(text1); wchar_t msg3[] = L"是否卖古董 "; text1 = TTF_RenderUNICODE_Solid(font, (Uint16*)msg3, colorblue); texQuestion[3] = SDL_CreateTextureFromSurface(renderer, text1); SDL_FreeSurface(text1); wchar_t msg4[] = L"是否替换古董"; text1 = TTF_RenderUNICODE_Solid(font, (Uint16*)msg4, colorblue); texQuestion[4] = SDL_CreateTextureFromSurface(renderer, text1); SDL_FreeSurface(text1); wchar_t msg5[] = L"是否向下走 "; text1 = TTF_RenderUNICODE_Solid(font, (Uint16*)msg5, colorblue); texQuestion[5] = SDL_CreateTextureFromSurface(renderer, text1); SDL_FreeSurface(text1); wchar_t msg6[] = L"是否加价 "; text1 = TTF_RenderUNICODE_Solid(font, (Uint16*)msg6, colorblue); texQuestion[6] = SDL_CreateTextureFromSurface(renderer, text1); SDL_FreeSurface(text1); wchar_t msg7[] = L"选择飞去哪里"; text1 = TTF_RenderUNICODE_Solid(font, (Uint16*)msg7, colorblue); texQuestion[7] = SDL_CreateTextureFromSurface(renderer, text1); SDL_FreeSurface(text1); wchar_t msg8[] = L"选择古董掠夺"; text1 = TTF_RenderUNICODE_Solid(font, (Uint16*)msg8, colorblue); texQuestion[8] = SDL_CreateTextureFromSurface(renderer, text1); SDL_FreeSurface(text1); wchar_t msg9[] = L"选择古董售卖"; text1 = TTF_RenderUNICODE_Solid(font, (Uint16*)msg9, colorblue); texQuestion[9] = SDL_CreateTextureFromSurface(renderer, text1); SDL_FreeSurface(text1); wchar_t msg10[] = L"选择失去伙计"; text1 = TTF_RenderUNICODE_Solid(font, (Uint16*)msg10, colorblue); texQuestion[10] = SDL_CreateTextureFromSurface(renderer, text1); SDL_FreeSurface(text1); wchar_t msg11[] = L"选择购买古董"; text1 = TTF_RenderUNICODE_Solid(font, (Uint16*)msg11, colorblue); texQuestion[11] = SDL_CreateTextureFromSurface(renderer, text1); SDL_FreeSurface(text1); wchar_t msg12[] = L"请点击骰子 "; text1 = TTF_RenderUNICODE_Solid(font, (Uint16*)msg12, colorblue); texQuestion[12] = SDL_CreateTextureFromSurface(renderer, text1); SDL_FreeSurface(text1); wchar_t msg13[] = L"选择物品抵押"; text1 = TTF_RenderUNICODE_Solid(font, (Uint16*)msg13, colorblue); texQuestion[13] = SDL_CreateTextureFromSurface(renderer, text1); SDL_FreeSurface(text1); rectQuestion.x = 80; rectQuestion.y = 320; rectQuestion.w = 240; rectQuestion.h = 40; rectQuestion1.x = 80; rectQuestion1.y = 320; rectQuestion1.w = 240; rectQuestion1.h = 240; specialHelp[1] = 0; specialHelp[2] = 3; specialHelp[3] = 7; specialHelp[4] = 11; specialHelp[5] = 14; specialHelp[6] = 15; specialHelp[7] = 20; specialHelp[8] = 23; text1 = IMG_Load("picture//help.png"); help[0] = SDL_CreateTextureFromSurface(renderer, text1); SDL_FreeSurface(text1); for (int i = 1; i < 9; i++) { s = "picture//help" + std::to_string(specialHelp[i]) + ".png"; text1 = IMG_Load(s.c_str()); help[i] = SDL_CreateTextureFromSurface(renderer, text1); SDL_FreeSurface(text1); } for (int i = 0; i < 4; i++) { s = "picture//help10" + std::to_string(i) + ".png"; text1 = IMG_Load(s.c_str()); helpFri[i] = SDL_CreateTextureFromSurface(renderer, text1); SDL_FreeSurface(text1); } for (int i = 2; i < 8; i++) { s = "picture//help20" + std::to_string(i) + ".png"; text1 = IMG_Load(s.c_str()); helpAn[i] = SDL_CreateTextureFromSurface(renderer, text1); SDL_FreeSurface(text1); } s = "picture//star.png"; starPhoto = IMG_Load(s.data()); if (starPhoto == NULL) { printf(SDL_GetError()); } } void signIn() { bool play = false; uint32_t time = SDL_GetTicks(); uint32_t nowTime = SDL_GetTicks(); int choose = 0; SDL_Event e; //获取玩家输入以及画面制作 while (!play) { nowTime = SDL_GetTicks(); SDL_Delay(time + 33 > nowTime ? time + 33 - nowTime : 0);//控制为30帧每秒 time += 33; if (winer != -1) { break; } //获取输入 while (!quit) { if (SDL_PollEvent(&e) != 0) { //如果用户点击了关闭 if (e.type == SDL_QUIT) { std::cout << "Quit!" << std::endl; quit = true; winer = -2; break; } else if (e.type == SDL_MOUSEBUTTONDOWN) { if (e.button.x > 593 && e.button.x < 775 && e.button.y > 388 && e.button.y < 487) { if (e.button.y < 433) { choose = 1; } else if (e.button.y > 442) { choose = 2; } } else if (e.button.x > 525 && e.button.x < 686 && e.button.y > 505 && e.button.y < 546) { return; } } } } } } //main函数返回值必须为int int main(int argc, char* args[]) { SDLPrepare(); std::cout << SDL_GetError() << std::endl; winPrepare(); numsInit(); playGame(); SDL_Delay(2000); //4 关闭套节字closesocket closesocket(sock); char c = getchar(); //清楚Windows socket环境 WSACleanup(); SDL_FreeSurface(screenSurface); SDL_FreeSurface(beginBackground); SDL_FreeSurface(nowBackground); for (int i = 0; i < 4; i++) { SDL_FreeSurface(friendsPhoto[i]); } for (int i = 0; i < n; i++) { SDL_FreeSurface(playerFlag[i]); SDL_DestroyTexture(playerPhoto[i]); } for (int i = 2; i < 8; i++) { SDL_FreeSurface(antiqueminSur[i]); SDL_DestroyTexture(antiqueminPhoto[i]); } for (int i = 1; i < 9; i++) { SDL_DestroyTexture(dicePhoto[i]); } for (int i = 0; i < 14; i++) { SDL_DestroyTexture(texQuestion[i]); } SDL_FreeSurface(starPhoto); TTF_CloseFont(font); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); return 0; }



