Virtual Pet (Python OOP) — Multi-Pet with JSON (Codeboard)
OnlineGBD project: (Link)
Use the starter code provided in the OnlineGBD project. You will implement missing logic in pet.py and make small in Root/main.py as described below.
Learning Goals
- Build and use classes (attributes, methods)
- Manage program state with a game loop
- Persist data with JSON (save/load)
- Handle input validation and clean CLI UX
What You’ll Build
A text-based Virtual Pet game. Each pet has hunger, energy, and happiness that change over time. You can create multiple pets, switch the active pet, play with it, and save/load all pets to/from a JSON file.
Files in Codeboard
pet.py— thePetclass (you implement most logic here)Root/main.py— menus, multi-pet management, JSON save/load scaffolding and game loop
Minimum Requirements (MVP)
1) Pet class (pet.py)
Implement and/or tune:
- Attributes (initialize in
__init__):name,hunger(0–100; higher = hungrier),energy(0–100),happiness(0–100),age_days,alive
- Actions (return a short message string; clamp values to 0–100):
feed(self)play(self)sleep(self)pet(self)(affection)
- Time & life
tick(self)— called after every action; stats drift;age_daysincrements; call life checks_life_checks(self)— setalive = Falseon your chosen lose conditions (e.g., hunger ≥ 100 or energy ≤ 0)
- Reporting
status(self)— return a readable multi-line string of the pet’s current state (ASCII only)
- JSON serialization
to_dict(self)andfrom_dict(cls, data)— convert between Pet instances and plain dicts
2) Multiple Pets (in Root/main.py)
Main menu must let the player:
- Create a new pet
- Select active pet
- List pets
- Play with active pet (action loop)
- Save all pets to one JSON file (e.g.,
pets.json) - Load pets from that JSON file
3) Save/Load (JSON)
- Save all pets into a single JSON object
{name: pet_dict, ...} - Load from that file and reconstruct
Petobjects usingPet.from_dict - Handle missing/invalid files gracefully (don’t crash)
4) Game Loop & UX
- While the active pet is alive: show status → choose action → apply →
tick() - Validate input (no crashes on bad choices)
- End the pet’s loop if it dies (show an ASCII message)
Stretch Features (pick ≥ 2 for creativity points)
- Random events (found a toy, got sick, etc.)
- Simple ASCII moods (no Unicode)
- Items/inventory (treats, medicine)
- Aging/evolution milestones
- Per-pet save files in addition to the global save
- Basic unit tests (e.g.,
test_pet.pyin the project)
How to Run on Codeboard
- Open the project and click Run.
- Follow on-screen menus to Create Pet, Select Active Pet, then Play with Active Pet.
- Use Save All Pets to write
pets.jsonin the project. - Use Load Pets to load them back later.
If you see encoding errors, you still have a Unicode symbol somewhere. Replace em dashes (
—) with--, ellipsis (…) with..., and remove emojis.
JSON Examples (students can copy/paste)
Write a dict to JSON (already used by the starter):
import json
data = {"answer": 42, "things": [1, 2, 3]}
with open("pets.json", "w") as f:
json.dump(data, f, indent=2)
Read JSON back:
import json
with open("pets.json", "r") as f:
loaded = json.load(f)
# loaded is now a dict
Save pets using to_dict():
data = {}
for name in pets:
data[name] = pets[name].to_dict()
with open("pets.json", "w") as f:
json.dump(data, f, indent=2)
Load pets using from_dict():
from pet import Pet
import json
with open("pets.json", "r") as f:
raw = json.load(f)
pets = {}
for name in raw:
pets[name] = Pet.from_dict(raw[name])
Example pets.json:
{
"Fluffy": {
"name": "Fluffy",
"hunger": 25,
"energy": 82,
"happiness": 70,
"age_days": 3,
"alive": true
},
"Rex": {
"name": "Rex",
"hunger": 60,
"energy": 41,
"happiness": 55,
"age_days": 7,
"alive": true
}
}
What to Edit (Quick Checklist)
In pet.py
- Tune starting values in
__init__ - Implement stat changes in
feed,play,sleep,pet(remember to clamp) - Implement
tick(drift stats + aging +_life_checks) - Implement
_life_checks(your lose rules) - Keep all output ASCII in
status()(no emojis, no “smart” punctuation)
In Root/main.py
- Keep
save_all_pets/load_all_petsworking withpets.json - Make sure Select Active Pet and Play logic handles “no active pet” safely
Testing
In main at the bottom there is a call to test_pet_runner(). You can uncomment this, and comment out the call to main() to test the code. It should pass all tests.
Deliverables
- Your edited Codeboard project (starter files completed)
- A short README.txt describing:
- Lose conditions you chose
- How each action affects stats
- Stretch features implemented
- How to run, save, and load
Suggested Timeline
| Week | Tasks |
|---|---|
| 1 | Implement Pet actions, tick, _life_checks, and game loop |
| 2 | Add multi-pet menus, JSON save/load, and at least 2 stretch features |
Grading Rubric (100 pts)
| Category | Description | Points |
|---|---|---|
| OOP Design | Pet class with required attributes/methods; sensible clamping | 20 |
| Time & Life System | tick() drifts stats, ages pet, and enforces lose conditions | 15 |
| Game Loop & UX | Menus, input validation, readable status() | 15 |
| Multiple Pets | Create/select/list; clean switching and play flow | 15 |
| JSON Save/Load | Correctly saves all pets; loads into working Pet objects; robust handling | 20 |
| Code Quality | Clear structure, comments, meaningful names | 10 |
| Creativity/Stretch | ≥ 2 enhancements that improve gameplay | 5 |
| TOTAL | 100 |
Extra credit (+ up to 10): outstanding polish, novel mechanics, or strong tests.
Troubleshooting (OnlineGBD)
- JSON errors: ensure you’re writing a dict, not a string; verify file name
pets.json - Crash on bad input: wrap menus with a loop that re-prompts until a valid choice
