把之前制作的框架添加到俄罗斯方块上

客户端

Game.h中添加

int gamePort;            //连接端口
    bool sendMsg = false;    //判断是否发送消息
    struct data {                    //数据结构
        bool useable = false;        //数据是否可用
        int player = -1;            //玩家id
        int eventType = -1;            //事件类型
        int eventButton = -1;        //按钮类型
        int keyCode = -1;            //按键类型
        int nextShape[2] = { -1 };                //下一个形状
        int nextColor[2] = { -1 };                //下一个颜色
    }mydata, anotherData;            //玩家1,2的数据
    void inputJudge();
    void connect(int port);
    void sendToService(data mydata, int port);
    int reciveService();
    int connectService();

对部分函数做一些优化

接收服务器消息

int Game::reciveService() {                    //接受服务器的消息
    while (true) {
        TcpSocket socket;
        socket.connect("127.0.0.1", gamePort + 10);
        Packet p;
        if (socket.receive(p) == Socket::Done) {
            int player, type, button, keycode, shape, color;
            bool useable;
            p >> player >> type >> button >> keycode >> shape >> color;
            anotherData.player = player;
            anotherData.eventType = type;
            anotherData.eventButton = button;
            anotherData.keyCode = keycode;
            inputJudge();
        };
    }
    return 0;
}

对接收到的消息进行判断

void Game::inputJudge() {
    if (mydata.player == 0) {
        if (anotherData.eventType == Event::KeyPressed) {
            if (anotherData.keyCode == Keyboard::Up)
                if (player2.currentShapeNum != player2.shapeO)
                    player2.rotate = true;
            if (anotherData.keyCode == Keyboard::Left)
                player2.dx = -1;
            if (anotherData.keyCode == Keyboard::Right)
                player2.dx = 1;
            if (anotherData.keyCode == Keyboard::Down)
                player2.delay = DELAYVALUE / 10;
        }
        if (anotherData.eventType == Event::KeyReleased) {
            if (anotherData.keyCode == Keyboard::RControl)
                player2.hold = true;
            if (anotherData.keyCode == Keyboard::Enter)
                player2.hardDrop = true;
            if (anotherData.keyCode == Keyboard::Left || anotherData.keyCode == Keyboard::Right)
                player2.dx = 0;
            if (anotherData.keyCode == Keyboard::Down)
                player2.delay = DELAYVALUE;
        }
    } else if (mydata.player == 1) {
        if (anotherData.eventType == Event::KeyPressed) {
            if (anotherData.keyCode == Keyboard::W)
                if (player1.currentShapeNum != player1.shapeO)
                    player1.rotate = true;
            if (anotherData.keyCode == Keyboard::A)
                player1.dx = -1;
            if (anotherData.keyCode == Keyboard::D)
                player1.dx = 1;
            if (anotherData.keyCode == Keyboard::S)
                player1.delay = DELAYVALUE / 10;
        }
        if (anotherData.eventType == Event::KeyReleased) {
            if (anotherData.keyCode == Keyboard::LControl)
                player1.hold = true;
            if (anotherData.keyCode == Keyboard::Space)
                player1.hardDrop = true;
            if (anotherData.keyCode == Keyboard::A || anotherData.keyCode == Keyboard::D)
                player1.dx = 0;
            if (anotherData.keyCode == Keyboard::S)
                player1.delay = DELAYVALUE;
        }
    }
    anotherData.eventButton = -1;
    anotherData.eventType = -1;
    anotherData.keyCode = -1;
}

输入函数中操纵游戏

void Game::gameInput() {
    Event event;
    window.setKeyRepeatEnabled(false);
    while (window.pollEvent(event)) {
        ////
        mydata.eventType = event.type;
        mydata.eventButton = event.mouseButton.button;
        mydata.keyCode = event.key.code;
        if (mydata.player == 0) {
            player1.Input(event);
        }
        if (mydata.player == 1) {
            player2.Input(event);
        }
        sendMsg = true;
    }
}

主程序

int main() {
    thread t1(&Game::connectService, &tetris);
    t1.detach();
    thread t2(&Game::reciveService, &tetris);
    t2.detach();
    tetris.gameRun();
    return 0;
}

服务器端

int reciveClient(int port) {
    while (true) {
        TcpListener listener;
        TcpSocket socket;
        Packet packet;
        listener.listen(port);
        listener.accept(socket);
        if (socket.receive(packet) == Socket::Status::Done) {
            int player, eventType, eventButton, keyCode, shape[2], color[2];
            packet >> player >> eventType >> eventButton >> keyCode >> shape[0] >> shape[1]
                >> color[0] >> color[1];
            if (player == 0) {
                player1Data.player = player;
                player1Data.eventType = eventType;
                player1Data.eventButton = eventButton;
                player1Data.keyCode = keyCode;
                inputJudge(player1Data);
            } else if (player == 1) {
                player2Data.player = player;
                player2Data.eventType = eventType;
                player2Data.eventButton = eventButton;
                player2Data.keyCode = keyCode;
                inputJudge(player2Data);
            }
        }
    }
    return 0;
}
int sendToClient(int port) {
    while (true) {
        TcpListener listener;
        TcpSocket socket;
        Packet p;
        listener.listen(port);
        if (listener.accept(socket) == Socket::Status::Done) {
            if (port == 1010) {
                p << player2Data.player << player2Data.eventType << player2Data.eventButton
                    << player2Data.keyCode;
                socket.send(p);
            } else if (port == 1011) {
                p << player1Data.player << player1Data.eventType << player1Data.eventButton
                    << player1Data.keyCode;
                socket.send(p);
            }
        }
    }
}

效果演示

74

看起来延迟小多了,但是我觉得不该有延迟吧,可能是我不会用的原因….

而且还有很多bug,有待改进

Last modification:July 18th, 2020 at 10:23 pm