2019.3.2 Qt学习---简单五子棋
主要用的东西:
<kbd>void paintEvent(QPaintEvent *event);</kbd>
<kbd>void mousePressEvent(QMouseEvent *event);</kbd>
QPainter
QPen
QMessageBox
黑白棋子的图片是自己随便用win自带的画图画的,拿美图秀秀改成透明背景。。。
写代码时发现的一些问题:
1.隐藏最大化按钮和固定窗口大小
setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint); //禁止最大化
setFixedSize(800, 800); //固定大小
2.drawPixmap
的图片路径是在Debug中
进入下一目录需用两条\
QPixmap("pic\\black.png")
3.画线画图什么的不能写在自己写的函数中 会报错 只能写在paintEvent
中
代码:
mywidget.h代码:
class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
~MyWidget();
void Init();
int check();
protected:
void mousePressEvent(QMouseEvent *event);
void paintEvent(QPaintEvent *event);
private:
Ui::MyWidget *ui;
int step; // 下棋步数
int turn; // 0表示黑子下 1表示白子下
int w,h; // 每一小格的宽度和高度
int chessX,chessY; // 棋子坐标所对应的棋盘坐标
int state[N][N]; // -1表示没有棋子 0表示黑子 1表示白子
};
mywidget.cpp部分代码:
int MyWidget::check()
{
if(step == N*N)
return 2; //平局
int num = 0; //检查chessY那一横行
for(int i = 0;i < N && num < 5;i++)
if(state[i][chessY] == turn)
num++;
else num = 0;
if(num >= 5)
return turn;
num = 0; //检查chessX那一竖行
for(int i = 0;i < N && num < 5;i++)
if(state[chessX][i] == turn)
num++;
else num = 0;
if(num >= 5)
return turn;
num = 0; //检查次对角线
int start,end,sum,sub;
sum = chessX + chessY;
sub = chessX - chessY;
if(sum > N-1)
{
start = sum - N + 1;
end = N;
}
else
{
start = 0;
end = sum + 1;
}
for(int i = start;i < end && num < 5;i++)
if(state[i][sum-i] == turn)
num++;
else num = 0;
if(num >= 5)
return turn;
num = 0; //检查主对角线
if(sub > 0)
{
start = sub;
end = N;
}
else
{
start = 0;
end = N - sub;
}
for(int i = start;i < end && num < 5;i++)
if(state[i][i-sub] == turn)
num++;
else num = 0;
if(num >= 5)
return turn;
return -1; //没有胜负
}
void MyWidget::paintEvent(QPaintEvent *event)
{
QPainter p(this);
QPen pen;
pen.setWidth(4);
p.setPen(pen);
if( turn == 0) //提示下棋颜色
{
this->setWindowTitle("Turn to Black");
p.drawPixmap(this->width()/2-0.5*w+8, 8, 0.8*w, 0.8*h, QPixmap("pic\\black.png"));
}
else
{
this->setWindowTitle("Turn to White");
p.drawPixmap(this->width()/2-0.5*w+8, 8, 0.8*w, 0.8*h, QPixmap("pic\\white.png"));
}
for(int i = 0;i <= N;i++)
p.drawLine(w, h+i*h, w+N*w, h+i*h); //画横线
for(int i = 0;i <= N;i++)
p.drawLine(w+i*w, h, w+i*w, h+N*h); //画竖线
for(int i = 0;i < N;i++)
for(int j = 0;j < N;j++) //画棋子
if(state[i][j] != -1)
{
if(state[i][j] == 0)
p.drawPixmap(w+i*w, h+j*h, w, h, QPixmap("pic\\black.png"));
else p.drawPixmap(w+i*w, h+j*h, w, h, QPixmap("pic\\white.png"));
}
}
void MyWidget::mousePressEvent(QMouseEvent *event)
{
int x = event->x();
int y = event->y();
if(x < w || x > (N+1)*w || y < h || y > (N+1)*h)
return;
chessX = (x-w)/w;
chessY = (y-h)/h;
if(state[chessX][chessY] != -1)
return;
state[chessX][chessY] = turn;
step++;
qDebug() << chessX << chessY << turn << step;
int flag = check(); //检查胜负
if(flag == 0)
{
QMessageBox::information(this,"Tip","Black Win!!!");
Init();
return;
}
else if(flag == 1)
{
QMessageBox::information(this,"Tip","White Win!!!");
Init();
return;
}
else if(flag == 2)
{
QMessageBox::information(this,"Tip","Draw!!!");
Init();
return;
}
turn = (turn+1)%2;
update();
}
效果图:
立个Flag:
以后有水平了写一个AI的五子棋~~~~