温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用QT编写一个五子棋小游戏

发布时间:2020-11-06 17:04:51 来源:亿速云 阅读:647 作者:Leah 栏目:开发技术

这期内容当中小编将会给大家带来有关使用QT编写一个五子棋小游戏,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
#include <QBrush>
#include <QMouseEvent>
#include <QPen>
#include <QDebug>
#include <QMessageBox>
#include <QTime>
#include <QMainWindow>
#include <QTimer>
#include <QDateTime>
#include <QDesktopWidget>
#include <QApplication>
#include <QCoreApplication>
#include <QEventLoop>
#include <QProcess>
#include <QString>
Widget::Widget(QWidget *parent)
  : QWidget(parent)
  , ui(new Ui::Widget)
{
  ui->setupUi(this);

    ui->label_2->hide();
  // resize(640, 640);
  memset(a, 0, 15 * 15 * sizeof(int));
  player = 0;
  ui->lcdNumber->setSegmentStyle(QLCDNumber::Flat);
  ui->lcdNumber->display(QDateTime::currentDateTime().toString("000"));
  ui->lcdNumber_2->setSegmentStyle(QLCDNumber::Flat);
  ui->lcdNumber_2->display(QDateTime::currentDateTime().toString("000"));
  timer1=new QTimer(this);
  connect(timer1,&QTimer::timeout,this,&Widget::handle_timeout);
  timer1->start(1000);
  timer2=new QTimer(this);
  connect(timer2,&QTimer::timeout,this,&Widget::handle_timeout);
  timer2->start(1000);



}

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

 void Widget::handle_timeout()
{

  if(flag%2==0){
    for(sec=numb1;sec>0;sec--)
    {
      ui->lcdNumber->display(QDateTime::currentDateTime().toString(QString::number(sec)));
        Delay_MSec(1000);
        if(flag%2==1){
          numb1=sec;
          break;
        }
        if(sec<=0)
          QMessageBox::information(this, "Win", "White Win", QMessageBox::Ok);
    }
    // timer1->stop();
  }
  else if(flag%2==1)
  {
    for(sec=numb2;sec>0;sec--)
    {
      ui->lcdNumber_2->display(QDateTime::currentDateTime().toString(QString::number(sec)));
        Delay_MSec(1000);
        if(flag%2==0) {
          numb2=sec;
          break;}
        if(sec<=0)
          QMessageBox::information(this, "Win", "Black Win", QMessageBox::Ok);
    }
    // timer2->stop();
  }

}
void Widget::Delay_MSec(unsigned int msec)
{
  QTime _Timer=QTime::currentTime().addMSecs(msec);
  while(QTime::currentTime()<_Timer)
  {
    QCoreApplication::processEvents(QEventLoop::AllEvents,100);
  }
}
void Widget::paintEvent(QPaintEvent *)
{
  gridW = width()/19; //窗口宽度分18份
  gridH = height()/19; //窗口高度分18份
  //棋盘起点坐标
  startX = gridW;
  startY = gridH;
  QPainter p(this);
  //背景图
  p.drawPixmap(0,0, width(),height(),QPixmap(":/new/prefix1/C:/tupian/image/board1.jpg"));
  p.setRenderHint(QPainter::Antialiasing, true);

  int i, j;
  for (i = 0; i < 16; i++)
  {
    p.drawLine(startX, startY + i * gridH, startX+15*gridW, startY + i * gridH);
    p.drawLine(startX + i * gridW, startY, startX + i * gridW, startY+15*gridH);
  }

  QBrush brush;
  brush.setStyle(Qt::SolidPattern);
  for (i = 0; i < 15; i++)
  {
    for (j = 0; j < 15; j++)
    {
      if (a[i][j] == 1)
      {
        p.drawPixmap(startX+i*gridW, startY+j*gridH,
               gridW, gridH,
          QPixmap(":/new/prefix1/C:/tupian/image/white.png")
          );
        ui->label->hide();
        ui->label_2->show();

        /*brush.setColor(Qt::black);
        p.setBrush(brush);
        p.drawEllipse(QPoint((i + 1) * 40, (j + 1) * 40), 15, 15);*/
      }
      else if (a[i][j] == 2)
      {
        p.drawPixmap(startX+i*gridW, startY+j*gridH,
            gridW, gridH,
            QPixmap(":/new/prefix1/C:/tupian/image/black.png")
            );
        ui->label->show();
          ui->label_2->hide();

        /*brush.setColor(Qt::white);
        p.setBrush(brush);
        p.drawEllipse(QPoint((i + 1) * 40, (j + 1) * 40), 15, 15);*/
      }
    }
  }
}

