Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new game: cat vs dog #1795

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ list(APPEND SOURCE_FILES
displayapp/screens/Tile.cpp
displayapp/screens/InfiniPaint.cpp
displayapp/screens/Paddle.cpp
displayapp/screens/CatDog.cpp
displayapp/screens/StopWatch.cpp
displayapp/screens/BatteryIcon.cpp
displayapp/screens/BleIcon.cpp
Expand All @@ -383,6 +384,7 @@ list(APPEND SOURCE_FILES
displayapp/screens/ApplicationList.cpp
displayapp/screens/Notifications.cpp
displayapp/screens/Twos.cpp
displayapp/screens/CatDog.cpp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are adding this file twice (see line 371)

displayapp/screens/HeartRate.cpp
displayapp/screens/FlashLight.cpp
displayapp/screens/List.cpp
Expand Down
1 change: 1 addition & 0 deletions src/displayapp/DisplayApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "displayapp/screens/FirmwareValidation.h"
#include "displayapp/screens/InfiniPaint.h"
#include "displayapp/screens/Paddle.h"
#include "displayapp/screens/CatDog.h"
#include "displayapp/screens/StopWatch.h"
#include "displayapp/screens/Metronome.h"
#include "displayapp/screens/Music.h"
Expand Down
1 change: 1 addition & 0 deletions src/displayapp/apps/Apps.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace Pinetime {
Paint,
Paddle,
Twos,
CatDog,
HeartRate,
Navigation,
StopWatch,
Expand Down
1 change: 1 addition & 0 deletions src/displayapp/apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ else ()
set(DEFAULT_USER_APP_TYPES "${DEFAULT_USER_APP_TYPES}, Apps::Paint")
set(DEFAULT_USER_APP_TYPES "${DEFAULT_USER_APP_TYPES}, Apps::Paddle")
set(DEFAULT_USER_APP_TYPES "${DEFAULT_USER_APP_TYPES}, Apps::Twos")
set(DEFAULT_USER_APP_TYPES "${DEFAULT_USER_APP_TYPES}, Apps::CatDog")
set(DEFAULT_USER_APP_TYPES "${DEFAULT_USER_APP_TYPES}, Apps::Metronome")
set(DEFAULT_USER_APP_TYPES "${DEFAULT_USER_APP_TYPES}, Apps::Navigation")
#set(DEFAULT_USER_APP_TYPES "${DEFAULT_USER_APP_TYPES}, Apps::Weather")
Expand Down
2 changes: 1 addition & 1 deletion src/displayapp/fonts/fonts.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
{
"file": "FontAwesome5-Solid+Brands+Regular.woff",
"range": "0xf294, 0xf242, 0xf54b, 0xf21e, 0xf1e6, 0xf017, 0xf129, 0xf03a, 0xf185, 0xf560, 0xf001, 0xf3fd, 0xf1fc, 0xf45d, 0xf59f, 0xf5a0, 0xf027, 0xf028, 0xf6a9, 0xf04b, 0xf04c, 0xf048, 0xf051, 0xf095, 0xf3dd, 0xf04d, 0xf2f2, 0xf024, 0xf252, 0xf569, 0xf06e, 0xf015, 0xf00c, 0xf743"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which icon (0xf743) gets removed here and why?

"range": "0xf294, 0xf242, 0xf54b, 0xf21e, 0xf1e6, 0xf017, 0xf129, 0xf03a, 0xf185, 0xf560, 0xf001, 0xf3fd, 0xf1fc, 0xf45d, 0xf59f, 0xf5a0, 0xf027, 0xf028, 0xf6a9, 0xf04b, 0xf04c, 0xf048, 0xf051, 0xf095, 0xf3dd, 0xf04d, 0xf2f2, 0xf024, 0xf252, 0xf569, 0xf06e, 0xf015, 0xf00c, 0xf5d7, 0xf6d3, 0xf6be"
}
],
"bpp": 1,
Expand Down
283 changes: 283 additions & 0 deletions src/displayapp/screens/CatDog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
#include "displayapp/screens/CatDog.h"
#include "displayapp/DisplayApp.h"
#include "displayapp/LittleVgl.h"
#include "displayapp/screens/Symbols.h"

#include <cstdlib> // for rand()

using namespace Pinetime::Applications::Screens;

CatDog::CatDog() {

lv_obj_t* background = lv_obj_create(lv_scr_act(), nullptr);
lv_obj_set_size(background, LV_HOR_RES, LV_VER_RES);
lv_obj_set_style_local_radius(background, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 0);
lv_obj_set_style_local_bg_color(background, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x154360));

info = lv_label_create(lv_scr_act(), nullptr);
lv_label_set_text_static(info, "Touch to Start");
lv_obj_align(info, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 0, 40);

bone = lv_label_create(lv_scr_act(), nullptr);
lv_label_set_text_static(bone, Symbols::bone);
lv_obj_set_pos(bone, 240, 240);

cat = lv_label_create(lv_scr_act(), nullptr);
lv_label_set_text_static(cat, Symbols::cat);
lv_obj_set_style_local_text_color(cat, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0xa2d9ce));
lv_obj_set_pos(cat, 215, 195);

dog = lv_label_create(lv_scr_act(), nullptr);
lv_label_set_text_static(dog, Symbols::dog);
lv_obj_set_style_local_text_color(dog, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0xf7dc6f));
lv_obj_set_pos(dog, 5, 195);

