|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Function to display the animation |
| 4 | +function display_animation() { |
| 5 | + clear |
| 6 | + echo "G C" |
| 7 | + sleep 0.3 |
| 8 | + clear |
| 9 | + echo "G C" |
| 10 | + sleep 0.3 |
| 11 | + clear |
| 12 | + echo "G C" |
| 13 | + sleep 0.3 |
| 14 | + clear |
| 15 | + echo "G C" |
| 16 | + sleep 0.3 |
| 17 | + clear |
| 18 | + echo G C |
| 19 | + sleep 0.3 |
| 20 | + clear |
| 21 | + echo GC |
| 22 | + sleep 0.3 |
| 23 | +} |
| 24 | + |
| 25 | +# Start the animation |
| 26 | +display_animation |
| 27 | + |
| 28 | +# After the animation, you can call your start_game function or any other function to continue with the game. |
| 29 | + |
| 30 | +# Function to start the game |
| 31 | +function start_game() { |
| 32 | + # Pac-Man clone in Bash |
| 33 | + |
| 34 | + # Initialize variables |
| 35 | + player="C" |
| 36 | + ghost="G" |
| 37 | + empty=" " |
| 38 | + wall="#" |
| 39 | + food="." |
| 40 | + score=0 |
| 41 | + |
| 42 | + # Define the game board |
| 43 | + declare -a board=( |
| 44 | + "################################" |
| 45 | + "#C #" |
| 46 | + "# #" |
| 47 | + "# #" |
| 48 | + "# #" |
| 49 | + "# #" |
| 50 | + "# #" |
| 51 | + "# #" |
| 52 | + "# #" |
| 53 | + "# #" |
| 54 | + "################################" |
| 55 | + ) |
| 56 | + |
| 57 | + # Function to print the game board |
| 58 | + function print_board() { |
| 59 | + clear |
| 60 | + for row in "${board[@]}"; do |
| 61 | + echo "$row" |
| 62 | + done |
| 63 | + echo "Score: $score" |
| 64 | + } |
| 65 | + |
| 66 | + # Function to check if a given position is valid on the board |
| 67 | + function is_valid_position() { |
| 68 | + local row=$1 |
| 69 | + local col=$2 |
| 70 | + [[ $row -ge 0 && $row -lt ${#board[@]} && $col -ge 0 && $col -lt ${#board[0]} ]] |
| 71 | + } |
| 72 | + |
| 73 | + # Function to move the player |
| 74 | + function move_player() { |
| 75 | + local new_row=$((player_row + $1)) |
| 76 | + local new_col=$((player_col + $2)) |
| 77 | + |
| 78 | + if is_valid_position $new_row $new_col && [[ ${board[$new_row]:$new_col:1} != "$wall" ]]; then |
| 79 | + if [[ ${board[$new_row]:$new_col:1} == "$food" ]]; then |
| 80 | + ((score++)) |
| 81 | + fi |
| 82 | + board[$player_row]=${board[$player_row]:0:$player_col}$empty${board[$player_row]:$((player_col + 1))} |
| 83 | + player_row=$new_row |
| 84 | + player_col=$new_col |
| 85 | + board[$player_row]=${board[$player_row]:0:$player_col}$player${board[$player_row]:$((player_col + 1))} |
| 86 | + fi |
| 87 | + } |
| 88 | + |
| 89 | + # Function to move the ghosts |
| 90 | + function move_ghosts() { |
| 91 | + local player_distance=1000 |
| 92 | + local next_row=$ghost_row |
| 93 | + local next_col=$ghost_col |
| 94 | + |
| 95 | + local dirs=(-1 0 1 0 0 -1 0 1) |
| 96 | + |
| 97 | + # Find the direction that minimizes distance to the player |
| 98 | + for ((dir=0;dir<4;dir++)); do |
| 99 | + local dr=$((ghost_row + dirs[dir * 2])) |
| 100 | + local dc=$((ghost_col + dirs[dir * 2 + 1])) |
| 101 | + local dist=$(( (player_row - dr) ** 2 + (player_col - dc) ** 2 )) |
| 102 | + if [[ $dist -lt $player_distance ]]; then |
| 103 | + player_distance=$dist |
| 104 | + next_row=$dr |
| 105 | + next_col=$dc |
| 106 | + fi |
| 107 | + done |
| 108 | + |
| 109 | + # Move the ghost |
| 110 | + if is_valid_position $next_row $next_col && [[ ${board[$next_row]:$next_col:1} != "$wall" ]]; then |
| 111 | + board[$ghost_row]=${board[$ghost_row]:0:$ghost_col}$empty${board[$ghost_row]:$((ghost_col + 1))} |
| 112 | + ghost_row=$next_row |
| 113 | + ghost_col=$next_col |
| 114 | + board[$ghost_row]=${board[$ghost_row]:0:$ghost_col}$ghost${board[$ghost_row]:$((ghost_col + 1))} |
| 115 | + fi |
| 116 | + } |
| 117 | + |
| 118 | + # Function to spawn ghosts at random positions |
| 119 | + function spawn_ghosts() { |
| 120 | + ghost_row=$((RANDOM % (${#board[@]} - 2) + 1)) |
| 121 | + ghost_col=$((RANDOM % (${#board[0]} - 2) + 1)) |
| 122 | + board[$ghost_row]=${board[$ghost_row]:0:$ghost_col}$ghost${board[$ghost_row]:$((ghost_col + 1))} |
| 123 | + } |
| 124 | + |
| 125 | + # Function to spawn food at random positions |
| 126 | + function spawn_food() { |
| 127 | + local row |
| 128 | + local col |
| 129 | + local num_food=20 # Adjust the number of food items here |
| 130 | + for ((i=0; i<num_food; i++)); do |
| 131 | + row=$((RANDOM % (${#board[@]} - 2) + 1)) |
| 132 | + col=$((RANDOM % (${#board[0]} - 2) + 1)) |
| 133 | + if [[ ${board[$row]:$col:1} == "$empty" ]]; then |
| 134 | + board[$row]=${board[$row]:0:$col}$food${board[$row]:$((col + 1))} |
| 135 | + fi |
| 136 | + done |
| 137 | + } |
| 138 | + |
| 139 | + # Function to check if the game is over with a timer |
| 140 | + function game_over_with_timer() { |
| 141 | + print_board |
| 142 | + local answer |
| 143 | + while true; do |
| 144 | + read -p "You died! Do you want to restart? (yes/no): " answer |
| 145 | + if [[ "$answer" == "yes" || "$answer" == "no" ]]; then |
| 146 | + break |
| 147 | + else |
| 148 | + echo "Please type 'yes' or 'no'" |
| 149 | + fi |
| 150 | + done |
| 151 | + |
| 152 | + if [ "$answer" == "yes" ]; then |
| 153 | + reset_game |
| 154 | + else |
| 155 | + echo "Goodbye!" |
| 156 | + exit |
| 157 | + fi |
| 158 | + } |
| 159 | + |
| 160 | + # Function to display the victory message |
| 161 | + function game_over_with_victory() { |
| 162 | + print_board |
| 163 | + echo "Congratulations! You collected all the food. You win!" |
| 164 | + read -p "Press Enter to return to the main menu or type 'exit' to quit: " answer |
| 165 | + if [ "$answer" == "" ]; then |
| 166 | + main_menu |
| 167 | + elif [ "$answer" == "exit" ]; then |
| 168 | + echo "Goodbye!" |
| 169 | + exit |
| 170 | + else |
| 171 | + echo "Invalid input. Returning to the main menu." |
| 172 | + main_menu |
| 173 | + fi |
| 174 | + } |
| 175 | + |
| 176 | + # Function to reset the game |
| 177 | + function reset_game() { |
| 178 | + player_row=1 |
| 179 | + player_col=1 |
| 180 | + score=0 |
| 181 | + board=( |
| 182 | + "################################" |
| 183 | + "#C #" |
| 184 | + "# #" |
| 185 | + "# #" |
| 186 | + "# #" |
| 187 | + "# #" |
| 188 | + "# #" |
| 189 | + "# #" |
| 190 | + "# #" |
| 191 | + "# #" |
| 192 | + "################################" |
| 193 | + ) |
| 194 | + spawn_ghosts |
| 195 | + spawn_food |
| 196 | + player_died=false # Reset the player_died flag |
| 197 | + } |
| 198 | + |
| 199 | + # Initialize player position |
| 200 | + player_row=1 |
| 201 | + player_col=1 |
| 202 | + |
| 203 | + # Initialize enemy directions |
| 204 | + dirs=(-1 0 1 0 0 -1 0 1) |
| 205 | + |
| 206 | + # Spawn ghosts |
| 207 | + spawn_ghosts |
| 208 | + |
| 209 | + # Spawn food |
| 210 | + spawn_food |
| 211 | + |
| 212 | + # Flag to track if the player has died |
| 213 | + player_died=false |
| 214 | + |
| 215 | + # Main game loop |
| 216 | + while true; do |
| 217 | + print_board |
| 218 | + |
| 219 | + # Read user input |
| 220 | + read -rsn1 direction |
| 221 | + |
| 222 | + # Move player based on input |
| 223 | + case $direction in |
| 224 | + "w") move_player -1 0 ;; |
| 225 | + "s") move_player 1 0 ;; |
| 226 | + "a") move_player 0 -1 ;; |
| 227 | + "d") move_player 0 1 ;; |
| 228 | + "q") echo "Goodbye!"; exit ;; |
| 229 | + "o") main_menu ;; |
| 230 | + *) ;; |
| 231 | + esac |
| 232 | + |
| 233 | + # Move ghosts |
| 234 | + move_ghosts |
| 235 | + |
| 236 | + # Check if the player is dead |
| 237 | + if $player_died; then |
| 238 | + game_over_with_timer |
| 239 | + fi |
| 240 | + |
| 241 | + # Check if the player is dead after the move |
| 242 | + if [[ ${board[$player_row]:$player_col:1} == "$ghost" ]]; then |
| 243 | + player_died=true |
| 244 | + fi |
| 245 | + |
| 246 | + # Check if all food is collected |
| 247 | + if [[ $(grep -o "$food" <<< "${board[*]}") == "" ]]; then |
| 248 | + game_over_with_victory |
| 249 | + fi |
| 250 | + done |
| 251 | +} |
| 252 | + |
| 253 | +# Function to display help information |
| 254 | +function display_help() { |
| 255 | + clear |
| 256 | + echo "made by stuffbymax" |
| 257 | + echo "------------------" |
| 258 | + echo "Game Instructions" |
| 259 | + echo "------------------" |
| 260 | + echo "Player character: (C)" |
| 261 | + echo |
| 262 | + echo "Avoid the ghost (G) while collecting food (.) to increase your score." |
| 263 | + echo |
| 264 | + echo "Controls:" |
| 265 | + echo "------------------" |
| 266 | + echo "W: Move Up" |
| 267 | + echo "A: Move Left" |
| 268 | + echo "S: Move Down" |
| 269 | + echo "D: Move Right" |
| 270 | + echo |
| 271 | + echo "In-game shortcuts:" |
| 272 | + echo "------------------" |
| 273 | + echo "Q: Exit full terminal" |
| 274 | + echo "Ctrl: Exit only the program" |
| 275 | + echo "o: Return to the main menu" |
| 276 | + echo "--------------------------" |
| 277 | + echo "Press Enter to continue..." |
| 278 | + read -r |
| 279 | +} |
| 280 | + |
| 281 | +# Function to exit the script |
| 282 | +function exit_game() { |
| 283 | + echo "Goodbye!" |
| 284 | + exit |
| 285 | +} |
| 286 | + |
| 287 | +# Function to display the main menu |
| 288 | +function main_menu() { |
| 289 | + while true; do |
| 290 | + # Main menu |
| 291 | + clear |
| 292 | + echo "Options:" |
| 293 | + echo "1. Start" |
| 294 | + echo "2. Help" |
| 295 | + echo "3. Exit" |
| 296 | + |
| 297 | + # Wait for any key press |
| 298 | + read -n 1 key |
| 299 | + |
| 300 | + # Read user input for option |
| 301 | + read -n 1 option |
| 302 | + |
| 303 | + case $option in |
| 304 | + "1") start_game ;; |
| 305 | + "2") display_help ;; |
| 306 | + "3") exit_game ;; |
| 307 | + *) ;; |
| 308 | + esac |
| 309 | + done |
| 310 | +} |
| 311 | + |
| 312 | +# Start the main menu |
| 313 | +main_menu |
0 commit comments