c++小游戏合集(双人)

目录

1.双人枪战

2.双人贪吃蛇


1.双人枪战

#include<iostream>
#include<cstdio>
#include<windows.h>
#include<conio.h>
using namespace std;
int SIZ = 20;
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;

HANDLE hCon;
enum Color { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE };
void SetColor(Color c) {
    if(hCon == NULL)
        hCon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hCon, c);
}

SYSTEMTIME sys;

//sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute, sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek
struct PLAYER {
    int x,y;
    int hp;
    int gun;
    int direct;
} p1,p2;
int map[1005][1005];

int abs(int x) {
    if(x < 0) return -x;
    return x;
}

void locate(int x,int y) {
    coord.X=y - 1;
    coord.Y=x - 1;
    SetConsoleCursorPosition(hout,coord);
}

void print_map() {
    locate(1,1);
    SetColor(GRAY);
    for(int i = 1; i <= SIZ; i++) cout<<"■";
    locate(SIZ,1);
    for(int i = 1; i <= SIZ; i++) cout<<"■";
    for(int i = 2; i < SIZ; i++) {
        locate(i,1);
        cout<<"■";
        locate(i,SIZ*2-1);
        cout<<"■";
    }
    locate(SIZ+1,1);
    SetColor(WHITE);
}

void create_tree(int x,int y) {
    map[x][y] = map[x+1][y] = map[x-1][y] = map[x][y+1] = map[x][y-1] = 2;
}

void use_map(int x) {
    if(x == 1) {
        SIZ = 20;
        SetColor(DARKGREEN);
        map[16][6]=map[15][6]=map[17][6]=map[16][7]=map[16][5]=map[14][13]=map[13][12]=map[13][13]=2;
        for(int i = 2; i < SIZ; i++) {
            for(int j = 2; j < SIZ; j++) {
                if(map[i][j] == 2) {
                    locate(i,j*2-1);
                    cout<<"■";
                }
            }
        }
        SetColor(GRAY);
        for(int i = 5; i <= 15; i++) {
            map[i][i] = 1;
            locate(i,i*2-1);
            cout<<"■";
        }
        SetColor(WHITE);
    } else if(x == 2) {
        SIZ = 30;
        SetColor(GRAY);
        for(int i = 4; i <= 26; i++) {
            if(i == 13 || i == 14 ||i == 15) continue;
            map[i][4] = map[4][i] = map[26][i] = map[i][26] = 1;
        }
        for(int i = 1; i <= SIZ; i++) {
            for(int j = 1; j <= SIZ; j++) {
                if(map[i][j] == 1) {
                    locate(i,j*2-1);
                    cout<<"■";
                }
            }
        }
        SetColor(DARKGREEN);
        for(int i = 10; i<=20; i++) {
            if(i == 13 || i == 17) continue;
            map[i][10] = map[10][i] = map[20][i] = map[i][20] = 2;
        }
        create_tree(5,5);
        create_tree(18,18);
        for(int i = 1; i <= SIZ; i++) {
            for(int j = 1; j <= SIZ; j++) {
                if(map[i][j] == 2) {
                    locate(i,j*2-1);
                    cout<<"■";
                }
            }
        }
        SetColor(WHITE);
    }
}
void cleanbody(int x,int y);
void putbody(int x,int y,int z);

void player_init() {
    p1.hp = p2.hp = 300;
    p1.gun = p2.gun = 1;
    p1.direct = 4;
    p2.direct = 2;
    p1.x = 2;
    p1.y = 2;
    p2.x = SIZ - 1;
    p2.y = SIZ - 1;
    putbody(p1.x,p1.y,1);
    putbody(p2.x,p2.y,2);
}

void mapinit() {
    for(int i = 1; i <= SIZ; i++) {
        map[i][1] = map[1][i] = map[SIZ][i] = map[i][SIZ] = 1;
    }
}

void init() {
    printf("Use Which Map?\n");
    int x;
    cin>>x;
    system("cls");
    use_map(x);
    mapinit();
    print_map();
    player_init();
}

