back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/pacman.c
diff options
context:
space:
mode:
Diffstat (limited to 'pacman.c')
-rw-r--r--pacman.c168
1 files changed, 88 insertions, 80 deletions
diff --git a/pacman.c b/pacman.c
index 679fcc2..1468765 100644
--- a/pacman.c
+++ b/pacman.c
@@ -11,12 +11,13 @@ enum {
timeout_duration = 0,
sleep_duration = 190000,
key_escape = 27,
- count_get_out_moves = 6,
+ max_get_out_stage = 6,
chase_move_limit = 70,
scatter_move_limit = 35,
frightened_move_limit = 30,
phase_limit = 4,
- prison_limit = 30
+ prison_limit = 30,
+ max_score = 243
};
struct mode_type {
@@ -26,6 +27,7 @@ struct mode_type {
int frightened_count;
int phase_number;
int reverse_direction;
+ int get_out_stage;
};
struct game_params_type {
@@ -33,21 +35,6 @@ struct game_params_type {
int pause;
};
-static void initialize_params(struct game_params_type *basic_params)
-{
- srand(time(NULL));
- initscr();
- start_color();
- cbreak();
- noecho();
- curs_set(0);
- keypad(stdscr, 1);
- timeout(timeout_duration);
- start_color();
- basic_params->exit = 0;
- basic_params->pause = 0;
-}
-
static void initialize_ghosts(struct ghost_type *red_ghost,
struct ghost_type *pink_ghost,
struct ghost_type *blue_ghost,
@@ -59,7 +46,7 @@ static void initialize_ghosts(struct ghost_type *red_ghost,
initialize_ghost(orange_ghost, orange);
}
-static void initialize_modes(struct mode_type *mode_params, int *get_out_stage)
+static void initialize_modes(struct mode_type *mode_params)
{
mode_params->current_mode = scatter;
mode_params->chase_count = 0;
@@ -67,7 +54,26 @@ static void initialize_modes(struct mode_type *mode_params, int *get_out_stage)
mode_params->frightened_count = 0;
mode_params->phase_number = 0;
mode_params->reverse_direction = 0;
- *get_out_stage = count_get_out_moves;
+ mode_params->get_out_stage = max_get_out_stage;
+}
+
+static void initialize_params(game_space *field, struct queue *eaten_coins,
+ struct mode_type *mode_params, struct pacman *pac,
+ struct ghost_type *red_ghost,
+ struct ghost_type *pink_ghost,
+ struct ghost_type *blue_ghost,
+ struct ghost_type *orange_ghost)
+{
+ timeout(timeout_duration);
+ *field = get_new_field();
+ print_field(*field);
+ queue_init(eaten_coins);
+ initialize_pac(pac);
+ initialize_ghosts(red_ghost, pink_ghost, blue_ghost, orange_ghost);
+ initialize_modes(mode_params);
+ display_ghosts_on_field(red_ghost, pink_ghost, blue_ghost,
+ orange_ghost);
+ display_character(pac->position.y, pac->position.x, pac_char);
}
static int is_up_move_blocked(const struct ghost_type *ghost,
@@ -310,11 +316,11 @@ static int is_castling(struct pacman pac, struct ghost_type ghost)
}
static void catching_stage(game_space field,
- struct mode_type *mode_params, int *get_out_stage,
- struct pacman *pac, struct ghost_type *red_ghost,
- struct ghost_type *pink_ghost,
- struct ghost_type *blue_ghost,
- struct ghost_type *orange_ghost)
+ struct mode_type *mode_params, struct pacman *pac,
+ struct ghost_type *red_ghost,
+ struct ghost_type *pink_ghost,
+ struct ghost_type *blue_ghost,
+ struct ghost_type *orange_ghost)
{
struct ghost_type *current_ghost;
current_ghost = red_ghost;
@@ -358,8 +364,8 @@ static void catching_stage(game_space field,
catch_pac(pac);
if(pac->lives)
initialize_ghosts(red_ghost, pink_ghost, blue_ghost,
- orange_ghost);
- initialize_modes(mode_params, get_out_stage);
+ orange_ghost);
+ initialize_modes(mode_params);
usleep(sleep_duration);
return;
}
@@ -368,39 +374,32 @@ static void catching_stage(game_space field,
current_ghost->capture_info.status = 0;
}
}
-static void show_defeat_screen()
+
+static void final_stage(struct game_params_type *game_options, int win)
{
- move(17, 9);
- printw("GAME OVER");
+ int key;
+ if(win) {
+ move(17, 10);
+ printw("YOU WIN");
+ } else {
+ move(17, 9);
+ printw("GAME OVER");
+ }
+ move(14, 11);
+ printw("RETRY?");
+ move(15, 12);
+ printw("Y/N");
refresh();
+ timeout(-1);
+ while((key = getch()) != 'y' && key != 'n')
+ {}
+ game_options->exit = (key == 'y') ? 0 : 1;
}
-static void restart_game(struct game_params_type *basic_params,
- game_space field, struct queue *eaten_coins,
- struct pacman *pac, struct ghost_type *red_ghost,
- struct ghost_type *pink_ghost,
- struct ghost_type *blue_ghost,
- struct ghost_type *orange_ghost)
+static void clear_field_and_queue(game_space field, struct queue *eaten_coins)
{
- initialize_params(basic_params);
- initialize_pac(pac);
- initialize_ghosts(red_ghost, pink_ghost, blue_ghost, orange_ghost);
clear_field(field);
- field = get_new_field();
- print_field(field);
queue_clear(eaten_coins);
- queue_init(eaten_coins);
-}
-
-static void defeat_stage(struct game_params_type *basic_params)
-{
- int key;
- show_defeat_screen();
- timeout(-1);
- while((key = getch()) != 'y' && key != 'n')
- {}
- if(key == 'y')
- basic_params->exit = 0;
}
int main()
@@ -410,20 +409,21 @@ int main()
struct pacman pac;
struct queue eaten_coins;
struct mode_type mode_params;
- struct game_params_type basic_params;
- int key, get_out_stage;
+ struct game_params_type game_options;
+ int key;
enum movement_direction stored_direction;
+ srand(time(NULL));
+ initscr();
+ start_color();
+ cbreak();
+ noecho();
+ curs_set(0);
+ keypad(stdscr, 1);
stored_direction = none;
- initialize_params(&basic_params);
- field = get_new_field();
- print_field(field);
- initialize_ghosts(&red_ghost, &pink_ghost, &blue_ghost, &orange_ghost);
- initialize_pac(&pac);
- initialize_modes(&mode_params, &get_out_stage);
- 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);
+ game_options.exit = 0;
+ game_options.pause = 0;
+ initialize_params(&field, &eaten_coins, &mode_params, &pac, &red_ghost,
+ &pink_ghost, &blue_ghost, &orange_ghost);
usleep(sleep_duration);
while((key = getch()) != key_escape) {
/*
@@ -442,9 +442,9 @@ int main()
/*
* ghosts
*/
- if(get_out_stage)
- pull_out_ghosts(&get_out_stage, &red_ghost, &pink_ghost,
- &blue_ghost, &orange_ghost);
+ if(mode_params.get_out_stage)
+ pull_out_ghosts(&mode_params.get_out_stage, &red_ghost,
+ &pink_ghost, &blue_ghost, &orange_ghost);
else if(mode_params.reverse_direction) {
reverse_all_ghosts(&red_ghost, &pink_ghost,
&blue_ghost, &orange_ghost);
@@ -461,15 +461,19 @@ int main()
&blue_ghost, &orange_ghost);
make_ghost_moves(field, &red_ghost, &pink_ghost, &blue_ghost,
&orange_ghost, &eaten_coins);
-
/*
- * displaying characters
+ * catching
+ */
+ catching_stage(field, &mode_params, &pac, &red_ghost, &pink_ghost,
+ &blue_ghost, &orange_ghost);
+ /*
+ * displaying characters and score
*/
- catching_stage(field, &mode_params, &get_out_stage, &pac,
- &red_ghost, &pink_ghost, &blue_ghost, &orange_ghost);
display_ghosts_on_field(&red_ghost, &pink_ghost, &blue_ghost,
&orange_ghost);
display_character(pac.position.y, pac.position.x, pac_char);
+ display_score(pac.score);
+
usleep(sleep_duration);
prison_leaving_stage(field, &red_ghost, &pink_ghost, &blue_ghost,
&orange_ghost);
@@ -486,17 +490,21 @@ int main()
printw("LIVES: %d", pac.lives);
refresh();
#endif
- if(!pac.lives) {
- defeat_stage(&basic_params);
- if(basic_params.exit)
- return 0;
- else
- restart_game(&basic_params, field, &eaten_coins, &pac,
- &red_ghost, &pink_ghost, &blue_ghost,
- &orange_ghost);
+ if(!pac.lives || pac.score == max_score ) {
+ final_stage(&game_options, !pac.lives ? 0 : 1);
+ if(game_options.exit)
+ break;
+ else {
+ stored_direction = none;
+ clear_field_and_queue(field, &eaten_coins);
+ initialize_params(&field, &eaten_coins, &mode_params, &pac,
+ &red_ghost, &pink_ghost, &blue_ghost,
+ &orange_ghost);
+ usleep(sleep_duration);
+ }
}
}
- queue_clear(&eaten_coins);
+ clear_field_and_queue(field, &eaten_coins);
endwin();
return 0;
}