Untitled

 avatar
unknown
plain_text
4 days ago
3.5 kB
3
Indexable
import random
import time

# Player class to manage the player's state
class Player:
    def __init__(self):
        self.health = 100
        self.hunger = 50
        self.thirst = 50
        self.energy = 100
        self.food = 0
        self.water = 0
    
    def __str__(self):
        return (f"Health: {self.health}% | Hunger: {self.hunger}% | "
                f"Thirst: {self.thirst}% | Energy: {self.energy}% | "
                f"Food: {self.food} | Water: {self.water}")

    def is_alive(self):
        return self.health > 0 and self.hunger > 0 and self.thirst > 0

    def take_damage(self, damage):
        self.health -= damage
        if self.health < 0:
            self.health = 0

    def eat(self):
        if self.food > 0:
            self.hunger = min(self.hunger + 30, 100)
            self.food -= 1
            print("You eat some food and feel better.")
        else:
            print("You don't have any food left.")

    def drink(self):
        if self.water > 0:
            self.thirst = min(self.thirst + 30, 100)
            self.water -= 1
            print("You drink some water and feel refreshed.")
        else:
            print("You don't have any water left.")

    def rest(self):
        self.energy = min(self.energy + 30, 100)
        self.hunger -= 10
        self.thirst -= 10
        print("You take a rest and regain some energy.")

    def gather_resources(self):
        chance = random.randint(1, 10)
        if chance > 7:
            self.food += 1
            print("You find some food.")
        elif chance > 4:
            self.water += 1
            print("You find some water.")
        else:
            print("You don't find anything.")

    def suffer_from_hunger(self):
        if self.hunger <= 0:
            self.take_damage(10)
            print("You feel weak from hunger!")

    def suffer_from_thirst(self):
        if self.thirst <= 0:
            self.take_damage(10)
            print("You feel weak from thirst!")

    def suffer_from_fatigue(self):
        if self.energy <= 0:
            self.take_damage(5)
            print("You feel weak from exhaustion!")

def game_loop():
    player = Player()
    print("Welcome to the Survival Game!")
    print("Survive by managing hunger, thirst, energy, and health.")

    while player.is_alive():
        print("\n" + str(player))
        print("\nWhat would you like to do?")
        print("1. Gather resources")
        print("2. Eat food")
        print("3. Drink water")
        print("4. Rest")
        print("5. Do nothing (pass time)")

        choice = input("Choose an action: ")

        if choice == "1":
            player.gather_resources()
        elif choice == "2":
            player.eat()
        elif choice == "3":
            player.drink()
        elif choice == "4":
            player.rest()
        elif choice == "5":
            print("You decide to rest for a while...")
        else:
            print("Invalid choice, try again.")
            continue
        
        # Simulate time passing and resource consumption
        time.sleep(2)
        player.hunger -= 5
        player.thirst -= 5
        player.energy -= 5

        # Players suffer from hunger, thirst, and fatigue if not managed
        player.suffer_from_hunger()
        player.suffer_from_thirst()
        player.suffer_from_fatigue()

        if not player.is_alive():
            print("\nYou have perished. Game over.")
            break

if __name__ == "__main__":
    game_loop()
Editor is loading...
Leave a Comment