void Widget::mouseReleaseEvent(QMouseEvent *e)
{
  int x, y;

   flag++;
  if(e->x() >= startX && e->x() < startX+15*gridW && e->y() >= startY && e->y() < startY+15*gridH)
  {
    x = (e->x() - startX) / gridW;
    y = (e->y() - startY) / gridH;
    if (!a[x][y])
    {
      a[x][y] = player++ % 2 + 1;
    }
    if(iswin(x, y))
    {
      update();
      setEnabled(false);
      if(flag%2==1)
         QMessageBox::information(this, "Win", "Black Win", QMessageBox::Ok);
      else
         QMessageBox::information(this, "Win", "White Win", QMessageBox::Ok);
     
    }
  }
  update();
}

int Widget::iswin(int x, int y)
{
   return f1(x, y) || f2(x, y) || f3(x, y) || f4(x ,y);
}

int Widget::f1(int x, int y)
{
  int i;
  for (i = 0; i < 5; i++)
  {
    if(y - i >= 0 &&
      y + 4 - i <= 0xF &&
      a[x][y - i] == a[x][y + 1 - i] &&
      a[x][y - i] == a[x][y + 2 - i] &&
      a[x][y - i] == a[x][y + 3 - i] &&
      a[x][y - i] == a[x][y + 4 - i])
    return 1;
  }
  return 0;
}

int Widget::f2(int x, int y)
{
  int i;
  for (i = 0; i < 5; i++)
  {
    if(x - i >= 0 &&
      x + 4 - i <= 0xF &&
      a[x - i][y] == a[x + 1 - i][y] &&
      a[x - i][y] == a[x + 2 - i][y] &&
      a[x - i][y] == a[x + 3 - i][y] &&
      a[x - i][y] == a[x + 4 - i][y])
      return 1;
  }
  return 0;
}

int Widget::f3(int x, int y)
{
  int i;
  for (i = 0; i < 5; i++)
  {
    if(x - i >= 0 &&
      y - i >= 0 &&
      x + 4 - i <= 0xF &&
      y + 4 - i <= 0xF &&
      a[x - i][y - i] == a[x + 1 - i][y + 1 - i] &&
      a[x - i][y - i] == a[x + 2 - i][y + 2 - i] &&
      a[x - i][y - i] == a[x + 3 - i][y + 3 - i] &&
      a[x - i][y - i] == a[x + 4 - i][y + 4 - i])
      return 1;
  }
  return 0;
}

int Widget::f4(int x, int y)
{
  int i;
  for (i = 0; i < 5; i++)
  {
    if(x + i <= 0xF &&
      y - i >= 0 &&
      x - 4 + i >= 0 &&
      y + 4 - i <= 0xF &&
      a[x + i][y - i] == a[x - 1 + i][y + 1 - i] &&
      a[x + i][y - i] == a[x - 2 + i][y + 2 - i] &&
      a[x + i][y - i] == a[x - 3 + i][y + 3 - i] &&
      a[x + i][y - i] == a[x - 4 + i][y + 4 - i])
      return 1;
  }
  return 0;
}

void Widget::on_pushButtonOver_clicked()
{
  this->close();
}


/*-------main.cpp----------------------------*/
#include

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
/*---------widget.h-----------*/
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
  Q_OBJECT

public:
  Widget(QWidget *parent = nullptr);
  ~Widget();
  void paintEvent(QPaintEvent *event);
  void mouseReleaseEvent(QMouseEvent *event);
  QTimer *timer1,*timer2;
  void handle_timeout(void);
  void Delay_MSec(unsigned int msec);
  int flag=3;
  int sec=0;
  int numb1=900;
  int numb2=900;
  int player;

private slots:
  void on_pushButtonOver_clicked();

private:
  Ui::Widget *ui;
  int a [15][15];

  int iswin(int,int);
  int f1(int,int);
  int f2(int,int);
  int f3(int,int);
  int f4(int,int);
  int gridW; //棋盘水平方向一个格子的宽度
  int gridH; //棋盘水平方向一个格子的高度
  int startX; //棋盘起点x坐标
  int startY; //棋盘起点y坐标

  int chessX=-1, chessY=-1; //棋盘下标



};
#endif // WIDGET_H

上述就是小编为大家分享的使用QT编写一个五子棋小游戏了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI