2020-10-16

在这里插入图片描述
main.cpp
anemy.cpp
mainwindow.cpp
player.cpp
obstacle.cpp
javacup.cpp
mypushbutton.cpp

mainwindow.h
anemy.h
player.h
obstacle.h
javacup.h
mypushbutton.h

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

anemy.cpp

#include "anemy.h"
#include "player.h"
anemy::anemy(QObject *parent ,int postionx ,int postiony ,int RangeX,int RangeY,float horV ,float verV ) : QObject(parent)
{
    setHorizontalV(horV);
    setVerticalV(verV);
    setData(1,11);
    setRangeXY(RangeX,RangeY);
    BornPosX = postionx;
    BornPosY = postiony;
    setPosition(postionx,postiony);
    MoveFlat = 1;//初始化可移动
    MoveTimer = new QTimer ;
    RecoveryTimer = new QTimer;
    heroBlood = 10;
    connect(RecoveryTimer,&QTimer::timeout,[=](){
       HeroSkin = HeroNormalSkin;
       MoveFlat = 1;
       RecoveryTimer->stop();
    });
}
QRectF anemy::boundingRect()const
{
    qreal penWidth = 1;
    return QRectF(heroPosX-penWidth / 2, heroPosY- penWidth/2,PicWidth+penWidth, PicHeight+penWidth);
}
void anemy::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option)
    Q_UNUSED(widget)
//    painter->setBrush(!collidingItems().isEmpty()? Qt::red : Qt::green);
   // painter->drawRect(0,0,20,20);
   // painter->drawRect(0,50,30,20);
    //如果与其他图形项碰撞则显示红色,否则显示绿色
    painter->drawPixmap(heroPosX,heroPosY,PicWidth,PicHeight,HeroSkin);

}


void anemy::setPosition(int x,int y)
{
    heroPosX = x;
    heroPosY = y;
}
void anemy::setHorizontalV(float v)
{
    HorizontalV = v;
}
void anemy::setVerticalV(float v)
{
    VerticalV = v;
}
void anemy::setRangeXY(int x,int y)
{
    m_RangeX = x;
    m_RangeY = y;
}

void anemy::setMoveMode(int Mode)
{
    MoveMode = Mode;
}


