qt写一个2048小游戏
时间: 2023-05-25 17:07:03 浏览: 161
抱歉,我是一名语言模型AI,不能实际编写代码。但是,我可以给您提供以下的示例代码,您可以参考此代码来编写一个基于QT框架的2048小游戏:
```c++
#include <QApplication>
#include <QKeyEvent>
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
#include <QDebug>
class Game : public QWidget {
public:
Game(QWidget* parent = nullptr) : QWidget(parent) {
createBoard();
setLayout(new QVBoxLayout());
layout()->addWidget(m_board);
layout()->addWidget(m_button);
setFocusPolicy(Qt::StrongFocus);
}
private:
enum { BOARD_SIZE = 4 };
enum { CELL_SIZE = 100 };
struct Position {
Position(int x_, int y_) : x(x_), y(y_) {}
int x;
int y;
};
QLabel* cell(int x, int y) const {
return qobject_cast<QLabel*>(m_board->itemAtPosition(y, x)->widget());
}
bool valid(int x, int y) const {
return x >= 0 && y >= 0 && x < BOARD_SIZE && y < BOARD_SIZE;
}
Position findEmptyCell() const {
while (true) {
int x = qrand() % BOARD_SIZE;
int y = qrand() % BOARD_SIZE;
if (cell(x, y)->text().isEmpty())
return Position(x, y);
}
}
void createBoard() {
m_board = new QGridLayout();
for (int y = 0; y < BOARD_SIZE; ++y) {
for (int x = 0; x < BOARD_SIZE; ++x) {
QLabel* label = new QLabel();
label->setFixedSize(CELL_SIZE, CELL_SIZE);
label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
label->setStyleSheet("background-color: #fbb03b; font-size: 36px; font-weight: bold; color: #776e65; border-radius: 5px");
m_board->addWidget(label, y, x);
}
}
cell(0, 0)->setText("2");
cell(1, 0)->setText("2");
}
bool merge(Position from, Position to) {
if (!valid(to.x, to.y) || !valid(from.x, from.y)) return false;
QLabel* fromLabel = cell(from.x, from.y);
QLabel* toLabel = cell(to.x, to.y);
if (toLabel->text().isEmpty()) {
toLabel->setText(fromLabel->text());
fromLabel->setText("");
return true;
}
if (toLabel->text() == fromLabel->text()) {
int value = toLabel->text().toInt() * 2;
toLabel->setText(QString::number(value));
fromLabel->setText("");
return true;
}
return false;
}
void restart() {
for (int y = 0; y < BOARD_SIZE; ++y) {
for (int x = 0; x < BOARD_SIZE; ++x) {
cell(x, y)->setText("");
}
}
int x1 = qrand() % BOARD_SIZE;
int y1 = qrand() % BOARD_SIZE;
int x2 = qrand() % BOARD_SIZE;
int y2 = qrand() % BOARD_SIZE;
cell(x1, y1)->setText("2");
cell(x2, y2)->setText("2");
}
bool isGameOver() const {
for (int y = 0; y < BOARD_SIZE; ++y) {
for (int x = 0; x < BOARD_SIZE; ++x) {
if (cell(x, y)->text().isEmpty())
return false;
if (valid(x + 1, y) && cell(x + 1, y)->text() == cell(x, y)->text())
return false;
if (valid(x, y + 1) && cell(x, y + 1)->text() == cell(x, y)->text())
return false;
}
}
return true;
}
protected:
void keyPressEvent(QKeyEvent* event) override {
if (event->key() == Qt::Key_Escape) {
return qApp->quit();
}
if (isGameOver()) {
return;
}
bool changed = false;
switch (event->key()) {
case Qt::Key_Left:
for (int y = 0; y < BOARD_SIZE; ++y) {
for (int x = 0; x < BOARD_SIZE; ++x) {
for (int x2 = x + 1; x2 < BOARD_SIZE; ++x2) {
if (merge(Position(x2, y), Position(x, y)))
changed = true;
}
}
}
break;
case Qt::Key_Right:
for (int y = 0; y < BOARD_SIZE; ++y) {
for (int x = BOARD_SIZE - 1; x >= 0; --x) {
for (int x2 = x - 1; x2 >= 0; --x2) {
if (merge(Position(x2, y), Position(x, y)))
changed = true;
}
}
}
break;
case Qt::Key_Up:
for (int y = 0; y < BOARD_SIZE; ++y) {
for (int x = 0; x < BOARD_SIZE; ++x) {
for (int y2 = y + 1; y2 < BOARD_SIZE; ++y2) {
if (merge(Position(x, y2), Position(x, y)))
changed = true;
}
}
}
break;
case Qt::Key_Down:
for (int y = BOARD_SIZE - 1; y >= 0; --y) {
for (int x = 0; x < BOARD_SIZE; ++x) {
for (int y2 = y - 1; y2 >= 0; --y2) {
if (merge(Position(x, y2), Position(x, y)))
changed = true;
}
}
}
break;
default:
break;
}
if (changed) {
insertRandom();
if (isGameOver()) {
qDebug() << "Game over!";
}
}
}
void insertRandom() {
Position emptyCell = findEmptyCell();
int value = (qrand() % 10 == 0) ? 4 : 2;
cell(emptyCell.x, emptyCell.y)->setText(QString::number(value));
}
QLabel* m_button = new QLabel("Press Arrow keys to play.");
QGridLayout* m_board;
};
int main(int argc, char** argv) {
QApplication app(argc, argv);
app.setApplicationName("2048 Game by Qt");
Game game;
game.setGeometry(200, 200, 500, 500);
game.show();
return app.exec();
}
```
希望这可以帮助您开始编写自己的2048小游戏。
阅读全文