QT环境:QT5.2.0,Qt Creator3.0.0
1.自定义方块类
class Square : public QGraphicsItem
{
public:
explicit Square(QPoint pos,QString text,QString background_color);
QString getText() {return text;}
void updatePos(QPoint pos);
void updateText(QString text,QString background_color);
void updateBox(QPoint pos,QString text,QString background_color);
enum {Type = UserType + 1};
int type() const;
private :
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
QString text;
QString color;
};
Square::Square(QPoint pos,QString text,QString background_color) : QGraphicsItem() { setPos(pos); this->text = text; this->color = background_color; setFlag(QGraphicsItem::ItemIsFocusable); } int Square::type() const { return Type; } QRectF Square::boundingRect() const { int penWidth = 1; return QRectF(0-penWidth/2,0-penWidth/2,105+penWidth,105+penWidth); } void Square::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(option); Q_UNUSED(widget); //画背景 painter->setPen(QColor(color)); painter->setBrush(QBrush(QColor(color))); painter->drawRect(QRect(0,0,105,105)); //设置字体 字号 switch(text.length()) { case 1: painter->setFont(QFont(QString::fromLocal8Bit("微软雅黑"),50,QFont::Bold)); break; case 2: painter->setFont(QFont(QString::fromLocal8Bit("微软雅黑"),40,QFont::Bold)); break; case 3: painter->setFont(QFont(QString::fromLocal8Bit("微软雅黑"),35,QFont::Bold)); break; case 4: painter->setFont(QFont(QString::fromLocal8Bit("微软雅黑"),30,QFont::Bold)); break; default: break; } //设置字体颜色 if(text.toInt() == 2 || text.toInt() == 4) { painter->setPen(QColor("#7c736a")); } else if(text.toInt() >= 8) { painter->setPen(QColor("#fff7eb")); } painter->drawText(QRect(0,0,105,105),Qt::AlignCenter,text); } void Square::updatePos(QPoint pos) { setPos(pos); update(boundingRect()); } void Square::updateText(QString text,QString background_color) { this->text = text; this->color = background_color; update(boundingRect()); } void Square::updateBox(QPoint pos,QString text,QString background_color) { this->text = text; this->color = background_color; setPos(pos); update(boundingRect()); }
2.自定义视图类
class GameView : public QGraphicsView
{
Q_OBJECT
public:
explicit GameView();
~GameView();
private:
QGraphicsScene *g_scene;
QMap<qreal,QString> colorBox;
void createGrid();
void createColorBox();
void startGame();
void restartGame();
bool upMerge();
bool downMerge();
bool leftMerge();
bool rightMerge();
bool newSquare();
void countEmpty(QList<QPair<int,int> > &empty_index);
bool checkSucceeded();
bool checkFinished();
void keyPressEvent(QKeyEvent * event);
void drawBackground(QPainter * painter, const QRectF & rect);
public slots:
};
GameView::GameView() : QGraphicsView()
{
g_scene = new QGraphicsScene();
g_scene->setSceneRect(0,0,500,500);
setScene(g_scene);
setCacheMode(QGraphicsView::CacheBackground);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
createGrid();
createColorBox();
startGame();
}
GameVi

使用QT5.2.0和Qt Creator3.0.0,实现了2048小游戏。自定义了方块类和视图类,但游戏尚存在不足,如缺少合并特效、移动轨迹,新方块出现方式不理想,合并算法复杂,程序逻辑需优化。附带游戏效果图。
最低0.47元/天 解锁文章
4429





