简单的扫雷游戏代码示例:QT框架

简单的扫雷游戏代码示例:QT框架

首页休闲益智Minesweeper扫雷手机版更新时间:2024-09-23

以下是一个简单的扫雷游戏的代码示例,使用了QT框架:

```cpp

#include <QtWidgets>

class MineSweeper : public QWidget

{

Q_OBJECT

public:

MineSweeper(int rowCount = 10, int columnCount = 10, int mineCount = 10)

: rowCount(rowCount), columnCount(columnCount), mineCount(mineCount)

{

// 初始化游戏界面

gridLayout = new QGridLayout;

for (int i = 0; i < rowCount; i) {

for (int j = 0; j < columnCount; j) {

QPushButton *button = new QPushButton;

connect(button, &QPushButton::clicked, this, [=]() {

onSquareClicked(i, j);

});

connect(button, &QPushButton::customContextMenuRequested, this, [=](const QPoint &pos) {

onSquareRightClicked(i, j);

});

gridLayout->addWidget(button, i, j);

squareButtons[i][j] = button;

}

}

restartButton = new QPushButton("Restart");

connect(restartButton, &QPushButton::clicked, this, &MineSweeper::restartGame);

QVBoxLayout *mainLayout = new QVBoxLayout;

mainLayout->addLayout(gridLayout);

mainLayout->addWidget(restartButton);

setLayout(mainLayout);

// 初始化游戏数据

resetGame();

}

private slots:

void onSquareClicked(int row, int col)

{

// 处理方格点击事件

if (gameOver) return;

SquareStatus status = squareStatus[row][col];

if (status == SquareStatus::Flagged) return;

if (status == SquareStatus::Mine) {

// 点击到雷,游戏结束

squareButtons[row][col]->setText("*");

gameOver = true;

QMessageBox::information(this, "Game Over", "You clicked on a mine. Game over!");

} else {

// 显示周围的雷数

int mineCount = countAdjacentMines(row, col);

squareButtons[row][col]->setText(QString::number(mineCount));

squareButtons[row][col]->setEnabled(false);

if (mineCount == 0) {

// 递归点击周围的方格

for (int i = -1; i <= 1; i) {

for (int j = -1; j <= 1; j) {

int newRow = row i;

int newCol = col j;

if (newRow >= 0 && newRow < rowCount && newCol >= 0 && newCol < columnCount &&

squareStatus[newRow][newCol] == SquareStatus::Untouched) {

onSquareClicked(newRow, newCol);

}

}

}

}

checkGameWin();

}

}

void onSquareRightClicked(int row, int col)

{

// 处理方格右键点击事件

if (gameOver) return;

SquareStatus status = squareStatus[row][col];

if (status == SquareStatus::Untouched) {

squareStatus[row][col] = SquareStatus::Flagged;

squareButtons[row][col]->setText("F");

} else if (status == SquareStatus::Flagged) {

squareStatus[row][col] = SquareStatus::Untouched;

squareButtons[row][col]->setText("");

}

}

void restartGame()

{

// 重新开始游戏

resetGame();

for (int i = 0; i < rowCount; i) {

for (int j = 0; j < columnCount; j) {

squareButtons[i][j]->setText("");

squareButtons[i][j]->setEnabled(true);

}

}

}

private:

enum class SquareStatus {

Untouched,

Flagged,

Mine

};

int rowCount;

int columnCount;

int mineCount;

QGridLayout *gridLayout;

QPushButton *squareButtons[100][100];

SquareStatus squareStatus[100][100];

QPushButton *restartButton;

bool gameOver;

void resetGame()

{

// 重置游戏状态

gameOver = false;

// 初始化方格状态

for (int i = 0; i < rowCount; i) {

for (int j = 0; j < columnCount; j) {

squareStatus[i][j] = SquareStatus::Untouched;

}

}

// 随机生成雷

int minesLeft = mineCount;

while (minesLeft > 0) {

int row = qrand() % rowCount;

int col = qrand() % columnCount;

if (squareStatus[row][col] == SquareStatus::Untouched) {

squareStatus[row][col] = SquareStatus::Mine;

--minesLeft;

}

}

}

int countAdjacentMines(int row, int col)

{

// 计算周围的雷数

int count = 0;

for (int i = -1; i <= 1; i) {

for (int j = -1; j <= 1; j) {

int newRow = row i;

int newCol = col j;

if (newRow >= 0 && newRow < rowCount && newCol >= 0 && newCol < columnCount &&

squareStatus[newRow][newCol] == SquareStatus::Mine) {

count;

}

}

}

return count;

}

void checkGameWin()

{

// 检查是否胜利

for (int i = 0; i < rowCount; i) {

for (int j = 0; j < columnCount; j) {

if (squareStatus[i][j] != SquareStatus::Mine && squareButtons[i][j]->isEnabled()) {

return;

}

}

}

gameOver = true;

QMessageBox::information(this, "Congratulations", "You win!");

}

};

int main(int argc, char *argv[])

{

QApplication app(argc, argv);

MineSweeper game;

game.show();

return app.exec();

}

#include "main.moc"

```

该代码实现了一个简单的扫雷游戏,游戏界面由方格组成,每个方格可以点击或右键点击处理。游戏逻辑包括点击方格时显示周围的雷数,点击到雷方格时游戏结束,以及标记方格等。游戏可以重新开始,当所有非雷方格都被点击时游戏胜利。

以上代码仅供参考,可以根据自己的需求和喜好进行修改和扩展。

查看全文
大家还看了
也许喜欢
更多游戏

Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved