正文
增加 798 行删除 238 行
1234567891011121314151617181920212211 行未改动34353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220311 行未改动215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369**运行要求**:上方"工具"->"编译选项"->"编译时加入以下命令"```-std=c++14```、"在连接器命令行加入以下命令"```-lws2_32```**运行过程**:服务端在自己电脑上搜索```Windows PowerShell```->打开并输入```ipconfig```->找到"以太网适配器 以太网:"下方的"IPv4 地址 . . . . . . . . . . . . :"并复制其后内容发给客户端->服务端运行代码->客户端运行代码并将服务端发来的地址输入->开始游玩# 服务端```cpp#define _WIN32_WINNT 0x0500#include <iostream>#include <winsock2.h>#include <windows.h>#include <cstdlib>#include <ctime>#include <conio.h>#pragma comment(lib, "ws2_32.lib")using namespace std;const int W = 22;const int H = 22;const int MAP[H][W] = {{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0},{0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0},{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},11 行未改动{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},{0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0},{0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0}};HANDLE hConsole;int meX, meY;int youX, youY;int oldMeX, oldMeY;int oldYouX, oldYouY;bool solid(int x, int y) {if (x < 0 || y < 0 || x >= W || y >= H) return false;return MAP[y][x] == 0;}bool inSafeArea(int x, int y) {return x >= 4 && x <= 17 && y >= 4 && y <= 17;}void drawChar(int x, int y, char c, int color) {COORD pos;pos.X = x;pos.Y = y;SetConsoleCursorPosition(hConsole, pos);SetConsoleTextAttribute(hConsole, color);cout << c;}void drawCell(int x, int y) {if (MAP[y][x])drawChar(x, y, '#', 7);elsedrawChar(x, y, ' ', 7);}int randInt(int a, int b) {return a + rand() % (b - a + 1);}int main() {srand((unsigned int)time(NULL));hConsole = GetStdHandle(STD_OUTPUT_HANDLE);CONSOLE_CURSOR_INFO ci;ci.dwSize = 1;ci.bVisible = FALSE;SetConsoleCursorInfo(hConsole, &ci);WSADATA wsa;WSAStartup(MAKEWORD(2,2), &wsa);SOCKET svr = socket(AF_INET, SOCK_STREAM, 0);sockaddr_in addr;addr.sin_family = AF_INET;addr.sin_port = htons(8080);addr.sin_addr.s_addr = htonl(INADDR_ANY);bind(svr, (sockaddr*)&addr, sizeof(addr));listen(svr, 1);cout << "[服务端] 等待客户端..." << endl;SOCKET cli = accept(svr, NULL, NULL);u_long mode = 1;ioctlsocket(cli, FIONBIO, &mode);// 出生点限制在中间安全区do {meX = randInt(0, W-1);meY = randInt(0, H-1);} while (!solid(meX, meY) || !inSafeArea(meX, meY));do {youX = randInt(0, W-1);youY = randInt(0, H-1);} while (!solid(youX, youY) || !inSafeArea(youX, youY) ||(youX == meX && youY == meY));char init[128];sprintf(init, "[INIT|%d|%d|%d|%d]\n", meX, meY, youX, youY);send(cli, init, strlen(init), 0);system("cls");for (int y = 0; y < H; ++y)for (int x = 0; x < W; ++x)drawCell(x, y);drawChar(meX, meY, 'O', FOREGROUND_RED | FOREGROUND_INTENSITY);drawChar(youX, youY, 'O', FOREGROUND_BLUE | FOREGROUND_INTENSITY);oldMeX = meX; oldMeY = meY;oldYouX = youX; oldYouY = youY;char buf[256];string cache;DWORD lastMove = 0;while (true) {int r = recv(cli, buf, 255, 0);if (r > 0) {buf[r] = 0;cache += buf;int pos;while ((pos = cache.find('\n')) != string::npos) {string msg = cache.substr(0, pos);cache.erase(0, pos + 1);int x, y;if (sscanf(msg.c_str(), "[MOVE|%d|%d]", &x, &y) == 2) {if (solid(x, y) && !(x == meX && y == meY)) {drawCell(oldYouX, oldYouY);drawChar(x, y, 'O', FOREGROUND_BLUE | FOREGROUND_INTENSITY);youX = x; youY = y;oldYouX = youX; oldYouY = youY;send(cli, msg.c_str(), msg.length(), 0);send(cli, "\n", 1, 0);}}}}if (_kbhit() && GetTickCount() - lastMove > 120) {char key = _getch();int nx = meX, ny = meY;if (key == 'w' || key == 'W') ny--;else if (key == 's' || key == 'S') ny++;else if (key == 'a' || key == 'A') nx--;else if (key == 'd' || key == 'D') nx++;else continue;if (solid(nx, ny) && !(nx == youX && ny == youY)) {drawCell(oldMeX, oldMeY);drawChar(nx, ny, 'O', FOREGROUND_RED | FOREGROUND_INTENSITY);meX = nx; meY = ny;oldMeX = meX; oldMeY = meY;char move[64];sprintf(move, "[MOVE|%d|%d]\n", meX, meY);send(cli, move, strlen(move), 0);lastMove = GetTickCount();}}Sleep(10);}}```# 客户端```cpp#define _WIN32_WINNT 0x0500#include <winsock2.h>#include <windows.h>#include <iostream>#include <string>#include <conio.h>#pragma comment(lib, "ws2_32.lib")using namespace std;const int W = 22;const int H = 22;const int MAP[H][W] = {{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0},{0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0},{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},11 行未改动{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},{0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0},{0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0}};HANDLE hConsole;int myX, myY;int enemyX, enemyY;int oldMyX, oldMyY;int oldEnemyX, oldEnemyY;bool solid(int x, int y) {if (x < 0 || y < 0 || x >= W || y >= H) return false;return MAP[y][x] == 0;}void drawChar(int x, int y, char c, int color) {COORD pos;pos.X = x;pos.Y = y;SetConsoleCursorPosition(hConsole, pos);SetConsoleTextAttribute(hConsole, color);cout << c;}void drawCell(int x, int y) {if (MAP[y][x])drawChar(x, y, '#', 7);elsedrawChar(x, y, ' ', 7);}int main() {hConsole = GetStdHandle(STD_OUTPUT_HANDLE);CONSOLE_CURSOR_INFO ci;ci.dwSize = 1;ci.bVisible = FALSE;SetConsoleCursorInfo(hConsole, &ci);WSADATA wsa;WSAStartup(MAKEWORD(2,2), &wsa);SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);cout << "IP: ";string ip;getline(cin, ip);sockaddr_in addr;addr.sin_family = AF_INET;addr.sin_port = htons(8080);addr.sin_addr.s_addr = inet_addr(ip.c_str());if (connect(sock, (sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) {cout << "连接失败" << endl;return 0;}u_long mode = 1;ioctlsocket(sock, FIONBIO, &mode);char buf[256];string cache;bool ok = false;while (!ok) {int n = recv(sock, buf, 255, 0);if (n > 0) {buf[n] = 0;cache += buf;int pos;while ((pos = cache.find('\n')) != string::npos) {string msg = cache.substr(0, pos);cache.erase(0, pos + 1);int a, b, c, d;if (sscanf(msg.c_str(), "[INIT|%d|%d|%d|%d]", &a, &b, &c, &d) == 4) {enemyX = a; enemyY = b;myX = c; myY = d;ok = true;break;}}}Sleep(10);}system("cls");for (int y = 0; y < H; ++y)for (int x = 0; x < W; ++x)drawCell(x, y);drawChar(myX, myY, 'O', FOREGROUND_RED | FOREGROUND_INTENSITY);drawChar(enemyX, enemyY, 'O', FOREGROUND_BLUE | FOREGROUND_INTENSITY);oldMyX = myX; oldMyY = myY;oldEnemyX = enemyX; oldEnemyY = enemyY;DWORD lastMove = 0;while (true) {int n = recv(sock, buf, 255, 0);if (n > 0) {buf[n] = 0;cache += buf;int pos;while ((pos = cache.find('\n')) != string::npos) {string msg = cache.substr(0, pos);cache.erase(0, pos + 1);int x, y;if (sscanf(msg.c_str(), "[MOVE|%d|%d]", &x, &y) == 2) {if (!solid(x, y)) continue;// ? 关键:忽略自己发出的移动if (x == myX && y == myY) continue;drawCell(oldEnemyX, oldEnemyY);drawChar(x, y, 'O', FOREGROUND_BLUE | FOREGROUND_INTENSITY);enemyX = x; enemyY = y;oldEnemyX = enemyX; oldEnemyY = enemyY;}}}if (_kbhit() && GetTickCount() - lastMove > 120) {char key = _getch();int nx = myX, ny = myY;if (key == 'w' || key == 'W') ny--;else if (key == 's' || key == 'S') ny++;else if (key == 'a' || key == 'A') nx--;else if (key == 'd' || key == 'D') nx++;else continue;if (solid(nx, ny) && !(nx == enemyX && ny == enemyY)) {drawCell(oldMyX, oldMyY);drawChar(nx, ny, 'O', FOREGROUND_RED | FOREGROUND_INTENSITY);myX = nx; myY = ny;oldMyX = myX; oldMyY = myY;char move[64];sprintf(move, "[MOVE|%d|%d]\n", myX, myY);send(sock, move, strlen(move), 0);lastMove = GetTickCount();}}Sleep(10);}}```12345678910111213141516171811 行未改动3031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246311 行未改动475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929**运行要求**:上方"工具"->"编译选项"->"编译时加入以下命令"```-std=c++14```、"在连接器命令行加入以下命令"```-lws2_32```**运行过程**:服务端在自己电脑上搜索```Windows PowerShell```->打开并输入```ipconfig```->找到"以太网适配器 以太网:"下方的"IPv4 地址 . . . . . . . . . . . . :"并复制其后内容发给客户端->服务端运行代码->客户端运行代码并将服务端发来的地址输入->开始游玩# 服务端```cpp#define _WIN32_WINNT 0x0500#include <bits/stdc++.h>#include <winsock2.h>#include <windows.h>#include <conio.h>#pragma comment(lib, "ws2_32.lib")using namespace std;const int W=22,H=22;const int MAP[H][W]={{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0},{0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0},{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},11 行未改动{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},{0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0},{0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0}};HANDLE hConsole;int mx,my,tx,ty,omx,omy,otx,oty,ldx,ldy,mhp=10,thp=10;bool fr,rp=true,mr=false,tr=false,go=false,hfa=false;DWORD fs,acd=0,ru=0,selfRu=0,hfs;bool speedBoost=false,rayActive=false,normalRayActive=false,previewActive=false;DWORD rayEndTime=0,normalRayEndTime=0,previewEndTime=0;int raySx,raySy,rayDirX,rayDirY,normalRaySx,normalRaySy,normalRayDirX,normalRayDirY;int qSkill=0,eSkill=1;DWORD qCooldownUntil=0,eCooldownUntil=0;int prevSkillType=0;// 临时障碍物(光幕)bool tempWall[H][W]={false};DWORD wallTime[H][W]={0};struct Missile {bool active;int curX,curY,dirX,dirY;DWORD lastMoveTime;};Missile myMissile;bool sd(int x,int y){return x>=0&&y>=0&&x<W&&y<H && MAP[y][x]==0 && !tempWall[y][x];}bool sa(int x,int y){return x>=4&&x<=17&&y>=4&&y<=17;}bool rhe(int sx,int sy,int dx,int dy,int ex,int ey){int x=sx+dx,y=sy+dy;while(sd(x,y)){if(x==ex&&y==ey)return true;x+=dx;y+=dy;}return false;}void dc(int x,int y,char c,int col){SetConsoleCursorPosition(hConsole,{(SHORT)x,(SHORT)y});SetConsoleTextAttribute(hConsole,col);cout<<c;}void dcell(int x,int y){if(tempWall[y][x]) dc(x,y,'#',FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY);else dc(x,y,MAP[y][x]?'#':' ',7);}void drawRayLine(int sx,int sy,int dx,int dy,char ch,int col){int x=sx+dx,y=sy+dy;while(sd(x,y)){dc(x,y,ch,col);x+=dx;y+=dy;}}void clearRayLine(int sx,int sy,int dx,int dy){int x=sx+dx,y=sy+dy;while(sd(x,y)){dcell(x,y);x+=dx;y+=dy;}}void redrawPlayers(){dc(mx,my,'O',FOREGROUND_RED|FOREGROUND_INTENSITY);if(ru&&GetTickCount()<=ru) dc(tx,ty,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY);else dcell(tx,ty);}void showPreview(int skillType){if(previewActive&&prevSkillType==1&&ldx!=0&&ldy!=0) clearRayLine(mx,my,ldx,ldy);previewActive=false;if(skillType==1&&ldx!=0&&ldy!=0){drawRayLine(mx,my,ldx,ldy,'*',FOREGROUND_GREEN|FOREGROUND_INTENSITY);previewActive=true; previewEndTime=GetTickCount()+500; prevSkillType=1;}}void fsr(int col){CONSOLE_SCREEN_BUFFER_INFO c;GetConsoleScreenBufferInfo(hConsole,&c);SMALL_RECT s=c.srWindow;DWORD w;FillConsoleOutputAttribute(hConsole,(WORD)col,(s.Right-s.Left+1)*(s.Bottom-s.Top+1),{s.Left,s.Top},&w);}void clsLine(int y){SetConsoleCursorPosition(hConsole,{0,(SHORT)y});cout<<string(80,' ');SetConsoleCursorPosition(hConsole,{0,(SHORT)y});}string skillName(int id){switch(id){case 0: return "加速";case 1: return "射线";case 2: return "探测";case 3: return "飞弹";case 4: return "光幕";default: return "未知";}}void srs(){for(int i=0;i<6;i++) clsLine(i);SetConsoleCursorPosition(hConsole,{0,0});cout<<"===== 对战准备阶段 ====="<<endl;cout<<"我方: "<<(mr?"已准备":"未准备")<<" 对方: "<<(tr?"已准备":"未准备")<<endl;cout<<"按 空格 切换准备状态,按 Q/E 切换对应技能"<<endl;cout<<"[Q]技能: "<<skillName(qSkill)<<" [E]技能: "<<skillName(eSkill)<<endl;}void shp(){if(go) return;int mc=FOREGROUND_RED|FOREGROUND_INTENSITY,tc=FOREGROUND_BLUE|FOREGROUND_INTENSITY;if(hfa&&GetTickCount()-hfs<500) mc=FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY;SetConsoleCursorPosition(hConsole,{W+2,0});SetConsoleTextAttribute(hConsole,mc);cout<<"我方 HP: "<<mhp<<" ";SetConsoleCursorPosition(hConsole,{W+2,1});SetConsoleTextAttribute(hConsole,tc);cout<<"对方 HP: "<<thp<<" ";SetConsoleCursorPosition(hConsole,{W+2,2});SetConsoleTextAttribute(hConsole,FOREGROUND_GREEN|FOREGROUND_INTENSITY);DWORD now=GetTickCount();string qn=skillName(qSkill), en=skillName(eSkill);if(qCooldownUntil>now) cout<<"[Q]"<<qn<<" ("<<((qCooldownUntil-now+999)/1000)<<"s) ";else if(speedBoost && qSkill==0) cout<<"[Q]加速中 ";else cout<<"[Q]"<<qn<<" ";if(eCooldownUntil>now) cout<<"[E]"<<en<<" ("<<((eCooldownUntil-now+999)/1000)<<"s) ";else if(speedBoost && eSkill==0) cout<<"[E]加速中 ";else cout<<"[E]"<<en<<" ";SetConsoleTextAttribute(hConsole,7);}void sgo(string w){system("cls");SetConsoleCursorPosition(hConsole,{0,10});SetConsoleTextAttribute(hConsole,FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY);cout<<"========== 游戏结束 ==========\n 获胜方: "<<w<<"\n 我方 HP: "<<mhp<<" 对方 HP: "<<thp<<"\n==============================\n";SetConsoleTextAttribute(hConsole,7);}bool checkMyMissileHit(SOCKET cli, DWORD now) {if (myMissile.curX == tx && myMissile.curY == ty) {myMissile.active = false;thp -= 4;char bf[256];sprintf(bf,"[HP|%d|%d]\n",mhp,thp); send(cli,bf,strlen(bf),0);send(cli,"[MISSILE_HIT]\n",14,0);fr=true; fs=now; fsr(BACKGROUND_RED); hfa=true; hfs=now;if(thp<=0){go=true; send(cli,"[GAMEOVER|我方]\n",16,0); sgo("我方");}else shp();return true;}return false;}// 放置光幕辅助函数(服务端使用)void placeBarrier(SOCKET cli, DWORD now){if(ldx==0 && ldy==0) return;int px = mx + 2*ldx;int py = my + 2*ldy;auto canPlace = [](int x,int y)->bool{return x>=0 && x<W && y>=0 && y<H && MAP[y][x]==0;};if(!canPlace(px,py)) return;int dx = -ldy, dy = ldx;int pts[3][2] = {{px, py}, {px+dx, py+dy}, {px-dx, py-dy}};char bf[256];for(int i=0;i<3;i++){int x=pts[i][0], y=pts[i][1];if(canPlace(x,y)){tempWall[y][x] = true;wallTime[y][x] = now;dcell(x,y);sprintf(bf,"[BARRIER_PLACE|%d|%d]\n",x,y);send(cli,bf,strlen(bf),0);}}}void removeBarrierAt(SOCKET cli, int x, int y){if(tempWall[y][x]){tempWall[y][x] = false;dcell(x,y);char bf[64];sprintf(bf,"[BARRIER_REMOVE|%d|%d]\n",x,y);send(cli,bf,strlen(bf),0);}}void useSkill(SOCKET cli, int slot, DWORD now){int skill = (slot==0) ? qSkill : eSkill;DWORD &cooldown = (slot==0) ? qCooldownUntil : eCooldownUntil;if(now < cooldown) return;char bf[256];switch(skill){case 0:cooldown = now + 4000;speedBoost = true;send(cli,"[SPEED_ON]\n",11,0);shp();break;case 1:if(ldx==0 && ldy==0) return;cooldown = now + 10000;selfRu = now + 2000;send(cli,"[SELF_REVEAL]\n",14,0);raySx=mx; raySy=my; rayDirX=ldx; rayDirY=ldy;drawRayLine(mx,my,ldx,ldy,'*',FOREGROUND_RED|FOREGROUND_INTENSITY);rayActive=true; rayEndTime=now+200;if(rhe(mx,my,ldx,ldy,tx,ty)){send(cli,"[RAY_HIT]\n",10,0);thp -= 3;sprintf(bf,"[HP|%d|%d]\n",mhp,thp); send(cli,bf,strlen(bf),0);if(thp<=0){go=true; send(cli,"[GAMEOVER|我方]\n",16,0); sgo("我方");}else shp();}else shp();break;case 2:cooldown = now + 5000;{int dist = abs(mx-tx)+abs(my-ty);if(dist <= 3){ru = now + 3000;dcell(otx,oty); dc(tx,ty,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY);send(cli,"[DETECT]\n",9,0);}}shp();break;case 3:if(ldx==0 && ldy==0) return;cooldown = now + 10000;selfRu = now + 800;send(cli,"[SELF_REVEAL]\n",14,0);myMissile.active = true;myMissile.curX = mx + ldx; myMissile.curY = my + ldy;myMissile.dirX = ldx; myMissile.dirY = ldy;myMissile.lastMoveTime = now;if(!sd(myMissile.curX,myMissile.curY)){myMissile.active = false;}else{dc(myMissile.curX,myMissile.curY,'*',FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY);if(checkMyMissileHit(cli, now)) { shp(); break; }}shp();break;case 4:if(ldx==0 && ldy==0) return;cooldown = now + 20000;selfRu = now + 1500;send(cli,"[SELF_REVEAL]\n",14,0);placeBarrier(cli, now);shp();break;}}int main(){srand(time(NULL));hConsole=GetStdHandle(STD_OUTPUT_HANDLE);CONSOLE_CURSOR_INFO ci={1,FALSE};SetConsoleCursorInfo(hConsole,&ci);WSADATA w;WSAStartup(MAKEWORD(2,2),&w);SOCKET svr=socket(AF_INET,SOCK_STREAM,0);sockaddr_in ad={0};ad.sin_family=AF_INET;ad.sin_port=htons(8080);ad.sin_addr.s_addr=htonl(INADDR_ANY);bind(svr,(sockaddr*)&ad,sizeof(ad));listen(svr,1);cout<<"[服务端] 等待客户端...\n";SOCKET cli=accept(svr,NULL,NULL);u_long md=1;ioctlsocket(cli,FIONBIO,&md);do{mx=rand()%W;my=rand()%H;}while(!sd(mx,my)||!sa(mx,my));do{tx=rand()%W;ty=rand()%H;}while(!sd(tx,ty)||!sa(tx,ty)||(tx==mx&&ty==my));system("cls");char ini[128];sprintf(ini,"[INIT|%d|%d|%d|%d]\n",mx,my,tx,ty);send(cli,ini,strlen(ini),0);srs();char bf[256];string ca;DWORD lm=0;while(true){int r=recv(cli,bf,255,0);if(r>0){bf[r]=0;ca+=bf;size_t p;while((p=ca.find('\n'))!=string::npos){string msg=ca.substr(0,p);ca.erase(0,p+1);if(rp){if(msg=="[READY]"){tr=true;srs();if(mr&&tr){send(cli,"[START]\n",8,0);system("cls");for(int y=0;y<H;++y)for(int x=0;x<W;++x)dcell(x,y);dc(mx,my,'O',FOREGROUND_RED|FOREGROUND_INTENSITY);dcell(tx,ty);omx=mx;omy=my;otx=tx;oty=ty;rp=false;mhp=10;thp=10;shp();}}else if(msg=="[UNREADY]"){tr=false;srs();}}else if(!go){int x,y;if(sscanf(msg.c_str(),"[MOVE|%d|%d]",&x,&y)==2){if(sd(x,y)&&!(x==mx&&y==my)){dcell(otx,oty);tx=x;ty=y;otx=tx;oty=ty;if(ru&&GetTickCount()<=ru)dc(x,y,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY);}}else if(msg=="[HIT]"){mhp--;fr=true;fs=GetTickCount();fsr(BACKGROUND_RED);hfa=true;hfs=GetTickCount();sprintf(bf,"[HP|%d|%d]\n",mhp,thp);send(cli,bf,strlen(bf),0);if(mhp<=0){go=true;send(cli,"[GAMEOVER|对方]\n",16,0);sgo("对方");}else shp();}else if(msg=="[REVEAL]"){ru=GetTickCount()+1000;dcell(otx,oty);dc(tx,ty,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY);}else if(msg=="[DEAD]"){go=true;send(cli,"[GAMEOVER|我方]\n",16,0);sgo("我方");}else if(sscanf(msg.c_str(),"[HP|%d|%d]",&x,&y)==2){thp=x;mhp=y;shp();}else if(msg=="[SPEED_ON]"){speedBoost=true;shp();}else if(msg=="[SPEED_OFF]"){speedBoost=false;shp();}else if(msg=="[RAY_HIT]"){mhp-=3;fr=true;fs=GetTickCount();fsr(BACKGROUND_RED);hfa=true;hfs=GetTickCount();sprintf(bf,"[HP|%d|%d]\n",mhp,thp);send(cli,bf,strlen(bf),0);if(mhp<=0){go=true;send(cli,"[GAMEOVER|对方]\n",16,0);sgo("对方");}else shp();}else if(msg=="[DETECT]"){ru=GetTickCount()+3000;dcell(otx,oty);dc(tx,ty,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY);}else if(msg=="[SELF_REVEAL]"){ru=GetTickCount()+2000;dcell(otx,oty);dc(tx,ty,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY);}else if(msg=="[MISSILE_HIT]"){fr=true;fs=GetTickCount();fsr(BACKGROUND_RED);hfa=true;hfs=GetTickCount();shp();}else if(sscanf(msg.c_str(),"[BARRIER_PLACE|%d|%d]",&x,&y)==2){// 客户端放置光幕,服务端同步if(x>=0 && x<W && y>=0 && y<H && MAP[y][x]==0){tempWall[y][x] = true;wallTime[y][x] = GetTickCount();dcell(x,y);}}else if(sscanf(msg.c_str(),"[BARRIER_REMOVE|%d|%d]",&x,&y)==2){// 客户端移除光幕,服务端同步if(x>=0 && x<W && y>=0 && y<H && tempWall[y][x]){tempWall[y][x] = false;dcell(x,y);}}}}}if(_kbhit()&&GetTickCount()-lm>120&&!go){char k=_getch();DWORD nw=GetTickCount();if(rp){if(k==' '){mr=!mr;send(cli,mr?"[READY]\n":"[UNREADY]\n",mr?8:11,0);srs();if(mr&&tr){send(cli,"[START]\n",8,0);system("cls");for(int y=0;y<H;++y)for(int x=0;x<W;++x)dcell(x,y);dc(mx,my,'O',FOREGROUND_RED|FOREGROUND_INTENSITY);dcell(tx,ty);omx=mx;omy=my;otx=tx;oty=ty;rp=false;mhp=10;thp=10;shp();}lm=nw;}else if(k=='q'||k=='Q'){int next = (qSkill+1)%5;while(next == eSkill) next = (next+1)%5;qSkill = next;srs(); showPreview(qSkill); lm=nw;}else if(k=='e'||k=='E'){int next = (eSkill+1)%5;while(next == qSkill) next = (next+1)%5;eSkill = next;srs(); showPreview(eSkill); lm=nw;}}else{int nx=mx,ny=my,dx=0,dy=0;if(k=='w'||k=='W'){ny--;dy=-1;}else if(k=='s'||k=='S'){ny++;dy=1;}else if(k=='a'||k=='A'){nx--;dx=-1;}else if(k=='d'||k=='D'){nx++;dx=1;}else if(k==' '){if(nw-acd>=1000){acd=nw;sprintf(bf,"[REVEAL|%d|%d]\n",mx,my);send(cli,bf,strlen(bf),0);if(ldx!=0||ldy!=0){normalRaySx=mx;normalRaySy=my;normalRayDirX=ldx;normalRayDirY=ldy;drawRayLine(mx,my,ldx,ldy,'*',FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY);normalRayActive=true;normalRayEndTime=nw+200;}if(rhe(mx,my,ldx,ldy,tx,ty)) send(cli,"[HIT]\n",7,0);}lm=nw;continue;}else if(k=='q'||k=='Q'){useSkill(cli,0,nw);lm=nw;continue;}else if(k=='e'||k=='E'){useSkill(cli,1,nw);lm=nw;continue;}else continue;if(speedBoost){int sx=dx,sy=dy,ms=0;for(int i=1;i<=3;i++){int cx=mx+sx*i,cy=my+sy*i;if(!sd(cx,cy)||(cx==tx&&cy==ty))break;ms=i;}if(ms>0){nx=mx+sx*ms;ny=my+sy*ms;}else{lm=nw;continue;}}if(sd(nx,ny)&&!(nx==tx&&ny==ty)){dcell(omx,omy);dc(nx,ny,'O',FOREGROUND_RED|FOREGROUND_INTENSITY);mx=nx;my=ny;omx=mx;omy=my;ldx=dx;ldy=dy;sprintf(bf,"[MOVE|%d|%d]\n",mx,my);send(cli,bf,strlen(bf),0);if(speedBoost){speedBoost=false;send(cli,"[SPEED_OFF]\n",12,0);shp();}lm=nw;}}}if(!rp&&!go){DWORD now=GetTickCount();// 检查临时墙超时for(int y=0;y<H;y++){for(int x=0;x<W;x++){if(tempWall[y][x] && now - wallTime[y][x] >= 20000){removeBarrierAt(cli,x,y);}}}if(myMissile.active){if(now - myMissile.lastMoveTime >= 180){dcell(myMissile.curX,myMissile.curY);int newX = myMissile.curX + myMissile.dirX;int newY = myMissile.curY + myMissile.dirY;if(sd(newX,newY)){myMissile.curX = newX;myMissile.curY = newY;if(checkMyMissileHit(cli, now)) continue;dc(myMissile.curX,myMissile.curY,'*',FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY);myMissile.lastMoveTime = now;}else{myMissile.active = false;}}}if(fr&&now-fs>200){fr=false;fsr(0);for(int y=0;y<H;++y)for(int x=0;x<W;++x)dcell(x,y);redrawPlayers();shp();}if(hfa&&now-hfs>300){hfa=false;shp();}if(ru&&now<=ru) dc(tx,ty,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY);else if(ru&&now>ru){ru=0;dcell(tx,ty);}if(selfRu&&now<=selfRu) dc(mx,my,'O',FOREGROUND_RED|FOREGROUND_INTENSITY);else if(selfRu&&now>selfRu){selfRu=0;dcell(mx,my);dc(mx,my,'O',FOREGROUND_RED|FOREGROUND_INTENSITY);}if(previewActive&&now>previewEndTime){previewActive=false;if(prevSkillType==1&&ldx!=0&&ldy!=0){clearRayLine(mx,my,ldx,ldy);redrawPlayers();}}if(rayActive&&now>rayEndTime){rayActive=false;clearRayLine(raySx,raySy,rayDirX,rayDirY);redrawPlayers();}if(normalRayActive&&now>normalRayEndTime){normalRayActive=false;clearRayLine(normalRaySx,normalRaySy,normalRayDirX,normalRayDirY);redrawPlayers();}static DWORD lastShp=0;if(now-lastShp>=1000){lastShp=now;shp();}}Sleep(10);}}```# 客户端```cpp#define _WIN32_WINNT 0x0500#include <bits/stdc++.h>#include <winsock2.h>#include <windows.h>#include <conio.h>#pragma comment(lib, "ws2_32.lib")using namespace std;const int W=22,H=22;const int MAP[H][W]={{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0},{0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0},{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},11 行未改动{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},{0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0},{0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0}};HANDLE hConsole;int mx,my,ex,ey,omx,omy,oex,oey,ldx=0,ldy=0,mhp=10,ehp=10;bool fr,rp=true,mr=false,er=false,gs=false,go=false,hfa=false;DWORD fs,acd=0,ru=0,selfRu=0,hfs;bool speedBoost=false,rayActive=false,normalRayActive=false,previewActive=false;DWORD rayEndTime=0,normalRayEndTime=0,previewEndTime=0;int raySx,raySy,rayDirX,rayDirY,normalRaySx,normalRaySy,normalRayDirX,normalRayDirY;int qSkill=0,eSkill=1;DWORD qCooldownUntil=0,eCooldownUntil=0;int prevSkillType=0;// 临时障碍物(光幕)bool tempWall[H][W]={false};DWORD wallTime[H][W]={0};struct Missile {bool active;int curX,curY,dirX,dirY;DWORD lastMoveTime;};Missile myMissile;bool sd(int x,int y){return x>=0&&y>=0&&x<W&&y<H && MAP[y][x]==0 && !tempWall[y][x];}bool rhe(int sx,int sy,int dx,int dy,int ex,int ey){int x=sx+dx,y=sy+dy;while(sd(x,y)){if(x==ex&&y==ey)return true;x+=dx;y+=dy;}return false;}void dc(int x,int y,char c,int col){SetConsoleCursorPosition(hConsole,{(SHORT)x,(SHORT)y});SetConsoleTextAttribute(hConsole,col);cout<<c;}void dcell(int x,int y){if(tempWall[y][x]) dc(x,y,'#',FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY);else dc(x,y,MAP[y][x]?'#':' ',7);}void drawRayLine(int sx,int sy,int dx,int dy,char ch,int col){int x=sx+dx,y=sy+dy;while(sd(x,y)){dc(x,y,ch,col);x+=dx;y+=dy;}}void clearRayLine(int sx,int sy,int dx,int dy){int x=sx+dx,y=sy+dy;while(sd(x,y)){dcell(x,y);x+=dx;y+=dy;}}void redrawPlayers(){dc(mx,my,'O',FOREGROUND_RED|FOREGROUND_INTENSITY);if(ru&&GetTickCount()<=ru) dc(ex,ey,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY);else dcell(ex,ey);}void showPreview(int skillType){if(previewActive&&prevSkillType==1&&ldx!=0&&ldy!=0) clearRayLine(mx,my,ldx,ldy);previewActive=false;if(skillType==1&&ldx!=0&&ldy!=0){drawRayLine(mx,my,ldx,ldy,'*',FOREGROUND_GREEN|FOREGROUND_INTENSITY);previewActive=true; previewEndTime=GetTickCount()+500; prevSkillType=1;}}void fsr(int col){CONSOLE_SCREEN_BUFFER_INFO c;GetConsoleScreenBufferInfo(hConsole,&c);SMALL_RECT s=c.srWindow;DWORD w;FillConsoleOutputAttribute(hConsole,(WORD)col,(s.Right-s.Left+1)*(s.Bottom-s.Top+1),{s.Left,s.Top},&w);}void clsLine(int y){SetConsoleCursorPosition(hConsole,{0,(SHORT)y});cout<<string(80,' ');SetConsoleCursorPosition(hConsole,{0,(SHORT)y});}string skillName(int id){switch(id){case 0: return "加速";case 1: return "射线";case 2: return "探测";case 3: return "飞弹";case 4: return "光幕";default: return "未知";}}void srs(){clsLine(0); clsLine(1); clsLine(2); clsLine(3); clsLine(4); clsLine(5);SetConsoleCursorPosition(hConsole,{0,0});cout<<"===== 对战准备阶段 ====="<<endl;cout<<"我方: "<<(mr?"已准备":"未准备")<<" 对方: "<<(er?"已准备":"未准备")<<endl;cout<<"按 空格 切换准备状态,按 Q/E 切换对应技能"<<endl;cout<<"[Q]技能: "<<skillName(qSkill)<<" [E]技能: "<<skillName(eSkill)<<endl;}void shp(){if(go) return;int mc=FOREGROUND_RED|FOREGROUND_INTENSITY,ec=FOREGROUND_BLUE|FOREGROUND_INTENSITY;if(hfa&&GetTickCount()-hfs<500) mc=FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY;SetConsoleCursorPosition(hConsole,{W+2,0});SetConsoleTextAttribute(hConsole,mc);cout<<"我方 HP: "<<mhp<<" ";SetConsoleCursorPosition(hConsole,{W+2,1});SetConsoleTextAttribute(hConsole,ec);cout<<"对方 HP: "<<ehp<<" ";SetConsoleCursorPosition(hConsole,{W+2,2});SetConsoleTextAttribute(hConsole,FOREGROUND_GREEN|FOREGROUND_INTENSITY);DWORD now=GetTickCount();string qn=skillName(qSkill), en=skillName(eSkill);if(qCooldownUntil>now) cout<<"[Q]"<<qn<<" ("<<((qCooldownUntil-now+999)/1000)<<"s) ";else if(speedBoost && qSkill==0) cout<<"[Q]加速中 ";else cout<<"[Q]"<<qn<<" ";if(eCooldownUntil>now) cout<<"[E]"<<en<<" ("<<((eCooldownUntil-now+999)/1000)<<"s) ";else if(speedBoost && eSkill==0) cout<<"[E]加速中 ";else cout<<"[E]"<<en<<" ";SetConsoleTextAttribute(hConsole,7);}void sgo(string w){system("cls");SetConsoleCursorPosition(hConsole,{0,10});SetConsoleTextAttribute(hConsole,FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY);cout<<"========== 游戏结束 ==========\n 获胜方: "<<w<<"\n 我方 HP: "<<mhp<<" 对方 HP: "<<ehp<<"\n==============================\n";SetConsoleTextAttribute(hConsole,7);}bool checkMyMissileHit(SOCKET sk, DWORD now) {if (myMissile.curX == ex && myMissile.curY == ey) {myMissile.active = false;ehp -= 4;char bf[256];sprintf(bf,"[HP|%d|%d]\n",mhp,ehp); send(sk,bf,strlen(bf),0);send(sk,"[MISSILE_HIT]\n",14,0);fr=true; fs=now; fsr(BACKGROUND_RED); hfa=true; hfs=now;if(ehp<=0){go=true; send(sk,"[DEAD]\n",7,0); sgo("我方");}else shp();return true;}return false;}// 本地放置光幕(仅本地视觉,同时通知服务端)void placeBarrierLocal(DWORD now){if(ldx==0 && ldy==0) return;int px = mx + 2*ldx;int py = my + 2*ldy;auto canPlace = [](int x,int y)->bool{return x>=0 && x<W && y>=0 && y<H && MAP[y][x]==0;};if(!canPlace(px,py)) return;int dx = -ldy, dy = ldx;int pts[3][2] = {{px, py}, {px+dx, py+dy}, {px-dx, py-dy}};for(int i=0;i<3;i++){int x=pts[i][0], y=pts[i][1];if(canPlace(x,y)){tempWall[y][x] = true;wallTime[y][x] = now;dcell(x,y);}}}// 本地移除光幕void removeBarrierLocal(int x, int y){if(tempWall[y][x]){tempWall[y][x] = false;dcell(x,y);}}void useSkill(SOCKET sk, int slot, DWORD now){int skill = (slot==0) ? qSkill : eSkill;DWORD &cooldown = (slot==0) ? qCooldownUntil : eCooldownUntil;if(now < cooldown) return;char bf[256];switch(skill){case 0:cooldown = now + 4000;speedBoost = true;send(sk,"[SPEED_ON]\n",11,0);shp();break;case 1:if(ldx==0 && ldy==0) return;cooldown = now + 10000;selfRu = now + 2000;send(sk,"[SELF_REVEAL]\n",14,0);raySx=mx; raySy=my; rayDirX=ldx; rayDirY=ldy;drawRayLine(mx,my,ldx,ldy,'*',FOREGROUND_RED|FOREGROUND_INTENSITY);rayActive=true; rayEndTime=now+200;if(rhe(mx,my,ldx,ldy,ex,ey)){send(sk,"[RAY_HIT]\n",10,0);ehp -= 3;sprintf(bf,"[HP|%d|%d]\n",mhp,ehp); send(sk,bf,strlen(bf),0);if(ehp<=0){go=true; send(sk,"[DEAD]\n",7,0); sgo("我方");}else shp();}else shp();break;case 2:cooldown = now + 5000;{int dist = abs(mx-ex)+abs(my-ey);if(dist <= 3){ru = now + 3000;dcell(oex,oey); dc(ex,ey,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY);send(sk,"[DETECT]\n",9,0);}}shp();break;case 3:if(ldx==0 && ldy==0) return;cooldown = now + 10000;selfRu = now + 800;send(sk,"[SELF_REVEAL]\n",14,0);myMissile.active = true;myMissile.curX = mx + ldx; myMissile.curY = my + ldy;myMissile.dirX = ldx; myMissile.dirY = ldy;myMissile.lastMoveTime = now;if(!sd(myMissile.curX,myMissile.curY)){myMissile.active = false;}else{dc(myMissile.curX,myMissile.curY,'*',FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY);if(checkMyMissileHit(sk, now)) { shp(); break; }}shp();break;case 4:if(ldx==0 && ldy==0) return;cooldown = now + 20000;selfRu = now + 1500;send(sk,"[SELF_REVEAL]\n",14,0);placeBarrierLocal(now);// 通知服务端放置的光幕点{int px = mx + 2*ldx;int py = my + 2*ldy;auto canPlace = [](int x,int y)->bool{return x>=0 && x<W && y>=0 && y<H && MAP[y][x]==0;};if(canPlace(px,py)){int dx = -ldy, dy = ldx;int pts[3][2] = {{px, py}, {px+dx, py+dy}, {px-dx, py-dy}};for(int i=0;i<3;i++){int x=pts[i][0], y=pts[i][1];if(canPlace(x,y)){char buf[64];sprintf(buf,"[BARRIER_PLACE|%d|%d]\n",x,y);send(sk,buf,strlen(buf),0);}}}}shp();break;}}int main(){hConsole=GetStdHandle(STD_OUTPUT_HANDLE);CONSOLE_CURSOR_INFO ci={1,FALSE};SetConsoleCursorInfo(hConsole,&ci);WSADATA w;WSAStartup(MAKEWORD(2,2),&w);SOCKET sk=socket(AF_INET,SOCK_STREAM,0);cout<<"IP: ";string ip;getline(cin,ip);sockaddr_in ad={0};ad.sin_family=AF_INET;ad.sin_port=htons(8080);ad.sin_addr.s_addr=inet_addr(ip.c_str());if(connect(sk,(sockaddr*)&ad,sizeof(ad))==SOCKET_ERROR){cout<<"连接失败\n";return 0;}u_long md=1;ioctlsocket(sk,FIONBIO,&md);char bf[256];string ca;bool ok=false;while(!ok){int n=recv(sk,bf,255,0);if(n>0){bf[n]=0;ca+=bf;size_t p;while((p=ca.find('\n'))!=string::npos){string m=ca.substr(0,p);ca.erase(0,p+1);int a,b,c,d;if(sscanf(m.c_str(),"[INIT|%d|%d|%d|%d]",&a,&b,&c,&d)==4){ex=a;ey=b;mx=c;my=d;ok=true;break;}}}Sleep(10);}system("cls");srs();DWORD lm=0;while(true){int n=recv(sk,bf,255,0);if(n>0){bf[n]=0;ca+=bf;size_t p;while((p=ca.find('\n'))!=string::npos){string msg=ca.substr(0,p);ca.erase(0,p+1);if(rp){if(msg=="[START]"){system("cls");for(int y=0;y<H;++y) for(int x=0;x<W;++x) dcell(x,y);dc(mx,my,'O',FOREGROUND_RED|FOREGROUND_INTENSITY);dcell(ex,ey);omx=mx; omy=my; oex=ex; oey=ey; rp=false; gs=true; mhp=10; ehp=10; shp();}else if(msg=="[READY]"){er=true; srs();}else if(msg=="[UNREADY]"){er=false; srs();}}else if(!go){int x,y;if(sscanf(msg.c_str(),"[MOVE|%d|%d]",&x,&y)==2){if(sd(x,y)&&!(x==mx&&y==my)){dcell(oex,oey); ex=x; ey=y; oex=ex; oey=ey;if(ru&&GetTickCount()<=ru) dc(x,y,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY);}}else if(msg=="[HIT]"){mhp--; fr=true; fs=GetTickCount(); fsr(BACKGROUND_RED); hfa=true; hfs=GetTickCount();sprintf(bf,"[HP|%d|%d]\n",mhp,ehp); send(sk,bf,strlen(bf),0);if(mhp<=0){go=true; send(sk,"[DEAD]\n",7,0); sgo("对方");}else shp();}else if(sscanf(msg.c_str(),"[REVEAL|%d|%d]",&x,&y)==2){dcell(oex,oey); ex=x; ey=y; oex=ex; oey=ey; ru=GetTickCount()+1000;dc(ex,ey,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY);}else if(sscanf(msg.c_str(),"[GAMEOVER|%[^]]]",bf)==1){go=true; sgo(string(bf));}else if(sscanf(msg.c_str(),"[HP|%d|%d]",&x,&y)==2){ehp = x;mhp = y;shp();}else if(msg=="[SPEED_ON]"){speedBoost=true; shp();}else if(msg=="[SPEED_OFF]"){speedBoost=false; shp();}else if(msg=="[RAY_HIT]"){mhp-=3; fr=true; fs=GetTickCount(); fsr(BACKGROUND_RED); hfa=true; hfs=GetTickCount();sprintf(bf,"[HP|%d|%d]\n",mhp,ehp); send(sk,bf,strlen(bf),0);if(mhp<=0){go=true; send(sk,"[DEAD]\n",7,0); sgo("对方");}else shp();}else if(msg=="[DETECT]"){ru=GetTickCount()+3000;dcell(oex,oey);dc(ex,ey,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY);}else if(msg=="[SELF_REVEAL]"){ru=GetTickCount()+2000;dcell(oex,oey);dc(ex,ey,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY);}else if(msg=="[MISSILE_HIT]"){fr=true; fs=GetTickCount(); fsr(BACKGROUND_RED); hfa=true; hfs=GetTickCount();shp();}else if(sscanf(msg.c_str(),"[BARRIER_PLACE|%d|%d]",&x,&y)==2){// 服务端广播放置光幕if(x>=0 && x<W && y>=0 && y<H && MAP[y][x]==0){tempWall[y][x] = true;wallTime[y][x] = GetTickCount();dcell(x,y);}}else if(sscanf(msg.c_str(),"[BARRIER_REMOVE|%d|%d]",&x,&y)==2){// 服务端广播移除光幕if(x>=0 && x<W && y>=0 && y<H){removeBarrierLocal(x,y);}}}}}if(_kbhit()&&GetTickCount()-lm>120&&!go){char k=_getch(); DWORD nw=GetTickCount();if(rp){if(k==' '){mr=!mr; send(sk,mr?"[READY]\n":"[UNREADY]\n",mr?8:11,0); srs(); lm=nw;}else if(k=='q'||k=='Q'){int next = (qSkill+1)%5;while(next == eSkill) next = (next+1)%5;qSkill = next;srs(); showPreview(qSkill); lm=nw;}else if(k=='e'||k=='E'){int next = (eSkill+1)%5;while(next == qSkill) next = (next+1)%5;eSkill = next;srs(); showPreview(eSkill); lm=nw;}}else{int nx=mx,ny=my,dx=0,dy=0;if(k=='w'||k=='W'){ny--; dy=-1;}else if(k=='s'||k=='S'){ny++; dy=1;}else if(k=='a'||k=='A'){nx--; dx=-1;}else if(k=='d'||k=='D'){nx++; dx=1;}else if(k==' '){if(nw-acd>=1000){acd=nw; send(sk,"[REVEAL]\n",9,0);if(ldx!=0||ldy!=0){normalRaySx=mx; normalRaySy=my; normalRayDirX=ldx; normalRayDirY=ldy;drawRayLine(mx,my,ldx,ldy,'*',FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY);normalRayActive=true; normalRayEndTime=nw+200;}if(rhe(mx,my,ldx,ldy,ex,ey)) send(sk,"[HIT]\n",7,0);}lm=nw; continue;}else if(k=='q'||k=='Q'){useSkill(sk,0,nw);lm=nw; continue;}else if(k=='e'||k=='E'){useSkill(sk,1,nw);lm=nw; continue;}else continue;if(speedBoost){int sx=dx,sy=dy,ms=0;for(int i=1;i<=3;i++){int cx=mx+sx*i,cy=my+sy*i; if(!sd(cx,cy)||(cx==ex&&cy==ey)) break; ms=i;}if(ms>0){nx=mx+sx*ms; ny=my+sy*ms;} else{lm=nw; continue;}}if(sd(nx,ny)&&!(nx==ex&&ny==ey)){dcell(omx,omy); dc(nx,ny,'O',FOREGROUND_RED|FOREGROUND_INTENSITY);mx=nx; my=ny; omx=mx; omy=my; ldx=dx; ldy=dy;sprintf(bf,"[MOVE|%d|%d]\n",mx,my); send(sk,bf,strlen(bf),0);if(speedBoost){speedBoost=false; send(sk,"[SPEED_OFF]\n",12,0); shp();}lm=nw;}}}if(gs&&!go){DWORD now=GetTickCount();// 本地检查临时墙超时,并通知服务端for(int y=0;y<H;y++){for(int x=0;x<W;x++){if(tempWall[y][x] && now - wallTime[y][x] >= 20000){removeBarrierLocal(x,y);// 通知服务端移除char buf[64];sprintf(buf,"[BARRIER_REMOVE|%d|%d]\n",x,y);send(sk,buf,strlen(buf),0);}}}if(myMissile.active){if(now - myMissile.lastMoveTime >= 180){dcell(myMissile.curX,myMissile.curY);int newX = myMissile.curX + myMissile.dirX;int newY = myMissile.curY + myMissile.dirY;if(sd(newX,newY)){myMissile.curX = newX;myMissile.curY = newY;if(checkMyMissileHit(sk, now)) continue;dc(myMissile.curX,myMissile.curY,'*',FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY);myMissile.lastMoveTime = now;}else{myMissile.active = false;}}}if(fr&&now-fs>200){fr=false; fsr(0); for(int y=0;y<H;++y) for(int x=0;x<W;++x) dcell(x,y); redrawPlayers(); shp();}if(hfa&&now-hfs>300){hfa=false; shp();}if(ru&&now<=ru) dc(ex,ey,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY);else if(ru&&now>ru){ru=0; dcell(ex,ey);}if(selfRu&&now<=selfRu) dc(mx,my,'O',FOREGROUND_RED|FOREGROUND_INTENSITY);else if(selfRu&&now>selfRu){selfRu=0; dcell(mx,my);dc(mx,my,'O',FOREGROUND_RED|FOREGROUND_INTENSITY);}if(previewActive&&now>previewEndTime){previewActive=false; if(prevSkillType==1&&ldx!=0&&ldy!=0){clearRayLine(mx,my,ldx,ldy); redrawPlayers();}}if(rayActive&&now>rayEndTime){rayActive=false; clearRayLine(raySx,raySy,rayDirX,rayDirY); redrawPlayers();}if(normalRayActive&&now>normalRayEndTime){normalRayActive=false; clearRayLine(normalRaySx,normalRaySy,normalRayDirX,normalRayDirY); redrawPlayers();}static DWORD lastShp=0;if(now-lastShp>=1000){lastShp=now; shp();}}Sleep(10);}}```