From acdbbac995f5525eb1eb8143eb9d7554eb9221f8 Mon Sep 17 00:00:00 2001 From: Ivan Davydov Date: Sat, 5 Apr 2025 18:03:37 +0300 Subject: Initial commit --- clicker-ncurses.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 clicker-ncurses.c (limited to 'clicker-ncurses.c') diff --git a/clicker-ncurses.c b/clicker-ncurses.c new file mode 100644 index 0000000..6adf5bf --- /dev/null +++ b/clicker-ncurses.c @@ -0,0 +1,111 @@ +#include +#include +#include +#include + +struct game +{ + int click; + int multiplifier; + int score; +}; + +int get_score (struct game *game); +void *set_score (struct game *game, int score); +int get_click (struct game *game); +void *set_click (struct game *game, int click); +int get_multiplifier (struct game *game); + +int +main (void) +{ + /* Initialize NCurses screen and get the default window, stdscr */ + if ( ! initscr() ) + { + printf("Too little amount of memory\n"); + return 1; + } + + /* This fixes the problem of input that shows up */ + noecho(); + + /* Place a border with default settings (0 means default) */ + border(0, 0, 0, 0, 0, 0, 0, 0); + + /* Get window size */ + int sy, sx; + getmaxyx(stdscr, sy, sx); + + /* Place a string onto the window in specified location */ + char *name = "NCurses clicker"; + mvprintw(0, sx / 2 - strlen(name) / 2, name); + + char *help = "[ ] - click, [q]uit, [u]pgrade"; + mvprintw(sy - 2, sx / 2 - strlen(help) / 2, help); + + /* Refresh our window to include settings above */ + refresh(); + + struct game *cur_game = malloc(sizeof(struct game)); + cur_game->click = 1; + cur_game->multiplifier = 2; + + /* The main loop that also catches user input */ + int ch = 0; + while ( ch = getch() ) + { + if ( ch == ' ') + { + set_score(cur_game, get_score(cur_game) + get_click(cur_game)); + mvprintw(3, 1, "%d", get_score(cur_game)); + } + + if ( ch == 'q') + { + break; + } + + if ( ch == 'u') + { + if (get_score(cur_game) >= 2 * get_multiplifier(cur_game)) + { + set_score(cur_game, get_score(cur_game) - get_click(cur_game) * 2); + set_click(cur_game, get_click(cur_game) * get_multiplifier(cur_game)); + mvprintw(3, 1, "%d", get_score(cur_game)); + } + } + } + + /* Remove the main window and exit */ + endwin(); +} + +int +get_score (struct game *game) +{ + return game->score; +} + +void * +set_score (struct game *game, int score) +{ + game->score = score; +} + +int +get_click (struct game *game) +{ + return game->click; +} + +void * +set_click (struct game *game, int click) +{ + game->click = click; +} + +int +get_multiplifier (struct game *game) +{ + return game->multiplifier; +} -- cgit v1.2.3