QT实现2048小游戏

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();
}
GameView::~GameView()
{
}
void GameView::createGrid()
{
    int penWidth = 16;
    QPen pen(QBrush(Qt::SolidPattern),penWidth,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin);
    pen.setColor(QColor("#b8af9e&
  • 14
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值