windTxt = lv_label_create(lv_scr_act(), nullptr);
lv_obj_align(windTxt, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 0, 0);
lv_label_set_text_static(windTxt, "");

windBarBG = lv_obj_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_bg_color(windBarBG, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
lv_obj_set_size(windBarBG, 120, 4);
lv_obj_set_pos(windBarBG, 240, 240);

windBar = lv_obj_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_bg_color(windBar, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLUE);
lv_obj_set_pos(windBar, 240, 240);

powerBar = lv_bar_create(lv_scr_act(), NULL);
lv_obj_set_style_local_bg_color(powerBar, LV_BAR_PART_BG, LV_STATE_DEFAULT, LV_COLOR_GRAY);
lv_obj_set_style_local_bg_opa(powerBar, LV_BAR_PART_BG, LV_STATE_DEFAULT, LV_OPA_100);
lv_obj_set_style_local_bg_color(powerBar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_RED);
lv_bar_set_range(powerBar, MIN_POWER, MAX_POWER);
lv_obj_set_size(powerBar, 120, 4);
lv_obj_set_pos(powerBar, 240, 240);

catLifeBar = lv_obj_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_bg_color(catLifeBar, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);

dogLifeBar = lv_obj_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_bg_color(dogLifeBar, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);

UpdateLife();

taskRefresh = lv_task_create(RefreshTaskCallback, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, this);
}

CatDog::~CatDog() {
lv_task_del(taskRefresh);
lv_obj_clean(lv_scr_act());
}

void CatDog::Refresh() {
switch (state) {
case CatDog::State::Idle:
// add a delay to reject previous press event
if (animationTime > 0) {
animationTime--;
} else {
if (isGameStopped) {
state = CatDog::State::Stopped;
} else {
state = CatDog::State::Prompt;
}
}
Comment on lines +78 to +84
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else {
if (isGameStopped) {
state = CatDog::State::Stopped;
} else {
state = CatDog::State::Prompt;
}
}
} else if (isGameStopped) {
state = CatDog::State::Stopped;
} else {
state = CatDog::State::Prompt;
}

break;
case CatDog::State::Stopped:
// start game
if (isPressing) {
lv_obj_align(windBarBG, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 0, 30);
catLifePoint = 100;
dogLifePoint = 100;
UpdateLife();
isDogsTurn = true;
animationTime = 20;
isGameStopped = false;
state = CatDog::State::Idle;
}
break;
case CatDog::State::Prompt:
RandomWind();
if (isDogsTurn) {
lv_label_set_text_static(info, "Your turn");
lv_obj_align(info, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 0, 40);
state = CatDog::State::WaitUser;
} else {
lv_label_set_text_static(info, "Cat's turn");
lv_obj_align(info, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 0, 40);
lv_obj_align(powerBar, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 0, 70);
// throwPower for perfect hit is 370 + wind*12;
PCTargetThrowPower = 343 + wind * 6 + rand() % 54;
lv_obj_set_pos(bone, 215, HORIZON / 100);
state = CatDog::State::ComputerThrowing;
}
break;
case CatDog::State::WaitUser:
if (isPressing) {
lv_obj_align(powerBar, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 0, 70); // show powerBar
lv_obj_set_pos(bone, 5, HORIZON / 100);
state = CatDog::State::UserThrowing;
}
break;
case CatDog::State::UserThrowing:
if ((!isPressing && throwPower > MIN_POWER + 20) || throwPower >= MAX_POWER) {
Throw(throwPower);
} else {
throwPower += 3;
lv_bar_set_value(powerBar, throwPower, LV_ANIM_OFF);
}
break;
case CatDog::State::ComputerThrowing:
if (throwPower < PCTargetThrowPower) {
throwPower += 3;
lv_bar_set_value(powerBar, throwPower, LV_ANIM_OFF);
} else {
Throw(-throwPower);
}
break;
case CatDog::State::BoneFlying:
boneVY += 33; // gravity
boneVX += wind; // acceleration caused by wind
boneX += boneVX;
boneY += boneVY;
lv_obj_set_pos(bone, boneX / 100, boneY / 100);
if (boneY >= HORIZON || boneX > 24000 || boneX < -2400) {
if (-2400 < boneX && boneX < 24000) {
if (isDogsTurn) {
handleDamage(abs(boneX - 21500) / 100);
} else {
handleDamage(abs(boneX - 500) / 100);
Comment on lines +144 to +149
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are all those magic numbers coming from? Can we replace them by a variable with a good name and constexpr?

}
} else {
handleDamage(100); // missed
}
}
break;
case CatDog::State::CatHit:
if (animationTime > 0) {
animationTime--;
if (animationTime % 4 == 0) {
lv_obj_set_style_local_text_color(cat, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0xa2d9ce));
} else if (animationTime % 4 == 2) {
lv_obj_set_style_local_text_color(cat, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
}
} else {
state = CatDog::State::Result;
}
break;
case CatDog::State::DogHit:
if (animationTime > 0) {
animationTime--;
if (animationTime % 4 == 0) {
lv_obj_set_style_local_text_color(dog, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0xf7dc6f));
} else if (animationTime % 4 == 2) {
lv_obj_set_style_local_text_color(dog, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
}
} else {
state = CatDog::State::Result;
}
break;
case CatDog::State::Missed:
if (animationTime > 0) {
animationTime--;
} else {
state = CatDog::State::Result;
}
break;
case CatDog::State::Result:
if (dogLifePoint == 0) {
lv_label_set_text_static(info, "Cat Won!");
lv_obj_align(info, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 0, 40);
animationTime = 20;
isGameStopped = true;
state = CatDog::State::Idle;
} else if (catLifePoint == 0) {
lv_label_set_text_static(info, "You Won!");
lv_obj_align(info, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 0, 40);
animationTime = 20;
isGameStopped = true;
state = CatDog::State::Idle;
} else {
isDogsTurn = !isDogsTurn;
state = CatDog::State::Prompt;
}
break;
}
isPressing = false;
}

