跳到正文

个人记录

联机小游戏

bailuoboyu约 22 分钟
原文

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

文章信息与版本

文章信息

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

版本

  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 0x0600
#include <iostream>
#include <winsock2.h>
#include <windows.h>
#include <cstring>
#include <random>
#include <chrono>
#include <conio.h>
#include <algorithm>
#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,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, youX, youY;
int oldMeX = -1, oldMeY = -1;
int oldYouX = -1, oldYouY = -1;

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

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

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int main() {
    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 = {AF_INET, htons(8080)};
    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);

    const int cx = 10, cy = 10, rx = 8, ry = 8;
    do {
        meX = uniform_int_distribution<int>(0, W-1)(rng);
        meY = uniform_int_distribution<int>(0, H-1)(rng);
    } while (!solid(meX, meY) ||
             ((meX-cx)*(meX-cx)/float(rx*rx) + (meY-cy)*(meY-cy)/float(ry*ry)) > 1.0f);

    do {
        youX = uniform_int_distribution<int>(0, W-1)(rng);
        youY = uniform_int_distribution<int>(0, H-1)(rng);
    } while (!solid(youX, youY) ||
             ((youX-cx)*(youX-cx)/float(rx*rx) + (youY-cy)*(youY-cy)/float(ry*ry)) > 1.0f ||
             (youX == meX && youY == meY));

    string init = "[INIT|" + to_string(meX) + "|" + to_string(meY) + "|" +
                  to_string(youX) + "|" + to_string(youY) + "]\n";
    send(cli, init.c_str(), init.size(), 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 lastMoveTime = 0;

    while (true) {
        int r = recv(cli, buf, sizeof(buf)-1, 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;
                    }
                }
            }
        }

        if (_kbhit()) {
            DWORD now = GetTickCount();
            if (now - lastMoveTime > 150) {
                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;
                    string move = "[MOVE|" + to_string(meX) + "|" + to_string(meY) + "]\n";
                    send(cli, move.c_str(), move.size(), 0);
                    lastMoveTime = now;
                }
            }
        }
        Sleep(10);
    }

    closesocket(cli);
    closesocket(svr);
    WSACleanup();
    return 0;
}

客户端

C++
#define _WIN32_WINNT 0x0600
#include <iostream>
#include <winsock2.h>
#include <windows.h>
#include <cstring>
#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,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 = -1, meY = -1;
int himX = -1, himY = -1;
int oldMeX = -1, oldMeY = -1;
int oldHimX = -1, oldHimY = -1;
bool ok = false;

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

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

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 = {AF_INET, htons(8080)};
    addr.sin_addr.s_addr = inet_addr(ip.c_str());
    connect(sock, (sockaddr*)&addr, sizeof(addr));

    u_long mode = 1;
    ioctlsocket(sock, FIONBIO, &mode);

    char buf[256];
    string cache;

    while (!ok) {
        int r = recv(sock, buf, sizeof(buf)-1, 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 aX, aY, bX, bY;
                if (sscanf(msg.c_str(), "[INIT|%d|%d|%d|%d]", &aX, &aY, &bX, &bY) == 4) {
                    himX = aX; himY = aY;
                    meX = bX; meY = bY;
                    ok = true;
                }
            }
        }
        Sleep(10);
    }

    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(himX, himY, 'O', FOREGROUND_BLUE | FOREGROUND_INTENSITY);

    oldMeX = meX; oldMeY = meY;
    oldHimX = himX; oldHimY = himY;

    DWORD lastMoveTime = 0;

    while (true) {
        int r = recv(sock, buf, sizeof(buf)-1, 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)) {
                        drawCell(oldHimX, oldHimY);
                        drawChar(x, y, 'O', FOREGROUND_BLUE | FOREGROUND_INTENSITY);
                        himX = x; himY = y;
                        oldHimX = himX; oldHimY = himY;
                    }
                }
            }
        }

        if (_kbhit()) {
            DWORD now = GetTickCount();
            if (now - lastMoveTime > 150) {
                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 == himX && ny == himY)) {
                    drawCell(oldMeX, oldMeY);
                    drawChar(nx, ny, 'O', FOREGROUND_RED | FOREGROUND_INTENSITY);
                    meX = nx; meY = ny;
                    oldMeX = meX; oldMeY = meY;
                    string move = "[MOVE|" + to_string(meX) + "|" + to_string(meY) + "]\n";
                    send(sock, move.c_str(), move.size(), 0);
                    lastMoveTime = now;
                }
            }
        }
        Sleep(10);
    }

    closesocket(sock);
    WSACleanup();
    return 0;
}