5_4_Chess(四)普通落子
作者:互联网
文章目录
1、效果
2、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";//画白色
}
else if (chessData[i][j]==Black) {
chessFilename = "../png/black.gif";//画黑色
}
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;
qDebug()<<"i:"<<i<<"j"<<j<<endl;
chessData[i][j] = Black;
this->update();//刷新
}
}
}
//-----------end event----------
void Chess::Init(){
bgFilename.clear();
bgFilename = "../png/2.jpg";
//线的默认值
lineColor = Qt::red;
lineStyle = Qt::SolidLine;
lineWidth = 3;
//初始化棋子数据
memset(chessData,0,sizeof(int)*64);
chessData[3][3]=Black;chessData[4][4]=Black;
chessData[4][3]=White;chessData[3][4]=White;
}
//-----------function-----------
//提供方法,供外部改变背景
void Chess::ChangeBackgroundImage(const QString filename){
this->bgFilename=filename;
}
//提供方法,供外部改变线颜色、类型、宽度
void Chess::ChangeLine(const QColor color, Qt::PenStyle style, const int width){
this->lineColor = color;
this->lineStyle = style;
this->lineWidth = width;
}
//-----------end function-------
3、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};//空,白,黑
//保存棋子数据
int chessData[8][8];
void ChangeBackgroundImage(const QString filename); //提供方法,供外部改变背景
void ChangeLine(const QColor color,Qt::PenStyle style,const int width);//提供方法,供外部改变线颜色\类型\宽度
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;//格子宽高,划线起始点
void Init();
};
#endif // CHESS_H
标签:startx,starty,落子,int,void,gridwidth,普通,Chess 来源: https://blog.csdn.net/oDuanYanGuHong/article/details/122609405