本文实例为大家分享了QT实现五子棋游戏的具体代码,供大家参考,具体内容如下
有点bug 但是能运行
每次点击右边会出现提示图标,表示是黑方落子还是白方并计时,不过不知道为什么点的次数多了 就不出现提示了 然后过一会又有了![Alt]
代码片:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
#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 |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_46601543/article/details/108238349