void MainWindow::createBird(Bird_type type){ if(birdNum<=0){ return; } if(type == random){ srand(time(NULL)); int ran=rand()%4; switch (ran) { case 0: type=original; break; case 1: type=yellow; break; case 2: type=black; break; case 3: type=blue; break; default: break; } } nowHoldState=release; GameItem *birdie; switch (type) { case original: birdie = new Bird_original(3.3f,2.7f,0.5f,&timer,QPixmap(":/bird.png").scaled(height()/15.0,height()/15.0),world,scene); nowBird=original; break; case yellow: birdie = new Bird_yellow(3.3f,2.7f,0.5f,&timer,QPixmap(":/image/yellow.png").scaled(height()/15.0,height()/15.0),world,scene); birdie->inWaterD=-0.5; nowBird=yellow; break; case black: birdie = new Bird_black(3.3f,2.7f,0.5f,&timer,QPixmap(":/image/black.png").scaled(height()/15.0,height()/12.0),world,scene); nowBird=black; birdie->inWaterD=0; break; case blue: birdie = new Bird_blue(3.3f,2.7f,0.5f,&timer,QPixmap(":/image/blue.png").scaled(height()/15.0,height()/15.0),world,scene,birdList); nowBird=blue; birdie->inWaterD=-2; break; default: break; } birdie->getBody().SetGravityScale(0); birdList.push_back(birdie); birdNum--; mousePoint.setX(120); mousePoint.setY(580); pos_x=120;pos_y=580; timer_waiter.stop(); }
void MainWindow::handleContact() { // One hurt if(myContactListener->contactType == 0){ // set health GameItem* item = (GameItem*)myContactListener->bodyInfo_1->GetUserData(); float healthLeft = (item->getHealth()) - (myContactListener->impInfo); item->setHealth(healthLeft); // add score scorePoint += (myContactListener->impInfo)*3; // change image and delete if no health if(item->getType() == 'o'){ // for obstacle(boxes) if(healthLeft >=300 && healthLeft < 600){ QPixmap *Pixmap_box2 = new QPixmap(QPixmap(":/image/box_2.png").scaled(45,45)); item->g_pixmap.setPixmap(*Pixmap_box2); if(item->g_size == QSize(11,1)){ QPixmap *Pixmap_bar2 = new QPixmap(QPixmap(":/image/bar_2.png").scaled(165,15)); item->g_pixmap.setPixmap(*Pixmap_bar2); } } if(healthLeft >=0 && healthLeft < 300){ QPixmap *Pixmap_box3 = new QPixmap(QPixmap(":/image/box_3.png").scaled(45,45)); item->g_pixmap.setPixmap(*Pixmap_box3); if(item->g_size == QSize(11,1)){ QPixmap *Pixmap_bar3 = new QPixmap(QPixmap(":/image/bar_3.png").scaled(165,15)); item->g_pixmap.setPixmap(*Pixmap_bar3); } } if(healthLeft < 0){ scene->removeItem(&(item->g_pixmap)); item->getBody()->DestroyFixture(item->getBody()->GetFixtureList()); } } if(item->getType() == 'e'){ //for enemy(pigs) if(healthLeft >=300 && healthLeft < 600){ QPixmap *Pixmap_pig2 = new QPixmap(QPixmap(":/image/pig_2.png").scaled(45,45)); item->g_pixmap.setPixmap(*Pixmap_pig2); } if(healthLeft >=0 && healthLeft < 300){ QPixmap *Pixmap_pig3 = new QPixmap(QPixmap(":/image/pig_3.png").scaled(45,45)); item->g_pixmap.setPixmap(*Pixmap_pig3); } if(healthLeft < 0){ scene->removeItem(&(item->g_pixmap)); item->getBody()->DestroyFixture(item->getBody()->GetFixtureList()); pigLeft -= 1; if(pigLeft == 0){ mySceneManager->buildWinScene(); this->buildEndButton(); } } } } // Two hurt if(myContactListener->contactType == 1){ // set health GameItem* item1 = (GameItem*)myContactListener->bodyInfo_1->GetUserData(); GameItem* item2 = (GameItem*)myContactListener->bodyInfo_2->GetUserData(); float healthLeft1 = (item1->getHealth()) - (myContactListener->impInfo); item1->setHealth(healthLeft1); float healthLeft2 = (item2->getHealth()) - (myContactListener->impInfo); item2->setHealth(healthLeft2); // add score scorePoint += (myContactListener->impInfo)*6; // change image and delete if no health if(item1->getType() == 'o' && item2->getType() == 'e'){ if(healthLeft1 >=300 && healthLeft1 < 600){ QPixmap *Pixmap_box2 = new QPixmap(QPixmap(":/image/box_2.png").scaled(45,45)); item1->g_pixmap.setPixmap(*Pixmap_box2); if(item1->g_size == QSize(11,1)){ QPixmap *Pixmap_bar2 = new QPixmap(QPixmap(":/image/bar_2.png").scaled(165,15)); item1->g_pixmap.setPixmap(*Pixmap_bar2); } } if(healthLeft1 >=0 && healthLeft1 < 300){ QPixmap *Pixmap_box3 = new QPixmap(QPixmap(":/image/box_3.png").scaled(45,45)); item1->g_pixmap.setPixmap(*Pixmap_box3); if(item1->g_size == QSize(11,1)){ QPixmap *Pixmap_bar3 = new QPixmap(QPixmap(":/image/bar_3.png").scaled(165,15)); item1->g_pixmap.setPixmap(*Pixmap_bar3); } } if(healthLeft1 < 0){ scene->removeItem(&(item1->g_pixmap)); item1->getBody()->DestroyFixture(item1->getBody()->GetFixtureList()); } if(healthLeft2 >=300 && healthLeft2 < 600){ QPixmap *Pixmap_pig2 = new QPixmap(QPixmap(":/image/pig_2.png").scaled(45,45)); item2->g_pixmap.setPixmap(*Pixmap_pig2); } if(healthLeft2 >=0 && healthLeft2 < 300){ QPixmap *Pixmap_pig3 = new QPixmap(QPixmap(":/image/pig_3.png").scaled(45,45)); item2->g_pixmap.setPixmap(*Pixmap_pig3); } if(healthLeft2 < 0){ scene->removeItem(&(item2->g_pixmap)); item2->getBody()->DestroyFixture(item2->getBody()->GetFixtureList()); pigLeft -= 1; if(pigLeft == 0){ mySceneManager->buildWinScene(); this->buildEndButton(); } } } if(item2->getType() == 'o' && item1->getType() == 'e'){ if(healthLeft2 >=300 && healthLeft2 < 600){ QPixmap *Pixmap_box2 = new QPixmap(QPixmap(":/image/box_2.png").scaled(45,45)); item2->g_pixmap.setPixmap(*Pixmap_box2); if(item2->g_size == QSize(11,1)){ QPixmap *Pixmap_bar2 = new QPixmap(QPixmap(":/image/bar_2.png").scaled(165,15)); item2->g_pixmap.setPixmap(*Pixmap_bar2); } } if(healthLeft2 >=0 && healthLeft2 < 300){ QPixmap *Pixmap_box3 = new QPixmap(QPixmap(":/image/box_3.png").scaled(45,45)); item2->g_pixmap.setPixmap(*Pixmap_box3); if(item2->g_size == QSize(11,1)){ QPixmap *Pixmap_bar3 = new QPixmap(QPixmap(":/image/bar_3.png").scaled(165,15)); item2->g_pixmap.setPixmap(*Pixmap_bar3); } } if(healthLeft2 < 0){ scene->removeItem(&(item2->g_pixmap)); item2->getBody()->DestroyFixture(item2->getBody()->GetFixtureList()); } if(healthLeft1 >=300 && healthLeft1 < 600){ QPixmap *Pixmap_pig2 = new QPixmap(QPixmap(":/image/pig_2.png").scaled(45,45)); item1->g_pixmap.setPixmap(*Pixmap_pig2); } if(healthLeft1 >=0 && healthLeft1 < 300){ QPixmap *Pixmap_pig3 = new QPixmap(QPixmap(":/image/pig_3.png").scaled(45,45)); item1->g_pixmap.setPixmap(*Pixmap_pig3); } if(healthLeft1 < 0){ scene->removeItem(&(item1->g_pixmap)); item1->getBody()->DestroyFixture(item1->getBody()->GetFixtureList()); pigLeft -= 1; if(pigLeft == 0){ mySceneManager->buildWinScene(); this->buildEndButton(); } } } } }