diff options
-rw-r--r-- | CMakeLists.txt | 3 | ||||
-rw-r--r-- | app/app_manager.cpp | 4 | ||||
-rw-r--r-- | apps/game_falldown/ball.cpp | 32 | ||||
-rw-r--r-- | apps/game_falldown/ball.hpp | 20 | ||||
-rw-r--r-- | apps/game_falldown/main.cpp | 92 | ||||
-rw-r--r-- | apps/game_falldown/main.hpp | 39 | ||||
-rw-r--r-- | apps/game_falldown/platform.cpp | 41 | ||||
-rw-r--r-- | apps/game_falldown/platform.hpp | 24 | ||||
-rw-r--r-- | apps/home_menu/main.hpp | 4 | ||||
-rw-r--r-- | globals.hpp | 3 |
10 files changed, 259 insertions, 3 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 0efeb1f..28b46af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,6 +52,9 @@ add_executable(pico-watch apps/main_clock/main.hpp apps/settings/main.cpp apps/settings/main.hpp + apps/game_falldown/main.cpp + apps/game_falldown/ball.cpp + apps/game_falldown/platform.cpp ) pico_set_program_name(pico-watch "pico-watch") diff --git a/app/app_manager.cpp b/app/app_manager.cpp index 07e68fa..516f38f 100644 --- a/app/app_manager.cpp +++ b/app/app_manager.cpp @@ -8,7 +8,8 @@ #include "../apps/main_clock/main.hpp" #include "../apps/home_menu/main.hpp" #include "../apps/settings/main.hpp" -#define NUMBER_OF_APPS 3 +#include "../apps/game_falldown/main.hpp" +#define NUMBER_OF_APPS 4 // From pico-watch.c: extern Api app_api; @@ -53,6 +54,7 @@ BaseApp* app_mgr::app_create(int app_id) { case 0: new_app = new app_home_menu(&app_api); break; case 1: new_app = new app_main_clock(&app_api); break; case 2: new_app = new app_settings(&app_api); break; + case 3: new_app = new app_game_falldown(&app_api); break; default: __breakpoint(); return open_apps.front(); // Should be home_menu } diff --git a/apps/game_falldown/ball.cpp b/apps/game_falldown/ball.cpp new file mode 100644 index 0000000..98d464f --- /dev/null +++ b/apps/game_falldown/ball.cpp @@ -0,0 +1,32 @@ +#include "ball.hpp" + +Ball::Ball(int x, int y, int speed) { + m_position[0] = x; + m_position[1] = y; + m_speed = speed; +} + +void Ball::update() { + m_position[1] += 1 * m_speed; + + if (m_pressed_left) + m_position[0] -= 5 * m_speed; + if (m_pressed_right) + m_position[0] += 5 * m_speed; + + if (m_position[0] > DISPLAY_WIDTH) + m_position[0] = DISPLAY_WIDTH; + else if (m_position[0] < 0) + m_position[0] = 0; + + if (m_position[1] > DISPLAY_HEIGHT) + m_position[1] = DISPLAY_HEIGHT; + else if (m_position[1] < 0) { + m_position[1] = 0; + m_has_died = true; + } +} + +void Ball::draw(Api *app_api) { + app_api->display_draw_ellipse((int)m_position[0], (int)m_position[1], m_radius, m_radius, 1, 1); +}
\ No newline at end of file diff --git a/apps/game_falldown/ball.hpp b/apps/game_falldown/ball.hpp new file mode 100644 index 0000000..33fd781 --- /dev/null +++ b/apps/game_falldown/ball.hpp @@ -0,0 +1,20 @@ +#pragma once +#include "../../hal/api.hpp" +#include "../../globals.hpp" + +class Ball { + private: + float m_position[2]; + int m_speed; + int m_radius = 5; + + public: + bool m_pressed_left = false; + bool m_pressed_right = false; + + bool m_has_died = false; + + Ball(int x, int y, int speed = 1); + void update(); + void draw(Api *app_api); +}; diff --git a/apps/game_falldown/main.cpp b/apps/game_falldown/main.cpp new file mode 100644 index 0000000..290eba4 --- /dev/null +++ b/apps/game_falldown/main.cpp @@ -0,0 +1,92 @@ +#include "main.hpp" + +// TODO in this app +// - + +void app_game_falldown::update_game() { + m_player_ball.update(); + for (auto platform : m_platforms) { + platform.update(); + } + + if (m_player_ball.m_has_died) + m_gamestate = gameover; +} + +void app_game_falldown::draw_game() { + m_app_api->display_fill(0, 0); + m_player_ball.draw(m_app_api); + for (auto platform : m_platforms) { + platform.draw(m_app_api); + } +} + +void app_game_falldown::draw_main_menu() { + m_app_api->gui_header_text("A Falldown clone", 0,0,true); + m_app_api->gui_header_text("Press select to start", 0,2); + m_app_api->gui_header_text("In game:", 0,3); + m_app_api->gui_header_text("up/down btns to move", 0,4); +} + +void app_game_falldown::draw_gameover_menu() { + m_app_api->gui_header_text("Game Over!", 30,2,true); +} + +void app_game_falldown::init_game() { + // place the player + m_platforms.push_back(30); + m_gamestate = game; +} + +BaseApp::AppReturnValues app_game_falldown::render(Api *app_api) { + switch (m_gamestate) { + case menu: + draw_main_menu(); break; + case game: + update_game(); draw_game(); break; + case gameover: + draw_gameover_menu(); break; + } + + return AppReturnValues::OK; +} + +BaseApp::AppReturnValues app_game_falldown::btn_pressed(Api *app_api, uint gpio, unsigned long delta) { + switch (m_gamestate) { + case menu: + if (gpio == BUTTON_SELECT) + init_game(); + break; + case game: + switch (gpio) { + case BUTTON_DOWN: + m_player_ball.m_pressed_left = true; break; + case BUTTON_UP: + m_player_ball.m_pressed_right = true; break; + } + break; + case gameover: + draw_gameover_menu(); break; + } + + return AppReturnValues::OK; +} + +BaseApp::AppReturnValues app_game_falldown::btn_released(Api *app_api, uint gpio, unsigned long delta) { + if (m_gamestate == game) { + switch (gpio) { + case BUTTON_DOWN: + m_player_ball.m_pressed_left = false; break; + case BUTTON_UP: + m_player_ball.m_pressed_right = false; break; + } + } + return AppReturnValues::OK; +} + +app_game_falldown::app_game_falldown(Api *app_api) { + m_app_api = app_api; +} + +app_game_falldown::~app_game_falldown() { +} diff --git a/apps/game_falldown/main.hpp b/apps/game_falldown/main.hpp new file mode 100644 index 0000000..6e8cbf0 --- /dev/null +++ b/apps/game_falldown/main.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include <vector> + +// Includes also buttons, API and ss_oled +#include "../../app/base_app.hpp" + +#include "ball.hpp" +#include "platform.hpp" + +class app_game_falldown : public BaseApp { + private: + enum GameStates {menu, game, gameover}; + + GameStates m_gamestate = menu; + Api* m_app_api; + + Ball m_player_ball = {0,0}; + std::vector<Platform> m_platforms; + + void draw_main_menu(); + void draw_game(); + void draw_gameover_menu(); + void init_game(); + void update_game(); + + AppAttributes app_attributes = {1, true, + 30}; // Around 30 fps + public: + const AppAttributes& app_get_attributes() { + return app_attributes; + } + + app_game_falldown(Api *app_api); + AppReturnValues render(Api *app_api); + AppReturnValues btn_pressed(Api *app_api, uint gpio, unsigned long delta); + AppReturnValues btn_released(Api *app_api, uint gpio, unsigned long delta); + ~app_game_falldown(); +}; diff --git a/apps/game_falldown/platform.cpp b/apps/game_falldown/platform.cpp new file mode 100644 index 0000000..810f469 --- /dev/null +++ b/apps/game_falldown/platform.cpp @@ -0,0 +1,41 @@ +#include "platform.hpp" + +Platform::Platform() { + m_position_y = DISPLAY_HEIGHT-1; + m_speed = 1; + + create_tiles(); +} + +Platform::Platform(int y, int speed) { + m_position_y = y; + m_speed = speed; + + create_tiles(); +} + +void Platform::create_tiles() { + // example of platform creation + for (size_t i = 1; i < m_tiles_solid.size(); i += 2) { + m_tiles_solid.set(i); + } +} + +void Platform::update() { + m_position_y -= m_speed * 1; +} + +void Platform::draw(Api *app_api) { + // display_draw_line(int x1, int y1, int x2, int y2, int bRender); + // for each tile + // FIXME: Is broken + int position_x = 0; + for (size_t i = 0; i < m_tiles_solid.size(); ++i) { + if (m_tiles_solid[i]) { + int tile_start_x = PLATFORM_TILE_SIZE * i; + int tile_end_x = tile_start_x + PLATFORM_TILE_SIZE; + + app_api->display_draw_line(tile_start_x, m_position_y, tile_end_x, m_position_y, 1); + } + } +}
\ No newline at end of file diff --git a/apps/game_falldown/platform.hpp b/apps/game_falldown/platform.hpp new file mode 100644 index 0000000..e7936cf --- /dev/null +++ b/apps/game_falldown/platform.hpp @@ -0,0 +1,24 @@ +#pragma once +#include "../../hal/api.hpp" +#include "../../globals.hpp" + +#include "bitset" +#define PLATFORM_TILE_SIZE 16 +// The original game on TI-84 Plus had 12 tiles per platform +#define PLATFORM_NUM_TILES (DISPLAY_WIDTH/PLATFORM_TILE_SIZE) + +class Platform { + private: + int m_position_y; + int m_speed; + + // Bitset where solid tiles have a size of + std::bitset<PLATFORM_NUM_TILES> m_tiles_solid; + + void create_tiles(); + public: + Platform(); + Platform(int y = DISPLAY_HEIGHT-1, int speed = 1); + void update(); + void draw(Api *app_api); +}; diff --git a/apps/home_menu/main.hpp b/apps/home_menu/main.hpp index 242d2a3..40c8aad 100644 --- a/apps/home_menu/main.hpp +++ b/apps/home_menu/main.hpp @@ -5,12 +5,12 @@ // Includes also buttons, API and ss_oled #include "../../app/base_app.hpp" -#define NUMBER_OF_APPS 3 +#define NUMBER_OF_APPS 4 #define SIZE_APP_NAME 12 class app_home_menu : public BaseApp { private: - const char *APPS_NAME[NUMBER_OF_APPS] = {"Home", "Clock", "Settings"}; + const char *APPS_NAME[NUMBER_OF_APPS] = {"Home", "Clock", "Settings", "Falldown"}; int selected_app = 0; char display_app_name[SIZE_APP_NAME]; diff --git a/globals.hpp b/globals.hpp index fd24542..b07012f 100644 --- a/globals.hpp +++ b/globals.hpp @@ -24,5 +24,8 @@ struct user_settings { bool time_format = true; }; +#define DISPLAY_WIDTH 128 +#define DISPLAY_HEIGHT 64 + extern global_status g_s; extern user_settings g_user;
\ No newline at end of file |