-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchessgame.cpp
More file actions
391 lines (325 loc) · 11.6 KB
/
chessgame.cpp
File metadata and controls
391 lines (325 loc) · 11.6 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include "chessgame.h"
#include "mainwindow.h"
#include<QPushButton>
#include <QPixmap>
#include <QPalette>
#include <QPainter>
#include <QMouseEvent>
#include <QButtonGroup>
#include <QRadioButton>
#include <QMessageBox>
#include <QMediaPlayer>
#include <QMediaPlaylist>
ChessGame::ChessGame(QWidget *parent)
: QMainWindow(parent),isAllMusicPlaying(true), hasWinner(false)
{
// 设置游戏窗口标题名称
setWindowTitle("五子棋游戏");
// 设置游戏窗口的尺寸
setMinimumSize(1000, 800);
setMaximumSize(1000, 800);
// 设置游戏背景图片
QPixmap background(":/new/prefix1/images/8d732e1e83e0d423821f95019b109392.jpg");
if (background.isNull()) {
qWarning() << "Failed to load background image";
return;
}
// 缩放图片以适应窗口大小,并保持纵横比
background = background.scaled(size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
QPalette palette; // 创建palette对象,并且将背景图片设置为窗口背景
palette.setBrush(QPalette::Window, background);
this->setPalette(palette);
// 展示游戏UI信息
QPushButton *button_mode = new QPushButton(this);
button_mode->setStyleSheet("font:Bold;font-size:36px;color:white;background-color: rgb(30, 144, 255);border:2px ;border-radius:10px;padding:2px 4px;");
button_mode->setGeometry(QRect(785, 100, 200, 50));
button_mode->setText("PVP");
QLabel *label_name = new QLabel(this);
label_name->setStyleSheet("color:white;font-size:32px;");
label_name->setText("经典智益游戏");
label_name->setGeometry(785, 700, 200, 50);
// 玩家先手按钮实现
game_type = 1;
SelectModeGroup = new QButtonGroup(this);
QRadioButton *Play1 = new QRadioButton("黑棋先手", this);
SelectModeGroup->addButton(Play1, 1);
Play1->setStyleSheet("font: Bold; font-size:36px; color:white;");
Play1->setGeometry(825, 200, 100, 30);
connect(Play1, SIGNAL(clicked()), this, SLOT(SelectRadio()));
QRadioButton *Play2 = new QRadioButton("白棋先手", this);
SelectModeGroup->addButton(Play2, 2);
Play2->setStyleSheet("font: Bold; font-size:36px; color:white;");
Play2->setGeometry(825, 270, 100, 30);
connect(Play2, SIGNAL(clicked()), this, SLOT(SelectRadio()));
Play1->setChecked(true);
// 实现游戏开始/游戏结束按钮
flag = 0;
button = new QPushButton(this);
button->setStyleSheet("font:Bold;font-size:36px;color:white;background-color: rgb(30, 144, 255);border:2px ;border-radius:10px;padding:2px 4px;");
button->setGeometry(QRect(785, 390, 200, 50));
button->setText("游戏开始");
connect(button, SIGNAL(clicked()), this, SLOT(GameOper()));
// 初始化:处理开启鼠标监听操作
setMouseTracking(true);
lock = true;
memset(chessboard, 0, sizeof(chessboard));
// 创建并初始化媒体播放器实例
mediaPlayer = new QMediaPlayer(this);
winSound = new QMediaPlayer(this);
blackMoveSound = new QMediaPlayer(this);
whiteMoveSound = new QMediaPlayer(this);
// 加载音频文件
mediaPlayer->setMedia(QUrl("qrc:/new/prefix1/images/yuzgouchangwan.mp3"));
winSound->setMedia(QUrl("qrc:/new/prefix1/images/sound.mp3"));
blackMoveSound->setMedia(QUrl("qrc:/new/prefix1/images/black_move.mp3"));
whiteMoveSound->setMedia(QUrl("qrc:/new/prefix1/images/white_move.mp3"));
// 自动播放背景音乐
if (isAllMusicPlaying) {
mediaPlayer->play();
if (mediaPlayer->state() != QMediaPlayer::PlayingState) {
qDebug() << "Background music not playing, state:" << mediaPlayer->state();
}
}
// 创建音乐开关按钮
musicButton = new QPushButton(this);
musicButton->setStyleSheet("font:Bold;font-size:36px;color:white;background-color: rgb(30, 144, 255);border:2px ;border-radius:10px;padding:2px 4px;");
musicButton->setGeometry(QRect(785, 500, 200, 50));
musicButton->setText("关闭音乐");
// 连接音乐开关按钮点击事件到槽函数
connect(musicButton, &QPushButton::clicked, this, &ChessGame::toggleAllMusic);
}
ChessGame::~ChessGame()
{
delete ui;
}
// 成员函数
// 更新棋盘上的棋子位置
void ChessGame::update_chessboard(int x, int y)
{
if (!chessboard[x][y]) {
chessboard[x][y] = player; // 切换棋子颜色操作
currentX = x;
currentY = y;
update();
// 检查是否有玩家获胜
Is_Someone_Winner(x, y);
if (!hasWinner && isAllMusicPlaying) {
if (player == 1) { // 黑棋
blackMoveSound->play();
} else { // 白棋
whiteMoveSound->play();
}
}
player = (player % 2) + 1; // 切换玩家
}
}
// 检测是否有玩家获胜
void ChessGame::Is_Someone_Winner(int x, int y)
{
QPoint ps(x, y);
if (isWinner(ps)) {
lock = true;
hasWinner = true; // 设置胜利标志
if (game_type == 1) {
if (chessboard[x][y] == 1)
strWinner = "BLACK";
else
strWinner = "WHITE";
}
// 播放胜利音效
if (isAllMusicPlaying) {
winSound->play();
}
QMessageBox::information(NULL, "退出", strWinner + "战胜,请继续努力!", QMessageBox::Ok);
}
}
// 获取指定位置的棋子类型 dir方向 offset位移
int ChessGame::getPointAt(QPoint p, int dir, int offset)
{
int x = p.x(), y = p.y();
x = x + offset * m_dir[dir].x();
y = y + offset * m_dir[dir].y();
// 边界检查
if (x >= 15 || x < 0 || y >= 15 || y < 0) {
return -100;
}
// 如果坐标有效,该函数返回位置的棋子类型
return chessboard[x][y];
}
// 判断指定位置是否形成五子连珠
int ChessGame::isWinner(QPoint p)
{
int dir_cnt[8] = {0};
memset(dir_cnt, 0, sizeof(dir_cnt));
for (int i = 0; i < 8; i++) {
for (int j = 1;; j++) {
if (getPointAt(p, i, j) != chessboard[p.x()][p.y()])
break;
dir_cnt[i]++;
}
}
for (int i = 0; i < 4; i++) {
if (dir_cnt[i] + dir_cnt[i + 4] + 1 >= 5) {
return 1;
}
}
// 如果在任何一个方向检测到五子连珠,返回1表示玩家获胜
return 0;
}
// 人物下棋 重载鼠标按压,获取位置,处理玩家的下棋操作
void ChessGame::PERSON_time(QMouseEvent *e)
{
// 通过计算操作,将鼠标的屏幕坐标转换成棋盘上面的行列索引
// 这样做可以在棋盘逻辑坐标系当中确定玩家点击的位置
int x, y;
if ((e->x() >= 25 && e->x() <= 755) && (e->y() >= 25 && e->y() <= 755)) {
x = (e->x() + 25) / 50 - 1;
y = (e->y() + 25) / 50 - 1;
update_chessboard(x, y);
}
}
// 绘制棋盘和棋子(绘图事件 重写绘图事件)
void ChessGame::paintEvent(QPaintEvent *e)
{
QPainter pt(this);
// 调用此接口来设置绘制图时的渲染操作
pt.setRenderHint(QPainter::Antialiasing, true);
// 绘制棋盘
QBrush brush;
brush.setColor(QColor(123, 176, 141));
brush.setStyle(Qt::SolidPattern);
pt.setBrush(brush);
// 处理边框
QPen pen = pt.pen();
pt.setPen(pen);
pt.drawRect(30, 30, 740, 740);
for (int i = 0; i < 15; i++) {
pt.drawLine(50, 50 + i * 50, 750, 50 + i * 50); // 横线
pt.drawLine(50 + i * 50, 50, 50 + i * 50, 750); // 竖线
}
// 绘制5个正方形黑色点
brush.setColor(Qt::black);
pt.setBrush(brush);
pt.drawRect(195, 195, 10, 10); // 上左
pt.drawRect(595, 195, 10, 10); // 上右
pt.drawRect(195, 595, 10, 10); // 下左
pt.drawRect(595, 595, 10, 10); // 下右
pt.drawRect(395, 395, 10, 10); // 中间
// 绘制棋子
brush.setStyle(Qt::SolidPattern);
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
if (chessboard[i][j] == 1) {
// 黑棋
brush.setColor(Qt::black);
pt.setBrush(brush);
pt.drawEllipse(QPoint(i * 50 + 50, j * 50 + 50), 20, 20);
} else if (chessboard[i][j] == 2) {
// 白棋
brush.setColor(Qt::white);
pt.setPen(Qt::NoPen);
pt.setBrush(brush);
pt.drawEllipse(QPoint(i * 50 + 50, j * 50 + 50), 20, 20);
}
}
}
if (!flag)
return;
// 绘制最近一个落子的标识
pen.setColor(Qt::red);
pen.setWidth(1);
pt.setPen(pen);
pt.setBrush(Qt::NoBrush); // 移除画刷
pt.drawRect(currentX * 50 + 50 - 23, currentY * 50 + 50 - 23, 46, 46);
// 锁住棋盘(禁止下棋),产生胜负/游戏结束
if (lock)
return;
// 绘制鼠标跟踪预览标识
if((moveX>=0 && moveX<=14) && (moveY>=0 && moveY<=14) && chessboard[moveX][moveY]==0){
if (player == 1) {
// 黑色预览标识
brush.setColor(QColor(0, 0, 0, 150));
} else {
// 白色预览标识
brush.setColor(QColor(255, 255, 255, 150));
}
pt.setPen(Qt::NoPen);
pt.setBrush(brush);
pt.drawEllipse(QPoint(moveX * 50 + 50, moveY * 50 + 50), 20, 20);
}
}
// 更新moveX moveY(处理鼠标移动事件)
void ChessGame::mouseMoveEvent(QMouseEvent *e) // OK
{
moveX=(e->x()+25)/50-1;
moveY=(e->y()+25)/50-1;
update();
}
// 玩家下棋(处理鼠标按压事件)
void ChessGame::mousePressEvent(QMouseEvent *e)
{
if (lock)
return;
// 根据游戏类型和当前玩家判断是否允许下棋
if (game_type == 1) {
PERSON_time(e);
}
}
void ChessGame::GameOper()
{
if (flag == 0) {
SelectPlayer();
lock = false;
currentX = -100;
currentY = -100;
moveX = -100;
moveY = -100;
memset(chessboard, 0, sizeof(chessboard));
button->setText("游戏结束");
button->setStyleSheet("font:Bold;font-size:36px;color:white;background-color: rgb(255, 0, 0);border:2px ;border-radius:10px;padding:2px 4px;");
} else {
button->setText("游戏开始");
button->setStyleSheet("font:Bold;font-size:36px;color:white;background-color: rgb(30, 144, 255);border:2px ;border-radius:10px;padding:2px 4px;");
lock = true;
memset(chessboard, 0, sizeof(chessboard));
update();
// 停止背景音乐
mediaPlayer->stop();
}
flag = !flag;
}
void ChessGame::SelectPlayer()
{
switch (SelectModeGroup->checkedId()) // OK
{
case 1: // 黑棋先手
game_type = 1;
player = 1;
break;
case 2: // 白棋先手
game_type = 1;
player = 2;
break;
}
}
void ChessGame::SelectRadio()
{
SelectPlayer();
}
void ChessGame::toggleAllMusic()
{
qDebug() << "Toggling all music, current state:" << isAllMusicPlaying;
if (isAllMusicPlaying) {
mediaPlayer->pause();
winSound->pause();
blackMoveSound->pause();
whiteMoveSound->pause();
isAllMusicPlaying = false;
musicButton->setText("开启音乐");
} else {
mediaPlayer->play();
isAllMusicPlaying = true;
musicButton->setText("关闭音乐");
}
qDebug() << "After toggle, media player state:" << mediaPlayer->state();
}