From 155a3c5f91c7a3bd89febfeb9927478961f5ee28 Mon Sep 17 00:00:00 2001 From: scratko Date: Sun, 7 Apr 2024 22:40:47 +0300 Subject: Tracking coins eaten The initialization of ncurses parameters and ghosts was put into functions. The order of movements has been changed: now pacman moves first. Accounting for eaten coins. Correct coin erasure. --- pacman.c | 54 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 19 deletions(-) (limited to 'pacman.c') diff --git a/pacman.c b/pacman.c index 5b5b50a..9346055 100644 --- a/pacman.c +++ b/pacman.c @@ -25,15 +25,19 @@ static void pathfinder_stage(game_space field, struct pacman pac, redirect(field, intersection, red_ghost, pac, breadth_first_search); } -int main() +static void initialize_ghosts(struct ghost_type *red_ghost, + struct ghost_type *pink_ghost, + struct ghost_type *blue_ghost, + struct ghost_type *orange_ghost) +{ + initialize_ghost(red_ghost, red); + initialize_ghost(pink_ghost, pink); + initialize_ghost(blue_ghost, blue); + initialize_ghost(orange_ghost, orange); +} + +static void ncurses_params() { - game_space field = NULL; - struct ghost_type red_ghost, pink_ghost, blue_ghost, orange_ghost; - struct pacman pac; - int key, get_out_stage; - enum movement_direction stored_direction; - stored_direction = none; - get_out_stage = count_get_out_moves; initscr(); start_color(); cbreak(); @@ -42,36 +46,48 @@ int main() keypad(stdscr, 1); timeout(timeout_duration); start_color(); +} + +int main() +{ + game_space field = NULL; + struct ghost_type red_ghost, pink_ghost, blue_ghost, orange_ghost; + struct pacman pac; + struct queue eaten_coins; + int key, get_out_stage; + enum movement_direction stored_direction; + stored_direction = none; + get_out_stage = count_get_out_moves; + ncurses_params(); field = get_new_field(); print_field(field); - initialize_ghost(&red_ghost, red); - initialize_ghost(&pink_ghost, pink); - initialize_ghost(&blue_ghost, blue); - initialize_ghost(&orange_ghost, orange); + initialize_ghosts(&red_ghost, &pink_ghost, &blue_ghost, &orange_ghost); initialize_pac(&pac); + queue_init(&eaten_coins); display_ghosts_on_field(&red_ghost, &pink_ghost, &blue_ghost, &orange_ghost); display_character(pac.position.y, pac.position.x, pac_char); usleep(sleep_duration); while((key = getch()) != key_escape) { + if(key != ERR) + change_pac_direction(field, &pac, key, &stored_direction); + else + check_remaining_direction(field, &pac, &stored_direction); if(get_out_stage) pull_out_ghosts(&get_out_stage, &red_ghost, &pink_ghost, &blue_ghost, &orange_ghost); else pathfinder_stage(field, pac, &red_ghost, &pink_ghost, &blue_ghost, &orange_ghost); - if(key != ERR) - change_pac_direction(field, &pac, key, &stored_direction); - else - check_remaining_direction(field, &pac, &stored_direction); + make_pac_move(field, &pac, &eaten_coins); make_ghost_moves(field, &red_ghost, &pink_ghost, &blue_ghost, - &orange_ghost); - make_pac_move(field, &pac); + &orange_ghost, &eaten_coins); + display_character(pac.position.y, pac.position.x, pac_char); display_ghosts_on_field(&red_ghost, &pink_ghost, &blue_ghost, &orange_ghost); - display_character(pac.position.y, pac.position.x, pac_char); usleep(sleep_duration); } + queue_clear(&eaten_coins); endwin(); return 0; } -- cgit v1.2.3