void putbody(int x,int y,int z) {
    if(z == 1) SetColor(BLUE);
    else if(z == 2) SetColor(RED);
    locate(x,y*2-1);
    cout<<"■";
    SetColor(WHITE);
}
void cleanbody(int x,int y) {
    locate(x,y*2-1);
    cout<<" ";
}
/*
    LIST
    direct:
        w 1
        a 2
        s 3
        d 4
    gun:
        usp 1
        mimigun 2
        awp 3
    block:
        void 0
        stone 1
        tree 2
        player 3
        clip 4
*/
bool judge(int x,int y) {
    if(map[x][y] == 1) return false;
    if(map[x][y] == 2) return false;
    if(map[x][y] == 3) return false;
    return true;
}

bool judge_gun(int x,int y) {
    if(map[x][y] == 1) return 0;
    if(map[x][y] == 2) return 0;
    if(map[x][y] == 3) {
        if(p1.x == x && p1.y == y) p1.hp -= 50;//此处暂时不管威力
        else p2.hp -= 50;
        return 0;
    }
    return 1;
}

int cnt;
struct Clip {
    int x,y;
    int derect;
    int force;
    int start;
    bool flag;
} clip[1000000];
void create_clip(int y,int x,int a,int b) {
    int X,Y;
    if(y == 1) {
        if(!judge_gun(a-1,b)) return;
        X = a-1;
        Y = b;
    } else if(y == 2) {
        if(!judge_gun(a,b-1)) return;
        X = a;
        Y = b-1;
    } else if(y == 3) {
        if(!judge_gun(a+1,b)) return;
        X = a+1;
        Y = b;
    } else if(y == 4) {
        if(!judge_gun(a,b+1)) return;
        X = a;
        Y = b+1;
    }
    cnt++;
    GetLocalTime( &sys );
    clip[cnt].start = sys.wMilliseconds + sys.wSecond * 60 + sys.wHour * 3600;
    clip[cnt].x = X;
    clip[cnt].y = Y;
    if(x == 1) {
        clip[cnt].derect = p1.direct;
    } else if(x == 2) {
        clip[cnt].derect = p2.direct;
    }
}

void shoot(int x) {
    if(x == 1) {
        create_clip(p1.direct,1,p1.x,p1.y);
    } else if(x == 2) {
        create_clip(p2.direct,2,p2.x,p2.y);
    }
}

void clean_clip(int x,int y) {
    locate(x,y*2-1);
    cout<<"  ";
    locate(1,1);
}

void print_clip(int x,int y,int i) {
    if(clip[i].flag) {
        clean_clip(x,y);
        return;
    }
    locate(x,y*2-1);
    SetColor(YELLOW);
    cout<<"''";
    locate(1,1);
//  system("pause");
}

void clipmove() {
    GetLocalTime( &sys );
    int t = sys.wMilliseconds + sys.wSecond * 60 + sys.wHour * 3600;
    for(int i = 1; i <= cnt; i++) {
        if(clip[i].flag) continue;
        if(abs(clip[i].start - t) > 50) {
            clip[i].start = t;
            int x = clip[i].x;
            int y = clip[i].y;
            if(clip[i].derect==1) {
                if(!judge_gun(clip[i].x-1,clip[i].y)) {
                    clip[i].flag = 1;
                    clean_clip(x,y);
                    continue;
                }
                clean_clip(clip[i].x,clip[i].y);
                clip[i].x--;
                print_clip(clip[i].x,clip[i].y,i);
            } else if(clip[i].derect==2) {
                if(!judge_gun(clip[i].x,clip[i].y-1)) {
                    clip[i].flag = 1;
                    clean_clip(x,y);
                    continue;
                }
                clean_clip(clip[i].x,clip[i].y);
                clip[i].y--;
                print_clip(clip[i].x,clip[i].y,i);
            } else if(clip[i].derect==3) {
                if(!judge_gun(clip[i].x+1,clip[i].y)) {
                    clip[i].flag = 1;
                    clean_clip(x,y);
                    continue;
                }
                clean_clip(clip[i].x,clip[i].y);
                clip[i].x++;
                print_clip(clip[i].x,clip[i].y,i);
            } else if(clip[i].derect==4) {
                if(!judge_gun(clip[i].x,clip[i].y+1)) {
                    clip[i].flag = 1;
                    clean_clip(x,y);
                    continue;
                }
                clean_clip(clip[i].x,clip[i].y);
                clip[i].y++;
                print_clip(clip[i].x,clip[i].y,i);
            }
        }
    }
}

