flappy_bird.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. #include <stdlib.h>
  2. #include <flappy_bird_icons.h>
  3. #include <furi.h>
  4. #include <gui/gui.h>
  5. #include <gui/icon_animation_i.h>
  6. #include <input/input.h>
  7. #include <dolphin/dolphin.h>
  8. #define TAG "Flappy"
  9. #define DEBUG false
  10. #define FLAPPY_BIRD_HEIGHT 15
  11. #define FLAPPY_BIRD_WIDTH 10
  12. #define FLAPPY_PILAR_MAX 6
  13. #define FLAPPY_PILAR_DIST 35
  14. #define FLAPPY_GAB_HEIGHT 25
  15. #define FLAPPY_GAB_WIDTH 10
  16. #define FLAPPY_GRAVITY_JUMP -1.1
  17. #define FLAPPY_GRAVITY_TICK 0.15
  18. #define FLIPPER_LCD_WIDTH 128
  19. #define FLIPPER_LCD_HEIGHT 64
  20. typedef enum {
  21. EventTypeTick,
  22. EventTypeKey,
  23. } EventType;
  24. typedef struct {
  25. int x;
  26. int y;
  27. } POINT;
  28. typedef struct {
  29. float gravity;
  30. POINT point;
  31. IconAnimation* sprite;
  32. } BIRD;
  33. typedef struct {
  34. POINT point;
  35. int height;
  36. int visible;
  37. bool passed;
  38. } PILAR;
  39. typedef enum {
  40. GameStateLife,
  41. GameStateGameOver,
  42. } State;
  43. typedef struct {
  44. BIRD bird;
  45. int points;
  46. int pilars_count;
  47. PILAR pilars[FLAPPY_PILAR_MAX];
  48. bool debug;
  49. State state;
  50. FuriMutex* mutex;
  51. } GameState;
  52. typedef struct {
  53. EventType type;
  54. InputEvent input;
  55. } GameEvent;
  56. typedef enum {
  57. DirectionUp,
  58. DirectionRight,
  59. DirectionDown,
  60. DirectionLeft,
  61. } Direction;
  62. static void flappy_game_random_pilar(GameState* const game_state) {
  63. PILAR pilar;
  64. pilar.passed = false;
  65. pilar.visible = 1;
  66. pilar.height = random() % (FLIPPER_LCD_HEIGHT - FLAPPY_GAB_HEIGHT) + 1;
  67. pilar.point.y = 0;
  68. pilar.point.x = FLIPPER_LCD_WIDTH + FLAPPY_GAB_WIDTH + 1;
  69. game_state->pilars_count++;
  70. game_state->pilars[game_state->pilars_count % FLAPPY_PILAR_MAX] = pilar;
  71. }
  72. static void flappy_game_state_init(GameState* const game_state) {
  73. BIRD bird;
  74. bird.gravity = 0.0f;
  75. bird.point.x = 15;
  76. bird.point.y = 32;
  77. bird.sprite = icon_animation_alloc(&A_bird);
  78. game_state->debug = DEBUG;
  79. game_state->bird = bird;
  80. game_state->pilars_count = 0;
  81. game_state->points = 0;
  82. game_state->state = GameStateLife;
  83. memset(game_state->pilars, 0, sizeof(game_state->pilars));
  84. flappy_game_random_pilar(game_state);
  85. }
  86. static void flappy_game_state_free(GameState* const game_state) {
  87. icon_animation_free(game_state->bird.sprite);
  88. free(game_state);
  89. }
  90. static void flappy_game_tick(GameState* const game_state) {
  91. if(game_state->state == GameStateLife) {
  92. if(!game_state->debug) {
  93. game_state->bird.gravity += FLAPPY_GRAVITY_TICK;
  94. game_state->bird.point.y += game_state->bird.gravity;
  95. }
  96. // Checking the location of the last respawned pilar.
  97. PILAR* pilar = &game_state->pilars[game_state->pilars_count % FLAPPY_PILAR_MAX];
  98. if(pilar->point.x == (FLIPPER_LCD_WIDTH - FLAPPY_PILAR_DIST))
  99. flappy_game_random_pilar(game_state);
  100. // Updating the position/status of the pilars (visiblity, posotion, game points)
  101. // | | | | |
  102. // | | | | |
  103. // |__| | |__|
  104. // _____X | X_____
  105. // | | | | | // [Pos + Width of pilar] >= [Bird Pos]
  106. // |_____| | |_____|
  107. // X <----> | X <->
  108. // Bird Pos + Lenght of the bird] >= [Pilar]
  109. for(int i = 0; i < FLAPPY_PILAR_MAX; i++) {
  110. PILAR* pilar = &game_state->pilars[i];
  111. if(pilar != NULL && pilar->visible && game_state->state == GameStateLife) {
  112. pilar->point.x--;
  113. if(game_state->bird.point.x >= pilar->point.x + FLAPPY_GAB_WIDTH &&
  114. pilar->passed == false) {
  115. pilar->passed = true;
  116. game_state->points++;
  117. }
  118. if(pilar->point.x < -FLAPPY_GAB_WIDTH) pilar->visible = 0;
  119. if(game_state->bird.point.y <= 0 - FLAPPY_BIRD_WIDTH) {
  120. game_state->bird.point.y = 64;
  121. }
  122. if(game_state->bird.point.y > 64 - FLAPPY_BIRD_WIDTH) {
  123. game_state->bird.point.y = FLIPPER_LCD_HEIGHT - FLAPPY_BIRD_WIDTH;
  124. }
  125. // Bird inbetween pipes
  126. if((game_state->bird.point.x + FLAPPY_BIRD_HEIGHT >= pilar->point.x) &&
  127. (game_state->bird.point.x <= pilar->point.x + FLAPPY_GAB_WIDTH)) {
  128. // Bird below Bottom Pipe
  129. if(game_state->bird.point.y + FLAPPY_BIRD_WIDTH - 2 >=
  130. pilar->height + FLAPPY_GAB_HEIGHT) {
  131. game_state->state = GameStateGameOver;
  132. break;
  133. }
  134. // Bird above Upper Pipe
  135. if(game_state->bird.point.y < pilar->height) {
  136. game_state->state = GameStateGameOver;
  137. break;
  138. }
  139. }
  140. }
  141. }
  142. }
  143. }
  144. static void flappy_game_flap(GameState* const game_state) {
  145. game_state->bird.gravity = FLAPPY_GRAVITY_JUMP;
  146. }
  147. static void flappy_game_render_callback(Canvas* const canvas, void* ctx) {
  148. furi_assert(ctx);
  149. const GameState* game_state = ctx;
  150. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  151. canvas_draw_frame(canvas, 0, 0, 128, 64);
  152. if(game_state->state == GameStateLife) {
  153. // Pilars
  154. for(int i = 0; i < FLAPPY_PILAR_MAX; i++) {
  155. const PILAR* pilar = &game_state->pilars[i];
  156. if(pilar != NULL && pilar->visible == 1) {
  157. canvas_draw_frame(
  158. canvas, pilar->point.x, pilar->point.y, FLAPPY_GAB_WIDTH, pilar->height);
  159. canvas_draw_frame(
  160. canvas, pilar->point.x + 1, pilar->point.y, FLAPPY_GAB_WIDTH, pilar->height);
  161. canvas_draw_frame(
  162. canvas,
  163. pilar->point.x + 2,
  164. pilar->point.y,
  165. FLAPPY_GAB_WIDTH - 1,
  166. pilar->height);
  167. canvas_draw_frame(
  168. canvas,
  169. pilar->point.x,
  170. pilar->point.y + pilar->height + FLAPPY_GAB_HEIGHT,
  171. FLAPPY_GAB_WIDTH,
  172. FLIPPER_LCD_HEIGHT - pilar->height - FLAPPY_GAB_HEIGHT);
  173. canvas_draw_frame(
  174. canvas,
  175. pilar->point.x + 1,
  176. pilar->point.y + pilar->height + FLAPPY_GAB_HEIGHT,
  177. FLAPPY_GAB_WIDTH - 1,
  178. FLIPPER_LCD_HEIGHT - pilar->height - FLAPPY_GAB_HEIGHT);
  179. canvas_draw_frame(
  180. canvas,
  181. pilar->point.x + 2,
  182. pilar->point.y + pilar->height + FLAPPY_GAB_HEIGHT,
  183. FLAPPY_GAB_WIDTH - 1,
  184. FLIPPER_LCD_HEIGHT - pilar->height - FLAPPY_GAB_HEIGHT);
  185. }
  186. }
  187. // Switch animation
  188. game_state->bird.sprite->frame = 1;
  189. if(game_state->bird.gravity < -0.5)
  190. game_state->bird.sprite->frame = 0;
  191. else if(game_state->bird.gravity > 0.5)
  192. game_state->bird.sprite->frame = 2;
  193. canvas_draw_icon_animation(
  194. canvas, game_state->bird.point.x, game_state->bird.point.y, game_state->bird.sprite);
  195. canvas_set_font(canvas, FontSecondary);
  196. char buffer[12];
  197. snprintf(buffer, sizeof(buffer), "Score: %u", game_state->points);
  198. canvas_draw_str_aligned(canvas, 100, 12, AlignCenter, AlignBottom, buffer);
  199. if(game_state->debug) {
  200. char coordinates[20];
  201. snprintf(coordinates, sizeof(coordinates), "Y: %u", game_state->bird.point.y);
  202. canvas_draw_str_aligned(canvas, 1, 12, AlignCenter, AlignBottom, coordinates);
  203. }
  204. }
  205. if(game_state->state == GameStateGameOver) {
  206. // Screen is 128x64 px
  207. canvas_set_color(canvas, ColorWhite);
  208. canvas_draw_box(canvas, 34, 20, 62, 24);
  209. canvas_set_color(canvas, ColorBlack);
  210. canvas_draw_frame(canvas, 34, 20, 62, 24);
  211. canvas_set_font(canvas, FontPrimary);
  212. canvas_draw_str(canvas, 37, 31, "Game Over");
  213. canvas_set_font(canvas, FontSecondary);
  214. char buffer[12];
  215. snprintf(buffer, sizeof(buffer), "Score: %u", game_state->points);
  216. canvas_draw_str_aligned(canvas, 64, 41, AlignCenter, AlignBottom, buffer);
  217. }
  218. furi_mutex_release(game_state->mutex);
  219. }
  220. static void flappy_game_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  221. furi_assert(event_queue);
  222. GameEvent event = {.type = EventTypeKey, .input = *input_event};
  223. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  224. }
  225. static void flappy_game_update_timer_callback(FuriMessageQueue* event_queue) {
  226. furi_assert(event_queue);
  227. GameEvent event = {.type = EventTypeTick};
  228. furi_message_queue_put(event_queue, &event, 0);
  229. }
  230. int32_t flappy_game_app(void* p) {
  231. UNUSED(p);
  232. int32_t return_code = 0;
  233. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(GameEvent));
  234. GameState* game_state = malloc(sizeof(GameState));
  235. flappy_game_state_init(game_state);
  236. game_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  237. if(!game_state->mutex) {
  238. FURI_LOG_E(TAG, "cannot create mutex\r\n");
  239. return_code = 255;
  240. goto free_and_exit;
  241. }
  242. // Set system callbacks
  243. ViewPort* view_port = view_port_alloc();
  244. view_port_draw_callback_set(view_port, flappy_game_render_callback, game_state);
  245. view_port_input_callback_set(view_port, flappy_game_input_callback, event_queue);
  246. FuriTimer* timer =
  247. furi_timer_alloc(flappy_game_update_timer_callback, FuriTimerTypePeriodic, event_queue);
  248. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 25);
  249. // Open GUI and register view_port
  250. Gui* gui = furi_record_open(RECORD_GUI);
  251. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  252. // Call dolphin deed on game start
  253. dolphin_deed(DolphinDeedPluginGameStart);
  254. GameEvent event;
  255. for(bool processing = true; processing;) {
  256. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  257. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  258. if(event_status == FuriStatusOk) {
  259. // press events
  260. if(event.type == EventTypeKey) {
  261. if(event.input.type == InputTypePress) {
  262. switch(event.input.key) {
  263. case InputKeyUp:
  264. if(game_state->state == GameStateLife) {
  265. flappy_game_flap(game_state);
  266. }
  267. break;
  268. case InputKeyDown:
  269. break;
  270. case InputKeyRight:
  271. break;
  272. case InputKeyLeft:
  273. break;
  274. case InputKeyOk:
  275. if(game_state->state == GameStateGameOver) {
  276. flappy_game_state_init(game_state);
  277. }
  278. if(game_state->state == GameStateLife) {
  279. flappy_game_flap(game_state);
  280. }
  281. break;
  282. case InputKeyBack:
  283. processing = false;
  284. break;
  285. default:
  286. break;
  287. }
  288. }
  289. } else if(event.type == EventTypeTick) {
  290. flappy_game_tick(game_state);
  291. }
  292. }
  293. view_port_update(view_port);
  294. furi_mutex_release(game_state->mutex);
  295. }
  296. furi_timer_free(timer);
  297. view_port_enabled_set(view_port, false);
  298. gui_remove_view_port(gui, view_port);
  299. furi_record_close(RECORD_GUI);
  300. view_port_free(view_port);
  301. furi_mutex_free(game_state->mutex);
  302. free_and_exit:
  303. flappy_game_state_free(game_state);
  304. furi_message_queue_free(event_queue);
  305. return return_code;
  306. }