QT开发中文网

我想用 QT C++写一个飞行棋客户端,请告诉我详细代码案例以及c服务端Linux的实现

飞行棋客户端代码案例:

首先,需要引入QT的头文件和其他必要的库:

#include<QApplication>
#include<QWidget>
#include<QLabel>
#include<QGridLayout>
#include<cstdlib>
#include<ctime>
#include<QDebug>
#include<QPushButton>
#include<QMessageBox>

接下来,定义飞行棋游戏中用到的一些常量、变量及数据结构:

const int totalStep=41;
struct Chess
{
    QPushButton *button;
    int pos;//棋子在棋盘上的位置
    bool isReady;//是否准备起飞
    bool isFlying;//是否正在飞行中
    int stepNum;//已经走了多少步
    int color;//棋子颜色,1为红色,2为黄色,3为蓝色,4为绿色
};
int dice=0;//骰子点数
int currentPlayer=0;//当前玩家,1-4分别表示红、黄、蓝、绿四个玩家
bool canThrow=true;//是否可以投掷骰子
Chess chess[4];//表示四个棋子

//以下是各个格子的状态,0表示该格为空,1表示该格有一个红色棋子,2表示该格有一个黄色棋子,3表示该格有一个蓝色棋子,4表示该格有一个绿色棋子
int board[totalStep]={0,0,0,0,0,0,0,1,0,0,0,0,0,2,0,0,0,0,0,3,0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0};

然后,我们需要实现棋盘的绘制和骰子随机点数的生成等功能:

//初始化棋盘
void initBoard()
{
    for(int i=0;i<4;i++)
    {
        chess[i].button=new QPushButton("棋子",this);
        chess[i].pos=-1;
        chess[i].isFlying=false;
        chess[i].isReady=false;
        chess[i].stepNum=0;
        chess[i].color=i+1;

        connect(chess[i].button,SIGNAL(clicked(bool)),this,SLOT(onChessClicked()));
    }
}

//将棋子放到起点
void resetChess(Chess &chess)
{
    if(chess.isReady==false)
    {
        chess.isReady=true;
        chess.isFlying=false;
        chess.pos=0;
        chess.stepNum=0;
        chess.button->move(50,50);
        board[0]=chess.color;
    }
}

//移动棋子
void moveChess(Chess &chess)
{
    int targetPos=chess.pos+dice;
    if(targetPos>=totalStep-1)//已经到达终点,胜利
    {
        chess.isFlying=false;
        chess.isReady=false;
        chess.pos=totalStep-1;
        chess.stepNum=totalStep-1;
        QMessageBox::information(this,"恭喜","玩家"+QString::number(currentPlayer)+"获得游戏胜利!");
        return;
    }

    //检查目标位置上是否已经有棋子
    if(board[targetPos]!=0)
    {
        //被吃掉了,回到起点
        for(int i=0;i<4;i++)
        {
            if(chess.color!=chess[i].color && chess.pos==chess[i].pos)
            {
                resetChess(chess[i]);
            }
        }
        resetChess(chess);
    }
    else
    {
        //将原位置上的棋子移走
        board[chess.pos]=0;
        chess.stepNum+=dice;
        if(chess.stepNum>=totalStep-1)//已经到达终点,胜利
        {
            chess.isFlying=false;
            chess.isReady=false;
            chess.pos=totalStep-1;
            board[totalStep-1]=chess.color;
            QMessageBox::information(this,"恭喜","玩家"+QString::number(currentPlayer)+"获得游戏胜利!");
        }
        else
        {
            //将棋子移动到目标位置
            board[targetPos]=chess.color;
            chess.pos=targetPos;
            QPointF target=getPosition(targetPos);
            chess.button->move(target.x(),target.y());
        }
    }
}

//获取某个格子在界面上的坐标
QPointF getPosition(int pos)
{
    int x,y;
    if(pos<=10)
    {
        x=50+(10-pos)*60;
        y=480;
    }
    else if(pos<=20)
    {
        x=50;
        y=420-(pos-11)*60;
    }
    else if(pos<=30)
    {
        x=110+(pos-21)*60;
        y=60;
    }
    else
    {
        x=560;
        y=120+(pos-31)*60;
    }

    return QPointF(x,y);
}

//投掷骰子,生成随机点数
int throwDice()
{
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    return qrand()%6+1;
}

接下来,我们需要处理玩家的投掷骰子和移动棋子等操作:

//处理棋子点击事件
void onChessClicked()
{
    if(!canThrow) return;

    QPushButton *btn=qobject_cast<QPushButton*>(sender());
    for(int i=0;i<4;i++)
    {
        if(btn==chess[i].button && chess[i].color==currentPlayer)
        {
            if(chess[i].isReady)//已经在起点准备好了
            {
                chess[i].isFlying=true;
                moveChess(chess[i]);
                nextPlayer();

                if(dice!=6)
                {
                    canThrow=true;
                }
            }
            else if(chess[i].isFlying)//正在飞行中
            {
                moveChess(chess[i]);
                nextPlayer();
            }
            break;
        }
    }
}

//下一个玩家
void nextPlayer()
{
    currentPlayer++;
    if(currentPlayer>4)
    {
        currentPlayer=1;
    }

    //检查当前玩家是否所有棋子都还未准备好起飞
    bool allNotReady=true;
    for(int i=0;i<4;i++)
    {
        if(chess[i].color==currentPlayer && chess[i].isReady)
        {
            allNotReady=false;
            break;
        }
    }

    if(allNotReady)//当前玩家无法进行操作,直接进入下一位玩家
    {
        nextPlayer();
        return;
    }

    canThrow=true;
    updateStatusLabel();
}

//更新状态栏显示
void updateStatusLabel()
{
    QString status="当前玩家:";
    switch(currentPlayer)
    {
    case 1:
        status+="红色";
        break;
    case 2:
        status+="黄色";
        break;
    case 3:
        status+="蓝色";
        break;
    case 4:
        status+="绿色";
        break;
    default:
        break;
    }

    ui->statusLabel->setText(status);
}

//投掷骰子按钮被点击
void onThrowButtonClicked()
{
    if(!canThrow) return;

    dice=throwDice();//生成随机点数
    ui->diceLabel->setText(QString::number(dice));

    //检查当前玩家是否有可移动的棋子
    bool hasMovableChess=false;
    for(int i=0;i<4;i++)
    {
        if(chess[i].color==currentPlayer && chess[i].isFlying==false && chess[i].isReady==true)
        {
            hasMovableChess=true;
            break;
        }
    }

    if(hasMovableChess==false)//没有可以移动的棋子,切换下一个玩家
    {
        nextPlayer();
        return;
    }

    canThrow=false;//防止重复投掷
}

最后,我们需要将棋盘和游戏界面进行布局,并且定义QT程序的主函数:

int main(int argc,char *argv[])
{
    QApplication app(argc,argv);
    QWidget *mainWidget=new QWidget();
    mainWidget->setWindowTitle("飞行棋");
    QGridLayout *layout=new QGridLayout();

    //初始化棋盘
    initBoard();

    //设置布局
    layout->addWidget(chess[0].button,0,0);
    layout->addWidget(chess[1].button,0,1);
    layout->addWidget(chess[2].button,1,0);
    layout->addWidget(chess[3].button,1,1);

    ui->throwButton->setText("投掷骰子");
    layout->addWidget(ui->throwButton,2,0);
    layout->addWidget(ui->diceLabel,2,1);
    layout->addWidget(ui->statusLabel,3,0,1,2);

    QObject::connect(ui->throwButton,SIGNAL(clicked(bool)),this,SLOT(onThrowButtonClicked()));

    mainWidget->setLayout(layout);
    mainWidget->show();

    return app.exec();
}

c服务端Linux的实现:

首先,我们需要引入一些头文件和库:

#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

接下来,定义TCP服务器程序的主函数:

int main()
{
    int sock=socket(AF_INET,SOCK_STREAM,0);//创建套接字

    struct sockaddr_in addr;
    addr.sin_family=AF_INET;
    addr.sin_port=htons(12345);
    addr.sin_addr.s_addr=INADDR_ANY;

    bind(sock,(struct sockaddr *)&addr,sizeof(addr));//绑定地址和端口号

    listen(sock,1);//监听客户端连接请求

    while(1)
    {
        int clientSock=accept(sock,NULL,NULL);//接受客户端连接

        char buffer[1024];
        memset(buffer,0,sizeof(buffer));

        ssize_t len=recv(clientSock,buffer,sizeof(buffer),0);//接收客户端发送的消息
        if(len<=0)
        {
            continue;
        }

        printf("Received message: %s\n",buffer);

        const char *msg="Welcome to the server!";//向客户端发送欢迎消息
        send(clientSock,msg,strlen(msg)+1,0);

        close(clientSock);//关闭连接
    }

    close(sock);

    return 0;
}

这样,我们就实现了一个简单的TCP服务器程序。当客户端连接到该服务器时,服务器会接受客户端发送的消息,并向客户端发送一个欢迎消息后关闭连接。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=984

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?