#include "TjdUiAppBirdFly.h" #include "TjdUiMultiLanguageExt.h" #include "common/image_cache_manager.h" #include "sys_config.h" #include "dock/input_device.h" #include "NativeAbility.h" #include "sql_bt.h" #define ENABLE_PRINT_INFO 1 #if ENABLE_PRINT_INFO #define static_print_info(...) sys_ui_log_i(__VA_ARGS__) // 一般信息打印宏控制 #define static_print_warn(...) sys_ui_log_w(__VA_ARGS__) // 警告信息打印一般常开 #define static_print_error(...) sys_ui_log_e(__VA_ARGS__) // 错误信息打印一般常开 #define static_print_debug(...) sys_ui_log_d(__VA_ARGS__) // 调试信息打印 #else #define static_print_info(...) #define static_print_warn(...) #define static_print_error(...) #define static_print_debug(...) #endif #define BIRD_ANIM_IMAGE_MAX_NUM 5 #define BIRD_DIE_ANIM_IMAGE_MAX_NUM 10 #define BACKGROUND_MAX_NUM 2 #define GOLD_ANIM_MAX_NUM 8 namespace TJD{ static GameBirdFlyView *g_pv_BirdFlyView = nullptr; static GameBirdFlyAnimotorCallback *g_pv_BirdFlyAnimotorCallback = nullptr; static int16_t HorizontalCenter(int16_t width) { return (OHOS::HORIZONTAL_RESOLUTION - width) / 2; } static inline int16_t VerticalCenter(int16_t height, int16_t parentHeight) { return (parentHeight - height) / 2; } OHOS::ImageInfo * imageBGInfo = nullptr; static uint32_t history_score = 0; static void InitLabel(OHOS::UILabel &label, uint8_t size, int16_t y,const char *text) { label.SetFont(TJD_VECTOR_FONT_FILENAME, size); label.SetText(text); label.SetLineBreakMode(OHOS::UILabel::LINE_BREAK_ADAPT); label.SetAlign(OHOS::TEXT_ALIGNMENT_CENTER, OHOS::TEXT_ALIGNMENT_CENTER); label.SetPosition(HorizontalCenter(label.GetWidth()), y); } GameBirdFlyView::GameBirdFlyView() { g_pv_BirdFlyView = this; // if(mainConner_ == nullptr){ // mainConner_ = new OHOS::UIViewGroup(); // } if(keyInputListener_ == nullptr){ keyInputListener_ = new BirdFlyViewKeyInputListener(); } TjdUiCommonOnKeyListener::GetInstance()->SetOnKeyActListener(keyInputListener_, KeyModelType::APP_KEY_TYPE); OHOS::ImageCacheManager::GetInstance().LoadAllInMultiRes(IMAGE_BIN_PATH); SetPosition(-466, 0, 466 * 3, OHOS::VERTICAL_RESOLUTION); SetStyle(STYLE_BACKGROUND_OPA, 0); SetViewId("GameBirdFlyView"); if(initGameView_ == nullptr){ initGameView_ = new OHOS::UIViewGroup(); } initGameView_->SetPosition(466, 0, OHOS::HORIZONTAL_RESOLUTION, OHOS::VERTICAL_RESOLUTION); initGameView_->SetViewId("InitGameView"); initGameView_->SetVisible(false); if(onClickListenser_ == nullptr){ onClickListenser_ = new GameBirdFlyViewOnClickListenser(); } InitGameView(); OHOS::RootView::GetInstance()->Add(this); Add(initGameView_); // Add(startGameView_); // Add(startGameIngView_); // Add(stopGameView_); // OHOS::RootView::GetInstance()->Add(&initGameView_); // OHOS::RootView::GetInstance()->Add(&startGameView_); // OHOS::RootView::GetInstance()->Add(&startGameIngView_); } GameBirdFlyView::~GameBirdFlyView() { g_pv_BirdFlyView = nullptr; OHOS::ImageCacheManager::GetInstance().UnloadAllInMultiRes(IMAGE_BIN_PATH); OHOS::RootView::GetInstance()->Remove(this); RemoveAll(); if (initGameView_) initGameView_->RemoveAll(); if(startGameView_) startGameView_->RemoveAll(); if(startGameIngView_) startGameIngView_->RemoveAll(); if(stopGameView_) stopGameView_->RemoveAll(); if (startGameIngView_) { if(startGameIngView_->GetOnClickListener() != nullptr){ delete startGameIngView_->GetOnClickListener(); startGameIngView_->GetOnClickListener() = nullptr; } } if(initGameView_ != nullptr){ delete initGameView_; initGameView_ = nullptr; } if(startGameView_ != nullptr){ delete startGameView_; startGameView_ = nullptr; } if(startGameIngView_ != nullptr){ delete startGameIngView_; startGameIngView_ = nullptr; } if(stopGameView_ != nullptr){ delete stopGameView_; stopGameView_ = nullptr; } if(bgGameView != nullptr){ delete bgGameView; bgGameView = nullptr; } if(bgGameView1 != nullptr){ delete bgGameView1; bgGameView1 = nullptr; } if(birdIcon != nullptr){ delete birdIcon; birdIcon = nullptr; } if(scoreLabel != nullptr){ delete scoreLabel; scoreLabel = nullptr; } if(startGameView != nullptr){ delete startGameView; startGameView = nullptr; } if(titleLabel != nullptr){ delete titleLabel; titleLabel = nullptr; } if(scoreLabel1 != nullptr){ delete scoreLabel1; scoreLabel1 = nullptr; } if(stopView_bg != nullptr){ delete stopView_bg; stopView_bg = nullptr; } if(stopView_block != nullptr){ delete stopView_block; stopView_block = nullptr; } if(stopView_icon != nullptr){ delete stopView_icon; stopView_icon = nullptr; } if(stopView_start != nullptr){ delete stopView_start; stopView_start = nullptr; } if(stopView_return != nullptr){ delete stopView_return; stopView_return = nullptr; } if(animator != nullptr){ animator->Stop(); delete animator; animator = nullptr; } if(animotorCallback != nullptr){ delete animotorCallback; animotorCallback = nullptr; } for(int i = 0; i < BARRIER_MAX_NUM; i++){ if(barrier[i] != nullptr){ delete barrier[i]; barrier[i] = nullptr; } } for(int i = 0; i < RESOURCE_MAX_NUM; i++){ if(resource[i] != nullptr){ delete resource[i]; resource[i] = nullptr; } } for(int i = 0; i < 2; i++){ if(backGround[i] != nullptr){ delete backGround[i]; backGround[i] = nullptr; } } if(bird != nullptr){ delete bird; bird = nullptr; } if(timerToStart_ != nullptr){ timerToStart_->Stop(); delete timerToStart_; timerToStart_ = nullptr; } if(timeAnimator != nullptr){ timeAnimator->Stop(); delete timeAnimator; timeAnimator = nullptr; } if(onClickListener_ != nullptr){ delete onClickListener_; onClickListener_ = nullptr; } if(onClickListenser_ != nullptr){ delete onClickListenser_; onClickListenser_ = nullptr; } if(keyInputListener_ != nullptr){ delete keyInputListener_; keyInputListener_ = nullptr; } } void GameBirdFlyView::Show() { initGameView_->SetVisible(true); SetVisible(true); } void GameBirdFlyView::InitGameView() { if(bgGameView == nullptr){ bgGameView = new OHOS::UIImageView(); } OHOS::ImageInfo *descRes = nullptr; descRes = OHOS::ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_GAME_BIRDFLY_BIRDFLY_BJ, IMAGE_BIN_PATH); if(descRes != nullptr){ bgGameView->SetSrc(descRes); } bgGameView->SetPosition(0, 0); bgGameView->SetVisible(true); if(birdIcon == nullptr){ birdIcon = new OHOS::UIImageView(); } descRes = OHOS::ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_GAME_BIRDFLY_BIRDFLY_START, IMAGE_BIN_PATH); if(descRes != nullptr){ birdIcon->SetSrc(descRes); } birdIcon->SetPosition(114, 66); birdIcon->SetVisible(true); if(scoreLabel == nullptr){ scoreLabel = new OHOS::UILabel(); } char scoreText[36] = {0}; history_score = sql_bt_get_game_score(GAME_BIRDFLY); sprintf(scoreText, "历史最高分: %d分\n", history_score); InitLabel(*scoreLabel, 34, 284, scoreText); scoreLabel->SetVisible(true); if(startGameView == nullptr){ startGameView = new OHOS::UIButton(); } descRes = OHOS::ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_GAME_BIRDFLY_BIRDFLY_CONFIRM, IMAGE_BIN_PATH); if(descRes != nullptr){ startGameView->SetImageSrc(descRes,descRes); } // startGameView->SetOnClickListener(new GameBirdFlyViewOnClickListenser()); startGameView->SetOnClickListener(onClickListenser_); startGameView->SetViewId("StartGameView"); startGameView->SetPosition(180, 347, 106, 106); startGameView->SetStyleForState(STYLE_BORDER_COLOR, 0xff000000, UIButton::PRESSED); startGameView->SetTouchable(true); startGameView->SetVisible(true); initGameView_->Add(bgGameView); initGameView_->Add(birdIcon); initGameView_->Add(scoreLabel); initGameView_->Add(startGameView); // initGameView_->SetPosition(0, 0, OHOS::HORIZONTAL_RESOLUTION, OHOS::VERTICAL_RESOLUTION); initGameView_->SetViewId("InitGameView"); initGameView_->SetVisible(true); } OHOS::ImageAnimatorInfo g_pv_waittimeInfo_[3] = {0}; void TimerToStartCallback(void *arg) { if(GameBirdFlyView::GetInstance()->GetTimeAnimator()->GetState() == OHOS::Animator::STOP){ GameBirdFlyView::GetInstance()->GetTimerToStart()->Stop(); GameBirdFlyView::GetInstance()->StartGameIng(); } } void TimerToBirdCallback(void *arg) { GameBirdFlyView * gameBirdFlyView = static_cast(arg); if(gameBirdFlyView->GetBird()->GetBirdStatue() == BIRDFLY_NORMAL){ gameBirdFlyView->GetBird()->MoveDownBird(); }else if(gameBirdFlyView->GetBird()->GetBirdStatue() == BIRDFLY_PAUSE){ gameBirdFlyView->GetBird()->GetAnimator()->Pause(); }else if(gameBirdFlyView->GetBird()->GetBirdStatue() == BIRDFLY_RESUME){ gameBirdFlyView->GetBird()->GetAnimator()->Resume(); } } void TimerToBackgroundCallback(void *arg) { GameBirdFlyView * gameBirdFlyView = static_cast(arg); gameBirdFlyView->GetBackGround(0)->MoveBackGround(); gameBirdFlyView->GetBackGround(1)->MoveBackGround(); } void GameBirdFlyView::StartGame() { if(startGameView_ == nullptr){ startGameView_ = new OHOS::UIViewGroup(); } startGameView_->SetPosition(466, 0, OHOS::HORIZONTAL_RESOLUTION, OHOS::VERTICAL_RESOLUTION); startGameView_->SetViewId("StartGameView"); startGameView_->SetVisible(false); Add(startGameView_); initGameView_->SetVisible(false); startGameView_->SetVisible(true); if(bgGameView1 == nullptr){ bgGameView1 = new OHOS::UIImageView(); } OHOS::ImageInfo *descRes = nullptr; descRes = OHOS::ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_GAME_BIRDFLY_BIRDFLY_BJ, IMAGE_BIN_PATH); if(descRes != nullptr){ bgGameView1->SetSrc(descRes); } bgGameView1->SetPosition(0, 0); bgGameView1->SetVisible(true); // startGameView_->SetPosition(0, 0, OHOS::HORIZONTAL_RESOLUTION, OHOS::VERTICAL_RESOLUTION); const int WaitAnimatorId[3] = {IMG_GAME_3, IMG_GAME_2, IMG_GAME_1}; auto &imgManager = OHOS::ImageCacheManager::GetInstance(); for (int i = 0; i < 3; i++) { g_pv_waittimeInfo_[i].imageInfo = imgManager.LoadOneInMultiRes(WaitAnimatorId[i], IMAGE_COMMON_BIN_PATH); g_pv_waittimeInfo_[i].pos = {166, 87}; g_pv_waittimeInfo_[i].width = 132; g_pv_waittimeInfo_[i].height = 184; g_pv_waittimeInfo_[i].imageType = OHOS::IMG_SRC_IMAGE_INFO; } // Resize(OHOS::HORIZONTAL_RESOLUTION, OHOS::VERTICAL_RESOLUTION); /* 动画页面不允许滑动 */ startGameView_->SetIntercept(true); startGameView_->SetTouchable(false); if(timeAnimator == nullptr){ timeAnimator = new OHOS::UIImageAnimatorView(); } timeAnimator->SetPosition(150, 113, 166, 164); timeAnimator->SetImageAnimatorSrc(g_pv_waittimeInfo_, 3, 1000); timeAnimator->SetTimeOfPause(1000); timeAnimator->SetRepeat(false); if(tipLabel == nullptr){ tipLabel = new OHOS::UILabel(); } InitLabel(*tipLabel, 34, 304, "点击屏幕控制小鸟飞行\n"); tipLabel->SetVisible(true); startGameView_->Add(bgGameView1); startGameView_->Add(timeAnimator); startGameView_->Add(tipLabel); timeAnimator->Start(); if (timerToStart_ == nullptr) { static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); timerToStart_ = new OHOS::GraphicTimer(1000, TimerToStartCallback, this, true); } static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); timerToStart_->Start(); // bird.InitBird(0, 233, 98, 83, BIRDFLY_NORMAL); // bird.DrawBird(); // if (timerToBird_ == nullptr) { // timerToBird_ = new OHOS::GraphicTimer(30, TimerToBirdCallback, this, true); // } // if (timerToBackground_ == nullptr) { // timerToBackground_ = new OHOS::GraphicTimer(10, TimerToBackgroundCallback, this, true); // } // OHOS::ImageCacheManager::GetInstance().UnloadOneInMultiRes(IMG_GAME_BIRDFLY_BIRDFLY_BJ, IMAGE_BIN_PATH); // OHOS::ImageCacheManager::GetInstance().UnloadOneInMultiRes(IMG_GAME_BIRDFLY_BIRDFLY_START, IMAGE_BIN_PATH); // OHOS::ImageCacheManager::GetInstance().UnloadOneInMultiRes(IMG_GAME_BIRDFLY_BIRDFLY_CONFIRM, IMAGE_BIN_PATH); // imageBGInfo = OHOS::ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_GAME_BIRDFLY_BIRD_BG2, IMAGE_BIN_PATH); } OHOS::ImageAnimatorInfo g_pv_goldInfo_[8] = {0}; void GameBirdFlyView::StartGameIng() { static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); if(startGameIngView_ == nullptr){ startGameIngView_ = new OHOS::UIViewGroup(); } startGameIngView_->SetPosition(0, 0, 466 * 3 , OHOS::VERTICAL_RESOLUTION); startGameIngView_->SetViewId("StartGameIngView"); startGameIngView_->SetVisible(false); Add(startGameIngView_); startGameView_->SetVisible(false); if(pause == nullptr){ pause = new OHOS::UIButton(); } ImageInfo * descRes = OHOS::ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_GAME_BIRDFLY_PAUSE, IMAGE_BIN_PATH); if(descRes != nullptr){ pause->SetImageSrc(descRes,descRes); } // pause->SetOnClickListener(new GameBirdFlyViewOnClickListenser()); pause->SetOnClickListener(onClickListenser_); pause->SetViewId("PauseButton"); pause->SetPosition(617, 115, 164, 164); pause->SetStyleForState(STYLE_BORDER_COLOR, 0xff000000, UIButton::PRESSED); pause->SetTouchable(true); pause->SetVisible(false); for(int i = 0; i < 2; i++){ if(backGround[i] == nullptr){ backGround[i] = static_cast(new BackGround(466 * (i + 1),0,466,466)); } backGround[i]->DrawBackGround(); backGround[i]->GetBackGroundView()->SetVisible(true); startGameIngView_->Add(backGround[i]->GetBackGroundView()); } static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); for(int i = 0; i < BARRIER_MAX_NUM; i++){ // srand(time(nullptr)); // int score = rand() % 1000; int resId = (i % 2 == 0 ? IMG_GAME_BIRDFLY_PILLAR_BOTTOM : IMG_GAME_BIRDFLY_PILLAR_TOP); if(barrier[i] == nullptr){ barrier[i] = static_cast(new Barrier(932 + i * 100,resId)); } barrier[i]->DrawBarrier(); barrier[i]->GetBarrierView()->SetVisible(true); startGameIngView_->Add(barrier[i]->GetBarrierView()); } static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); for(int i = 0; i < GOLD_ANIM_MAX_NUM; i++){ const int GoldAnimatorId[GOLD_ANIM_MAX_NUM] = { IMG_GAME_BIRDFLY_GOLD_0, IMG_GAME_BIRDFLY_GOLD_1, IMG_GAME_BIRDFLY_GOLD_2, IMG_GAME_BIRDFLY_GOLD_3, IMG_GAME_BIRDFLY_GOLD_4, IMG_GAME_BIRDFLY_GOLD_5, IMG_GAME_BIRDFLY_GOLD_6, IMG_GAME_BIRDFLY_GOLD_7}; g_pv_goldInfo_[i].imageInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(GoldAnimatorId[i], IMAGE_BIN_PATH); g_pv_goldInfo_[i].pos = {0, 233}; g_pv_goldInfo_[i].width = 45; g_pv_goldInfo_[i].height = 38; g_pv_goldInfo_[i].imageType = OHOS::IMG_SRC_IMAGE_INFO; } static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); for(int i = 0; i < RESOURCE_MAX_NUM; i++){ // srand(time(nullptr)); // int randNum = rand() % 10; // int resId = (randNum <= 5 ? 0 : (randNum == 6 ? IMG_GAME_BIRDFLY_BIRD_RIGHT : (randNum == 7 ? IMG_GAME_BIRDFLY_BIRD_UP : (randNum == 8 ? IMG_GAME_BIRDFLY_BIRD_UP_DOWN : IMG_GAME_BIRDFLY_BIRD_DOWN)))); static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); if(resource[i] == nullptr){ resource[i] = static_cast(new Resource(932 + i * 50,RESOURCE_GOLD,0)); } resource[i]->DrawResource(); if(resource[i]->GetResourceView() != nullptr){ resource[i]->GetResourceView()->SetVisible(true); startGameIngView_->Add(resource[i]->GetResourceView()); }else if(resource[i]->GetResourceAnimator() != nullptr){ resource[i]->GetResourceAnimator()->SetVisible(true); startGameIngView_->Add(resource[i]->GetResourceAnimator()); } } startGameIngView_->SetIntercept(true); startGameIngView_->SetTouchable(true); // startGameIngView_->SetOnClickListener(onClickListenser_); startGameIngView_->SetOnClickListener(new GameStartIngOnClickListenser()); if(bird == nullptr){ bird = new Bird(); } bird->InitBird(500, 233, 98, 83, BIRDFLY_NORMAL); bird->DrawBird(); static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); // timerToBird_->Start(); // timerToBackground_->Start(); if(animotorCallback == nullptr){ static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); animotorCallback = new GameBirdFlyAnimotorCallback(bird, backGround[0], backGround[1], barrier, resource ,startGameIngView_, BIRDFLY_NORMAL); } if(animator == nullptr){ static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); animator = new OHOS::Animator(animotorCallback, this, 500, true); } static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); bird->GetAnimator()->SetVisible(true); static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); startGameIngView_->Add(bird->GetAnimator()); static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); animator->Start(); static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); startGameIngView_->SetVisible(true); startGameIngView_->Add(pause); // Invalidate(); } void GameBirdFlyView::PauseGame() { if (animator == nullptr) return ; animator->Pause(); bird->GetAnimator()->Pause(); pause->SetVisible(true); } void GameBirdFlyView::ResumeGame() {} void GameBirdFlyView::RestartGame() { bird->SetX(500); bird->SetY(233); bird->SetPosition(); bird->DrawBird(); score = 0; for(int i = 0; i < BARRIER_MAX_NUM; i++){ if(barrier[i] != nullptr){ srand(time(nullptr)); int randNum = rand() % 50 + 30 * i; barrier[i]->SetX(932 + randNum); barrier[i]->SetPosition(); } } static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); for(int i = 0; i < RESOURCE_MAX_NUM; i++){ if(resource[i] != nullptr){ srand(time(nullptr)); int randNum = rand() % 50 + 30 * i; resource[i]->SetX(932 + randNum); resource[i]->SetPosition(); } } static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); GameBirdFlyAnimotorCallback::GetInstance()->Setevent(BIRDFLY_NORMAL); GameBirdFlyView::GetInstance()->GetAnimator()->Start(); // GameBirdFlyView::GetInstance()->GetTimerToStart()->Start(); } void GameBirdFlyView::StopGame() { if(stopGameView_ == nullptr){ stopGameView_ = new OHOS::UIViewGroup(); } stopGameView_->SetPosition(466, 0, OHOS::HORIZONTAL_RESOLUTION, OHOS::VERTICAL_RESOLUTION); stopGameView_->SetViewId("StopGameView"); stopGameView_->SetVisible(false); Add(stopGameView_); if(stopView_bg == nullptr){ stopView_bg = new OHOS::UIImageView(); } OHOS::ImageInfo *descRes = nullptr; descRes = OHOS::ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_GAME_BIRDFLY_BIRDFLY_BJ, IMAGE_BIN_PATH); if(descRes != nullptr){ stopView_bg->SetSrc(descRes); } stopView_bg->SetPosition(0, 0); stopView_bg->SetVisible(true); if(titleLabel == nullptr){ titleLabel = new OHOS::UILabel(); } InitLabel(*titleLabel, 46, 51, "GAME OVER\n"); titleLabel->SetVisible(true); if(stopView_block == nullptr){ stopView_block = new OHOS::UIImageView(); } descRes = OHOS::ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_GAME_BIRDFLY_BIRDFLY_OVER_BJ, IMAGE_BIN_PATH); if(descRes != nullptr){ stopView_block->SetSrc(descRes); } stopView_block->SetPosition(81, 102); stopView_block->SetVisible(true); if(stopView_icon == nullptr){ stopView_icon = new OHOS::UIImageView(); } descRes = OHOS::ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_GAME_BIRDFLY_BIRDFLY_OVER_ICON, IMAGE_BIN_PATH); if(descRes != nullptr){ stopView_icon->SetSrc(descRes); } stopView_icon->SetPosition(120, 143); stopView_icon->SetVisible(true); if(scoreLabel1 == nullptr){ scoreLabel1 = new OHOS::UILabel(); } char scoreText[16] = {0}; sprintf(scoreText, "得分: %d分\n", score); sql_bt_set_game_score(GAME_BIRDFLY, &score); InitLabel(*scoreLabel1, 28, 159, scoreText); scoreLabel1->SetPosition(198,155); scoreLabel1->SetStyle(STYLE_TEXT_COLOR, Color::Black().full); scoreLabel1->SetVisible(true); if(stopView_start == nullptr){ stopView_start = new OHOS::UIButton(); } descRes = OHOS::ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_GAME_BIRDFLY_BIRDFLY_CONTINUE, IMAGE_BIN_PATH); if(descRes != nullptr){ stopView_start->SetImageSrc(descRes,descRes); } stopView_start->SetOnClickListener(onClickListenser_); // stopView_start->SetOnClickListener(new GameBirdFlyViewOnClickListenser()); stopView_start->SetViewId("ContinueButton"); stopView_start->SetPosition(112, 216, 242, 76); stopView_start->SetStyleForState(STYLE_BORDER_COLOR, 0xff000000, UIButton::PRESSED); stopView_start->SetTouchable(true); stopView_start->SetVisible(true); if(stopView_return == nullptr){ stopView_return = new OHOS::UIButton(); } descRes = OHOS::ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_GAME_BIRDFLY_BIRDFLY_RETURE, IMAGE_BIN_PATH); if(descRes != nullptr){ stopView_return->SetImageSrc(descRes,descRes); } stopView_return->SetOnClickListener(onClickListenser_); stopView_return->SetViewId("ReturnButton"); stopView_return->SetPosition(112, 296, 242, 76); stopView_return->SetStyleForState(STYLE_BORDER_COLOR, 0xff000000, UIButton::PRESSED); stopView_return->SetTouchable(true); stopView_return->SetVisible(true); stopGameView_->Add(stopView_bg); stopGameView_->Add(titleLabel); stopGameView_->Add(stopView_block); stopGameView_->Add(stopView_icon); stopGameView_->Add(scoreLabel1); stopGameView_->Add(stopView_start); stopGameView_->Add(stopView_return); // initGameView_->SetPosition(0, 0, OHOS::HORIZONTAL_RESOLUTION, OHOS::VERTICAL_RESOLUTION); stopGameView_->SetViewId("StopGameView"); stopGameView_->SetVisible(true); } bool GameBirdFlyView::CheckBirdRamBarriers() { //检测小鸟是否撞到障碍物 for(int i = 0; i < BARRIER_MAX_NUM; i++){ if(barrier[i]->GetX() <= bird->GetX() + bird->GetWidth() && barrier[i]->GetX() + barrier[i]->GetWidth() >= bird->GetX()){ if(barrier[i]->GetY() <= bird->GetY() + bird->GetHeight() && barrier[i]->GetY() + barrier[i]->GetHeight() >= bird->GetY()){ return true; } } } return false; } bool GameBirdFlyView::CheckBirdRamResource() { //检测小鸟是否撞到资源 for(int i = 0; i < RESOURCE_MAX_NUM; i++){ if(resource[i]->GetX() <= bird->GetX() + bird->GetWidth() && resource[i]->GetX() + resource[i]->GetWidth() >= bird->GetX()){ if(resource[i]->GetY() <= bird->GetY() + bird->GetHeight() && resource[i]->GetY() + resource[i]->GetHeight() >= bird->GetY()){ // resource[i]->ReleaseResource(); return true; } } } return false; } GameBirdFlyAnimotorCallback *GameBirdFlyAnimotorCallback::GetInstance(){return g_pv_BirdFlyAnimotorCallback;} GameBirdFlyView *GameBirdFlyView::GetInstance() { return g_pv_BirdFlyView; } BackGround *GameBirdFlyView::GetBackGround(uint8_t index) { if(index >= BACKGROUND_MAX_NUM) return nullptr; return backGround[index]; } #pragma region Bird Bird::Bird() {} Bird::~Bird() { if(animator != nullptr){ animator->Stop(); delete animator; animator = nullptr; } } void Bird::InitBird(int x, int y, int width, int height, uint8_t statue) { this->x = x; this->y = y; this->width = width; this->height = height; this->statue = statue; } void Bird::MoveDownBird() { if(statue == BIRDFLY_NORMAL){ y += 2; if(y > OHOS::VERTICAL_RESOLUTION - height){ // y = OHOS::VERTICAL_RESOLUTION - height; SetBirdStatue(BIRDFLY_DIE); GameBirdFlyAnimotorCallback::GetInstance()->Setevent(BIRDFLY_DIE); } SetPosition(); } } void Bird::MoveUpBird() { if(statue == BIRDFLY_NORMAL){ y -= 50; if(y < 0){ y = 0; } SetPosition(); } } OHOS::ImageAnimatorInfo g_pv_birdInfo_[5] = {0}; OHOS::ImageAnimatorInfo g_pv_birdDieInfo_[10] = {0}; void Bird::DrawBird() { static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); const int BirdAnimatorId[BIRD_ANIM_IMAGE_MAX_NUM] = { IMG_GAME_BIRDFLY_BIRD_FLY0, IMG_GAME_BIRDFLY_BIRD_FLY1, IMG_GAME_BIRDFLY_BIRD_FLY2, IMG_GAME_BIRDFLY_BIRD_FLY3, IMG_GAME_BIRDFLY_BIRD_FLY4}; // auto &imgManager = OHOS::ImageCacheManager::GetInstance(); for (int i = 0; i < BIRD_ANIM_IMAGE_MAX_NUM; i++) { g_pv_birdInfo_[i].imageInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(BirdAnimatorId[i], IMAGE_BIN_PATH); g_pv_birdInfo_[i].pos = {0, 233}; g_pv_birdInfo_[i].width = 98; g_pv_birdInfo_[i].height = 83; g_pv_birdInfo_[i].imageType = OHOS::IMG_SRC_IMAGE_INFO; } if(animator == nullptr){ animator = new OHOS::UIImageAnimatorView(); } animator->SetPosition(g_pv_birdInfo_->pos.x, g_pv_birdInfo_->pos.y); animator->Resize(g_pv_birdInfo_->width, g_pv_birdInfo_->height); animator->SetImageAnimatorSrc(g_pv_birdInfo_, BIRD_ANIM_IMAGE_MAX_NUM, 500); animator->SetSizeFixed(true); animator->Start(); } void Bird::ChangeBirdDieAnimator() { if(animator != nullptr){ animator->Pause(); const int BirdDieAnimatorId[BIRD_DIE_ANIM_IMAGE_MAX_NUM] = { IMG_GAME_BIRDFLY_BIRD_FALL0, IMG_GAME_BIRDFLY_BIRD_FALL1, IMG_GAME_BIRDFLY_BIRD_FALL2, IMG_GAME_BIRDFLY_BIRD_FALL3, IMG_GAME_BIRDFLY_BIRD_FALL4,IMG_GAME_BIRDFLY_BIRD_FALL5, IMG_GAME_BIRDFLY_BIRD_FALL6, IMG_GAME_BIRDFLY_BIRD_FALL7, IMG_GAME_BIRDFLY_BIRD_FALL8,IMG_GAME_BIRDFLY_BIRD_FALL9}; // auto &imgManager = OHOS::ImageCacheManager::GetInstance(); for (int16_t i = 0; i < BIRD_DIE_ANIM_IMAGE_MAX_NUM; i++) { g_pv_birdDieInfo_[i].imageInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(BirdDieAnimatorId[i], IMAGE_BIN_PATH); g_pv_birdDieInfo_[i].pos = {animator->GetX(), (int16_t)(animator->GetY())}; g_pv_birdDieInfo_[i].width = 98; g_pv_birdDieInfo_[i].height = 83; g_pv_birdDieInfo_[i].imageType = OHOS::IMG_SRC_IMAGE_INFO; } if(animator == nullptr){ animator = new OHOS::UIImageAnimatorView(); } animator->SetPosition(g_pv_birdDieInfo_->pos.x, g_pv_birdDieInfo_->pos.y); animator->Resize(g_pv_birdDieInfo_->width, g_pv_birdDieInfo_->height); animator->SetImageAnimatorSrc(g_pv_birdDieInfo_, BIRD_DIE_ANIM_IMAGE_MAX_NUM, 1000); animator->SetSizeFixed(true); animator->Start(); } } void Bird::ChangeBirdFlyingAnimator(void) { if(animator != nullptr){ animator->Pause(); const int BirdDieAnimatorId[BIRD_DIE_ANIM_IMAGE_MAX_NUM] = { IMG_GAME_BIRDFLY_BIRD_FALL0, IMG_GAME_BIRDFLY_BIRD_FALL1, IMG_GAME_BIRDFLY_BIRD_FALL2, IMG_GAME_BIRDFLY_BIRD_FALL3, IMG_GAME_BIRDFLY_BIRD_FALL4,IMG_GAME_BIRDFLY_BIRD_FALL5, IMG_GAME_BIRDFLY_BIRD_FALL6, IMG_GAME_BIRDFLY_BIRD_FALL7, IMG_GAME_BIRDFLY_BIRD_FALL8,IMG_GAME_BIRDFLY_BIRD_FALL9}; // auto &imgManager = OHOS::ImageCacheManager::GetInstance(); for (int16_t i = 0; i < BIRD_DIE_ANIM_IMAGE_MAX_NUM; i++) { g_pv_birdDieInfo_[i].imageInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(BirdDieAnimatorId[i], IMAGE_BIN_PATH); g_pv_birdDieInfo_[i].pos = {animator->GetX(), (int16_t)(animator->GetY() + i * 5)}; g_pv_birdDieInfo_[i].width = 98; g_pv_birdDieInfo_[i].height = 83; g_pv_birdDieInfo_[i].imageType = OHOS::IMG_SRC_IMAGE_INFO; } if(animator == nullptr){ animator = new OHOS::UIImageAnimatorView(); } animator->SetPosition(g_pv_birdDieInfo_->pos.x, g_pv_birdDieInfo_->pos.y); animator->Resize(g_pv_birdDieInfo_->width, g_pv_birdDieInfo_->height); animator->SetImageAnimatorSrc(g_pv_birdDieInfo_, BIRD_DIE_ANIM_IMAGE_MAX_NUM, 1000); animator->SetSizeFixed(true); animator->Start(); } } void Bird::SetBirdStatue(uint8_t statue) {} uint8_t Bird::GetBirdStatue() { return 0; } #pragma endregion BackGround::~BackGround() { if(backGround != nullptr){ delete backGround; backGround = nullptr; } } void BackGround::DrawBackGround() { if(backGround == nullptr){ backGround = new OHOS::UIImageView(); } // OHOS::ImageCacheManager::GetInstance().UnloadAllInMultiRes(IMAGE_BIN_PATH); OHOS::ImageInfo * imageInfo = OHOS::ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_GAME_BIRDFLY_BIRD_BG1, IMAGE_BIN_PATH); if(imageInfo != nullptr){ backGround->SetSrc(imageInfo); } backGround->SetPosition(this->x, this->y, this->width, this->height); backGround->SetVisible(true); } void BackGround::MoveBackGround() { int x = this->x; x--; // printf("x = %d, x1 = %d\n", x, x1); if (x <= 0) { x = 932; } SetX(x); SetPosition(); } bool GameStartIngOnClickListenser::OnClick(OHOS::UIView &view, const OHOS::ClickEvent &event) { if(view.GetViewId() == "StartGameIngView"){ GameBirdFlyView::GetInstance()->GetBird()->MoveUpBird(); } return false; } // bool GameBirdFlyView::OnKeyAct(OHOS::UIView &view, const OHOS::KeyEvent &event) // { // if (event.GetState() == OHOS::InputDevice::STATE_RELEASE) { // static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); // PauseGame(); // return true; // } // return false; // } bool GameBirdFlyViewOnClickListenser::OnClick(OHOS::UIView &view, const OHOS::ClickEvent &event) { if(view.GetViewId() == "StartGameView"){ GameBirdFlyView::GetInstance()->StartGame(); }else if(view.GetViewId() == "ContinueButton"){ // GameBirdFlyView::GetInstance()->GetAnimator()->Resume(); GameBirdFlyView::GetInstance()->GetStopGameView()->SetVisible(false); GameBirdFlyView::GetInstance()->RestartGame(); }else if(view.GetViewId() == "ReturnButton"){ TjdUiAppGameView::GetInstance()->ReturnToMainPage(); }else if(view.GetViewId() == "PauseButton"){ GameBirdFlyView::GetInstance()->GetAnimator()->Start(); GameBirdFlyView::GetInstance()->GetBird()->GetAnimator()->Start(); GameBirdFlyView::GetInstance()->GetPauseBtn()->SetVisible(false); } return false; } GameBirdFlyAnimotorCallback::GameBirdFlyAnimotorCallback(Bird *bird, BackGround *backGround1, BackGround *backGround2,Barrier **barrier, Resource **resource, UIViewGroup *gameBirdFlyView, uint8_t event):bird(bird), backGround1(backGround1), backGround2(backGround2), barrier(barrier), resource(resource),gameBirdFlyView(gameBirdFlyView), event(event) { g_pv_BirdFlyAnimotorCallback = this; } GameBirdFlyAnimotorCallback::~GameBirdFlyAnimotorCallback() { g_pv_BirdFlyAnimotorCallback = nullptr; } void GameBirdFlyAnimotorCallback::Callback(UIView *view) { // static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); if(BIRDFLY_NORMAL == event){ // static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); bird->MoveDownBird(); backGround1->MoveBackGround(); backGround2->MoveBackGround(); int x1, x2, y1, y2, xd, yd = 0; short delta_x, delta_y = 0; x1 = bird->GetX() + (bird->GetWidth() >> 1); y1 = bird->GetY() + (bird->GetHeight() >> 1); for(int i = 0; i < BARRIER_MAX_NUM; i++){ barrier[i]->MoveBarrier(); x2 = barrier[i]->GetX() + (barrier[i]->GetWidth() >> 1); y2 = barrier[i]->GetY() + (barrier[i]->GetHeight() >> 1); xd = ((bird->GetWidth() + barrier[i]->GetWidth()) >> 1) - 4; yd = ((bird->GetHeight() + barrier[i]->GetHeight()) >> 1) - 2; delta_x = x2 - x1; delta_y = y2 - y1; if(delta_x < xd && delta_y < yd){//撞到障碍物 bird->SetBirdStatue(BIRDFLY_DIE); Setevent(BIRDFLY_DIE); } } for(int i = 0; i < RESOURCE_MAX_NUM; i++){ resource[i]->MoveResource(); x2 = resource[i]->GetX() + (resource[i]->GetWidth() >> 1); y2 = resource[i]->GetY() + (resource[i]->GetHeight() >> 1); xd = ((bird->GetWidth() + resource[i]->GetWidth()) >> 1) - 4; yd = ((bird->GetHeight() + resource[i]->GetHeight()) >> 1) - 2; delta_x = x2 - x1; delta_y = y2 - y1; if(delta_x < xd && delta_y < yd){//撞到资源 resource[i]->ReleaseResource(); GameBirdFlyView::GetInstance()->SetScore(); } // if(resource[i]->GetX() <= bird->GetX() + bird->GetWidth() && resource[i]->GetX() + resource[i]->GetWidth() >= bird->GetX()){ // if(resource[i]->GetY() <= bird->GetY() + bird->GetHeight() && resource[i]->GetY() + resource[i]->GetHeight() >= bird->GetY()){ // resource[i]->ReleaseResource(); // GameBirdFlyView::GetInstance()->SetScore(); // } } // if(GameBirdFlyView::GetInstance()->CheckBirdRamResource()){ // resource[i]->ReleaseResource(); // } // if(GameBirdFlyView::GetInstance()->CheckBirdRamBarriers()){ // static_print_debug("bird ram barrier..."); // // animator->Stop(); // // animator->SetTimeOfPause(1000); // } // }else if(GameBirdFlyView::GetInstance()->CheckBirdRamResource()){ // static_print_debug("bird ram resource..."); // if() // // animator->Stop(); // // animator->SetTimeOfPause(1000); // } }else if(BIRDFLY_DIE == event){ GameBirdFlyView::GetInstance()->GetAnimator()->Pause(); bird->ChangeBirdDieAnimator(); while(bird->GetY() >= 0){ bird->SetY(bird->GetY() + 20); bird->SetPosition(); } GameBirdFlyView::GetInstance()->StopGame(); } // static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); gameBirdFlyView->Invalidate(); } Barrier::~Barrier() { if(barrierView != nullptr){ delete barrierView; barrierView = nullptr; } } void Barrier::DrawBarrier() { if(barrierView == nullptr){ barrierView = new OHOS::UIImageView(); } OHOS::ImageInfo * imageInfo = OHOS::ImageCacheManager::GetInstance().LoadOneInMultiRes(this->resId, IMAGE_BIN_PATH); if(imageInfo != nullptr){ barrierView->SetSrc(imageInfo); } barrierView->SetPosition(this->x, this->y, this->width, this->height); barrierView->SetVisible(true); } void Barrier::MoveBarrier() { int x = this->x; x--; if (x + this->width <= 466) { srand(time(nullptr)); int randNum = rand() % 300 + 30 * rand() % 3; x = 932 + randNum; srand(time(nullptr)); randNum = rand() % 200 + 50; SetHeight(randNum); } SetX(x); SetPosition(); } Resource::Resource(int x ,uint8_t resourceType,int resId): x(x), resId(resId), resourceType(resourceType) { this->y = 233; this->width = 45; this->height = 38; } Resource::~Resource() { if(resourceView != nullptr) { delete resourceView; resourceView = nullptr; } if(resourceAnimator != nullptr) { resourceAnimator->Stop(); delete resourceAnimator; resourceAnimator = nullptr; } } void Resource::ReleaseResource() { if(resourceView != nullptr){ // resourceView->SetVisible(false); srand(time(nullptr)); int randNum = rand() % 300 + 50; resourceView->SetX(932 + randNum); SetX(932 + randNum); // resourceView->SetPosition(x, y, width, height); } if(resourceAnimator != nullptr){ srand(time(nullptr)); int randNum = rand() % 300 + 50; resourceAnimator->SetX(932 + randNum); SetX(932 + randNum); // resourceAnimator->SetPosition(x, y, width, height); // resourceAnimator->SetVisible(false); } } void Resource::DrawResource() { static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); if(resourceView == nullptr && (resourceType == RESOURCE_BIRD_RIGHT || resourceType == RESOURCE_BARRIER_UP || resourceType == RESOURCE_BARRIER_DOWN || resourceType == RESOURCE_BARRIER_UPDOWN)){ static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); resourceView = new OHOS::UIImageView(); }else if(resourceAnimator == nullptr && resourceType == RESOURCE_GOLD){ static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); resourceAnimator = new OHOS::UIImageAnimatorView(); } static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); if(resourceView != nullptr){ static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); OHOS::ImageInfo * imageInfo = OHOS::ImageCacheManager::GetInstance().LoadOneInMultiRes(resId, IMAGE_BIN_PATH); if(imageInfo != nullptr){ resourceView->SetSrc(imageInfo); } resourceView->SetPosition(this->x, this->y, this->width, this->height); resourceView->SetVisible(true); }else if(resourceAnimator != nullptr){ static_print_debug("%s , line is %d", __FUNCTION__, __LINE__); resourceAnimator->SetImageAnimatorSrc(g_pv_goldInfo_, GOLD_ANIM_MAX_NUM, 800); resourceAnimator->SetPosition(this->x, this->y, this->width, this->height); resourceAnimator->SetSizeFixed(true); resourceAnimator->SetVisible(true); resourceAnimator->Start(); } } void Resource::MoveResource() { int x = this->x; x--; // printf("x = %d, x1 = %d\n", x, x1); if (x + this->width <= 466) { srand(time(nullptr)); int randNum = rand() % 300 + 30 * rand() % 3; x = 932 + randNum; // if(resourceView != nullptr){ // resourceView->SetVisible(true); // }else if(resourceAnimator != nullptr){ // resourceAnimator->SetVisible(true); // } } SetX(x); SetPosition(); } void Resource::SetPosition() { if(resourceView != nullptr){ resourceView->SetPosition(x, y, width, height); }else if(resourceAnimator != nullptr){ resourceAnimator->SetPosition(x, y, width, height); } } int Resource::GetX(void) { if(resourceView != nullptr){ return resourceView->GetX(); }else if(resourceAnimator != nullptr){ return resourceAnimator->GetX(); } return 0; } int Resource::GetY(void) { if(resourceView != nullptr){ return resourceView->GetY(); }else if(resourceAnimator != nullptr){ return resourceAnimator->GetY(); } return 0; } int Resource::GetWidth(void) { if(resourceView != nullptr){ return resourceView->GetWidth(); }else if(resourceAnimator != nullptr){ return resourceAnimator->GetWidth(); } return 0; } int Resource::GetHeight(void) { if(resourceView != nullptr){ return resourceView->GetHeight(); }else if(resourceAnimator != nullptr){ return resourceAnimator->GetHeight(); } return 0; } bool BirdFlyViewKeyInputListener::OnKeyAct(UIView &view, const KeyEvent &event) { if (!TjdUiCommonOnKeyListener::CheckIsExitEvent(event)) { return true; } if (event.GetState() == OHOS::InputDevice::STATE_RELEASE && !GameBirdFlyView::GetInstance()->GetPauseBtn()->IsVisible()) { GameBirdFlyView::GetInstance()->PauseGame(); } else if(GameBirdFlyView::GetInstance()->GetPauseBtn()->IsVisible()){ TjdUiAppGameView::GetInstance()->ReturnToMainPage(); }; return false; } } // namespace TJD