summaryrefslogtreecommitdiff
path: root/game.c
diff options
context:
space:
mode:
authorMatheus <matheus.guedes.mg.m@gmail.com>2025-10-19 14:52:49 -0300
committerMatheus <matheus.guedes.mg.m@gmail.com>2025-10-19 14:52:49 -0300
commitc100e56944590985ed8ecbdcbc5f0d27b6542767 (patch)
tree9488c54256350055ee15592a0a573b8c5e86ef91 /game.c
parenta38868de0918da3979d2f7115f9164c979a48346 (diff)
feat:the game
Kinda forgot commit.
Diffstat (limited to 'game.c')
-rw-r--r--game.c104
1 files changed, 104 insertions, 0 deletions
diff --git a/game.c b/game.c
new file mode 100644
index 0000000..7a213c3
--- /dev/null
+++ b/game.c
@@ -0,0 +1,104 @@
+#include <locale.h>
+#include <ncurses.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include "snake.h"
+
+#define DEFAULT_WIDTH 30
+#define DEFAULT_HEIGHT 25
+
+/* draws the game map. Excepts a window large enough to print the map */
+static void draw(WINDOW *display, Map *map) {
+ werase(display);
+
+ box(display, 0, 0);
+
+ if (!map->fruit_eaten) {
+ mvwaddch(display, map->fruit.y, map->fruit.x, '*');
+ }
+
+ for (PointList *i = map->snake->tail; i != NULL; i = i->next) {
+ mvwaddch(display, i->point.y, i->point.x, '#');
+ }
+
+ wrefresh(display);
+}
+
+int main(int argc, char **argv) {
+ setlocale(LC_ALL, "");
+
+ initscr();
+ cbreak();
+ noecho();
+ nodelay(stdscr, true);
+ curs_set(0);
+
+ const unsigned short target_fps = 5;
+ bool quit = false;
+
+ Map *map;
+ if (argc == 3) {
+ unsigned int width = (unsigned int)strtol(argv[1], NULL, 10);
+ unsigned int height = (unsigned int)strtol(argv[2], NULL, 10);
+ map = create_map(width, height);
+ } else {
+ map = create_map(DEFAULT_WIDTH, DEFAULT_HEIGHT);
+ }
+
+ if (map == NULL) {
+ fprintf(stderr, "ERROR: Failed to allocate map");
+ endwin();
+ exit(1);
+ }
+
+ WINDOW *map_window = newwin(map->height, map->width, (LINES/2) - (map->height/2), (COLS/2) - (map->width/2));
+
+ draw(map_window, map);
+
+ while (!quit) {
+ int key;
+
+ /* Get Input */
+ key = getch();
+
+ switch(key) {
+ case 'w':
+ map->snake->moving_direction = NORTH;
+ break;
+ case 'a':
+ map->snake->moving_direction = WEST;
+ break;
+ case 's':
+ map->snake->moving_direction = SOUTH;
+ break;
+ case 'd':
+ map->snake->moving_direction = EAST;
+ break;
+ case 'q':
+ quit = true;
+ break;
+ }
+
+ /* Process game logic */
+ if (!move_snake(map)) {
+ nodelay(stdscr, false);
+ /* TODO: Proper game over logic */
+ clear();
+ mvprintw(0, 0, "Game Over");
+ getch();
+ quit = true;
+ }
+
+ /* Draw frame */
+ if (!quit) draw(map_window, map);
+
+ /* TODO: calculate a proper wait time */
+ napms((1.0f/(float)target_fps) * 1000);
+ }
+
+ free_map(map);
+
+ delwin(map_window);
+ endwin();
+ return 0;
+}