赞
踩
功能演示:
整体思路:
!!!!!!(只讲大概实现,源码最后会给,代码段只是一些伪代码)
- #define ROW 15 //行
- #define COL 15 //列
- #define xsize 80
- #define ysize 80
-
- //handchess_color用于之后颜色变化
-
- void MainWindow::InitUI()
- {
- QFont font;
- this->resize((ROW+7)*xsize,(COL+1)*ysize);//绘制画板大小
- this->setFixedSize((ROW+7)*xsize,(COL+1)*ysize);//固定了画板大小
- this->setWindowTitle("Qtchess");
- handchess_color = false; // false -> 黑色 true -> 白色
- }
格子x的边界距离为0.5个xsize,y的边界距离为0.5yszie
棋盘长为ROW * xsize
宽为COL * ysize
- void MainWindow::drawchessboard()
- {
- QPainter painter(this);//创建画家对象
- painter.setPen(QPen(QColor(Qt::black),4));//给边框线颜色和大小
- painter.setBrush(Qt::gray);//给其他格子颜色
- for(int i = 0;i < COL;i++)
- for(int j = 0;j < ROW;j++)
- painter.drawRect((0.5+i)*xsize,(0.5+j)*ysize,xsize,ysize);//绘制棋盘
- }
- void MainWindow::drawhandchess()
- {
- QPainter painter(this);
- painter.setPen(QPen(QColor(Qt::transparent)));//给棋子边框设置透明
- if(handchess_color == true)
- painter.setBrush(Qt::white);
- else
- painter.setBrush(Qt::black);
- //获得鼠标单击点的全局位置,长度,宽度
- painter.drawEllipse(mapFromGlobal(QCursor::pos()),xsize/2,ysize/2);
利用绘画事件painterEvent与update()
- void MainWindow::paintEvent(QPaintEvent *)
- {
- drawchessboard();
- drawhandchess();
- drawchessitem();
- update();//会触发paintEvent事件
- }
chessitem.h与chessitem.c
需要给定棋子颜色与位置
- //chessitem.h
- #ifndef CHESSITEM_H
- #define CHESSITEM_H
- #include <QPoint>
-
- class ChessItem
- {
- public:
- ChessItem();
- ChessItem(QPoint point,bool iscolor);//构造函数 -- 完成对象的初始化
-
- //重载运算符 用于比较 位置,颜色
- bool operator==(const ChessItem &t1)const
- {
- return ((_pt == t1._pt) && (_color == t1._color));
- }
-
- QPoint _pt;//棋子位置
- bool _color;//棋子颜色
- };
-
- #endif // CHESSITEM_H
-
-
- //chessitem.c
- #include "chessitem.h"
-
- ChessItem::ChessItem()
- {
-
- }
-
- ChessItem::ChessItem(QPoint point,bool iscolor)
- {
- _pt = point;
- _color = iscolor;
- }
如果按下位置有棋子 return; 否则用Qvector记录下来
- //QVector<ChessItem> p_chessItem;定义棋子集合
-
- void MainWindow::mousePressEvent(QMouseEvent *event)
- {
- QPoint pt;
- if(event->pos().x()/xsize >= 16)
- return;
- pt.setX(event->pos().x()/xsize);
- pt.setY(event->pos().y()/ysize);
- qDebug()<<"x:"<<event->pos().x()/xsize<<"y:"<<event->pos().y()/ysize<<' ';
- for(int i = 0;i <p_chessItem.size();i++)
- {
- ChessItem item = p_chessItem[i];
- if(item._pt == pt)
- return;
- }
-
- ChessItem item(pt,handchess_color);
- p_chessItem.append(item);//添加元素
- }
- //在棋盘上形成棋子
- void MainWindow::drawchessitem()
- {
- QPainter painter(this);
- painter.setPen(QPen(QColor(Qt::transparent)));
- for(int i = 0;i < p_chessItem.size();i++)
- {
- ChessItem item = p_chessItem[i];
- if(item._color == true)
- {
- painter.setBrush(Qt::white);
- }
- else
- {
- painter.setBrush(Qt::black);
- }
- drawchessplace(painter,item._pt);
- }
- }
-
- void MainWindow::drawchessplace(QPainter &painter, QPoint &point)
- {
- QPoint ptCenter((point.x() + 0.5) * xsize,(point.y() + 0.5) * ysize);
- painter.drawEllipse(ptCenter,xsize/2,ysize/2);
- }
-
- int left = checkchess(item,QPoint(-1,0));
- int right = checkchess(item,QPoint(1,0));
- int leftup = checkchess(item,QPoint(-1,-1));
- int rightup = checkchess(item,QPoint(1,-1));
- int leftdown = checkchess(item,QPoint(-1,1));
- int rightdown = checkchess(item,QPoint(1,1));
- int up = checkchess(item,QPoint(0,-1));
- int down = checkchess(item,QPoint(0,1));
-
- if((left + right)>= 4 || (up + down)>=4 ||(leftup+rightdown)>=4 || (leftdown+rightup)>=4)
- {
- QString str = handchess_color ?"White victory!" : "Black victory";
- QMessageBox::information(NULL,"Game Over",str,QMessageBox::Yes);
- p_chessItem.clear();
- }
- handchess_color = !handchess_color;
- int MainWindow::checkchess(ChessItem item,QPoint pt)
- {
- int count = 0;
- item._pt += pt;
- //contains() -> This function requires the value type to have an implementation of operator==().
- while(p_chessItem.contains(item))
- {
- count++;
- item._pt+=pt;
- }
- return count;
- }
利用一个Button去实现
- font.setPointSize(20);
- button = new QPushButton("悔棋",this);
- button->setFont(font);
- button->move((ROW+1)*xsize,13.5*ysize);
- button->resize(5.5*xsize,2.0*ysize);
- connect(button,SIGNAL(clicked(void)),this,SLOT(chessback()));
-
- //悔棋功能:
- void MainWindow::chessback()
- {
- if(!p_chessItem.empty())
- {
- QPainter painter(this);
- ChessItem item = p_chessItem[p_chessItem.size()-1];
- qDebug() << item._pt << ' ';
- p_chessItem.pop_back();
- }
- else
- qDebug()<<"empty";
- }
1.棋子棋盘显示都是通过paintEvent()去显示,鼠标按下容器记录下数据再paintEvent()中drawchessitem()去显示。
2.不懂函数可以通过F1打开Qt assitant查看或上网搜搜。个人不展开说,主要体会思路
链接:https://pan.baidu.com/s/1N69Ic5mA6Q9kSRayIMBQ9Q?pwd=y259
提取码:y259
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。