资源下载地址:https://download.csdn.net/download/sheziqiong/85788151
资源下载地址:https://download.csdn.net/download/sheziqiong/85788151
2048小游戏:通过上下左右键合并数字块,合并得出2048时,游戏胜利;当上下左右操作均无法移动任一数字块,游戏失败。
系统环境: Windows 10
开发环境: Qt Creator 4.3.1
编译器环境: MinGW 5.3.0 32bit (C/C++)
Qt依赖版本: Qt 5.9.0
学生自选题目,使用C++语言完成一个图形化的小程序。
图形化平台不限,可以是MFC、QT等。
程序内容主题不限,可以是小游戏、小工具等。
Widget是Qt中创建用户界面的主要元素,它可以显示数据和状态信息,接收用户输入,并为其他应该组合在一起的Widget提供一个容器(可以堆叠盛放其他的Widgets)。最外层的Widget称为Window。
GameBoard::GameBoard(QWidget *parent) : // 由Qwidget继承来的组件
QWidget(parent)
在创建游戏面板时,我们在文件初始创建时就可以选中继承对象QWidget

// 设置主窗口垂直布局
mainLayout = new QVBoxLayout(); // QLayout将Widget呈现垂直排列
setLayout(mainLayout);
// 创建游戏面板为网格状布局
boardLayout = new QGridLayout(); // QLayout将Widget呈现二维网格排列
CSS的样式设置,通过方法setStyleSheet启用样式设置 auto cell = new QLabel();
cell->setText("2");
cell->setAlignment(Qt::AlignCenter);
cell->setStyleSheet("QLabel { background: rgb(238,228,218); color: rgb(119,110,101); font: bold; border-radius: 10px; font: 40pt; }");
// 加入score显示模块
score = new QLabel(QString("SCORE: %1").arg(0)); // 用QLabel显示分数score
score->setStyleSheet("QLabel { color: rgb(235,224,214); font: 16pt; }"); // 设置样式
score->setFixedHeight(50); // 高度自适应
mainLayout->insertWidget(1, score, 0, Qt::AlignRight); // 在主窗口中插入分数模块
drawBoard()函数代替。void GameBoard::drawBoard()
{
delete boardLayout;
boardLayout = new QGridLayout();
for (int i = 0; i < NCells; ++i) {
for (int j = 0; j < NCells; ++j) {
delete cells[i][j];
cells[i][j] = new Cell(game.board[i][j]);
boardLayout->addWidget(cells[i][j], i, j);
cells[i][j]->draw();
}
}
mainLayout->insertLayout(0, boardLayout);
}
cell类声明了draw()成员函数,用于设置网格的样式。Cell::Cell(int v): value(v)
{
setAlignment(Qt::AlignCenter);
}
void Cell::draw() //draw()函数可以按照cell不同的值来设置样式
{
setText(QString::number(value));
auto style = QString("Cell { background: %1; color: %2; font: bold; border-radius: 10px; font: 40pt; }");
switch (value) {
case 2: {
setStyleSheet(style.arg("rgb(238,228,218)").arg("rgb(119,110,101)"));
break;
}
case 4: {
setStyleSheet(style.arg("rgb(237,224,200)").arg("rgb(119,110,101)"));
break;
}
case 8: {
setStyleSheet(style.arg("rgb(242,177,121)").arg("rgb(255,255,255)"));
break;
}
case 16: {
setStyleSheet(style.arg("rgb(245,150,100)").arg("rgb(255,255,255)"));
break;
}
case 32: {
setStyleSheet(style.arg("rgb(245,125,95)").arg("rgb(255,255,255)"));
break;
}
case 64: {
setStyleSheet(style.arg("rgb(245,95,60)").arg("rgb(255,255,255)"));
break;
}
case 128: {
setStyleSheet(style.arg("rgb(237,207,114)").arg("rgb(255,255,255)"));
break;
}
case 256: {
QGraphicsDropShadowEffect *dse = new QGraphicsDropShadowEffect();
dse->setColor(Qt::yellow);
dse->setBlurRadius(20);
dse->setOffset(-1);
setGraphicsEffect(dse);
setStyleSheet(style.arg("rgb(237,204,97)").arg("rgb(255,255,255)"));
break;
}
case 512: {
QGraphicsDropShadowEffect *dse = new QGraphicsDropShadowEffect();
dse->setColor(Qt::yellow);
dse->setBlurRadius(30);
dse->setOffset(-1);
setGraphicsEffect(dse);
setStyleSheet(style.arg("rgb(237,204,97)").arg("rgb(255,255,255)"));
break;
}
case 1024: {
QGraphicsDropShadowEffect *dse = new QGraphicsDropShadowEffect();
dse->setColor(Qt::yellow);
dse->setBlurRadius(40);
dse->setOffset(-1);
setGraphicsEffect(dse);
setStyleSheet(style.arg("rgb(237,204,97)").arg("rgb(255,255,255)"));
break;
}
case 2048: {
QGraphicsDropShadowEffect *dse = new QGraphicsDropShadowEffect();
dse->setColor(Qt::yellow);
dse->setBlurRadius(50);
dse->setOffset(-1);
setGraphicsEffect(dse);
setStyleSheet(style.arg("rgb(237,204,97)").arg("rgb(255,255,255)"));
break;
}
default: {
setText("");
setStyleSheet("Cell { background: rgb(204,192,179); border-radius: 10px; }");
}
}
在设置Cell样式时,还使用了QString来构建字符串。可以使用arg()重载函数来访问值。取得Label的值后,根据值的不同设置不同样式。
可以在main.cpp中设置随机数srand(time(NULL));,使得每次打开应用都不一样。







资源下载地址:https://download.csdn.net/download/sheziqiong/85788151
资源下载地址:https://download.csdn.net/download/sheziqiong/85788151