基于图形视图框架的打砖块游戏

本文详细介绍了一个基于Qt的游戏砖块消除程序的实现过程。包括砖块、球体及控制板的创建,以及如何通过键盘控制实现球体的移动与碰撞检测等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


大笑密集恐惧症~


首先来创建上面的砖块,然后呢~设置一下碰撞模式

    QPixmap *Pixmap = new QPixmap(u8":/img/砖块.png");
    const int &Width = Pixmap->width();
    const int &Height = Pixmap->height();
    const int XCount = width() / Width;
    const int YCount = height() / Height -10;

    for(int x=0;x<XCount;x++)
        for(int y=0;y<YCount;y++)
        {
            QGraphicsPixmapItem *Item = new QGraphicsPixmapItem(*Pixmap);
            m_Scene->addItem(Item);
            Item->setPos(x*Width,y*Height);
            Item->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
        }

再创建球和下面砖块

   m_Ball = new Ball(QPixmap(u8":/img/球.png"),size());
    m_Con = new Control(QPixmap(u8":/img/砖块.png"));
    m_Scene->addItem(m_Ball);
    m_Scene->addItem(m_Con);

    m_Con->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
    m_Con->setPos((width()-m_Con->pixmap().width())/2, height()-m_Con->pixmap().height());
    m_Ball->setPos((width()-m_Ball->pixmap().width())/2, m_Con->y()-m_Ball->pixmap().height());

利用时间设置球的移动

void Ball::timerEvent(QTimerEvent *event)
{
    if(m_Time->timerId() == event->timerId())
    {
        checkStage();//检测一下是否碰撞砖块
        moveBy(m_xSpeed,m_ySpeed);
        checkBaffle();<span style="font-family: Arial, Helvetica, sans-serif;">//检查一下是否超出舞台</span>
    }
}

再来就是唯一可以控制砖块的移动啦

void BlockBreaker::keyPressEvent(QKeyEvent *event)
{
    switch(event->key())
    {
    case Qt::Key_Left:
        if(m_Con->pos().x()<0)
            return;
        m_Con->moveLeft();
        if(!m_Ball->isState())
            m_Ball->moveBy(-m_Con->getSpeed(),0);
        break;

    case Qt::Key_Right:
        if(m_Con->pos().x()+m_Con->pixmap().width() > width())
            return;
        m_Con->moveRight();
        if(!m_Ball->isState())
            m_Ball->moveBy(m_Con->getSpeed(),0);
        break;

    case Qt::Key_Space:
        m_Ball->start();
    }
}



完整代码: http://download.youkuaiyun.com/detail/qq_17813937/9511229

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小鱼游戏开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值