xero -- little terminal based rogue-like game for testing xurses... I promise I'll update this when it's finished...
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

107 行
2.8KB

  1. /*
  2. Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
  3. Xero is free software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation.
  4. And when you do redistribute it or modify it, it will use either version 3 of the License, or (at yours truly opinion) any later version.
  5. It is distributed in the hope that it will be useful or harmful, it really depends... But no warranty what so ever, seriously. See GNU/GPLv3.
  6. */
  7. #include <xolatile/xtandard.c>
  8. #include <xolatile/xurses.c>
  9. enum {
  10. ENTITY_TYPE_NONE,
  11. ENTITY_TYPE_FOOD,
  12. ENTITY_TYPE_COUNT
  13. };
  14. enum {
  15. ENTITY_NULL,
  16. ENTITY_APPLE,
  17. ENTITY_LEMON,
  18. ENTITY_WATERMELON,
  19. ENTITY_PLAYER,
  20. ENTITY_COUNT
  21. };
  22. static int map_width = 0;
  23. static int map_height = 0;
  24. static int * map = NULL;
  25. static int player_x = 0;
  26. static int player_y = 0;
  27. static struct {
  28. int type;
  29. char symbol;
  30. char symbol_alternative [3];
  31. int effect;
  32. int colour;
  33. char * name;
  34. int value;
  35. int logic;
  36. } const entity_data [ENTITY_COUNT] = {
  37. { ENTITY_TYPE_NONE, '.', " ", EFFECT_NORMAL, COLOUR_WHITE, "-", 0, 0 },
  38. { ENTITY_TYPE_FOOD, 'o', " ", EFFECT_BOLD, COLOUR_RED, "Apple", 2, 0 },
  39. { ENTITY_TYPE_FOOD, 'o', " ", EFFECT_BOLD, COLOUR_YELLOW, "Lemon", 3, 0 },
  40. { ENTITY_TYPE_FOOD, 'O', " ", EFFECT_BOLD, COLOUR_GREEN, "Watermelon", 5, 0 },
  41. { ENTITY_TYPE_NONE, '@', " ", EFFECT_BOLD, COLOUR_BLUE, "I", 0, 1 }
  42. };
  43. static void player_move_up (void) { --player_y; limit (& player_y, 0, map_height - 1); }
  44. static void player_move_down (void) { ++player_y; limit (& player_y, 0, map_height - 1); }
  45. static void player_move_left (void) { --player_x; limit (& player_x, 0, map_width - 1); }
  46. static void player_move_right (void) { ++player_x; limit (& player_x, 0, map_width - 1); }
  47. static void map_render (void) {
  48. int i = 0;
  49. int j = 0;
  50. map [player_y * map_width + player_x] = ENTITY_PLAYER;
  51. for (i = 0; i != map_height; ++i) {
  52. for (j = 0; j != map_width; ++j) {
  53. int index = map [i * map_width + j];
  54. char symbol = entity_data [index]. symbol;
  55. int effect = entity_data [index]. effect;
  56. int colour = entity_data [index]. colour;
  57. curses_render_character (symbol, colour, effect, j, i);
  58. }
  59. }
  60. }
  61. int main (void) {
  62. terminal_show_cursor (0);
  63. curses_initialize ();
  64. map_width = curses_screen_width;
  65. map_height = curses_screen_height;
  66. map = allocate (map_width * map_height * (int) sizeof (* map));
  67. curses_bind ('Q', curses_exit);
  68. curses_bind ('w', player_move_up);
  69. curses_bind ('s', player_move_down);
  70. curses_bind ('a', player_move_left);
  71. curses_bind ('d', player_move_right);
  72. while (curses_active != 0) {
  73. map_render ();
  74. curses_synchronize ();
  75. }
  76. curses_deinitialize ();
  77. map = deallocate (map);
  78. terminal_show_cursor (1);
  79. echo ("Good byte hero! (:\n");
  80. return (EXIT_SUCCESS);
  81. }