void CatDog::Throw(int16_t speed) {
lv_obj_set_pos(powerBar, 240, 240); // hide it
boneY = HORIZON;
boneVX = speed;
boneVY = 0;
if (speed > 0) {
boneX = 500;
boneVY -= 25 * speed / 10;
} else {
// 100*(240-BONE_SIZE-5)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to turn this into a constexpr instead.

boneX = 21500;
boneVY += 25 * speed / 10;
}
throwPower = MIN_POWER;
state = CatDog::State::BoneFlying;
}

void CatDog::handleDamage(uint8_t distance) {
if (distance < 30) {
// animationTime is propotional to damage
animationTime = (50 - distance);
if (isDogsTurn) {
catLifePoint -= animationTime / 2;
state = CatDog::State::CatHit;
} else {
dogLifePoint -= animationTime / 2;
state = CatDog::State::DogHit;
}
} else {
animationTime = 50;
lv_label_set_text_static(info, "Missed");
lv_obj_align(info, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 0, 40);
state = CatDog::State::Missed;
}
UpdateLife();
}

void CatDog::RandomWind() {
wind = rand() % 9 - 4; // -4 to 4
lv_obj_set_size(windBar, abs(wind) * 15, 4);
lv_obj_set_pos(windBar, 50, 30);
lv_obj_align(windBar, lv_scr_act(), LV_ALIGN_IN_TOP_MID, wind * 15 / 2, 30);

if (wind > 0) {
lv_label_set_text_static(windTxt, " wind>");
} else if (wind < 0) {
lv_label_set_text_static(windTxt, "<wind ");
} else {
lv_label_set_text_static(windTxt, " wind ");
}
lv_obj_align(windTxt, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 0, 0);
}

void CatDog::UpdateLife() {
if (catLifePoint > 100) {
catLifePoint = 0;
}
if (dogLifePoint > 100) {
dogLifePoint = 0;
}
lv_obj_set_size(catLifeBar, catLifePoint, 2);
lv_obj_align(catLifeBar, lv_scr_act(), LV_ALIGN_IN_BOTTOM_RIGHT, 0, -2);
lv_obj_set_size(dogLifeBar, dogLifePoint, 2);
lv_obj_align(dogLifeBar, lv_scr_act(), LV_ALIGN_IN_BOTTOM_LEFT, 0, -2);
}

bool CatDog::OnTouchEvent(Pinetime::Applications::TouchEvents /*event*/) {
isPressing = true;
return true;
}

bool CatDog::OnTouchEvent(uint16_t /*x*/, uint16_t /*y*/) {
isPressing = true;
return true;
}
Loading