版本比较
联机小游戏
正文
增加 236 行删除 229 行
12345667891091011121516171813141516171811 行未改动3031323338343536373839444546474041424344454950514647484954555051525354555657585859606162636459606162636467686970716566676874756970717273747576777878797980818283848182838485858687888990919293878894959697989091929394959699100101102989910010110210310410510610710810911011111211311410311611710410510610712012112210810911011112412511211311411511612712812911711813211912013413513613713813914014114212112212312412512612712812914414514614714814915015115215313013113213313413513613713813914014114214314414515916016114614714814915015115215315415515615715815916316416516616716016116216316416516616917017117217317417517617717816716816917017117217317417517617717817918018118218318418518618718818919019119019119219319419519619719819919619719819920020111 行未改动213214215216219217218219220221222225226227228223224225226227228230231232229230231232235236237238239240241233234235236237238239240241244245246247248242243244245246247251252253254255256248249250251258259252253254255256257258259260261261262263264265266263264265266267268269270271272273274267268269270271272273274275276277278279277278279280281282283284285286287288289290291292293294295296297298299282283284285286287288289290291292293294295296297298299300301302307308309303304305306311312307308309310311314315316317318319320321322323324325326327328329330331332333334335336337338339312313314315316317318319320321322323324325326327328329330331332333334335336344345346337338339340341342343344345346347348349350348349350351352351352353354355356357354355356357358359360361362363358359360361362363364365366367368369370371372373374375376**运行要求**:上方"工具"->"编译选项"->"编译时加入以下命令"```-std=c++14```、"在连接器命令行加入以下命令"```-lws2_32``` **运行过程**:服务端在自己电脑上搜索```Windows PowerShell```->打开并输入```ipconfig```->找到"以太网适配器 以太网:"下方的"IPv4 地址 . . . . . . . . . . . . :"并复制其后内容发给客户端->服务端运行代码->客户端运行代码并将服务端发来的地址输入->开始游玩# 服务端```cpp#define _WIN32_WINNT 0x0500#include <iostream>#include <bits/stdc++.h>#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] = {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,1,1,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,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;int meX,meY,youX,youY;int oldMeX,oldMeY,oldYouX,oldYouY;int lastDirX=0,lastDirY=0;bool flashRed;DWORD flashStart;
bool solid(int x, int y) { if (x < 0 || y < 0 || x >= W || y >= H) return false; return MAP[y][x] == 0;bool solid(int x,int y){ return x>=0&&y>=0&&x<W&&y<H&&MAP[y][x]==0;}
bool inSafeArea(int x, int y) { return x >= 4 && x <= 17 && y >= 4 && y <= 17;bool rayHitEnemy(int sx,int sy,int dx,int dy,int ex,int ey){ int x=sx+dx,y=sy+dy; while(solid(x,y)){ if(x==ex&&y==ey)return true; x+=dx;y+=dy; } return false;}
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 drawChar(int x,int y,char c,int color){ SetConsoleCursorPosition(hConsole,{(SHORT)x,(SHORT)y}); SetConsoleTextAttribute(hConsole,color); cout<<c;}
void drawCell(int x, int y) { if (MAP[y][x]) drawChar(x, y, '#', 7); else drawChar(x, y, ' ', 7);void drawCell(int x,int y){ drawChar(x,y,MAP[y][x]?'#':' ',7);}
int randInt(int a, int b) { return a + rand() % (b - a + 1);void fillScreen(int color){ CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(hConsole,&csbi); SMALL_RECT sr=csbi.srWindow; DWORD w; FillConsoleOutputAttribute(hConsole,(WORD)color, (sr.Right-sr.Left+1)*(sr.Bottom-sr.Top+1), {sr.Left,sr.Top},&w);}
int main() { srand((unsigned int)time(NULL));int main(){ srand(time(NULL)); hConsole=GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO ci={1,FALSE}; SetConsoleCursorInfo(hConsole,&ci);
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={0}; 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);
WSADATA wsa; WSAStartup(MAKEWORD(2,2), &wsa); cout<<"[服务端] 等待客户端...\n"; SOCKET cli=accept(svr,NULL,NULL); u_long mode=1; ioctlsocket(cli,FIONBIO,&mode);
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); do{meX=rand()%W;meY=rand()%H;}while(!solid(meX,meY)); do{youX=rand()%W;youY=rand()%H;} while(!solid(youX,youY)||(youX==meX&&youY==meY));
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); 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); 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); drawChar(meX,meY,'O',FOREGROUND_RED|FOREGROUND_INTENSITY); drawChar(youX,youY,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY); oldMeX=meX;oldMeY=meY; oldYouX=youX;oldYouY=youY;
oldMeX = meX; oldMeY = meY; oldYouX = youX; oldYouY = youY;
char buf[256]; string cache; DWORD lastMove = 0; 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); while(true){ int r=recv(cli,buf,255,0); if(r>0){ buf[r]=0;cache+=buf; size_t 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); 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; } }else if(msg=="[HIT]"){ flashRed=true; flashStart=GetTickCount(); fillScreen(BACKGROUND_RED); } } }
if (_kbhit() && GetTickCount() - lastMove > 120) { char key = _getch(); int nx = meX, ny = meY; if(_kbhit()&&GetTickCount()-lastMove>120){ char k=_getch(); int nx=meX,ny=meY,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(rayHitEnemy(meX,meY,lastDirX,lastDirY,youX,youY)) send(cli,"[HIT]\n",7,0); lastMove=GetTickCount(); continue; }else continue;
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; lastDirX=dx;lastDirY=dy;
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(); sprintf(buf,"[MOVE|%d|%d]\n",meX,meY); send(cli,buf,strlen(buf),0); lastMove=GetTickCount(); } }
if(flashRed&&GetTickCount()-flashStart>200){ flashRed=false; fillScreen(0); 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); } Sleep(10); }}```# 客户端```cpp#define _WIN32_WINNT 0x0500#include <bits/stdc++.h>#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] = {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,1,1,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,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;int myX,myY,enemyX,enemyY;int oldMyX,oldMyY,oldEnemyX,oldEnemyY;int lastDirX=0,lastDirY=0;bool flashRed;DWORD flashStart;
bool solid(int x, int y) { if (x < 0 || y < 0 || x >= W || y >= H) return false; return MAP[y][x] == 0;bool solid(int x,int y){ return x>=0&&y>=0&&x<W&&y<H&&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;bool rayHitEnemy(int sx,int sy,int dx,int dy,int ex,int ey){ int x=sx+dx,y=sy+dy; while(solid(x,y)){ if(x==ex&&y==ey)return true; x+=dx;y+=dy; } return false;}
void drawCell(int x, int y) { if (MAP[y][x]) drawChar(x, y, '#', 7); else drawChar(x, y, ' ', 7);void drawChar(int x,int y,char c,int color){ SetConsoleCursorPosition(hConsole,{(SHORT)x,(SHORT)y}); SetConsoleTextAttribute(hConsole,color); cout<<c;}
int main() { hConsole = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO ci; ci.dwSize = 1; ci.bVisible = FALSE; SetConsoleCursorInfo(hConsole, &ci);void drawCell(int x,int y){ drawChar(x,y,MAP[y][x]?'#':' ',7);}
WSADATA wsa; WSAStartup(MAKEWORD(2,2), &wsa);void fillScreen(int color){ CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(hConsole,&csbi); SMALL_RECT sr=csbi.srWindow; DWORD w; FillConsoleOutputAttribute(hConsole,(WORD)color, (sr.Right-sr.Left+1)*(sr.Bottom-sr.Top+1), {sr.Left,sr.Top},&w);}
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);int main(){ hConsole=GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO ci={1,FALSE}; SetConsoleCursorInfo(hConsole,&ci);
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; 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={0}; 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<<"连接失败\n";return 0; } u_long mode=1; ioctlsocket(sock,FIONBIO,&mode);
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; bool ok=false; while(!ok){ int n=recv(sock,buf,255,0); if(n>0){ buf[n]=0;cache+=buf; size_t 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); 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); drawChar(myX,myY,'O',FOREGROUND_RED|FOREGROUND_INTENSITY); drawChar(enemyX,enemyY,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY); oldMyX=myX;oldMyY=myY; oldEnemyX=enemyX;oldEnemyY=enemyY;
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; DWORD lastMove=0; while(true){ int n=recv(sock,buf,255,0); if(n>0){ buf[n]=0;cache+=buf; size_t 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==myX&&y==myY)){ drawCell(oldEnemyX,oldEnemyY); drawChar(x,y,'O',FOREGROUND_BLUE|FOREGROUND_INTENSITY); enemyX=x;enemyY=y; oldEnemyX=enemyX;oldEnemyY=enemyY; } }else if(msg=="[HIT]"){ flashRed=true; flashStart=GetTickCount(); fillScreen(BACKGROUND_RED); } } }
if (_kbhit() && GetTickCount() - lastMove > 120) { char key = _getch(); int nx = myX, ny = myY; if(_kbhit()&&GetTickCount()-lastMove>120){ char k=_getch(); int nx=myX,ny=myY,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(rayHitEnemy(myX,myY,lastDirX,lastDirY,enemyX,enemyY)) send(sock,"[HIT]\n",7,0); lastMove=GetTickCount(); continue; }else continue;
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; lastDirX=dx;lastDirY=dy;
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(); sprintf(buf,"[MOVE|%d|%d]\n",myX,myY); send(sock,buf,strlen(buf),0); lastMove=GetTickCount(); } }
if(flashRed&&GetTickCount()-flashStart>200){ flashRed=false; fillScreen(0); 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); } Sleep(10); }}```