跳到正文

个人记录

联机小游戏

bailuoboyu约 24 分钟
原文

这是 2026年7月18日 存档的第 3 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年7月17日
收录
2026年7月17日
更新
2026年11月14日
篇幅
约 24 分钟

版本

  1. v6当前
  2. v5
  3. v4
  4. v3
更早的 2 个版本
  1. v2
  2. v1

运行要求:上方”工具”->“编译选项”->“编译时加入以下命令”-std=c++14、“在连接器命令行加入以下命令”-lws2_32
运行过程:服务端在自己电脑上搜索Windows PowerShell->打开并输入ipconfig->找到”以太网适配器 以太网:“下方的”IPv4 地址 … … … … :“并复制其后内容发给客户端->服务端运行代码->客户端运行代码并将服务端发来的地址输入->开始游玩

服务端

C++
#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},
    {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,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
    {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
    {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
    {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
    {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 meX,meY,youX,youY;
int oldMeX,oldMeY,oldYouX,oldYouY;
int lastDirX=0,lastDirY=0;
bool flashRed;
DWORD flashStart;

bool solid(int x,int y){
    return x>=0&&y>=0&&x<W&&y<H&&MAP[y][x]==0;
}

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){
    SetConsoleCursorPosition(hConsole,{(SHORT)x,(SHORT)y});
    SetConsoleTextAttribute(hConsole,color);
    cout<<c;
}

void drawCell(int x,int y){
    drawChar(x,y,MAP[y][x]?'#':' ',7);
}

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(time(NULL));
    hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO ci={1,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);

    cout<<"[服务端] 等待客户端...\n";
    SOCKET cli=accept(svr,NULL,NULL);
    u_long mode=1;
    ioctlsocket(cli,FIONBIO,&mode);

    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));

    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;
            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;
                    }
                }else if(msg=="[HIT]"){
                    flashRed=true;
                    flashStart=GetTickCount();
                    fillScreen(BACKGROUND_RED);
                }
            }
        }

        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(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;

                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);
    }
}

客户端

C++
#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},
    {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,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
    {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
    {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
    {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
    {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 myX,myY,enemyX,enemyY;
int oldMyX,oldMyY,oldEnemyX,oldEnemyY;
int lastDirX=0,lastDirY=0;
bool flashRed;
DWORD flashStart;

bool solid(int x,int y){
    return x>=0&&y>=0&&x<W&&y<H&&MAP[y][x]==0;
}

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){
    SetConsoleCursorPosition(hConsole,{(SHORT)x,(SHORT)y});
    SetConsoleTextAttribute(hConsole,color);
    cout<<c;
}

void drawCell(int x,int y){
    drawChar(x,y,MAP[y][x]?'#':' ',7);
}

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(){
    hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO ci={1,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={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);

    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;
            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);

    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;
            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 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(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;

                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);
    }
}