实现功能
1.初始化棋盘(exe启动后,展示完整的棋盘)
2.重置功能:有重置按钮,点击重新初始化棋盘
3.计时功能:第一颗棋子落下后,开始计时,五颗棋子连在一起以后结束计时,并提示完成游戏
4.下棋功能:点击棋盘中的位置,可以显示一颗棋子。(只有一个人下,五颗棋子连在一起可以自动消除)
实现过程
使用 button 作为棋子,label 中存储已用时间。使用 QPainter 画出棋盘,QTimer 写出简单计时器,MouseEvent 事件实现 button 棋子,并用 list 保存棋子。reset 函数重置棋盘,包括时间的清空,棋子的清除。分别写四个函数用来判断四个方向上是否连成 5 个棋子,判断结束。所有函数全都塞进一个cpp文件中,代码写的丑陋,请见谅。
效果图
具体不多解释,下面看代码
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QObject>
#include <QPair>
#include <QLabel>
#include <qvector.h>
#include <qmap.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QPushButton b;
QPushButton resetbtn;
QPushButton* btnlist[1000];
QLabel lbl;
QLabel time_lbl;
//QTimer *timer;
int cnt = 0;
int Alltime=0;
//QVector<int>g1, g2;
QMap<QPair<int, int>, int>mp;
void reset();
void paintEvent(QPaintEvent *);
void mousePressEvent(QMouseEvent * e);
void timerEvent(QTimerEvent *e);
public slots:
void onUpdate();
public:
bool victory(int, int);
bool f1(int, int);
bool f2(int, int);
bool f3(int, int);
bool f4(int, int);
private:
Ui::MainWindow *ui;
int itimer_id_ = 0;
};
#endif // MAINWINDOW_H
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qpushbutton.h>
#include <QObject>
#include <QWidget>
#include <QMouseEvent>
#include <QPainter>
#include <QLabel>
#include <qmessagebox.h>
#include <qtimer.h>
#include <QTime>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->resize(800,800);
resetbtn.setText("reset");
resetbtn.setParent(this);
resetbtn.resize(80,30);
resetbtn.move(100,20);
resetbtn.show();
lbl.setText("Spent Time:");
lbl.setParent(this);
lbl.setFixedSize(80,30);
lbl.move(400,20);
lbl.show();
connect(&resetbtn,&QPushButton::pressed,this,&MainWindow::reset);
//connect(&b,&QPushButton::released,this,&MainWindow::click);
//connect()
}
void MainWindow::reset()
{
//清空棋子
//setClearButtonEnabled;
for (int i = 1; i <= cnt; i++)
{
QPushButton * btn2 = btnlist[i];
btn2->hide();
btn2->deleteLater();
}
cnt = 0;
mp.clear();
//清空时间
Alltime = 0;
//timer->stop();
}
void MainWindow::timerEvent(QTimerEvent *e)
{
if (e->timerId() == itimer_id_) {
++Alltime;
onUpdate();
}
}
void MainWindow::paintEvent(QPaintEvent *)
{
QPainter p(this);
for(int i=0;i<16;i++)
{
p.drawLine(70,70+i*40,670,70+i*40);
p.drawLine(70+i*40,70,70+i*40,670);
}
}
void MainWindow::mousePressEvent(QMouseEvent * e)
{
int sx=e->x();
int sy=e->y();
if (sx <= 70 || sx >= 70 + 15 * 40 || sy <= 70 || sy >= 70 + 15 * 40)return ;
for(int i=0; i<15; i++)
{
if (sx >= 70 + i * 40 && sx <= 70 + (i + 1) * 40)
{
sx = 70 + i * 40 + 10;
break;
}
}
for (int i = 0; i<15; i++)
{
if (sy >= 70 + i * 40 && sy <= 70 + (i + 1) * 40)
{
sy = 70 + i * 40 + 10;
break;
}
}
//g1.push_back(sx);
//g2.push_back(sy);
mp[qMakePair(sx, sy)] = 1;
QPushButton * btnn = new QPushButton;
btnn->setParent(this);
btnn->move(sx,sy);
btnn->resize(20,20);
QPalette pal = btnn->palette();
btnn->setStyleSheet("background-color:black;border-radius:10px;");
btnn->setPalette(pal);
btnn->show();
btnlist[++cnt] = btnn;
QTimer *timer = new QTimer(this);
if (cnt == 1)
{
//connect(timer, SIGNAL(timeout()), this, SLOT(update()));
//timer->start(1000);
itimer_id_ = startTimer(1000);
time_lbl.setParent(this);
//time_lbl.setText("sss");
time_lbl.setFixedSize(80, 30);
time_lbl.move(480, 20);
time_lbl.show();
}
//timer->stop();
if (victory(sx, sy))
{
//timer->stop();
if (0 != itimer_id_) {
killTimer(itimer_id_);
itimer_id_ = 0;
}
QMessageBox::information(NULL, "hello", "Victory!",
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
timer->stop();
//cnt = 0;
mp.clear();
Alltime = 0;
}
}
void MainWindow::onUpdate()
{
//Alltime++;
time_lbl.setText(QString::number(Alltime, 10));
}
bool MainWindow::victory(int x, int y)
{
if (f1(x, y) || f2(x, y) || f3(x, y) || f4(x, y))return true;
return false;
}
bool MainWindow::f1(int x, int y)
{
for (int i = 0; i < 5; i++)
{
int tot = 0;
for (int j = 0; j < 5; j++)
{
int sx = x + (j - i) * 40;
int sy = y;
if (mp[qMakePair(sx, sy)] == 1) {
tot++;
}
}
if (tot > 4)return true;
}
return false;
}
bool MainWindow::f2(int x, int y)
{
for (int i = 0; i < 5; i++)
{
int tot = 0;
for (int j = 0; j < 5; j++)
{
int sx = x;
int sy = y + (j - i) * 40;
if (mp[qMakePair(sx, sy)] == 1) {
tot++;
}
}
if (tot == 5)return true;
}
return false;
}
bool MainWindow::f3(int x, int y)
{
for (int i = 0; i < 5; i++)
{
int tot = 0;
for (int j = 0; j < 5; j++)
{
int sx = x - (j - i) * 40;
int sy = y + (j - i) * 40;
if (mp[qMakePair(sx, sy)] == 1) {
tot++;
}
}
if (tot == 5)return true;
}
return false;
}
bool MainWindow::f4(int x, int y)
{
for (int i = 0; i < 5; i++)
{
int tot = 0;
for (int j = 0; j < 5; j++)
{
int sx = x + (j - i) * 40;
int sy = y + (j - i) * 40;
if (mp[qMakePair(sx, sy)] == 1) {
tot++;
}
}
if (tot == 5)return true;
}
return false;
}
MainWindow::~MainWindow()
{
delete ui;
}