snake_game.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. #include <input/input.h>
  4. #include <stdlib.h>
  5. #include <dolphin/dolphin.h>
  6. #include <notification/notification.h>
  7. #include <notification/notification_messages.h>
  8. typedef struct {
  9. // +-----x
  10. // |
  11. // |
  12. // y
  13. uint8_t x;
  14. uint8_t y;
  15. } Point;
  16. typedef enum {
  17. GameStateLife,
  18. // https://melmagazine.com/en-us/story/snake-nokia-6110-oral-history-taneli-armanto
  19. // Armanto: While testing the early versions of the game, I noticed it was hard
  20. // to control the snake upon getting close to and edge but not crashing — especially
  21. // in the highest speed levels. I wanted the highest level to be as fast as I could
  22. // possibly make the device "run," but on the other hand, I wanted to be friendly
  23. // and help the player manage that level. Otherwise it might not be fun to play. So
  24. // I implemented a little delay. A few milliseconds of extra time right before
  25. // the player crashes, during which she can still change the directions. And if
  26. // she does, the game continues.
  27. GameStateLastChance,
  28. GameStateGameOver,
  29. } GameState;
  30. // Note: do not change without purpose. Current values are used in smart
  31. // orthogonality calculation in `snake_game_get_turn_snake`.
  32. typedef enum {
  33. DirectionUp,
  34. DirectionRight,
  35. DirectionDown,
  36. DirectionLeft,
  37. } Direction;
  38. #define MAX_SNAKE_LEN 253
  39. typedef struct {
  40. Point points[MAX_SNAKE_LEN];
  41. uint16_t len;
  42. Direction currentMovement;
  43. Direction nextMovement; // if backward of currentMovement, ignore
  44. Point fruit;
  45. GameState state;
  46. } SnakeState;
  47. typedef enum {
  48. EventTypeTick,
  49. EventTypeKey,
  50. } EventType;
  51. typedef struct {
  52. EventType type;
  53. InputEvent input;
  54. } SnakeEvent;
  55. const NotificationSequence sequence_fail = {
  56. &message_vibro_on,
  57. &message_note_ds4,
  58. &message_delay_10,
  59. &message_sound_off,
  60. &message_delay_10,
  61. &message_note_ds4,
  62. &message_delay_10,
  63. &message_sound_off,
  64. &message_delay_10,
  65. &message_note_ds4,
  66. &message_delay_10,
  67. &message_sound_off,
  68. &message_delay_10,
  69. &message_vibro_off,
  70. NULL,
  71. };
  72. const NotificationSequence sequence_eat = {
  73. &message_note_c7,
  74. &message_delay_50,
  75. &message_sound_off,
  76. NULL,
  77. };
  78. static void snake_game_render_callback(Canvas* const canvas, void* ctx) {
  79. const SnakeState* snake_state = acquire_mutex((ValueMutex*)ctx, 25);
  80. if(snake_state == NULL) {
  81. return;
  82. }
  83. // Before the function is called, the state is set with the canvas_reset(canvas)
  84. // Frame
  85. canvas_draw_frame(canvas, 0, 0, 128, 64);
  86. // Fruit
  87. Point f = snake_state->fruit;
  88. f.x = f.x * 4 + 1;
  89. f.y = f.y * 4 + 1;
  90. canvas_draw_rframe(canvas, f.x, f.y, 6, 6, 2);
  91. // Snake
  92. for(uint16_t i = 0; i < snake_state->len; i++) {
  93. Point p = snake_state->points[i];
  94. p.x = p.x * 4 + 2;
  95. p.y = p.y * 4 + 2;
  96. canvas_draw_box(canvas, p.x, p.y, 4, 4);
  97. }
  98. // Game Over banner
  99. if(snake_state->state == GameStateGameOver) {
  100. // Screen is 128x64 px
  101. canvas_set_color(canvas, ColorWhite);
  102. canvas_draw_box(canvas, 34, 20, 62, 24);
  103. canvas_set_color(canvas, ColorBlack);
  104. canvas_draw_frame(canvas, 34, 20, 62, 24);
  105. canvas_set_font(canvas, FontPrimary);
  106. canvas_draw_str(canvas, 37, 31, "Game Over");
  107. canvas_set_font(canvas, FontSecondary);
  108. char buffer[12];
  109. snprintf(buffer, sizeof(buffer), "Score: %u", snake_state->len - 7);
  110. canvas_draw_str_aligned(canvas, 64, 41, AlignCenter, AlignBottom, buffer);
  111. }
  112. release_mutex((ValueMutex*)ctx, snake_state);
  113. }
  114. static void snake_game_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  115. furi_assert(event_queue);
  116. SnakeEvent event = {.type = EventTypeKey, .input = *input_event};
  117. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  118. }
  119. static void snake_game_update_timer_callback(FuriMessageQueue* event_queue) {
  120. furi_assert(event_queue);
  121. SnakeEvent event = {.type = EventTypeTick};
  122. furi_message_queue_put(event_queue, &event, 0);
  123. }
  124. static void snake_game_init_game(SnakeState* const snake_state) {
  125. Point p[] = {{8, 6}, {7, 6}, {6, 6}, {5, 6}, {4, 6}, {3, 6}, {2, 6}};
  126. memcpy(snake_state->points, p, sizeof(p));
  127. snake_state->len = 7;
  128. snake_state->currentMovement = DirectionRight;
  129. snake_state->nextMovement = DirectionRight;
  130. Point f = {18, 6};
  131. snake_state->fruit = f;
  132. snake_state->state = GameStateLife;
  133. }
  134. static Point snake_game_get_new_fruit(SnakeState const* const snake_state) {
  135. // 1 bit for each point on the playing field where the snake can turn
  136. // and where the fruit can appear
  137. uint16_t buffer[8];
  138. memset(buffer, 0, sizeof(buffer));
  139. uint8_t empty = 8 * 16;
  140. for(uint16_t i = 0; i < snake_state->len; i++) {
  141. Point p = snake_state->points[i];
  142. if(p.x % 2 != 0 || p.y % 2 != 0) {
  143. continue;
  144. }
  145. p.x /= 2;
  146. p.y /= 2;
  147. buffer[p.y] |= 1 << p.x;
  148. empty--;
  149. }
  150. // Bit set if snake use that playing field
  151. uint16_t newFruit = rand() % empty;
  152. // Skip random number of _empty_ fields
  153. for(uint8_t y = 0; y < 8; y++) {
  154. for(uint16_t x = 0, mask = 1; x < 16; x += 1, mask <<= 1) {
  155. if((buffer[y] & mask) == 0) {
  156. if(newFruit == 0) {
  157. Point p = {
  158. .x = x * 2,
  159. .y = y * 2,
  160. };
  161. return p;
  162. }
  163. newFruit--;
  164. }
  165. }
  166. }
  167. // We will never be here
  168. Point p = {0, 0};
  169. return p;
  170. }
  171. static bool snake_game_collision_with_frame(Point const next_step) {
  172. // if x == 0 && currentMovement == left then x - 1 == 255 ,
  173. // so check only x > right border
  174. return next_step.x > 30 || next_step.y > 14;
  175. }
  176. static bool
  177. snake_game_collision_with_tail(SnakeState const* const snake_state, Point const next_step) {
  178. for(uint16_t i = 0; i < snake_state->len; i++) {
  179. Point p = snake_state->points[i];
  180. if(p.x == next_step.x && p.y == next_step.y) {
  181. return true;
  182. }
  183. }
  184. return false;
  185. }
  186. static Direction snake_game_get_turn_snake(SnakeState const* const snake_state) {
  187. // Sum of two `Direction` lies between 0 and 6, odd values indicate orthogonality.
  188. bool is_orthogonal = (snake_state->currentMovement + snake_state->nextMovement) % 2 == 1;
  189. return is_orthogonal ? snake_state->nextMovement : snake_state->currentMovement;
  190. }
  191. static Point snake_game_get_next_step(SnakeState const* const snake_state) {
  192. Point next_step = snake_state->points[0];
  193. switch(snake_state->currentMovement) {
  194. // +-----x
  195. // |
  196. // |
  197. // y
  198. case DirectionUp:
  199. next_step.y--;
  200. break;
  201. case DirectionRight:
  202. next_step.x++;
  203. break;
  204. case DirectionDown:
  205. next_step.y++;
  206. break;
  207. case DirectionLeft:
  208. next_step.x--;
  209. break;
  210. }
  211. return next_step;
  212. }
  213. static void snake_game_move_snake(SnakeState* const snake_state, Point const next_step) {
  214. memmove(snake_state->points + 1, snake_state->points, snake_state->len * sizeof(Point));
  215. snake_state->points[0] = next_step;
  216. }
  217. static void
  218. snake_game_process_game_step(SnakeState* const snake_state, NotificationApp* notification) {
  219. if(snake_state->state == GameStateGameOver) {
  220. return;
  221. }
  222. bool can_turn = (snake_state->points[0].x % 2 == 0) && (snake_state->points[0].y % 2 == 0);
  223. if(can_turn) {
  224. snake_state->currentMovement = snake_game_get_turn_snake(snake_state);
  225. }
  226. Point next_step = snake_game_get_next_step(snake_state);
  227. bool crush = snake_game_collision_with_frame(next_step);
  228. if(crush) {
  229. if(snake_state->state == GameStateLife) {
  230. snake_state->state = GameStateLastChance;
  231. return;
  232. } else if(snake_state->state == GameStateLastChance) {
  233. snake_state->state = GameStateGameOver;
  234. notification_message_block(notification, &sequence_fail);
  235. return;
  236. }
  237. } else {
  238. if(snake_state->state == GameStateLastChance) {
  239. snake_state->state = GameStateLife;
  240. }
  241. }
  242. crush = snake_game_collision_with_tail(snake_state, next_step);
  243. if(crush) {
  244. snake_state->state = GameStateGameOver;
  245. notification_message_block(notification, &sequence_fail);
  246. return;
  247. }
  248. bool eatFruit = (next_step.x == snake_state->fruit.x) && (next_step.y == snake_state->fruit.y);
  249. if(eatFruit) {
  250. snake_state->len++;
  251. if(snake_state->len >= MAX_SNAKE_LEN) {
  252. snake_state->state = GameStateGameOver;
  253. notification_message_block(notification, &sequence_fail);
  254. return;
  255. }
  256. }
  257. snake_game_move_snake(snake_state, next_step);
  258. if(eatFruit) {
  259. snake_state->fruit = snake_game_get_new_fruit(snake_state);
  260. notification_message(notification, &sequence_eat);
  261. }
  262. }
  263. int32_t snake_game_app(void* p) {
  264. UNUSED(p);
  265. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(SnakeEvent));
  266. SnakeState* snake_state = malloc(sizeof(SnakeState));
  267. snake_game_init_game(snake_state);
  268. ValueMutex state_mutex;
  269. if(!init_mutex(&state_mutex, snake_state, sizeof(SnakeState))) {
  270. FURI_LOG_E("SnakeGame", "cannot create mutex\r\n");
  271. free(snake_state);
  272. return 255;
  273. }
  274. ViewPort* view_port = view_port_alloc();
  275. view_port_draw_callback_set(view_port, snake_game_render_callback, &state_mutex);
  276. view_port_input_callback_set(view_port, snake_game_input_callback, event_queue);
  277. FuriTimer* timer =
  278. furi_timer_alloc(snake_game_update_timer_callback, FuriTimerTypePeriodic, event_queue);
  279. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 4);
  280. // Open GUI and register view_port
  281. Gui* gui = furi_record_open(RECORD_GUI);
  282. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  283. NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
  284. notification_message_block(notification, &sequence_display_backlight_enforce_on);
  285. DOLPHIN_DEED(DolphinDeedPluginGameStart);
  286. SnakeEvent event;
  287. for(bool processing = true; processing;) {
  288. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  289. SnakeState* snake_state = (SnakeState*)acquire_mutex_block(&state_mutex);
  290. if(event_status == FuriStatusOk) {
  291. // press events
  292. if(event.type == EventTypeKey) {
  293. if(event.input.type == InputTypePress) {
  294. switch(event.input.key) {
  295. case InputKeyUp:
  296. snake_state->nextMovement = DirectionUp;
  297. break;
  298. case InputKeyDown:
  299. snake_state->nextMovement = DirectionDown;
  300. break;
  301. case InputKeyRight:
  302. snake_state->nextMovement = DirectionRight;
  303. break;
  304. case InputKeyLeft:
  305. snake_state->nextMovement = DirectionLeft;
  306. break;
  307. case InputKeyOk:
  308. if(snake_state->state == GameStateGameOver) {
  309. snake_game_init_game(snake_state);
  310. }
  311. break;
  312. case InputKeyBack:
  313. processing = false;
  314. break;
  315. default:
  316. break;
  317. }
  318. }
  319. } else if(event.type == EventTypeTick) {
  320. snake_game_process_game_step(snake_state, notification);
  321. }
  322. } else {
  323. // event timeout
  324. }
  325. view_port_update(view_port);
  326. release_mutex(&state_mutex, snake_state);
  327. }
  328. // Wait for all notifications to be played and return backlight to normal state
  329. notification_message_block(notification, &sequence_display_backlight_enforce_auto);
  330. furi_timer_free(timer);
  331. view_port_enabled_set(view_port, false);
  332. gui_remove_view_port(gui, view_port);
  333. furi_record_close(RECORD_GUI);
  334. furi_record_close(RECORD_NOTIFICATION);
  335. view_port_free(view_port);
  336. furi_message_queue_free(event_queue);
  337. delete_mutex(&state_mutex);
  338. free(snake_state);
  339. return 0;
  340. }
  341. // Screen is 128x64 px
  342. // (4 + 4) * 16 - 4 + 2 + 2border == 128
  343. // (4 + 4) * 8 - 4 + 2 + 2border == 64
  344. // Game field from point{x: 0, y: 0} to point{x: 30, y: 14}.
  345. // The snake turns only in even cells - intersections.
  346. // ┌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┐
  347. // ╎ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ╎
  348. // ╎ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ╎
  349. // ╎ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ╎
  350. // ╎ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ╎
  351. // ╎ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ╎
  352. // ╎ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ╎
  353. // ╎ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ╎
  354. // ╎ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ╎
  355. // ╎ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ╎
  356. // ╎ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ╎
  357. // ╎ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ╎
  358. // ╎ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ╎
  359. // ╎ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ╎
  360. // ╎ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ╎
  361. // ╎ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ╎
  362. // └╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┘