My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

maze.cpp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../../../inc/MarlinConfigPre.h"
  23. #if ENABLED(MARLIN_MAZE)
  24. #include "game.h"
  25. int8_t move_dir, last_move_dir, // NESW0
  26. prizex, prizey, prize_cnt, old_encoder;
  27. fixed_t playerx, playery;
  28. // Up to 50 lines, then you win!
  29. typedef struct { int8_t x, y; } pos_t;
  30. uint8_t head_ind;
  31. pos_t maze_walls[50] = {
  32. { 0, 0 }
  33. };
  34. // Turn the player cw or ccw
  35. inline void turn_player(const bool cw) {
  36. if (move_dir == 4) move_dir = last_move_dir;
  37. move_dir += cw ? 1 : -1;
  38. move_dir &= 0x03;
  39. last_move_dir = move_dir;
  40. }
  41. // Reset the player for a new game
  42. void player_reset() {
  43. // Init position
  44. playerx = BTOF(1);
  45. playery = BTOF(GAME_H / 2);
  46. // Init motion with a ccw turn
  47. move_dir = 0;
  48. turn_player(false);
  49. // Clear prize flag
  50. prize_cnt = 255;
  51. // Clear the controls
  52. ui.encoderPosition = 0;
  53. old_encoder = 0;
  54. }
  55. void MazeGame::game_screen() {
  56. // Run the sprite logic
  57. if (game_frame()) do { // Run logic twice for finer resolution
  58. // Move the man one unit in the current direction
  59. // Direction index 4 is for the stopped man
  60. const int8_t oldx = FTOB(playerx), oldy = FTOB(playery);
  61. pos_t dir_add[] = { { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, 0 } };
  62. playerx += dir_add[move_dir].x;
  63. playery += dir_add[move_dir].y;
  64. const int8_t x = FTOB(playerx), y = FTOB(playery);
  65. } while(0);
  66. u8g.setColorIndex(1);
  67. // Draw Score
  68. if (PAGE_UNDER(HEADER_H)) lcd_put_int(0, HEADER_H - 1, score);
  69. // Draw the maze
  70. // LOOP_L_N(n, head_ind) {
  71. // const pos_t &p = maze_walls[n], &q = maze_walls[n + 1];
  72. // if (p.x == q.x) {
  73. // const int8_t y1 = GAMEY(_MIN(p.y, q.y)), y2 = GAMEY(_MAX(p.y, q.y));
  74. // if (PAGE_CONTAINS(y1, y2))
  75. // u8g.drawVLine(GAMEX(p.x), y1, y2 - y1 + 1);
  76. // }
  77. // else if (PAGE_CONTAINS(GAMEY(p.y), GAMEY(p.y))) {
  78. // const int8_t x1 = GAMEX(_MIN(p.x, q.x)), x2 = GAMEX(_MAX(p.x, q.x));
  79. // u8g.drawHLine(x1, GAMEY(p.y), x2 - x1 + 1);
  80. // }
  81. // }
  82. // Draw Man
  83. // const int8_t fy = GAMEY(foody);
  84. // if (PAGE_CONTAINS(fy, fy + FOOD_WH - 1)) {
  85. // const int8_t fx = GAMEX(foodx);
  86. // u8g.drawFrame(fx, fy, FOOD_WH, FOOD_WH);
  87. // if (FOOD_WH == 5) u8g.drawPixel(fx + 2, fy + 2);
  88. // }
  89. // Draw Ghosts
  90. // const int8_t fy = GAMEY(foody);
  91. // if (PAGE_CONTAINS(fy, fy + FOOD_WH - 1)) {
  92. // const int8_t fx = GAMEX(foodx);
  93. // u8g.drawFrame(fx, fy, FOOD_WH, FOOD_WH);
  94. // if (FOOD_WH == 5) u8g.drawPixel(fx + 2, fy + 2);
  95. // }
  96. // Draw Prize
  97. // if (PAGE_CONTAINS(prizey, prizey + PRIZE_WH - 1)) {
  98. // u8g.drawFrame(prizex, prizey, PRIZE_WH, PRIZE_WH);
  99. // if (PRIZE_WH == 5) u8g.drawPixel(prizex + 2, prizey + 2);
  100. // }
  101. // Draw GAME OVER
  102. if (!game_state) draw_game_over();
  103. // A click always exits this game
  104. if (ui.use_click()) exit_game();
  105. }
  106. void MazeGame::enter_game() {
  107. init_game(1, game_screen); // Game running
  108. reset_player();
  109. reset_enemies();
  110. }
  111. #endif // MARLIN_MAZE