//被攻击时不能移动,掉血,图片变红
void anemy::AttackedByJ()
{
    if(!collidingItems().isEmpty())
    {
        for(int i = 0; i < collidingItems().length(); i++)
        {
            if(collidingItems().at(i)->data(1).toInt()== 1)//与人物碰撞
            {
                MoveFlat = 0;//进入僵直
                HeroSkin = HeroBeAttackedSkin;
                RecoveryTimer->start(RecoveryInterval);//若干秒后恢复
                heroBlood--;//怪物扣血
                if(heroBlood == 0)
                {
                    emit BeKilled();
                    delete this;
                }
            }
        }
    }
}
void anemy::AttackedByK()
{
    if(!collidingItems().isEmpty())
    {
        for(int i = 0; i < collidingItems().length(); i++)
        {
            if(collidingItems().at(i)->data(1).toInt()== 1)//与人物碰撞
            {
                MoveFlat = 0;//进入僵直
                HeroSkin = HeroBeAttackedSkin;
                RecoveryTimer->start(RecoveryInterval);//若干秒后恢复
                heroBlood-=2;//怪物扣血
                if(heroBlood == 0)
                {
                    emit BeKilled();
                    delete this;
                }
            }
        }
    }
}
//L技能秒杀怪物**************************************************************************************************
void anemy::AttackedByL()
{
    if(!collidingItems().isEmpty())
    {
        for(int i = 0; i < collidingItems().length(); i++)
        {
            if(collidingItems().at(i)->data(1).toInt()== 1)//与人物碰撞
            {
                MoveFlat = 0;//进入僵直
                HeroSkin = HeroBeAttackedSkin;
                RecoveryTimer->start(RecoveryInterval);//若干秒后恢复
//                heroBlood--;//怪物扣血
//                if(heroBlood == 0)
//                {
                emit BeKilled();
                delete this;
//                }
            }
        }
    }
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setFixedSize(700,700);
    setWindowIcon(QIcon(":/m/back/hit.png"));
    setWindowTitle("SuperHiter");
    MyPushButton * startBtn = new MyPushButton(":/m/back/knife.png");
    startBtn->setParent(this);
    startBtn->move(this->width() * 0.5 - 80 * 0.5 ,this->height() * 0.6 );

    connect(startBtn,&MyPushButton::clicked,[=](){
        //qDebug() << "点击了开始";
        //做弹起特效
        startBtn->zoom1();
        startBtn->zoom2();

        //显示选择关卡场景
        // pView->show();

        QMessageBox::information(this,"游戏提示",
                                 "WAD控制角色左右移动和上下移动\nJKL释放技能\n打败怪兽有大量积分哦!!!"
                                 );
        if(clickedTimes==0)
        {
            //BGM->play();

            myPlayer->setMedia(QUrl("qrc:/m/back/bgm.mp3"));
            myPlayer->setVolume(80);
            myPlayer->play();
            firstLevelIni();
            this->hide();
            pView->show();
            clickedTimes++;
        }

    });
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/m/back/welcome.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::firstLevelIni()
{
    //初始化地面
    for(int i = 0; i <  GROUNDNUM; i++)
    {
        ground[i] = new obstacle;
    }
    nGround(0,GROUNDNUM,-400,650);

    //初始化砖块
    for(int i = 0; i <  BRICKNUM; i++)
    {
        brick[i] = new obstacle;
    }
    nBrick(0,2,-200,570);
    nBrick(2,4,0,470);
    nBrick(4,5,200,370);
    nBrick(5,7,500,380);
    nBrick(7,8,600,580);
    nBrick(8,10,700,480);
    nBrick(10,11,900,370);
    nBrick(11,12,1100,470);
    nBrick(12,13,1300,570);
    nBrick(13,14,1500,470);
    nBrick(14,15,1700,570);
    nBrick(16,17,1900,470);
    nBrick(17,18,2100,370);

    //初始化金币
//    for(int i = 0; i <  COINNUM; i++)
//    {
        coin[0] = new obstacle(nullptr,0);
        coin[1] = new obstacle(nullptr,1);
        coin[2] = new obstacle(nullptr,2);
        coin[3] = new obstacle(nullptr,0);
        coin[4] = new obstacle(nullptr,1);
        coin[5] = new obstacle(nullptr,2);
//    }
    nCoin(0,2,0,410);
    coin[0]->magic = 1;
    nCoin(2,3,600,310);
    nCoin(3,4,900,310);
    nCoin(4,5,1300,510);
    nCoin(5,6,1700,510);
    for(int i = 0; i <  COINNUM; i++)
    {
        connect(coin[i],&obstacle::GetCoin,[=](){
            item->Score+=20;
            if(item->Score>100)
            {
                item->Score-=100;
                item->heroBlood+=5;
            }
            emit item->DecBlood();
        });
    }

    //初始化书
    book = new obstacle;
    newOb(book,3,2500,550,180,200,4);

    //初始化云
    for(int i = 0; i < CLOUDNUM; i++)
    {
        cloud[i] = new obstacle;
        cloud[i]->setType(4);
        cloud[i]->setPos(-375 + 250 * i,300);
        cloud[i]->setWidthHeight(100,50);
        pScene->addItem(cloud[i]);
    }

    //初始化主楼
    h = new obstacle;
    newOb(h,5,2600,550,180,200,5);

    //初始化会动的砖
    for(int i = 0; i <  MBRICKNUM; i++)
    {
        mbrick[i] = new obstacle;
    }
    nmBrick(0,3,3000,550);

    //初始化树
    for(int i = 0; i < TREENUM; i++)
    {
        tree[i] = new obstacle;
        tree[i]->setType(8);
        tree[i]->setPos(-375 + 150 * i,600);
        tree[i]->setWidthHeight(100,100);
        pScene->addItem(tree[i]);
    }


//    //初始化作业
//    homework = new obstacle;
//    newOb(homework,7,550,550,180,200,6);
	
    //初始化角色
    item = new player;
    item->setFlag(QGraphicsItem::ItemIsFocusable);  //鼠标选中这个item之后就是聚焦, 然后可以用键盘控制这个item
    item->setFlag(QGraphicsItem::ItemIsMovable);
    pScene->addItem(item);
    pScene->setFocusItem(item);


    ///初始化怪物*************************************************
    Cups[0] = new javacup(nullptr,-250,900,40,0,3,0);
    Cups[1] = new javacup(nullptr,0,800,40,0,3,0);
    Cups[2] = new javacup(nullptr,100,700,40,0,3,0);
    Cups[3] = new javacup(nullptr,400,600,40,0,3,0);
    Cups[4] = new javacup(nullptr,900,700,40,0,3,0);
    Cups[5] = new javacup(nullptr,1200,900,20,0,3,0);
    Cups[6] = new javacup(nullptr,1700,750,60,0,3,0);
    Cups[7] = new javacup(nullptr,1950,900,50,0,3,0);
    Cups[8] = new javacup(nullptr,2300,1000,40,0,3,0);
    Cups[9] = new javacup(nullptr,2500,800,30,0,3,0);
    Cups[10] = new javacup(nullptr,900,700,40,0,3,0);
    Cups[11] = new javacup(nullptr,1200,850,40,0,3,0);
    Cups[12] = new javacup(nullptr,2600,1000,40,0,3,0);
    Cups[13] = new javacup(nullptr,2700,1300,40,0,3,0);

    Cups[14] = new javacup(nullptr,800,1400,40,0,3,0);
    Cups[15] = new javacup(nullptr,900,1400,40,0,3,0);
    Cups[16] = new javacup(nullptr,1000,1400,40,0,3,0);
    Cups[17] = new javacup(nullptr,1200,1400,40,0,3,0);
    Cups[18] = new javacup(nullptr,1700,1400,40,0,3,0);
    Cups[19] = new javacup(nullptr,1400,1400,40,0,3,0);
    Cups[20] = new javacup(nullptr,2300,1400,40,0,3,0);
    Cups[21] = new javacup(nullptr,1900,1400,40,0,3,0);
    Cups[22] = new javacup(nullptr,2400,1400,40,0,3,0);
    Cups[23] = new javacup(nullptr,1600,1400,40,0,3,0);
    Cups[24] = new javacup(nullptr,2700,1400,40,0,3,0);

    for(int i = 0; i < CupNum; i++)
    {
//        Cups[i] = new javacup;
        pScene->addItem(Cups[i]);
        Cups[i]->setPosition(Cups[i]->BornPosX,(i%5)+800);
        Cups[i]->setRangeXY(40+10*(i%4),40+10*(i%4));
        Cups[i]->setVerticalV(2+i%3);
        Cups[i]->setHorizontalV(2+i%3);
        Cups[i]->MoveMode = (i+2)%5+1;
        if(i>13)
        {
            Cups[i]->HeroSkin = QPixmap(":/C++/C++.png");
            Cups[i]->HeroNormalSkin = QPixmap(":/C++/C++.png");
            Cups[i]->HeroBeAttackedSkin = QPixmap(":/C++/CBeAttacked.png");
            Cups[i]->PicWidth = 48;
            Cups[i]->PicHeight = 54;
        }
    }
//    for(int i = 0; i < PlusNum; i++)
//    {
//        pScene->addItem(plus[i]);
//        plus[i]->setRangeXY(40,40);
//        plus[i]->setVerticalV(4+(i%4));
//        plus[i]->setHorizontalV(5+(i%4));
//        plus[i]->MoveMode = i%3 + 1;
//    }


    //连接攻击信号与怪物信号
    for(int i = 0; i < CupNum; i++)
    {
        connect(item,&player::Skill0,Cups[i],&javacup::AttackedByJ);
        connect(item,&player::Skill1,Cups[i],&javacup::AttackedByK);
        connect(item,&player::Skill2,Cups[i],&javacup::AttackedByL);
        connect(Cups[i],&javacup::BeKilled,[=](){
            item->Score+=50;
            if(item->Score>100)
            {
                item->Score-=100;
                item->heroBlood+=5;
            }
            emit item->DecBlood();
        });
    }
    // 将 item 添加至场景中
    // 为视图设置场景


    QFont font;
    font.setKerning(true);
    font.setBold(true);
    text->setPlainText("血量:"+QString::number(item->heroBlood)+"\n积分:"+QString::number(item->Score));
    text->setPos(-700,500);
    text->setFont(font);
    pScene->addItem(text);
    connect(item,&player::DecBlood,[=](){
        text->setPlainText("血量:"+QString::number(item->heroBlood)+"\n积分:"+QString::number(item->Score));
        text->update();
    });


    pView->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    pView->resize(SCREENWIDTH,SCREENHEIGHT);
    pView->setScene(pScene);
    pView->setStyleSheet("border:none; background:pink;");
    pView->centerOn(0,0);
    pView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    pView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    //背景移动
    connect(item,&player::BackGroundMove,[=](){
        for (int i = 0; i < BRICKNUM; i++)
        {
            brick[i]->moveBy(-2,0);
//            if(brick[i]->pos().x()<-500)
//            {
//                pScene->removeItem(brick[i]);

//            }
        }

        for (int i = 0; i < COINNUM; i++)
        {
            coin[i]->moveBy(-2,0);
        }

        book->moveBy(-2,0);
        h->moveBy(-2,0);
//        homework->moveBy(-2,0);

        for (int i = 0; i < 3; i++)
        {
            mbrick[i]->moveBy(-2,0);
        }

        for (int i = 0; i < CupNum; i++)
        {
            Cups[i]->moveBy(-3,0);
        }

    });

    connect(coin[0]->groundTimer,&QTimer::timeout,[=](){

        for(int i = 0;i<GROUNDNUM;i++)
        {
            if(count%2==0)
            {
                pScene->removeItem(ground[i]);
                pScene->update();
            }
            else
            {
                pScene->addItem(ground[i]);
                pScene->update();
            }

        }
         count++;
    });

    //胜利
    connect(item,&player::succeed,[=](){
        pView->close();
        QMessageBox::about(this,"Victory","你赢了!");
    });

    //失败
    connect(item,&player::failed,[=](){

        pView->close();
        QMessageBox::about(this,"Defeated","你输了!\n再来一次吧!奥利给!!!!");
        this->close();
    });
    connect(item,&player::failed,[=](){
        pView->close();
        this->close();
    });
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{


}

void MainWindow::newOb(obstacle *one, int type, int x, int y, int w, int h, int data)
{
    one->setType(type);
    one->setPos(x,y-300);
    one->setWidthHeight(w,h);
    one->setData(1,data);
    one->setPos(x,y);
    pScene->addItem(one);
}

void MainWindow::nGround(int begin, int end, int x, int y)
{
    for(int i = begin; i < end; i++)
    {
        newOb(ground[i],0,x + 150 * (i - begin),y,300,200,2);
    }
}

void MainWindow::nBrick(int begin, int end, int x, int y)
{
    for(int i = begin; i < end; i++)
    {
        newOb(brick[i],1,x + 50 * (i - begin),y,100,50,2);
    }
}

void MainWindow::nCoin(int begin, int end, int x, int y)
{
    for(int i = begin; i < end; i++)
    {
        newOb(coin[i],2,x + 40 * (i - begin),y,80,120,3);
    }
}


void MainWindow::nmBrick(int begin, int end, int x, int y)
{
    for(int i = begin; i < end; i++)
    {
        newOb(mbrick[i],6,x + 600 * (i - begin),y,50,50,2);
    }
}

QList<int> MainWindow::generateUniqueRandomNumber()
{
    int i,j;
    QList<int> numbersList;
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    for(i=0;i<8;i++)
    {
        numbersList.append(qrand()%8);
        bool flag = true;
        while(flag)
        {
            for(j=0;j<i;j++)
            {
                if(numbersList[i]==numbersList[j])
                {
                    break;
                }
                if(j<i)
                {
                    numbersList[i]=rand()%8;
                }
                if(j==i)
                {
                    flag=!flag;
                }
            }
        }
    }
        for(i=0;i<8;i++)
        {
            qDebug()<<numbersList[i];
        }
        return numbersList;
}

player.cpp

#include "player.h"
player::player(QObject *parent) : QObject(parent)
{
    setToolTip("憨憨!");//提示
    setCursor(Qt::OpenHandCursor);
    PicWidth = 46;
    PicHeight = 56;

    color = QColor(qrand()%256,qrand()%256,qrand()%256);
    connect(JumpTimer,&QTimer::timeout,[=](){
        FreeFalling();
        if(collidingItems().isEmpty())
        {
            update(pos().x()-50, pos().y()-50,46+100, 156);
        }
    });

    connect(KeyTimer,&QTimer::timeout,[=](){
        if(KeyPressed(Key_W))
        {
            Direction = up;
            JumpOrnot = true;
            setVelocity(10);
        }
        if(KeyPressed(Key_A))
        {
             HorizontalDir = left;
            // pos().x()-=HorizontalSpeed;
             moveBy(-HorizontalSpeed,0);  //相对现在的位置移动
             if(!SkillTimer1->isActive()&&!SkillTimer0->isActive()&&!SkillTimer2->isActive())
             {
                 PicWidth = 46;
                 PicHeight = 56;
                 RunSkinCounter--;
                 switch(RunSkinCounter)
                 {
                     case 0:
                         HeroSkin = HeroRunSkin1;
                         RunSkinCounter = 60;
                        // qDebug()<<"0001";
                         break;
                     case 10:
                         HeroSkin = HeroRunSkin2;
                        // qDebug()<<"0002";
                         break;
                     case 20:
                         HeroSkin = HeroRunSkin3;
                         //qDebug()<<"0003";
                         break;
                     case 30:
                         HeroSkin = HeroRunSkin4;
                        // qDebug()<<"0004";
                         break;
                     case 40:
                         HeroSkin = HeroRunSkin5;
                         //qDebug()<<"0005";
                     case 50:
                         HeroSkin = HeroRunSkin0;
                         break;
                 default:
                     break;
                 }
             }
        }
        if(KeyPressed(Key_D))
        {
           // qDebug()<<pos().x();
            HorizontalDir = right;
            if(pos().x()<0 || arrive)
            {
               moveBy(HorizontalSpeed,0);  //相对现在的位置移动
            }
            else
            {
                emit BackGroundMove();
            }
            if(!SkillTimer1->isActive()&&!SkillTimer0->isActive()&&!SkillTimer2->isActive())
            {
                PicWidth = 46;
                PicHeight = 56;
                RunSkinCounter++;
                switch(RunSkinCounter)
                {
                    case 0:
                        HeroSkin = HeroRunSkin1;
                       // qDebug()<<"0001";
                        break;
                    case 10:
                        HeroSkin = HeroRunSkin2;
                       // qDebug()<<"0002";
                        break;
                    case 20:
                        HeroSkin = HeroRunSkin3;
                        //qDebug()<<"0003";
                        break;
                    case 30:
                        HeroSkin = HeroRunSkin4;
                       // qDebug()<<"0004";
                        break;
                    case 40:
                        HeroSkin = HeroRunSkin5;
                        //qDebug()<<"0005";
                        break;
                    case 50:
                        HeroSkin = HeroRunSkin0;
                        break;
                    case 60:
                        RunSkinCounter=-10;
                      break;
                default:
                    break;
                }
            }
        }
        if(KeyPressed(Key_K)&&!SkillTimer0->isActive()&&!SkillTimer2->isActive())
        {
            SkillCounter = 0;
            SkillTimer1->start(100);
        }
        if(KeyPressed(Key_J)&&!SkillTimer1->isActive()&&!SkillTimer2->isActive())
        {
            SkillCounter = 0;
            SkillTimer0->start(100);
        }
        if(KeyPressed(Key_L)&&!SkillTimer1->isActive()&&!SkillTimer0->isActive())
        {
            SkillCounter = 0;
            SkillTimer2->start(100);
        }
    });
    connect(SkillTimer0,&QTimer::timeout,[=](){
        if(!SkillTimer1->isActive()&&!SkillTimer2->isActive())
        {
            emit Skill0();
            PicWidth = 100;
            PicHeight = 90;
            moveBy(0,-5);
            switch (SkillCounter) {
            case 0:
                HeroSkin = Attack30;
                break;
            case 1:
                HeroSkin = Attack31;
                break;
            case 2:
                HeroSkin = Attack32;
                break;
            case 3:
                HeroSkin = Attack33;
                break;
            case 4:
                HeroSkin = Attack34;
                break;
            case 5:
                HeroSkin = Attack35;
                break;
            case 6:
                SkillTimer0->stop();
                 break;
            default:
                break;
            }
            SkillCounter++;
        }

    });
    connect(SkillTimer1,&QTimer::timeout,[=](){
        if(!SkillTimer0->isActive()&&!SkillTimer2->isActive())
        {
        emit Skill1();
        PicWidth = 100;
        PicHeight = 90;
        moveBy(0,-5);
        switch (SkillCounter) {
        case 0:
            HeroSkin = Attack40;
            break;
        case 1:
            HeroSkin = Attack41;
            break;
        case 2:
            HeroSkin = Attack42;
            break;
        case 3:
            HeroSkin = Attack43;
            break;
        case 4:
            HeroSkin = Attack44;
            break;
        case 5:
            HeroSkin = Attack45;
            break;
        case 6:
            SkillTimer1->stop();
            break;
        default:
            break;
        }
        SkillCounter++;
        }
    });
    connect(SkillTimer2,&QTimer::timeout,[=](){
        if(!SkillTimer0->isActive()&&!SkillTimer1->isActive())
        {
        emit Skill2();
        PicWidth = 100;
        PicHeight = 90;
        moveBy(0,-5);
        switch (SkillCounter) {
        case 0:
            HeroSkin = Attack00;
            break;
        case 1:
            HeroSkin = Attack01;
            break;
        case 2:
            HeroSkin = Attack02;
            break;
        case 3:
            HeroSkin = Attack03;
            break;
        case 4:
            HeroSkin = Attack10;
            break;
        case 5:
            HeroSkin = Attack11;
            break;
        case 6:
            HeroSkin = Attack12;
            break;
        case 7:
            HeroSkin = Attack50;
            break;
        case 8:
            HeroSkin = Attack51;
            break;
        case 9:
            HeroSkin = Attack50;
            break;
        case 10:
            HeroSkin = Attack51;
            break;
        case 11:
            SkillTimer2->stop();
            break;
        default:
            break;
        }
        SkillCounter++;
        }
    });
    JumpTimer->start(40);
    KeyTimer->start(10);
    setPos(-300,500);
    setData(1,1);
    setVelocity(0);
}
QRectF player::boundingRect()const
{
    qreal penWidth = 1;
    return QRectF(pos().x()-penWidth / 2, pos().y() - penWidth/2,PicWidth+penWidth, PicHeight+penWidth);
}
void player::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option)
    Q_UNUSED(widget)
    //painter->setBrush(!collidingItems().isEmpty()? Qt::red : Qt::green);
   // painter->drawRect(0,0,20,20);
   // painter->drawRect(0,50,30,20);
    //如果与其他图形项碰撞则显示红色,否则显示绿色

    painter->drawPixmap(pos().x(),pos().y(),PicWidth,PicHeight,HeroSkin);

}
void player::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    setCursor(Qt::ClosedHandCursor);
    //moveBy(10,0);

}
void player::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
}
void player::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
}
void player::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_W && !JumpOrnot)
    {
        SaveKeyPressed(Key_W);
    }
    if(event->key() == Qt::Key_A)
    {
        SaveKeyPressed(Key_A);
    }
    if(event->key() == Qt::Key_D)
    {
        SaveKeyPressed(Key_D);
    }
    if(event->key() == Qt::Key_S)
    {
        moveBy(0,HorizontalSpeed);  //相对现在的位置移动
    }
    if(event->key() == Qt::Key_J)
    {
        SaveKeyPressed(Key_J);
    }
    if(event->key() == Qt::Key_K)
    {
        SaveKeyPressed(Key_K);
    }
    if(event->key() == Qt::Key_L)
    {
        SaveKeyPressed(Key_L);
    }
}
void player::keyReleaseEvent(QKeyEvent *event)
{
    if(event->key()==Qt::Key_A)
    {
        SaveKeyReleased(Key_A);
    }
    if(event->key()==Qt::Key_W)
    {
        SaveKeyReleased(Key_W);
    }
    if(event->key()==Qt::Key_D)
    {
        SaveKeyReleased(Key_D);
    }
    if(event->key()==Qt::Key_J)
    {
        SaveKeyReleased(Key_J);
    }
    if(event->key()==Qt::Key_K)
    {
        SaveKeyReleased(Key_K);
    }
    if(event->key()==Qt::Key_L)
    {
        SaveKeyReleased(Key_L);
    }


}
void player::FreeFalling(void)
{
    int i;
    if(pos().y()>700)
    {
        emit failed();
    }
    if(Velocity<=0)
    {
        Direction = down;
    }
    else if(Velocity>0)
    {
        Direction = up;
    }
    if(Direction == up || collidingItems().isEmpty())
    {
        moveBy(0,-Velocity);
        Velocity=0.7*Velocity-Gravity;
    }
    if(!collidingItems().isEmpty())
    {
        emit collided();
        for(i = 0;i < collidingItems().length(); i++)
        {
            switch (collidingItems().at(i)->data(1).toInt()) {
            case 2:
                if(Direction == down)
                {
                    moveBy(0,-1);
                    JumpOrnot = false;
                    UnderBrick = false;
                    Velocity=0;
                }
                else if(Direction == up)
                {
                  //  pos().y()+=Velocity;
                    moveBy(0,Velocity);
                    UnderBrick = true;
                }
                else
                {
                    JumpOrnot = true;
                    UnderBrick = false;
                }
                break;
            case 4:
                arrive = true;
                break;
            case 5:
                emit succeed();
                break;
            case 11:
                if(!SkillTimer1->isActive()&&!SkillTimer0->isActive()&&!SkillTimer2->isActive())
                {
                    heroBlood-=1;
                    emit DecBlood();
                   if(heroBlood<=0)
                   {
                      emit failed();
                   }
                }
                break;
            default:
                break;

            }
      }
        if(JumpOrnot)
        {
            moveBy(0,-Velocity);
            Velocity-=Gravity;
        }
        if(UnderBrick)//如果向上跳跃的时候碰到了砖头
        {
            if(Velocity>0)
            {
                Velocity =  0 - Velocity;
            }
           // pos().y()-=Velocity;//让人往下掉
            moveBy(0,-Velocity);
            Velocity=(0.9*Velocity-Gravity);
        }
    }
    else
    {
        emit notcollided();
    }



    return;
}
void player::setVelocity(int v)
{
    Velocity = v;
}
void player::advance(int phase)
{
}


