Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import time
- import random
- import os
- # Create directories if they don't exist
- os.makedirs("friends", exist_ok=True)
- os.makedirs("tamagotchis", exist_ok=True)
- # Tamagotchi Data Structure
- TAMAGOTCHI_TYPES = {
- "cat": {"name": "Whiskers", "hunger_rate": 3, "cleanliness_rate": 2},
- "dinosaur": {"name": "Tyranno", "hunger_rate": 5, "cleanliness_rate": 4},
- "dolphin": {"name": "Flipper", "hunger_rate": 4, "cleanliness_rate": 1},
- "ant": {"name": "Tiny", "hunger_rate": 1, "cleanliness_rate": 1},
- "robot": {"name": "Bolt", "hunger_rate": 2, "cleanliness_rate": 3},
- "pegasus": {"name": "Cloud", "hunger_rate": 4, "cleanliness_rate": 2},
- "alien": {"name": "Zyborg", "hunger_rate": 6, "cleanliness_rate": 5},
- "dragon": {"name": "Ember", "hunger_rate": 7, "cleanliness_rate": 3},
- "fox": {"name": "Rusty", "hunger_rate": 3, "cleanliness_rate": 2},
- "owl": {"name": "Hoot", "hunger_rate": 2, "cleanliness_rate": 1},
- "panda": {"name": "Bamboo", "hunger_rate": 4, "cleanliness_rate": 2},
- "penguin": {"name": "Waddle", "hunger_rate": 3, "cleanliness_rate": 1},
- "unicorn": {"name": "Sparkle", "hunger_rate": 2, "cleanliness_rate": 2},
- "wolf": {"name": "Howler", "hunger_rate": 5, "cleanliness_rate": 3},
- "dragonfly": {"name": "Zephyr", "hunger_rate": 1, "cleanliness_rate": 1}
- }
- # Toy Inventory System
- TOYS = {
- "ball": {"effect": lambda t: (t.update({"happiness": min(100, t["happiness"] + 15)}), "You played with the ball! πΎ")},
- "frisbee": {"effect": lambda t: (t.update({"happiness": min(100, t["happiness"] + 20), "energy": max(0, t["energy"] - 10)}), "You threw the frisbee! π₯")},
- "doll": {"effect": lambda t: (t.update({"happiness": min(100, t["happiness"] + 10)}), "You hugged your doll! π§Έ")},
- "remote": {"effect": lambda t: (t.update({"happiness": min(100, t["happiness"] + 5), "cleanliness": max(0, t["cleanliness"] - 5)}), "You smashed the remote! πΊ")},
- "bath_toy": {"effect": lambda t: (t.update({"cleanliness": min(100, t["cleanliness"] + 15)}), "You played in the bath! π")},
- "feather": {"effect": lambda t: (t.update({"happiness": min(100, t["happiness"] + 10), "energy": max(0, t["energy"] - 5)}), "You chased the feather! πͺΆ")},
- "tunnel": {"effect": lambda t: (t.update({"happiness": min(100, t["happiness"] + 8), "energy": max(0, t["energy"] - 3)}), "You ran through the tunnel! π")},
- "laser": {"effect": lambda t: (t.update({"happiness": min(100, t["happiness"] + 12), "energy": max(0, t["energy"] - 5)}), "You chased the laser dot! π΅")},
- "chew_toy": {"effect": lambda t: (t.update({"happiness": min(100, t["happiness"] + 10), "hunger": max(0, t["hunger"] - 5)}), "You chewed your toy! π¦·")},
- "tug_rope": {"effect": lambda t: (t.update({"happiness": min(100, t["happiness"] + 15), "energy": max(0, t["energy"] - 10)}), "You played tug-of-war! πͺ’")},
- "puzzle": {"effect": lambda t: (t.update({"happiness": min(100, t["happiness"] + 10), "hunger": max(0, t["hunger"] - 5)}), "You solved a puzzle! π§©")},
- "music_box": {"effect": lambda t: (t.update({"happiness": min(100, t["happiness"] + 8)}), "You danced to music! π΅")},
- "treat_toy": {"effect": lambda t: (t.update({"happiness": min(100, t["happiness"] + 10), "hunger": max(0, t["hunger"] - 10)}), "You got a treat! πͺ")},
- "catnip": {"effect": lambda t: (t.update({"happiness": min(100, t["happiness"] + 25), "energy": max(0, t["energy"] - 15)}), "You're high on catnip! πΏ")},
- "robot_dog": {"effect": lambda t: (t.update({"happiness": min(100, t["happiness"] + 20), "sick": False}), "Your robot dog cured you! π€")}
- }
- # Tamagotchi Base Structure
- def create_tamagotchi(ttype="cat", name=None):
- return {
- "id": f"TAMA{random.randint(100, 999)}",
- "name": name or TAMAGOTCHI_TYPES[ttype]["name"],
- "type": ttype,
- "age": 0,
- "hunger": 50,
- "happiness": 50,
- "health": 100,
- "cleanliness": 75,
- "energy": 100,
- "thirst": 30,
- "sick": False,
- "stage": "egg",
- "alive": True,
- "last_interacted": time.time(),
- "inventory": [],
- "friends": []
- }
- # Save/Load Functions
- def save_tamagotchi(data, folder="tamagotchis"):
- with open(f"{folder}/{data['id']}.json", "w") as f:
- json.dump(data, f, indent=2)
- def load_tamagotchi(id, folder="tamagotchis"):
- try:
- with open(f"{folder}/{id}.json", "r") as f:
- data = json.load(f)
- for key in create_tamagotchi():
- if key not in data:
- data[key] = create_tamagotchi()[key]
- return data
- except FileNotFoundError:
- print("No saved Tamagotchi found!")
- return create_tamagotchi()
- # Game Mechanics
- def update_stats():
- global tamagotchi
- time_passed = time.time() - tamagotchi["last_interacted"]
- if time_passed > 10:
- # Base stat decay
- tamagotchi["hunger"] = min(100, tamagotchi["hunger"] + TAMAGOTCHI_TYPES[tamagotchi["type"]]["hunger_rate"])
- tamagotchi["thirst"] = min(100, tamagotchi["thirst"] + 2)
- tamagotchi["cleanliness"] = max(0, tamagotchi["cleanliness"] - TAMAGOTCHI_TYPES[tamagotchi["type"]]["cleanliness_rate"])
- tamagotchi["happiness"] = max(0, tamagotchi["happiness"] - 2)
- tamagotchi["energy"] = max(0, tamagotchi["energy"] - 3)
- # Aging
- tamagotchi["age"] += 0.1
- update_stage()
- # Sickness Risk
- if tamagotchi["cleanliness"] < 20 or tamagotchi["hunger"] > 80:
- if random.random() < 0.1:
- tamagotchi["sick"] = True
- # Health Decay
- if tamagotchi["sick"]:
- tamagotchi["health"] = max(0, tamagotchi["health"] - 5)
- else:
- tamagotchi["health"] = min(100, tamagotchi["health"] + 1)
- tamagotchi["last_interacted"] = time.time()
- check_status()
- random_event()
- def update_stage():
- global tamagotchi
- if tamagotchi["age"] >= 5 and tamagotchi["stage"] == "egg":
- print("π£ It's hatching!")
- tamagotchi["stage"] = "baby"
- elif tamagotchi["age"] >= 10 and tamagotchi["stage"] == "baby":
- print("πΆ Your Tamagotchi is growing up!")
- tamagotchi["stage"] = "adult"
- def check_status():
- global tamagotchi
- if tamagotchi["health"] <= 0:
- print("\nπ Your Tamagotchi has died!")
- tamagotchi["alive"] = False
- # Player Actions
- def feed():
- global tamagotchi
- tamagotchi["hunger"] = max(0, tamagotchi["hunger"] - 15)
- tamagotchi["thirst"] = max(0, tamagotchi["thirst"] - 10)
- print("\nπ΄ You fed your Tamagotchi!")
- def play():
- global tamagotchi
- tamagotchi["happiness"] = min(100, tamagotchi["happiness"] + 10)
- tamagotchi["energy"] = max(0, tamagotchi["energy"] - 15)
- print("\nπ You played with your Tamagotchi!")
- def clean():
- global tamagotchi
- tamagotchi["cleanliness"] = min(100, tamagotchi["cleanliness"] + 20)
- print("\nπ§Ό You cleaned your Tamagotchi!")
- def sleep():
- global tamagotchi
- if tamagotchi["energy"] < 50:
- tamagotchi["energy"] = min(100, tamagotchi["energy"] + 50)
- print("\nπ€ Your Tamagotchi took a nap!")
- else:
- print("\nβ οΈ Your Tamagotchi isnβt tired yet!")
- def give_medicine():
- global tamagotchi
- if tamagotchi["sick"]:
- tamagotchi["sick"] = False
- print("\nπ You gave your Tamagotchi medicine!")
- else:
- print("\nβ οΈ Your Tamagotchi isnβt sick!")
- def use_toy():
- global tamagotchi
- if not tamagotchi["inventory"]:
- print("\nβ οΈ No toys in inventory!")
- return
- print("\nToys available:")
- for i, toy in enumerate(tamagotchi["inventory"], 1):
- print(f"{i}. {toy}")
- choice = input("Which toy to use? (Enter number or BACK): ")
- if choice.lower() == "back":
- return
- try:
- idx = int(choice) - 1
- toy = tamagotchi["inventory"][idx]
- msg = TOYS[toy]["effect"](tamagotchi)
- print(msg)
- tamagotchi["inventory"].pop(idx)
- except (IndexError, ValueError):
- print("\nInvalid choice!")
- def visit_park():
- global tamagotchi
- if random.random() < 0.1: # 10% chance
- friend_type = random.choice(list(TAMAGOTCHI_TYPES.keys()))
- friend = create_tamagotchi(friend_type)
- print(f"\nπ₯ You met a new friend! It's a {friend_type} named {friend['name']}!")
- tamagotchi["friends"].append(friend["id"])
- save_tamagotchi(friend, "friends")
- else:
- print("\nNo new friends today.")
- # Cheats
- def cheat_full():
- global tamagotchi
- for stat in ["hunger", "thirst", "cleanliness", "happiness", "energy"]:
- tamagotchi[stat] = 100
- tamagotchi["sick"] = False
- print("\nβ All stats maxed out!")
- def cheat_all_toys():
- global tamagotchi
- tamagotchi["inventory"].extend(TOYS.keys())
- print("\nπ You got all toys!")
- # Random Events (10 types)
- def random_event():
- global tamagotchi
- event = random.choice([
- "lost_toys", "found_toy", "bad_weather", "good_weather",
- "dream", "sick_friend", "party", "injury", "treasure", "runaway"
- ])
- if event == "lost_toys":
- if tamagotchi["inventory"]:
- lost = random.choice(tamagotchi["inventory"])
- tamagotchi["inventory"].remove(lost)
- print(f"\nπ« You lost your {lost}!")
- elif event == "found_toy":
- toy = random.choice(list(TOYS.keys()))
- tamagotchi["inventory"].append(toy)
- print(f"\nπ You found a {toy}!")
- elif event == "bad_weather":
- tamagotchi["happiness"] = max(0, tamagotchi["happiness"] - 20)
- tamagotchi["cleanliness"] = max(0, tamagotchi["cleanliness"] - 10)
- print("\nπ§οΈ Stormy weather made your Tamagotchi sad!")
- elif event == "good_weather":
- tamagotchi["happiness"] = min(100, tamagotchi["happiness"] + 20)
- print("\nβοΈ Sunny weather made your Tamagotchi happy!")
- elif event == "dream":
- print("\nπ Your Tamagotchi had a wonderful dream!")
- tamagotchi["happiness"] = min(100, tamagotchi["happiness"] + 10)
- elif event == "sick_friend":
- if tamagotchi["friends"]:
- friend_id = random.choice(tamagotchi["friends"])
- print(f"\nβ οΈ Your friend {friend_id} is sick!")
- if random.random() < 0.3:
- tamagotchi["sick"] = True
- elif event == "party":
- print("\nπ Surprise party! Everyone's happy!")
- tamagotchi["happiness"] = min(100, tamagotchi["happiness"] + 30)
- tamagotchi["energy"] = max(0, tamagotchi["energy"] - 20)
- elif event == "injury":
- print("\nπ©Ή Your Tamagotchi got a minor injury!")
- tamagotchi["health"] = max(0, tamagotchi["health"] - 10)
- elif event == "treasure":
- print("\nπ You found a treasure chest!")
- cheat_all_toys()
- elif event == "runaway":
- print("\nπ Your Tamagotchi almost ran away!")
- tamagotchi["happiness"] = max(0, tamagotchi["happiness"] - 30)
- tamagotchi["energy"] = max(0, tamagotchi["energy"] - 20)
- # Game Loop
- def game_loop():
- global tamagotchi
- print(f"\n{tamagotchi['name']} the {tamagotchi['type']} is ready to play!")
- while tamagotchi["alive"]:
- update_stats()
- # Mood Indicator
- mood = "sad"
- if tamagotchi["sick"]:
- mood = "sick"
- elif tamagotchi["energy"] < 20:
- mood = "tired"
- elif tamagotchi["happiness"] > 70:
- mood = "happy"
- print(f"\nCurrent Mood: {mood.capitalize()} | Age: {int(tamagotchi['age'])}")
- print(f"Stats: π{tamagotchi['happiness']} | π½οΈ{tamagotchi['hunger']} | π§{tamagotchi['thirst']}")
- print("1. Feed 2. Play 3. Clean 4. Sleep 5. Meds 6. Toys 7. Park 8. Status 9. Save 10. Exit")
- choice = input("What do you want to do? ")
- if choice == "1":
- feed()
- elif choice == "2":
- play()
- elif choice == "3":
- clean()
- elif choice == "4":
- sleep()
- elif choice == "5":
- give_medicine()
- elif choice == "6":
- use_toy()
- elif choice == "7":
- visit_park()
- elif choice == "8":
- print(f"\nName: {tamagotchi['name']} | Type: {tamagotchi['type'].capitalize()}")
- print(f"Friends ({len(tamagotchi['friends'])}) | Inventory ({len(tamagotchi['inventory'])})")
- elif choice == "9":
- save_tamagotchi(tamagotchi)
- print("\nπΎ Game saved!")
- elif choice == "10":
- print("π See you next time!")
- break
- elif choice.lower() == "full":
- cheat_full()
- elif choice.lower() == "alltoys":
- cheat_all_toys()
- time.sleep(2) # Simulate time passing
- # Start Game
- if __name__ == "__main__":
- tamagotchi = load_tamagotchi("TAMA123")
- game_loop()
Advertisement
Comments
-
- For lines 157 and 165, please change the apostrophe in "isn't" to the normal one. Apparently some compilers freeze at this simple mistake. If it runs, then ignore this message. If it does not, that is the sole reason why.
Add Comment
Please, Sign In to add comment
Advertisement