πŸš€ Section 14: Main Loop + Entry Point

πŸ“ Summary (What you will do)

In this section, you will wire everything together and make the game run. You will:

  • Build the main_loop() function
  • Show the dashboard each turn and process player choices
  • Add the entry point so the program starts correctly

This is the moment where all your classes and functions become a working game.


βœ… Checklist (You must complete these)

  • Open pet_manager.py
  • Scroll to the very bottom of the file
  • Add main_loop() and the entry point exactly as shown
  • Type the code by hand so you understand how the loop works

βœ… No new constants/settings are added in this section, so you do not need to edit the top-of-file constants.


πŸŽ“ Core Concepts (New learning for this section)

1) The main game loop

A game loop repeats forever until the player quits.
Each turn does the same pattern:

  1. Show the current state
  2. Ask the player what to do
  3. Run the chosen action
  4. Advance time

This loop is the heartbeat of the program.

2) Connecting UI and game logic

Inside the loop we call:

  • show_dashboard() to display info
  • choose_from() to get a valid menu choice
  • care_flow() or adopt_flow() to run actions
  • tick_all() to advance time

This ties the UI helpers to the game objects you built.

3) The entry point

if __name__ == "__main__":

This block ensures the game only starts when the file is run directly, not when it’s imported by tests.


πŸ’» Code to Write (Type this by hand in pet_manager.py)

Directions:

  1. Open pet_manager.py
  2. Scroll to the very bottom of the file
  3. Type the following code by hand:

Code image: s14-code


πŸ˜€ Emoji Copy/Paste List

Use these emojis exactly as shown in the code:

  • πŸ˜„

🧠 Code Review & Key Concepts (What important lines do)

Starting the game

player = Player(name)

This creates the Player object that will own all pets.

The main menu

choice = choose_from(...)

This uses your input helper to get a safe menu choice.

Quit behavior

sys.exit(0)

This cleanly exits the program when the player chooses β€œQuit.”

Entry point guard

if __name__ == "__main__":

This ensures main_loop() runs only when the file is executed directly.