QT项目:中国象棋

这是一个用C++和Qt Creator开发的中国象棋项目,支持双人对战。功能包括绘制棋盘和棋子,棋子放置,倒计时,以及遵循象棋规则进行轮流下棋。项目主要由mainwindow.h、mainwindow.cpp、chessmovelaw.h、chessmovelaw.cpp和main.cpp等文件构成,包含自定义的棋谱规则和资源文件。
摘要由CSDN通过智能技术生成

项目简介:

        开发语言:C++

        开发软件:Qt Creator 4.6.2 

        实现功能:目前所能实现的功能比较单一,只支持人人对战,也就是两个人在一台电脑上实现对战。

项目思路:

        1、绘制棋盘

        2、绘制棋子

        3、放置棋子于棋盘之上

        4、倒计时

        5、象棋轮流下

        6、制定象棋的具体规则

项目主要组成:

1、总的文件(其中chessmovelaw文件是自定义文件,res是资源文件,包括棋盘、棋子、应用程序logo等图片)

 2、主要代码

        mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

enum ChessPieces  //棋子
{
    NullChess,
    Ju_R,
    Ma_R,
    Xiang_R,
    Shi_R,
    Jiang_R,
    Pao_R,
    Bing_R,
    Ju_B,
    Ma_B,
    Xiang_B,
    Shi_B,
    Jiang_B,
    Pao_B,
    Bing_B
};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void paintEvent(QPaintEvent *e); //绘画事件

    void mousePressEvent(QMouseEvent * event); //鼠标按下事件

    bool CanMove(int tarRow,int tarCol); //被选中棋子,能否从选中位置移动到当前位置

    bool IsRedChess(ChessPieces chesspieces);

    void IsDead();

    void timerEvent(QTimerEvent *event);

    int eventID1;

    int d = 60;

    void InitChess();       //初始化所有棋子的位置

    void DrawChess(QPainter*p,int row,int col,ChessPieces pieces);

private slots:
    void on_pushButton_2_clicked();

private:
    Ui::MainWindow *ui;

    QPoint stPos,edPos;
    int W,H;
    ChessPieces chesspieces[10][9];
    int pressRow,pressCol;
    bool turn; //turn=0为红,turn=1为黑
};

#endif // MAINWINDOW_H

        mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QIcon>
#include <QPainter>
#include <QRect>
#include <QDebug>
#include <QPushButton>
#include <QPoint>
#include <QMouseEvent>
#include <QPen>
#include <QFont>
#include <QBrush>
#include <QColor>
#include <QImage>
#include "chessmovelaw.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setWindowIcon(QIcon(":/image/logo.png"));
    setWindowTitle("中国象棋");
    stPos = QPoint(30,30);
    edPos = QPoint(570,630);
    W = (edPos.x()-stPos.x())/8;
    H = (edPos.y()-stPos.y())/9;

    InitChess(); //未选中棋子
    pressRow = -1;
    pressCol = -1;
    turn = false;

    //定时器
    eventID1 = startTimer(1000); //可自定义切换快慢
}


MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::timerEvent(QTimerEvent *event)
{
    //设置字体大小
    QFont font;
    font.setPointSize(20);
    ui->label->setFont(font);

    //设置定时器(倒计时)
    if(event->timerId() == eventID1)
    {
        d--;
        ui->label->move(700,20);
        ui->label->resize(200,60);
        QString aa = "倒计时";
        QString bb = QString::number(d);
        ui->label->setText(aa+bb);
    }
}
void MainWindow::paintEvent(QPaintEvent *e)
{

    QPainter painter(this);
    QRect rect;
    rect = QRect(0,0,600,660);
    painter.drawPixmap(rect,QPixmap(":/image/chessboard2.png"));

    if(pressRow>=0 && pressCol>=0)
    {
        QRect r(stPos.x()+pressCol*W-0.5*W,stPos.y()+pressRow*H-0.5*H,W,H);
        painter.drawPixmap(r,QPixmap(":/image/4.jpg"));
    }
    for(int i=0;i<10;i++)
    {
        for(int j=0;j<9;j++)
        {
            if(chesspieces[i][j] != NullChess)
            DrawChess(&painter,i,j,chesspieces[i][j]);
        }

    }
}

void MainWindow::mousePressEvent(QMouseEvent * event)
{
    event->pos();
    if(event->button() == Qt::LeftButton)
    {
        int row=(event->y()-stPos.y()+0.5*H)/H;
        int col=(event->x()-stPos.x()+0.5*W)/W;
        //选中到行和列在棋盘内的时候
        if(row>=0 && row<=9 && col>=0 && col<=8)
        {
            //之前选中到棋子为空
            if(pressRow>=0 && pressCol>=0)
            {
                //如果满足象棋移动规则,移动棋子
                if(CanMove(row,col))
                {
                    d=61;
                    chesspieces[row][col] = chesspieces[pressRow][pressCol];
                    chesspieces[pressRow][pressCol] = NullChess;
                    pressRow = -1;
                    pressCol = -1;
                    turn =! turn;
                    IsDead();
                    repaint();
                    return;
                }
            }
            //当前选中棋子为空
            if(chesspieces[row][col] != NullChess)
            {
                //如果是红棋走且选中的是红棋,或如果是黑棋走且选中的是黑棋,把棋子选中
                if((turn==false &&IsRedChess(chesspiece
  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值