5_5_Chess(五)棋盘:增加位点、增加界面.


1、新建新界面
在这里插入图片描述

2、最终效果

在这里插入图片描述

3、ui

在这里插入图片描述
在这里插入图片描述

4、代码

4.1 chess.cpp

#include "chess.h"

Chess::Chess(QWidget *parent)
    : QWidget(parent)
{
    Init();
}

Chess::~Chess()
{

}

//-----------events-------------
//画背景 绘图事件里
void Chess::paintEvent(QPaintEvent *){
      QPainter painter(this);//画家
    //方式一:用画家画矩形的方式
//    QRect rec;
//    rec.setTopLeft(QPoint(0,0));
//    rec.setBottomRight(QPoint(this->width(),this->height()));
//    QPixmap pix("../png/2.jpg");
//    bool ret;
//        ret=pix.load("../png/2.jpg");
//    if(ret==true)
//       painter.drawPixmap(rec,pix);

    //方式二:直接使用构造函数
      QRect rec(QPoint(0,0),QPoint(this->width(),this->height()));//定义矩形
      QPixmap pix(bgFilename);
      qDebug()<<bgFilename<<endl;
      painter.drawPixmap(rec,pix);

    //画线
    QPen pen1;
    pen1.setStyle(lineStyle);
    pen1.setColor(lineColor);
    pen1.setWidth(lineWidth);
    painter.setPen(pen1);//笔交给画家
    for(int i=0;i<=8;i++){
        //x
        //painter.drawLine(startx,starty,9*gridwidth,starty);
        painter.drawLine(startx,starty+i*gridheight,9*gridwidth,starty+i*gridheight);//起点 终点坐标

        //y
        //painter.drawLine(startx,starty,startx,starty+8*gridheight);
        painter.drawLine(startx+i*gridwidth,starty,startx+i*gridwidth,starty+8*gridheight);
    }

    //画棋子
    QString chessFilename;
    for(int i=0;i<8;i++){
        for(int j=0;j<8;j++){
            //painter.drawPixmap(startx,starty,gridwidth,gridheight,QPixmap("../png/black.gif"));
            if(chessData[i][j]==White){
                chessFilename = "../png/white.png";//画白色
                qDebug()<<"draw white"<<endl;
            }
            else if (chessData[i][j]==Black) {
               chessFilename = "../png/black.gif";//画黑色
               qDebug()<<"draw black"<<endl;
            }
            else {
                chessFilename.clear();
                continue;
            }
            painter.drawPixmap(startx+i*gridwidth,starty+j*gridheight,gridwidth,gridheight,QPixmap(chessFilename));
        }
    }
}

//窗体改变立马赋值
void Chess::resizeEvent(QResizeEvent *event){
    gridwidth = event->size().width()/GRIDCOLINT;
    gridheight =event->size().height()/GRIDCOLINT;
    startx = gridwidth;
    starty = gridheight;
    qDebug()<<"x::"<<startx<<"y::"<<starty;
}

//鼠标按下事件
void Chess::mousePressEvent(QMouseEvent *event){
    int x=event->x();
    int y=event->y();


    if(x>=startx && (x<=startx+8*gridwidth)){      //x的有效范围
        if(y>=starty && (y<=starty+8*gridheight)){ //y的有效范围
        //确定当前坐标属于哪个格子
        int i=0,j=0;
        i = (x-startx)/gridwidth;
        j = (y-starty)/gridheight;
        #if 0
        qDebug()<<"i:"<<i<<"j"<<j<<endl;
        chessData[i][j] = Black;
        this->update();//刷新
        #endif
        SignalSendChessData(i,j);//发送信号
        }
    }
}
//-----------end event----------

//private
void Chess::Init(){
    bgFilename.clear();
    bgFilename = "../png/2.jpg";

    //线的默认值
    lineColor = Qt::black;
    lineStyle = Qt::SolidLine;
    lineWidth = 2;

    //初始化棋子数据
    memset(chessData,0,sizeof(int)*64);
    chessData[3][3]=Black;chessData[4][4]=Black;
    chessData[4][3]=White;chessData[3][4]=White;
}
void Chess::InitChess(){
    ;
}

//-----------function-----------
//public
//提供方法,供外部改变背景
void Chess::SetBackgroundImage(const QString filename){
    this->bgFilename=filename;
    this->update();//刷新
}
//提供方法,供外部改变线颜色、类型、宽度
void Chess::SeteLine(const QColor color, Qt::PenStyle style, const int width){
    this->lineColor = color;
    this->lineStyle = style;
    this->lineWidth = width;
    this->update();//刷新
}
void Chess::SetChessStatus(void *p){
    memcpy(chessData,p,sizeof(int)*8*8);
    this->update();
}

//-----------end function-------





4.2 cheeform.cpp

#include "chessform.h"
#include "ui_chessform.h"

ChessForm::ChessForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChessForm)
{
    ui->setupUi(this);
    Init();
}

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

//-----------function-----------
void ChessForm::Init(){
    //申请棋盘类空间
    myChess = new Chess();

    //把棋盘显示在当前容器中
    ui->gridLayout->addWidget(myChess);
}
//-----------end function-------

//-----------events-------------
//-----------end event----------

//-----------slots-------------
//-----------end slots---------

4.3 main.cpp

//#include "chess.h"
#include "chessform.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
//    Chess w;
//    w.show();

    ChessForm w;
    w.show();
    return a.exec();
}

4.4 chess.h

#ifndef CHESS_H
#define CHESS_H

#include <QWidget>
#include <QPainter>
#include <QPen>
#include <QDebug>
#include <QResizeEvent>
#include <QMouseEvent>
#define GRIDCOLINT 10
class Chess : public QWidget
{
    Q_OBJECT

public:
    Chess(QWidget *parent = 0);
    ~Chess();

    //棋子类型
    enum ChessType{Empyty = 0,White,Black};//空,白,黑

    //提供方法,供外部改变背景
    void SetBackgroundImage(const QString filename);
    //提供方法,供外部改变线颜色\类型\宽度
    void SeteLine(const QColor color,Qt::PenStyle style,const int width);
    //提供方法,改变棋盘的显示值
    void SetChessStatus(void *p);

protected:
    void paintEvent(QPaintEvent *);//绘图事件
    void resizeEvent(QResizeEvent *event);
    void mousePressEvent(QMouseEvent *);//鼠标按下事件
private:
    QString bgFilename;
    QColor lineColor;
    Qt::PenStyle lineStyle;
    int lineWidth;
    int gridwidth,gridheight,startx,starty;//格子宽高,划线起始点
    //保存棋子数据
    int chessData[8][8];
    void Init();
    void InitChess();//初始化棋盘
    //信号
signals:
    void SignalSendChessData(int i,int j);//发送点击的坐标


};

#endif // CHESS_H

4.5 chessform.h

#ifndef CHESSFORM_H
#define CHESSFORM_H

#include <QWidget>
#include <chess.h>
namespace Ui {
class ChessForm;
}

class ChessForm : public QWidget
{
    Q_OBJECT

public:
    explicit ChessForm(QWidget *parent = nullptr);
    ~ChessForm();

private:
    Ui::ChessForm *ui;
    Chess *myChess;
    void Init();
};

#endif // CHESSFORM_H

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值