obstacle.cpp

#include "obstacle.h"
#include<QDebug>
obstacle::obstacle(QObject *parent,int booktype) : QObject(parent)
{
    setShowFlag(1);
    switch(booktype)
    {
    case 0:
        Book = Book1;
        break;
    case 1:
        Book = Book2;
        break;
    case 2:
        Book = Book3;
        break;
    default:
        break;
    }

    connect(cloudTimer,&QTimer::timeout,[=](){
        if(type == 4)
        {
            setPos(pos().x() - 5,pos().y());
            if(pos().x() < -475)
            {
                setPos(375,pos().y());;
            }
            update(pos().x(),pos().y(),obWidth,obHeight);
        }
    });
    cloudTimer->start(200);

    connect(brickTimer,&QTimer::timeout,[=](){
        if(type == 6)
        {
            if(moveFlag)
            {
                setPos(pos().x() + 5,pos().y());
            }
            else
            {
                setPos(pos().x() - 5,pos().y());
            }
            if(pos().x() < -100)
            {
                moveFlag = 1;
            }
            else if(pos().x() > 200)
            {
                moveFlag = 0;
            }
            update(pos().x(),pos().y(),obWidth,obHeight);
        }
    });
    brickTimer->start(200);
}

QRectF obstacle::boundingRect()const
{
    return QRectF(pos().x(),pos().y(),obWidth,obHeight);
}

