QT实现五子棋游戏
程序员文章站
2022-09-05 12:39:25
本文实例为大家分享了qt实现五子棋游戏的具体代码,供大家参考,具体内容如下有点bug 但是能运行每次点击右边会出现提示图标,表示是黑方落子还是白方并计时,不过不知道为什么点的次数多了 就不出现提示了...
本文实例为大家分享了qt实现五子棋游戏的具体代码,供大家参考,具体内容如下
有点bug 但是能运行
每次点击右边会出现提示图标,表示是黑方落子还是白方并计时,不过不知道为什么点的次数多了 就不出现提示了 然后过一会又有了![alt]
代码片:
#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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Golang 变量申明的三种方式