QT中国象棋初版
Chinese-Chess中国象棋初版
实现了单人双方对战、人对战电脑、开两个客户端联机对战
佛曰:*写字楼里写字间,写字间里程序员;*程序人员写程序,又拿程序换酒钱。*酒醒只在网上坐,酒醉还来网下眠;*酒醉酒醒日复日,网上网下年复年。*但愿老死电脑间,不愿鞠躬老板前;*奔驰宝马贵者趣,公交自行程序员。*别人笑我忒疯癫,我笑自己命太贱;*不见满街漂亮妹,哪个归得程序员?界面展示主菜单界面:主界面部分代码:
#include"choosemainwindow.h"ChooseMainWindow::ChooseMainWindow(QWidget*parent):QDialog(parent){this->setWindowTitle("选择游戏方式");this->setFixedSize(250,120);this->setWindowIcon(QIcon(":/images/chess.svg"));QVBoxLayout*lay=newQVBoxLayout(this);lay->addWidget(m_buttons[0]=newQPushButton("玩家自己对战"));lay->addWidget(m_buttons[1]=newQPushButton("玩家和AI对战"));lay->addWidget(m_buttons[2]=newQPushButton("双人网络对战"));/*游戏方式一:自己和自己下棋【同一台PC机器】*/connect(m_buttons[0],&QPushButton::clicked,[=](){this->hide();m_pAgainstYourself=newChessBoard();m_pAgainstYourself->setWindowTitle("玩家自己对战");m_pAgainstYourself->show();//返回主窗口connect(m_pAgainstYourself,&ChessBoard::toMenu,[=](){m_pAgainstYourself->close();this->show();});});/*游戏方式二:自己和电脑下棋【同一台PC机器】*/connect(m_buttons[1],&QPushButton::clicked,[=](){this->hide();m_pRobotGame=newRobotGame();m_pRobotGame->setWindowTitle("玩家和AI对战");m_pRobotGame->show();//返回主窗口connect(m_pRobotGame,&ChessBoard::toMenu,[=](){m_pRobotGame->close();this->show();});});/*游戏方式三:双人局域网下棋【可在局域网下不同台PC机器】*/connect(m_buttons[2],&QPushButton::clicked,[=](){this->hide();QMessageBox::StandardButtonsret=QMessageBox::question(NULL,"提示","是否作为服务器启动[选择红方]?");boolbServer=false;if(ret==QMessageBox::Yes)bServer=true;m_pNetworkGame=newNetworkGame(bServer);m_pNetworkGame->setWindowTitle("双人网络对战");m_pNetworkGame->show();//返回主窗口connect(m_pNetworkGame,&ChessBoard::toMenu,[=](){m_pNetworkGame->close();this->show();});});}玩家和自己对战界面:棋盘绘制部分代码:
voidChessBoard::paintEvent(QPaintEvent*){QPainterpainter(this);painter.setRenderHint(QPainter::Antialiasing,true);//渲染提示intside=qMin(int(ui->centralwidget->width()-ui->rightWidget->width()),ui->label->height());painter.scale(side/960.0,side/960.0);m_offset=60;//距离界面的边距m_distance=90;//间距为90pxm_radius=m_distance/2;//棋子半径为d/2for(inti=0;i