void obstacle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option)
    Q_UNUSED(widget)
    switch(type)
    {
    case 0://{
        painter->drawPixmap(pos().x(),pos().y(),obWidth,obHeight,Ground);
        break;
    }
    case 1://砖块
    {
        painter->drawPixmap(pos().x(),pos().y(),obWidth,obHeight,Brick);
        break;
    }
    case 2://金币
    {
        if(showflag)
        {
            painter->drawPixmap(pos().x(),pos().y(),obWidth,obHeight,Book);
        }
        if(!collidingItems().isEmpty())
        {
            for(int i = 0; i < collidingItems().length(); i++)
            {
                if(collidingItems().at(i)->data(1).toInt()==1)
                {
                    if(!groundTimer->isActive()&&magic==1)
                    {
                        groundTimer->start(400);
                    }
                    setShowFlag(0);
                    update(pos().x(),pos().y(),obWidth,obHeight);
                    emit GetCoin();
                    delete this;
                }
            }
        }
        break;
    }
    case 3://{
        painter->drawPixmap(pos().x(),pos().y(),obWidth,obHeight,Book);
        break;
    }
    case 4://{
        painter->drawPixmap(pos().x(),pos().y(),obWidth,obHeight,Cloud);
        break;
    }
    case 5://主楼
    {
        painter->drawPixmap(pos().x(),pos().y(),obWidth,obHeight,H);
        break;
    }
    case 6://会动的砖
    {
        painter->drawPixmap(pos().x(),pos().y(),obWidth,obHeight,Brick);
        break;
    }
    case 7://作业
    {
        painter->drawPixmap(pos().x(),pos().y(),obWidth,obHeight,Homework);
        break;
    }
    case 8://{
        painter->drawPixmap(pos().x(),pos().y(),obWidth,obHeight,Tree);
        break;
    }
    }

}

