Пример #1
0
void MainWindow::showEvent(QShowEvent *)
{
    if(check==1){
        for(int i=0;i<itemList.size();++i){

        }
        itemList.clear();
        scene->removeItem(restart);
        scene->removeItem(exit);
        scene->addItem(birdtmp);
        scene->addItem(tool);
        itemList.clear();
        //bird,obticle,target
        birdNumber=0;
        ScreenMode=0;
        checkBird23=0;
        target1->hit=0;
//        qDebug()<<x<<" hit=0 "<<y<<endl;
    }
    if(playtimes==0){
        // Timer
        connect(&timer,SIGNAL(timeout()),this,SLOT(birdHit()));
        connect(&timer,SIGNAL(timeout()),this,SLOT(tick()));
        connect(this,SIGNAL(quitGame()),this,SLOT(QUITSLOT()));
        timer.start(100/6);
    }
// Setting the QGraphicsScene
    scene = new QGraphicsScene(0,0,width(),ui->graphicsView->height());
    ui->graphicsView->setScene(scene);
    QImage bg;
    bg.load(":/img/bg.jpg");
    bg=bg.scaled(960,540);
    ui->graphicsView->scene()->setBackgroundBrush(bg);
    //bg-bird
    birdtmp=new QGraphicsPixmapItem();
    QPixmap bir;
    bir.load(":/img/bird1.png");
    bir=bir.scaled(50,50);
    birdtmp->setPixmap(bir);
    birdtmp->setPos(205,360);
    ui->graphicsView->scene()->addItem(birdtmp);
    //bg-tool
    tool=new QGraphicsPixmapItem();
    QPixmap tol;
    tol.load(":/img/tool.png");
    tol=tol.scaled(50,70);
    tool->setPixmap(tol);
    tool->setPos(210,380);
    ui->graphicsView->scene()->addItem(tool);
    //score
    score=new Score();
    this->scene->addItem(score);
    //dot
    for(int i=0;i<15;++i){
        dots[i]=new QGraphicsPixmapItem();
        scene->addItem(dots[i]);
    }
    // Create world
    world = new b2World(b2Vec2(0.0f, -9.8f));

    // Setting Size
    GameItem::setGlobalSize(QSizeF(32,18),size());
//    qDebug()<<size().width()<<" "<<size().height()<<endl;
    // Create ground (You can edit here)

    land1=new Land(3,1.5,5,3,QPixmap(":/img/GROUND.png").scaled(225,height()/6.0),world,scene);
    itemList.push_back(land1);
    land2=new Land(25,1.5,20,3,QPixmap(":/img/GROUND.png").scaled(600,height()/6.0),world,scene);
    itemList.push_back(land2);

    //create target
    target1=new Target(20.2f,6.4f,0.1f,&timer,QPixmap(":/img/target1.png").scaled(40,40),world,scene);
    itemList.push_back(target1);

    target2=new Target(26.0f,5.8f,0.1f,&timer,QPixmap(":/img/target1.png").scaled(40,40),world,scene);
    itemList.push_back(target1);

    target3=new Target(22.0f,5.0f,0.1f,&timer,QPixmap(":/img/target2.png").scaled(40,40),world,scene);
    itemList.push_back(target1);

    target4=new Target(29.0f,5.0f,0.1f,&timer,QPixmap(":/img/target2.png").scaled(40,40),world,scene);
    itemList.push_back(target1);

    //create obticle
    createobticle();

}
Пример #2
0
void Ball::doCollision()
{
    if(!stopped){

    if(birdDeadCounter>0)
    {
        --birdDeadCounter;
        if(birdDeadCounter<=0)
        {
            scene()->removeItem(removeBird);
        }
    }

    //check ob es eine Kollision gibt
    QList<QGraphicsItem*> collideList = scene()->collidingItems(this);


    //if(!(collideList.isEmpty()))
    for(int i=0; i<collideList.size(); ++i)
    {

        //finde Typ des kollidierenden Objectes heraus (BorderLine oder GroundMaterial)
        int sw = collideList.at(i)->type();

        switch(sw)
        {
            case CourtElement::borderline_type: //Kollision mit Spielfeldrand, abprallen
            {
                //Jetzt weis man, dass es eine Borderline ist, also caste QGraphicsItem* in BorderLine*
                BorderLine* borderline = static_cast<BorderLine*>(collideList.at(i));
                int angle = borderline->getAngle();

                if(canCollide<=0)
                {
                    emit soundPlay(SoundEngine::borderCollisionSound);

                    //angle ist relativ zu 3 Uhr (Uhrzeigersinn), rotation ist relativ zu 12 Uhr (Uhrzeigersinn)
                    setRotation(2*(angle+90.0-rotation()) + rotation());

                    speed = speed * borderline->getReflectionCoefficient();

                    canCollide = 3;
                }
            }
            break;

            case CourtElement::groundmaterial_type:
            {
                GroundMaterial* groundmaterial = static_cast<GroundMaterial*>(collideList.at(i));

                double maxspeed = groundmaterial->getMaxSpeed();
                double minspeed = groundmaterial->getMinSpeed();
                double friction = groundmaterial->getFrictionCoefficient();

                switch(groundmaterial->getMaterial())
                {
                    case GroundMaterial::water_material:
                    {
                        emit soundPlay(SoundEngine::waterSound);
                        speed -= friction*speed;

                        if(speed<minspeed)
                        {
                            speed = 0.0;

                            emit ballInWater();
                        }
                    }
                    break;

                    case GroundMaterial::nonNewtonian_material:

                        if(speed > minspeed)
                        {
                            speed -= speed*friction;
                        }
                        else if(speed<minspeed)
                        {
                            speed = 0.0;
                            emit soundPlay(SoundEngine::lavaSound);
                            emit ballInWater(); //Ist ja im Prinzip das gleiche wie bei Wasser
                        }
                    break;

                    case GroundMaterial::hole_material:

                        if(speed<maxspeed)
                        {
                            emit soundPlay(SoundEngine::cheeringSound);
                            speed = 0.0;
                            stopped=true;
                            emit ballInHole();
                        }

                    break;

                    case GroundMaterial::grass_material:
                    {
                       speed -= friction;

                        if(speed<minspeed)
                        {
                            speed = 0.0;
                            if(!stopped) emit ballStopped();
                            stopped = true;
                        }
                    }
                    break;

                    case GroundMaterial::sand_material:
                    {
                        emit(soundPlay(SoundEngine::sandSound));
                        speed -= 1.5*friction;

                        if(speed<minspeed)
                        {
                            speed = 0.0;
                            if(!stopped) emit ballStopped();
                            stopped = true;
                        }
                    }
                    break;

                    //unused materials:
                    /*
                    case GroundMaterial::speedUp_material:
                    case GroundMaterial::concrete_material:
                    case GroundMaterial::wood_material:
                    */

                    default: break;
                }
            }
            break;

            case 7: //Vogel abgeschossen! (7 = Pixmap)
            {
                if (isBirdDead == false)
                {
                    emit soundPlay(SoundEngine::birdHitSound);
                    emit birdHit();
                    isBirdDead = true;
                }
                removeBird = static_cast<QGraphicsPixmapItem*>(collideList.at(i));
                removeBird->setPixmap(birdDeadPicture);
                birdDeadCounter = 30;


            }
            break;

            default: break;
        }
    }
  }
}