void judge_hp() {
    int x = p1.hp;
    int y = p2.hp;
    if(x<0 && y<0 && x > y) swap(x,y);
    if(x <= 0) {
        locate(1,1);
        system("cls");
        printf("GAME OVER!\nTHE WINNER IS P2!");
        Sleep(5000);
        printf("\n-MADE BY Floatiy-");
        exit(0);
    } else if(y <= 0) {
        locate(1,1);
        system("cls");
        printf("GAME OVER!\nTHE WINNER IS P1!");
        Sleep(5000);
        printf("\n-MADE BY Floatiy-");
        exit(0);
    }
}

void prog() {
    char ch;
    while(true) {
        if(kbhit()) {
            ch=getch();
            if(ch == 'w' && judge(p1.x-1,p1.y)) {
                p1.direct = 1;
                cleanbody(p1.x,p1.y);
                map[p1.x][p1.y] = 0;
                putbody(--p1.x,p1.y,1);
                map[p1.x][p1.y] = 3;
            } else if(ch == '8' && judge(p2.x-1,p2.y)) {
                p2.direct = 1;
                cleanbody(p2.x,p2.y);
                map[p2.x][p2.y] = 0;
                putbody(--p2.x,p2.y,2);
                map[p2.x][p2.y] = 3;
            } else if(ch == 'a' && judge(p1.x,p1.y-1)) {
                p1.direct = 2;
                cleanbody(p1.x,p1.y);
                map[p1.x][p1.y] = 0;
                putbody(p1.x,--p1.y,1);
                map[p1.x][p1.y] = 3;
            } else if(ch == '4' && judge(p2.x,p2.y-1)) {
                p2.direct = 2;
                cleanbody(p2.x,p2.y);
                map[p2.x][p2.y] = 0;
                putbody(p2.x,--p2.y,2);
                map[p2.x][p2.y] = 3;
            } else if(ch == 's' && judge(p1.x+1,p1.y)) {
                p1.direct = 3;
                cleanbody(p1.x,p1.y);
                map[p1.x][p1.y] = 0;
                putbody(++p1.x,p1.y,1);
                map[p1.x][p1.y] = 3;
            } else if(ch == '5' && judge(p2.x+1,p2.y)) {
                p2.direct = 3;
                cleanbody(p2.x,p2.y);
                map[p2.x][p2.y] = 0;
                putbody(++p2.x,p2.y,2);
                map[p2.x][p2.y] = 3;
            } else if(ch == 'd' && judge(p1.x,p1.y+1)) {
                p1.direct = 4;
                cleanbody(p1.x,p1.y);
                map[p1.x][p1.y] = 0;
                putbody(p1.x,++p1.y,1);
                map[p1.x][p1.y] = 3;
            } else if(ch == '6' && judge(p2.x,p2.y+1)) {
                p2.direct = 4;
                cleanbody(p2.x,p2.y);
                map[p2.x][p2.y] = 0;
                putbody(p2.x,++p2.y,2);
                map[p2.x][p2.y] = 3;
            } else if(ch == '0') {
                shoot(2);
            } else if(ch == ' ') {
                shoot(1);
            }
            Sleep(20);
        }
        clipmove();
        judge_hp();
    }
}
void welcome() {
    printf("操作方法:\n玩家1 wasd控制移动,空格攻击\n玩家2 数字小键盘4568控制移动,0攻击\n");
    Sleep(2000);
}
int main(){
    welcome();
    GetLocalTime( &sys );
    init();
    prog();
    return 0;
}

2.双人贪吃蛇

#include <iostream>
#include <windows.h>
#include <cstring>
#include <time.h>
#include <conio.h>
using namespace std;

int error1(char x,char y)
{
    if(x<y)
        if((y-x==3)||(y-x==4))
            return 1;
    if(x>y)
        if((x-y==3)||(x-y==4))
            return 1;
    if(x==y)
        return 1;
    return 0;
}
int error2(char x,char y)
{
    if(x<y)
        if(y-x==2)
            return 1;
    if(x>y)
        if(x-y==2)
            return 1;
    if(x==y)
        return 1;
    return 0;
}
//纠正强行自噬 