void obstacle::setType(int num)
{
    type = num;
}

void obstacle::setPosition(int x,int y)
{
    obPosX = x;
    obPosY = y;
}

void obstacle::setWidthHeight(int width,int height)
{
    obWidth = width;
    obHeight = height;
}

void obstacle::setShowFlag(int num)
{
    showflag = num;
}

javacup.cpp

#include "javacup.h"

javacup::javacup(QObject *parent,int postionx,int postiony,int RangeX,int RangeY,float horV,float verV):anemy(parent, postionx,postiony,RangeX,RangeY,horV,verV)
{
    setCursor(Qt::OpenHandCursor);
    PicWidth = 34;
    PicHeight = 64;
    color = QColor(qrand()%256,qrand()%256,qrand()%256);
    HorizontalDir = left;
    VerticalDir = down;
    HeroSkin = QPixmap(":/javacup/java.png");
    HeroNormalSkin = QPixmap(":/javacup/java.png");
    HeroBeAttackedSkin = QPixmap(":/javacup/javaBeAttacked.png");
    connect(MoveTimer,&QTimer::timeout,[=](){
        if(MoveFlat == 1)
        {
            switch (MoveMode)
            {
            case 1://左右横条
                if(heroPosX< BornPosX - m_RangeX)
                {
                    HorizontalDir = right;
                }
                if(heroPosX > BornPosX + m_RangeX)
                {
                    HorizontalDir = left;
                }
                if(HorizontalDir == left)
                {
                    heroPosX = heroPosX - HorizontalV;
                    //                moveBy(-HorizontalV,0);
                }
                else
                {
                    heroPosX = heroPosX + HorizontalV;
                    //                moveBy(HorizontalV,0);
                }
                break;

            case 2://上下起伏
                if(heroPosY< BornPosY - m_RangeY)
                {
                    VerticalDir = down;
                }
                if(heroPosY > BornPosY + m_RangeY)
                {
                    VerticalDir = up;
                }
                if(VerticalDir == up)
                {
                    heroPosY = heroPosY - VerticalV;
                    //                moveBy(-HorizontalV,0);
                }
                else
                {
                    heroPosY = heroPosY + VerticalV;
                    //                moveBy(HorizontalV,0);
                }
                break;

            case 3://六亲不认
                if(heroPosX< BornPosX - m_RangeX)
                {
                    HorizontalDir = right;
                }
                if(heroPosX > BornPosX + m_RangeX)
                {
                    HorizontalDir = left;
                }
                if(HorizontalDir == left)
                {
                    heroPosX = heroPosX - HorizontalV;
                    //                moveBy(-HorizontalV,0);
                }
                else
                {
                    heroPosX = heroPosX + HorizontalV;
                    //                moveBy(HorizontalV,0);
                }
                if(heroPosY< BornPosY - m_RangeY)
                {
                    VerticalDir = down;
                }
                if(heroPosY > BornPosY + m_RangeY)
                {
                    VerticalDir = up;
                }
                if(VerticalDir == up)
                {
                    heroPosY = heroPosY - VerticalV;
                    //                moveBy(-HorizontalV,0);
                }
                else
                {
                    heroPosY = heroPosY + VerticalV;
                    //                moveBy(HorizontalV,0);
                }
                break;

            case 4://孤儿不复还左右版本
                if(HorizontalDir == left)
                {
                    heroPosX = heroPosX - HorizontalV;
                    //                moveBy(-HorizontalV,0);
                }
                else
                {
                    heroPosX = heroPosX + HorizontalV;
                    //                moveBy(HorizontalV,0);
                }
                break;

            case 5://孤儿不复还上天
                if(VerticalDir == up)
                {
                    heroPosY = heroPosY - VerticalV;
                    //                moveBy(-HorizontalV,0);
                }
                else
                {
                    heroPosY = heroPosY + VerticalV;
                    //                moveBy(HorizontalV,0);
                }
                break;

            default:
                if(heroPosX< BornPosX - m_RangeX)
                {
                    HorizontalDir = right;
                }
                if(heroPosX > BornPosX + m_RangeX)
                {
                    HorizontalDir = left;
                }
                if(HorizontalDir == left)
                {
                    heroPosX = heroPosX - HorizontalV;
                    //                moveBy(-HorizontalV,0);
                }
                else
                {
                    heroPosX = heroPosX + HorizontalV;
                    //                moveBy(HorizontalV,0);
                }
                break;
            }

        }
//         update(heroPosX-50, heroPosY-50,46+100, 156);
    });
    MoveTimer->start(MoveInterval);
}

