Example #1
0
int main(int argc, char *argv[])
{
	Animal animal;
	Bird bird, mutantBird;
	Cat cat;
	Crow crow;

	newAnimal(&animal);
	animal.setName(&animal, animalName);

	newBird(&bird);
	((Animal *) &bird)->setName((Animal *) &bird, birdName);

	newBird(&mutantBird);
	((Animal *) &mutantBird)->setName((Animal *) &mutantBird, mutantBirdName);
	((Animal *) &mutantBird)->setLegs((Animal *) &mutantBird, 3);

	newCat(&cat);
	((Animal *) &cat)->setName((Animal *) &cat, catsName);

	newCrow(&crow);
	((Animal *) &crow)->setName((Animal *) &crow, crowName);

	test_animal(&animal);
	test_animal((Animal *) &bird);
	test_animal((Animal *) &mutantBird);
	test_animal((Animal *) &cat);
	test_animal((Animal *) &crow);

	test_cat(&cat);

	test_bird(&bird);
	test_bird((Bird *) &mutantBird);
	test_bird((Bird *) &crow);

	test_crow(&crow);

	return 0;
}
Example #2
0
bool MainWindow::eventFilter(QObject *, QEvent *event){
    QMouseEvent *mouseEvent = (QMouseEvent*) event;
    if(event->type() == QEvent::MouseButtonPress){
        if(mouseEvent->pos().x()<300){ //滑鼠位在此範圍才算準備發射
            isPressed = true;
            if(isFired==true&&skillUsed==false&&done==false&&birdnum<4) //收到發射後使用技能
                skillUsed = true;
            if(done==true) //已使用技能
                done=false;
        }
        //按下重玩
        if(mouseEvent->pos().x()>=550&&mouseEvent->pos().x()<650&&mouseEvent->pos().y()>=30&&mouseEvent->pos().y()<130){
            clear();
            startgame();
        }
        //按下離開
        if(mouseEvent->pos().x()>=800&&mouseEvent->pos().x()<900&&mouseEvent->pos().y()>=30&&mouseEvent->pos().y()<130){
            close();
        }
    }
    if(event->type() == QEvent::MouseMove){
        if(isPressed==true&&isFired==false&&skillUsed==false&&done==false){
            float x = mouseEvent->pos().x();
            float y = mouseEvent->pos().y();
            float yy = height() - mouseEvent->pos().y() + 30; //y需顛倒
            bird[birdnum]->setPos(x/30.0,yy/30.0);
            dx = x-init_x*30; //轉成像素相減
            dy = -(y-(height()-init_y*30)); //最後再顛倒
        }
    }
    if(event->type() == QEvent::MouseButtonRelease){
        if(isPressed==true&&isFired==false&&skillUsed==false&&done==false&&birdnum<4){ //發射
            //根據與準備位置的相對距離決定速度
            float vx=-(dx/7.0);
            float vy=-(dy/10.0);
            bird[birdnum]->setDynamic(); //設為動態
            bird[birdnum]->setVelocity(vx,vy);
            isPressed = false;
            isFired = true;
            if(birdnum<3) //尚未到最後一隻鳥,0.3秒後將下一隻鳥移到準備位置
                QTimer::singleShot(300,this,SLOT(newBird()));
            std::cout<<"shoot"<<std::endl;
        }

        if(isFired==true&&skillUsed==true&&birdnum<4){ //收到使用技能
            if(birdnum==0&&done==false){ //技能:無
                done=true;
                birdnum++;
                isFired=false;
                skillUsed=false;
                std::cout<<"0 skillused"<<std::endl;
            }
            if(birdnum==1&&done==false){ //技能:轉成當前反向速度的10倍
                float vx = bird[birdnum]->getVelocity().x;
                float vy = bird[birdnum]->getVelocity().x;
                bird[birdnum]->setVelocity(-vx*10,-vy*10);
                done=true;
                birdnum++;
                isFired=false;
                skillUsed=false;
                std::cout<<"1 skillused"<<std::endl;
            }
            if(birdnum==2&&done==false){ //技能:直線衝刺
                bird[birdnum]->setVelocity(100,0);
                done=true;
                birdnum++;
                isFired=false;
                skillUsed=false;
                std::cout<<"2 skillused"<<std::endl;
            }
            if(birdnum==3&&done==false){ //技能:快速旋轉與墜落
                bird[birdnum]->rotate(200);
                bird[birdnum]->setVelocity(0,-50);
                done=true;
                isFired=false;
                skillUsed=false;
                QTimer::singleShot(1000,this,SLOT(isAllDone()));
                std::cout<<"3 skillused"<<std::endl;
            }
        }
    }
    return false;
}