在之前的基础上再做些优化

首先要让游戏在只有两个玩家都点击了开始按钮后游戏再开始

在结构体中添加

struct data {
    bool useable = false;
    int player = -1;
    int eventType = -1;
    int eventButton = -1;
    int keyCode = -1;
    bool gameStart = false;        //游戏开始
    bool gamePause = false;        //游戏暂停
}player1Data, player2Data;

客户端

在游戏循环中判断游戏的开始与暂停

void Game::gameRun() {
    do {
        gameInitial();
        while (window.isOpen() && gameOver == false) {
            gameInput();
            if (isGameBegin && anotherData.gameStart)
                gameLogic();
            else if (!isGameBegin) {
                gameInitial();
            }
            gameDraw();
        }
    }
    while (!gameQuit);
}

传输游戏开始与暂停的信息

void Game::sendToService(struct data mydata, int port) {        //给服务器传送消息
    TcpSocket socket;
    socket.connect("127.0.0.1", port);
    Packet packet;
    packet << mydata.player << mydata.eventType << mydata.eventButton << mydata.keyCode << mydata.gameStart << mydata.gamePause;
    socket.send(packet);
}
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;
    }
    mydata.gamePause = isGameHold;
    mydata.gameStart = isGameBegin;
}
void Game::gameLogic() {
    if (isGameHold || anotherData.gamePause)
        return;
    ////
}
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;
            bool start, pause;
            p >> player >> type >> button >> keycode >> start >> pause;
            anotherData.player = player;
            anotherData.eventType = type;
            anotherData.eventButton = button;
            anotherData.keyCode = keycode;
            anotherData.gameStart = start;
            anotherData.gamePause = pause;
        }
        inputJudge();
    }
    return 0;
}

服务器端

int reciveClient(int port) {
    while (true) {
        TcpListener listener;
        TcpSocket socket;
        Packet packet;
        listener.listen(port);
        listener.accept(socket);
        socket.receive(packet);
        int player, eventType, eventButton, keyCode;
        bool start, pause;
        packet >> player >> eventType >> eventButton >> keyCode >> start >> pause;
        if (player == 0) {
            player1Data.player = player;
            player1Data.eventType = eventType;
            player1Data.eventButton = eventButton;
            player1Data.keyCode = keyCode;
            player1Data.gameStart = start;
            player1Data.gamePause = pause;
            inputJudge(player1Data);
        } else if (player == 1) {
            player2Data.player = player;
            player2Data.eventType = eventType;
            player2Data.eventButton = eventButton;
            player2Data.keyCode = keyCode;
            player2Data.gameStart = start;
            player2Data.gamePause = pause;
            inputJudge(player2Data);
        }
    }
    return 0;
}
int sendToClient(int port) {

    while (true) {
        TcpListener listener;
        TcpSocket socket;
        Packet p;
        listener.listen(port);
        listener.accept(socket);
        if (port == 1010) {
            p << player2Data.player << player2Data.eventType << player2Data.eventButton
                << player2Data.keyCode << player2Data.gameStart << player2Data.gamePause;
            socket.send(p);
        } else if (port == 1011) {
            p << player1Data.player << player1Data.eventType << player1Data.eventButton
                << player1Data.keyCode << player1Data.gameStart << player1Data.gamePause;
            socket.send(p);
        }
    }
}

问题解决

延迟问题

修改输入判断的函数,在某些地方加上sleep函数

如果旋转和hold不加的话会一直执行旋转和hold…

void Game::inputJudge() {
    Sleep(5);
    if (mydata.player == 0) {
        if (anotherData.eventType == Event::KeyPressed) {
            if (anotherData.keyCode == Keyboard::W)
                if (player2.currentShapeNum != player2.shapeO) {
                    player2.rotate = true;
                    Sleep(100);
                }
            if (anotherData.keyCode == Keyboard::Space)
                player2.hardDrop = true;
            if (anotherData.keyCode == Keyboard::A)
                player2.dx = -1;
            if (anotherData.keyCode == Keyboard::D)
                player2.dx = 1;
            if (anotherData.keyCode == Keyboard::S)
                player2.delay = DELAYVALUE / 10;
            if (anotherData.keyCode == Keyboard::LControl) {
                player2.hold = true;
                Sleep(100);
            }
        }
        if (anotherData.eventType == Event::KeyReleased) {

            if (anotherData.keyCode == Keyboard::Space)
                player2.hardDrop = false;
            if (anotherData.keyCode == Keyboard::A || anotherData.keyCode == Keyboard::D)
                player2.dx = 0;
            if (anotherData.keyCode == Keyboard::S)
                player2.delay = DELAYVALUE;
        }
    }
    if (mydata.player == 1) {
        if (anotherData.eventType == Event::KeyPressed) {
            if (anotherData.keyCode == Keyboard::W)
                if (player1.currentShapeNum != player2.shapeO) {
                    player1.rotate = true;
                    Sleep(100);
                }
            if (anotherData.keyCode == Keyboard::Space)
                player1.hardDrop = 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.keyCode == Keyboard::LControl) {
                player1.hold = true;
                Sleep(100);
            }
        }
        if (anotherData.eventType == Event::KeyReleased) {
            if (anotherData.keyCode == Keyboard::Space)
                player1.hardDrop = false;
            if (anotherData.keyCode == Keyboard::A || anotherData.keyCode == Keyboard::D)
                player1.dx = 0;
            if (anotherData.keyCode == Keyboard::S)
                player1.delay = DELAYVALUE;
        }
    }
}

效果演示

游戏开始

游戏暂停

移动

旋转

速降

Last modification:July 25th, 2020 at 03:39 pm