javacup::~javacup()
{

}

mypushbutton.cpp

#include "mypushbutton.h"
#include <QDebug>
#include <QPropertyAnimation>
//MyPushButton::MyPushButton(QWidget *parent) : QPushButton(parent)
//{

//}


MyPushButton::MyPushButton(QString normalImg, QString pressImg )
{
    this->normalImgPath = normalImg;
    this->pressImgPath = pressImg;

    QPixmap pix;
    bool ret = pix.load(normalImg);
    if(!ret)
    {
       qDebug() << "图片加载失败";
       return;
    }

    //设置图片固定大小
    this->setFixedSize(80,80);

    //设置不规则图片样式
    this->setStyleSheet("QPushButton{border:0px;}");

    //设置图标
    this->setIcon(pix);

    //设置图标大小
    this->setIconSize(QSize(80,80));
}


void MyPushButton::zoom1()
{
    //创建动态对象
    QPropertyAnimation * animation = new QPropertyAnimation(this,"geometry");
    //设置动画时间间隔
    animation->setDuration(200);

    //起始位置
    animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    //结束位置
    animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));

    //设置弹跳曲线
    animation->setEasingCurve(QEasingCurve::OutBounce);

    //执行动画
    animation->start();
}
void MyPushButton::zoom2()
{
    //创建动态对象
    QPropertyAnimation * animation = new QPropertyAnimation(this,"geometry");
    //设置动画时间间隔
    animation->setDuration(200);

    //起始位置
    animation->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    //结束位置
    animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));

    //设置弹跳曲线
    animation->setEasingCurve(QEasingCurve::OutBounce);

    //执行动画
    animation->start();

}

void MyPushButton::mousePressEvent(QMouseEvent *e)
{
    if(this->pressImgPath != "") //传入的按下图片不为空 说明需要有按下状态,切换图片
    {
        QPixmap pix;
        bool ret = pix.load(this->pressImgPath);
        if(!ret)
        {
           qDebug() << "图片加载失败";
           return;
        }

        //设置图片固定大小
        this->setFixedSize( pix.width(),pix.height());

        //设置不规则图片样式
        this->setStyleSheet("QPushButton{border:0px;}");

        //设置图标
        this->setIcon(pix);

        //设置图标大小
        this->setIconSize(QSize(pix.width(),pix.height()));

    }

    //让父类执行其他内容
    return QPushButton::mousePressEvent(e);

}

void MyPushButton::mouseReleaseEvent(QMouseEvent *e)
{
    if(this->pressImgPath != "") //传入的按下图片不为空 说明需要有按下状态,切换成初始图片
    {
        QPixmap pix;
        bool ret = pix.load(this->normalImgPath);
        if(!ret)
        {
           qDebug() << "图片加载失败";
           return;
        }

        //设置图片固定大小
        this->setFixedSize( pix.width(),pix.height());

        //设置不规则图片样式
        this->setStyleSheet("QPushButton{border:0px;}");

        //设置图标
        this->setIcon(pix);

        //设置图标大小
        this->setIconSize(QSize(pix.width(),pix.height()));

    }

    //让父类执行其他内容
    return QPushButton::mouseReleaseEvent(e);

}



mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include"player.h"
#include"obstacle.h"
#include <QMainWindow>
#include<QPushButton>
#include<QMessageBox>
#include<QFileDialog>
#include<QSound>//多媒体模块下的音效头文件
#include <QFileInfo>
#include<QMediaPlayer>
#include "javacup.h"
#include<QGraphicsTextItem>
#include<QFont>
#include<QTime>
#include<QPushButton>
#include"mypushbutton.h"
#define SCREENWIDTH 1500
#define SCREENHEIGHT 1080
#define CLOUDNUM 3
#define GROUNDNUM 6

#define BRICKNUM 20
#define COINNUM 6
#define CupNum 25


#define MBRICKNUM 5
#define TREENUM 5

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    void keyPressEvent(QKeyEvent *event);

    void shootBullet(int p,int q);
    ~MainWindow();
    void paintEvent(QPaintEvent *event);
    int clickedTimes=0;
    int count = 0;
    QGraphicsScene *pScene = new QGraphicsScene();
    QGraphicsView *pView = new QGraphicsView();
    player *item;
    obstacle *ground[GROUNDNUM];
    obstacle *brick[BRICKNUM];
    obstacle *coin[COINNUM];
    obstacle *book;
    obstacle *cloud[CLOUDNUM];
    obstacle *h;
    obstacle *mbrick[MBRICKNUM];
//    obstacle *homework;
    obstacle *tree[TREENUM];
    QGraphicsTextItem *text = new QGraphicsTextItem;
    void firstLevelIni();
    void newOb(obstacle *one, int type, int x, int y, int w, int h, int data);
    void nGround(int begin, int end, int x, int y);
    void nBrick(int begin, int end, int x, int y);
    void nCoin(int begin, int end, int x, int y);
    void nmBrick(int begin, int end, int x, int y);

    QList<int> generateUniqueRandomNumber();
 //  QSound *BGM = new QSound(":/m/back/BGM.wav");
    QMediaPlayer *myPlayer= new QMediaPlayer;

    //怪物*******************************************
    javacup * Cups[CupNum];
    QPixmap knife = QPixmap(":/m/back/knife.png");
    QPixmap knife2 = QPixmap(":/m/back/knife2.png");
    QPixmap start = QPixmap(":/m/back/start.png");
    QPixmap hit = QPixmap(":/m/back/hit.png");

