snake_game.c 13 KB

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