int Pos(long x,long y)
{
    HANDLE direct=GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos={x,y};
    SetConsoleCursorPosition(direct,pos);
}
//改变光标位置

int Color(long clr)
{
    HANDLE direct=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(direct,clr);
}
//改变输出颜色 

int Show(long judge)
{
    HANDLE direct=GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cci;
    GetConsoleCursorInfo(direct,&cci);
    cci.bVisible=judge;
    SetConsoleCursorInfo(direct,&cci);  
} 
//是否显示光标 

int main()
{
    char map[15][18];
    char Key1,Keys1,Key2,Keys2,Key;
    long xh1,yh1,xt1,yt1,start1,timeover1;
    long xh2,yh2,xt2,yt2,start2,timeover2;
    long xm,ym,level,move;
    long X,Y;

    memset(map,' ',sizeof(map));
    for(long i=0;i<=14;i++)
    {
        if((i==0)||(i==14))
            for(long j=0;j<=17;j++)    
                map[i][j]='b';                        
        else
            map[i][0]=map[i][17]='b';
    }
    //地图初始化 
    xh1=7,yh1=9;
    xt1=7,yt1=8;
    map[xt1][yt1]='d';
    map[xh1][yh1]='h';
    //蛇1初始化 
    xh2=4,yh2=9;
    xt2=4,yt2=8;
    map[xt2][yt2]='D';
    map[xh2][yh2]='H';
    //蛇2初始化 
    for(long i=1;i<=2;i++)
    {
        srand(time(0));
        do{
            xm=rand()%13+1;
            ym=rand()%16+1;
          }while(map[xm][ym]!=' ');
        map[xm][ym]='m';    
    }
    //出米 
    Pos(22,8);
    cout<<"please input the level(1-8): ";
    cin>>level; 
    //输入等级
    Show(0);
    X=20;Y=3;
    Pos(X,Y);
    for(long i=0;i<=14;i++)
    {
        for(long j=0;j<=17;j++)
        {
            switch(map[i][j])
            {
                case 'b':
                    Color(0x02);
                    cout<<"■";
                    Color(0x0F);   
                    break;
                case 'w':case 'a':case 's':case 'd':case 'h':
                    Color(0x01);
                    cout<<"■";
                    Color(0x0F);
                    break;
                case 'W':case 'A':case 'S':case 'D':case 'H':
                    Color(0x04);
                    cout<<"■";
                    Color(0x0F);
                    break;
                case 'm':
                    Color(0x03);
                    cout<<"■";
                    Color(0x0F);   
                    break;
                case ' ':
                    cout<<"  ";
                    break;
            }
        }
        Y++;
        Pos(X,Y);
    }
    //输出棋盘 
    Key1='d';
    Key2='l';

    timeover1=1;timeover2=1;
    start1=clock();start2=clock();
    move=0;

    loop:
        while((timeover1=(clock()-start1<=(900-level*100)))&&(timeover2=(clock()-start2<=(900-level*100)))&&!kbhit());

        if(timeover1&&timeover2)
        {
            Key=getch();
            switch(Key)
            {
                case 'W':case 'w':
                case 'S':case 's':
                case 'A':case 'a':
                case 'D':case 'd':
                    Keys1=Key1;Key1=Key;move=1;break;
                case 'I':case 'i':
                case 'K':case 'k':
                case 'J':case 'j':
                case 'L':case 'l':
                    Keys2=Key2;Key2=Key;move=2;break;
            }
            if(move==1)
                if(error1(Key1,Keys1))
                {
                    Key1=Keys1;
                    goto loop;
                }
            if(move==2)
                if(error2(Key2,Keys2))
                {
                    Key2=Keys2;
                    goto loop;
                }    
        }
        //判断按键归属 
        if(move==1||!timeover1)
        {
            switch(Key1)
            {
                case 'W':case 'w':map[xh1][yh1]='w';xh1--;break;
                case 'S':case 's':map[xh1][yh1]='s';xh1++;break;
                case 'A':case 'a':map[xh1][yh1]='a';yh1--;break;
                case 'D':case 'd':map[xh1][yh1]='d';yh1++;break;
            }
        }
        if(move==2||!timeover2)
        {
            switch(Key2)
            {
                case 'I':case 'i':map[xh2][yh2]='W';xh2--;break;
                case 'K':case 'k':map[xh2][yh2]='S';xh2++;break;
                case 'J':case 'j':map[xh2][yh2]='A';yh2--;break;
                case 'L':case 'l':map[xh2][yh2]='D';yh2++;break;
            }
        }
        //依照归属进行操纵 
        if(move==1||!timeover1)
        {
            if(map[xh1][yh1]!=' '&&map[xh1][yh1]!='m')
            {
                Pos(30,18);
                cout<<"GameOver1";
                return 0;
            }           
        }
        if(move==2||!timeover2)
        {
            if(map[xh2][yh2]!=' '&&map[xh2][yh2]!='m')
            {
                Pos(30,18);
                cout<<"GameOver2";
                return 0;
            }               
        }     
        //游戏结束 
        if(move==1||!timeover1)
        {
            X=20+yh1*2;Y=3+xh1;
            Pos(X,Y);
            Color(0x01);    
            cout<<"■";   
            Color(0x0F);

            if(map[xh1][yh1]=='m')
            {
                map[xh1][yh1]='h';
                srand(time(0));
                do{
                    xm=rand()%13+1;
                    ym=rand()%16+1;
                  }while(map[xm][ym]!=' ');
                map[xm][ym]='m';
                X=20+ym*2;Y=3+xm;
                Pos(X,Y);
                Color(0x03);
                cout<<"■";
                Color(0x0F);
                //出米 
            }                        
            else
            {
                map[xh1][yh1]='h';
                X=20+yt1*2;Y=3+xt1;
                Pos(X,Y);    
                cout<<"  ";
                switch(map[xt1][yt1])
                {
                    case 'w':map[xt1][yt1]=' ';xt1--;break;
                    case 's':map[xt1][yt1]=' ';xt1++;break;
                    case 'a':map[xt1][yt1]=' ';yt1--;break;
                    case 'd':map[xt1][yt1]=' ';yt1++;break;
                }
            }     
            //移动蛇1头与尾           
            move=0;
            timeover1=1;
            start1=clock();
            goto loop;
            //回到开始 
        }

        if(move==2||!timeover2)
        {
            X=20+yh2*2;Y=3+xh2;
            Pos(X,Y);   
            Color(0x04); 
            cout<<"■";   
            Color(0x0F);

            if(map[xh2][yh2]=='m')
            {
                map[xh2][yh2]='H';
                srand(time(0));
                do{
                    xm=rand()%13+1;
                    ym=rand()%16+1;
                  }while(map[xm][ym]!=' ');
                map[xm][ym]='m';
                X=20+ym*2;Y=3+xm;
                Pos(X,Y);
                Color(0x03);
                cout<<"■";
                Color(0x0F);
                //出米 
            }                        
            else
            {
                map[xh2][yh2]='H';
                X=20+yt2*2;Y=3+xt2;
                Pos(X,Y);    
                cout<<"  ";
                switch(map[xt2][yt2])
                {
                    case 'W':map[xt2][yt2]=' ';xt2--;break;
                    case 'S':map[xt2][yt2]=' ';xt2++;break;
                    case 'A':map[xt2][yt2]=' ';yt2--;break;
                    case 'D':map[xt2][yt2]=' ';yt2++;break;
                }
            }       
            move=0;
            timeover2=1;
            start2=clock();
            goto loop;  
        }

}

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 C++ 双人射击小游戏代码示例,使用了 Windows API 和 Winsock2 库来实现网络连接。注意:本代码仅供学习参考,不保证完全正确和稳定,使用时请自行测试和修改。 ```cpp // 双人射击小游戏代码示例 #include <iostream> #include <windows.h> #include <winsock2.h> using namespace std; const int PORT = 9000; const int BUF_SIZE = 1024; // 定义玩家类 class Player { public: Player(int x, int y) : posX(x), posY(y) {} int posX; int posY; void move(int dx, int dy) { posX += dx; posY += dy; } }; // 初始化 Winsock2 库 void initWinsock() { WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { cerr << "WSAStartup failed." << endl; exit(1); } } // 创建服务器端套接字 SOCKET createServerSocket() { SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (serverSocket == INVALID_SOCKET) { cerr << "Failed to create socket." << endl; exit(1); } // 绑定端口号 sockaddr_in serverAddr; serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons(PORT); serverAddr.sin_addr.s_addr = INADDR_ANY; if (bind(serverSocket, (sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) { cerr << "Failed to bind socket." << endl; closesocket(serverSocket); WSACleanup(); exit(1); } // 监听连接请求 if (listen(serverSocket, SOMAXCONN) == SOCKET_ERROR) { cerr << "Failed to listen to socket." << endl; closesocket(serverSocket); WSACleanup(); exit(1); } return serverSocket; } // 创建客户端套接字 SOCKET createClientSocket(const char* ipAddr) { SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (clientSocket == INVALID_SOCKET) { cerr << "Failed to create socket." << endl; exit(1); } // 连接服务器 sockaddr_in serverAddr; serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons(PORT); if (inet_pton(AF_INET, ipAddr, &serverAddr.sin_addr) != 1) { cerr << "Invalid IP address." << endl; closesocket(clientSocket); WSACleanup(); exit(1); } if (connect(clientSocket, (sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) { cerr << "Failed to connect to server." << endl; closesocket(clientSocket); WSACleanup(); exit(1); } return clientSocket; } // 发送数据 void sendData(SOCKET socket, const char* data, int len) { int sentBytes = 0; while (sentBytes < len) { int ret = send(socket, data + sentBytes, len - sentBytes, 0); if (ret == SOCKET_ERROR) { cerr << "Failed to send data." << endl; closesocket(socket); WSACleanup(); exit(1); } sentBytes += ret; } } // 接收数据 int recvData(SOCKET socket, char* buf) { int recvBytes = recv(socket, buf, BUF_SIZE, 0); if (recvBytes == SOCKET_ERROR) { cerr << "Failed to receive data." << endl; closesocket(socket); WSACleanup(); exit(1); } buf[recvBytes] = '\0'; return recvBytes; } // 主函数 int main() { // 初始化 Winsock2 库 initWinsock(); // 创建玩家 Player player1(0, 0); Player player2(10, 10); // 创建服务器端套接字 SOCKET serverSocket = createServerSocket(); cout << "Waiting for player 2 to connect..." << endl; // 接受连接请求 SOCKET clientSocket = accept(serverSocket, NULL, NULL); if (clientSocket == INVALID_SOCKET) { cerr << "Failed to accept connection." << endl; closesocket(serverSocket); WSACleanup(); exit(1); } cout << "Player 2 connected." << endl; // 创建客户端套接字 char ipAddr[BUF_SIZE]; cout << "Please enter the IP address of player 1: "; cin >> ipAddr; SOCKET player1Socket = createClientSocket(ipAddr); cout << "Connected to player 1." << endl; // 进入游戏循环 while (true) { // 向对方发送自己的位置 char buf[BUF_SIZE]; sprintf_s(buf, "%d %d", player1.posX, player1.posY); sendData(player2Socket, buf, strlen(buf)); sprintf_s(buf, "%d %d", player2.posX, player2.posY); sendData(player1Socket, buf, strlen(buf)); // 接受对方的位置 int recvBytes = recvData(player2Socket, buf); int posX, posY; sscanf_s(buf, "%d %d", &posX, &posY); player2.move(posX - player2.posX, posY - player2.posY); recvBytes = recvData(player1Socket, buf); sscanf_s(buf, "%d %d", &posX, &posY); player1.move(posX - player1.posX, posY - player1.posY); // 显示当前状态 system("cls"); cout << "Player 1: (" << player1.posX << ", " << player1.posY << ")" << endl; cout << "Player 2: (" << player2.posX << ", " << player2.posY << ")" << endl; // 处理输入 if (GetAsyncKeyState(VK_UP) & 0x8000) { player1.move(0, -1); } if (GetAsyncKeyState(VK_DOWN) & 0x8000) { player1.move(0, 1); } if (GetAsyncKeyState(VK_LEFT) & 0x8000) { player1.move(-1, 0); } if (GetAsyncKeyState(VK_RIGHT) & 0x8000) { player1.move(1, 0); } if (GetAsyncKeyState(VK_SPACE) & 0x8000) { // 射击 } // 延时 Sleep(50); } // 关闭套接字和 Winsock2 库 closesocket(player1Socket); closesocket(player2Socket); closesocket(serverSocket); WSACleanup(); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值