private slots:

private:
    Ui::MainWindow *ui;

};

#endif // MAINWINDOW_H

anemy.h

#ifndef ANEMY_H
#define ANEMY_H
#include <QString>
#include <QList>
#include <QObject>
#include <QGraphicsItem>
#include <QRectF>
#include<QCursor>
#include<QGraphicsSceneMouseEvent>
#include<QDrag>
#include<QWidget>
#include<QMimeData>
#include<QApplication>
#include<QGraphicsScene>
#include <QMainWindow>
#include <QGraphicsPixmapItem>
#include<QGraphicsView>
#include<QTimer>
#include <QPixmap>
#include <QObject>
#include <QDebug>
#include<QKeyEvent>
#include<QList>
#include<QPainter>
#include <QObject>

/*怪物的移动模式,默认为1
1.左右横跳
2.上下起伏
3.六亲不认*/
class anemy : public QObject,public QGraphicsItem
{
    Q_OBJECT
public:
    explicit anemy(QObject *parent = nullptr,int postionx = 0,int RangeX = 0,int RangeY = 0,int postiony = 0,float horV = 0,float verV = 0);
    int heroPosX;//人物的x位置
    int heroPosY;//人物的y位置
    int BornPosX;//出生位置
    int BornPosY;
    int m_RangeX;//x-range<x<x+range
    int m_RangeY;//
    float HorizontalV;//水平
    float VerticalV;//人物跳跃时候的垂直的速度
    int heroBlood;
    QColor color;
    int PicWidth;
    int PicHeight;
    int MoveMode;//怪物移动模式


    int MoveFlat;//1为可移动,2为不可移动
    static const int MoveInterval = 40; //移动间隔
    static const int RecoveryInterval = 1000;//硬直恢复

    enum Dir{up,down,left,right};//人物的方向
    Dir VerticalDir;
    Dir HorizontalDir;
    int RunSkinCounter = 0;//人物移动的时候皮肤切换计数器
    int SkillCounter = 0;

    QTimer *MoveTimer ;
    QTimer *RecoveryTimer;//从被攻击硬直到恢复移动
    QPixmap HeroSkin;
    QPixmap HeroNormalSkin;
    QPixmap HeroBeAttackedSkin;


    QRectF boundingRect() const override;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
    void setPosition(int x,int y);
    void setHorizontalV(float v);
    void setVerticalV(float v);
    void setRangeXY(int x,int y);
    void setMoveMode(int Mode);
    void attackJudge();
    //
private:

signals:
    void shoot();
    void BackGroundMove();
    void Skill0();
    void Skill1();
    void Skill2();
    void Skill3();
    void Skill4();
    void collided();
    void notcollided();
//    void die();
    void Behurt();
    void Recoveried();
    void BeKilled();
public slots:
    void AttackedByJ();
    void AttackedByK();
    void AttackedByL();
//    void ChangeRed();
};

#endif // ANEMY_H

player.h

#ifndef PLAYER_H
#define PLAYER_H
#include <QString>
#include <QList>
#include <QObject>
#include <QGraphicsItem>
#include <QRectF>
#include<QCursor>
#include<QGraphicsSceneMouseEvent>
#include<QDrag>
#include<QWidget>
#include<QMimeData>
#include<QApplication>
#include<QGraphicsScene>
#include <QMainWindow>
#include <QGraphicsPixmapItem>
#include<QGraphicsView>
#include<QTimer>
#include <QPixmap>
#include <QObject>
#include <QDebug>
#include<QKeyEvent>
#include<QList>
#include<QPainter>
class player : public QObject,public QGraphicsItem
{
    Q_OBJECT
public:
    explicit player(QObject *parent = 0);
    int heroPosX;//人物的x位置
    int heroPosY;//人物的y位置
    float Velocity;//人物跳跃时候的垂直的速度
    int heroBlood = 20;
    bool arrive = false;

    enum Dir{up,down,left,right};//人物的方向
    Dir Direction;
    Dir HorizontalDir;
    int RunSkinCounter = 0;//人物移动的时候皮肤切换计数器
    int SkillCounter = 0;
    int Score = 0;


    int HorizontalSpeed = 2;

    QTimer *JumpTimer = new QTimer;
    QTimer *KeyTimer = new QTimer;
    QTimer *SkillTimer0 = new QTimer;
    QTimer *SkillTimer1 = new QTimer;
    QTimer *SkillTimer2 = new QTimer;
    QTimer *SkillTimer3 = new QTimer;

    QPixmap HeroSkin = QPixmap(":/hero/adventurer-run-00.png");
    QPixmap HeroRunSkin0 = QPixmap(":/hero/adventurer-run-00.png");
    QPixmap HeroRunSkin1 = QPixmap(":/hero/adventurer-run-01.png");
    QPixmap HeroRunSkin2 = QPixmap(":/hero/adventurer-run-02.png");
    QPixmap HeroRunSkin3 = QPixmap(":/hero/adventurer-run-03.png");
    QPixmap HeroRunSkin4 = QPixmap(":/hero/adventurer-run-04.png");
    QPixmap HeroRunSkin5 = QPixmap(":/hero/adventurer-run-05.png");
    QPixmap Attack00 = QPixmap(":/hero/adventurer-air-attack1-00.png");
    QPixmap Attack01 = QPixmap(":/hero/adventurer-air-attack1-01.png");
    QPixmap Attack02 = QPixmap(":/hero/adventurer-air-attack1-02.png");
    QPixmap Attack03 = QPixmap(":/hero/adventurer-air-attack1-03.png");
    QPixmap Attack10 = QPixmap(":/hero/adventurer-air-attack2-00.png");
    QPixmap Attack11 = QPixmap(":/hero/adventurer-air-attack2-01.png");
    QPixmap Attack12 = QPixmap(":/hero/adventurer-air-attack2-02.png");
    QPixmap Attack20 = QPixmap(":/hero/adventurer-attack1-00.png");
    QPixmap Attack21 = QPixmap(":/hero/adventurer-attack1-01.png");
    QPixmap Attack22 = QPixmap(":/hero/adventurer-attack1-02.png");
    QPixmap Attack23 = QPixmap(":/hero/adventurer-attack1-03.png");
    QPixmap Attack24 = QPixmap(":/hero/adventurer-attack1-04.png");
    QPixmap Attack30 = QPixmap(":/hero/adventurer-attack2-00.png");
    QPixmap Attack31 = QPixmap(":/hero/adventurer-attack2-01.png");
    QPixmap Attack32 = QPixmap(":/hero/adventurer-attack2-02.png");
    QPixmap Attack33 = QPixmap(":/hero/adventurer-attack2-03.png");
    QPixmap Attack34 = QPixmap(":/hero/adventurer-attack2-04.png");
    QPixmap Attack35 = QPixmap(":/hero/adventurer-attack2-05.png");
    QPixmap Attack40 = QPixmap(":/hero/adventurer-attack3-00.png");
    QPixmap Attack41 = QPixmap(":/hero/adventurer-attack3-01.png");
    QPixmap Attack42 = QPixmap(":/hero/adventurer-attack3-02.png");
    QPixmap Attack43 = QPixmap(":/hero/adventurer-attack3-03.png");
    QPixmap Attack44 = QPixmap(":/hero/adventurer-attack3-04.png");
    QPixmap Attack45 = QPixmap(":/hero/adventurer-attack3-05.png");
    QPixmap Attack50 = QPixmap(":/hero/adventurer-air-attack3-loop-00.png");
    QPixmap Attack51 = QPixmap(":/hero/adventurer-air-attack3-loop-01.png");
    QPixmap Attack52 = QPixmap(":/hero/adventurer-air-attack3-rdy-00.png");

    QRectF boundingRect() const override;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
    void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
    void advance(int phase) override;


    void setPosition(int x,int y);
    void setVelocity(int v);
    void attackJudge();
    bool JumpOrnot;
    bool UnderBrick;
    //
private:
    QColor color;
    int PicWidth;
    int PicHeight;
    void keyPressEvent(QKeyEvent *event) override;
    void keyReleaseEvent(QKeyEvent *event) override;
    float Gravity = 4; //重力值
    inline void SaveKeyPressed( int key )
    {
        m_PressedKeys |= ( 1 << key );
    }
    inline void SaveKeyReleased( int key )
    {
        m_PressedKeys &= ~( 1 << key );
    }
    inline bool KeyPressed( int key )
    {
        return m_PressedKeys & ( 1 << key );
    }
    enum Interested_Keys
    {
        Key_A = 1,
        Key_W,
        Key_D,
        Key_K,
        Key_J,
        Key_L
    };
        quint32  m_PressedKeys=0;

signals:
    void shoot();
    void BackGroundMove();
    void Skill0();
    void Skill1();
    void Skill2();
    void Skill3();
    void Skill4();
    void collided();
    void notcollided();
    void succeed();
    void failed();
    void DecBlood();
public slots:
     void FreeFalling(void);
};

#endif // PLAYER_H

obstacle.h

#ifndef OBSTACLE_H
#define OBSTACLE_H

#include <QObject>
#include <QGraphicsItem>
#include <QRectF>
#include<QCursor>
#include<QGraphicsSceneMouseEvent>
#include<QDrag>
#include<QWidget>
#include<QMimeData>
#include<QApplication>
#include<QGraphicsScene>
#include <QMainWindow>
#include <QGraphicsPixmapItem>
#include<QGraphicsView>
#include<QTimer>
#include <QPixmap>
#include <QObject>
#include <QDebug>
#include<QKeyEvent>
#include<QList>
#include<QPainter>

class obstacle : public QObject,public QGraphicsItem
{
    Q_OBJECT
public:
     obstacle(QObject *parent = 0, int booktype = 0);

    int type;  //障碍物类型 0:地 1: 砖块 2: 金币 3:书 4:云 5:主楼 6:会动的砖 7:作业 8:树
    int obPosX;  //障碍物坐标
    int obPosY;
    int obWidth;  //障碍物的宽
    int obHeight;  //障碍物的高
    int showflag;  //障碍物是否显示 0: 不显示 1: 显示
    int magic = 0;
    int moveFlag;
    int bookType = 0;
    bool DeleteOrNot = false;
    QPixmap Ground = QPixmap(":/obstacle/obstacle/Grass.png");
    QPixmap Brick = QPixmap(":/obstacle/obstacle/brick01.png");
    QPixmap Coin = QPixmap(":/obstacle/obstacle/coin.png");
    QPixmap Book= QPixmap(":/obstacle/obstacle/book.jpg");
    QPixmap Book1 = QPixmap(":/obstacle/obstacle/book.jpg");
    QPixmap Book2 = QPixmap(":/obstacle/obstacle/book2.jpg");
    QPixmap Book3 = QPixmap(":/obstacle/obstacle/book3.jpg");
    QPixmap Cloud = QPixmap(":/obstacle/obstacle/cloud.png");
    QPixmap H = QPixmap(":/obstacle/obstacle/mainbuild.png");
    QPixmap Homework = QPixmap(":/obstacle/obstacle/homework.png");
    QPixmap Tree = QPixmap(":/obstacle/obstacle/trees.png");

    QRectF boundingRect() const override;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;

    QTimer *cloudTimer = new QTimer;
    QTimer *brickTimer = new QTimer;
    QTimer *groundTimer = new QTimer;

    void setType(int num);
    void setPosition(int x,int y);
    void setWidthHeight(int width,int height);
    void setShowFlag(int num);


signals:
    void GroundTwinkcle();
    void GetCoin();
public slots:
};

#endif // OBSTACLE_H

javacup.h

#ifndef JAVACUP_H
#define JAVACUP_H
#include "anemy.h"

class javacup:public anemy
{
public:
    javacup(QObject *parent = nullptr,int postionx = 0,int postiony = 0,int RangeX = 0,int RangeY = 0,float horV = 0,float verV = 0);
    ~javacup();
};

#endif // JAVACUP_H

mypushbutton.h

#ifndef MYPUSHBUTTON_H
#define MYPUSHBUTTON_H

#include <QPushButton>

class MyPushButton : public QPushButton
{
    Q_OBJECT
public:
    //explicit MyPushButton(QWidget *parent = 0);

    //构造函数 参数1  正常显示的图片路径   参数2   按下后显示的图片路径
    MyPushButton(QString normalImg, QString pressImg = "" );

    //成员属性 保存用户传入的默认显示路径 以及按下后显示的图片路径
    QString normalImgPath;
    QString pressImgPath;


    //弹跳特效
    void zoom1(); //向下跳
    void zoom2(); //向上跳

    //重写按钮 按下 和 释放事件
    void mousePressEvent(QMouseEvent *e);

    void mouseReleaseEvent(QMouseEvent *e);

signals:

public slots:
};

#endif // MYPUSHBUTTON_H

mainwindow.ui
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值