Last active
March 11, 2026 18:07
-
-
Save j2deme/e1a4d04052badac1a6de2bdce46129e1 to your computer and use it in GitHub Desktop.
Tamagotchi POO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from tamago import Tamago, Alimento # Importación | |
| pou = Tamago("Pou") | |
| # print(pou.alimento, pou.felicidad, pou.energia) | |
| '''print(pou) | |
| pou.jugar() | |
| print(pou) | |
| # pou.felicidad = 100 | |
| pou.jugar() | |
| print(pou) | |
| pou.comer() | |
| pou.alimento = 75 | |
| print(pou) | |
| pou.jugar() | |
| print(pou) | |
| pou.dormir() | |
| print(pou) | |
| ''' | |
| manzana = Alimento("Manzana") | |
| pera = Alimento("Pera", 1, "🍐") | |
| banana = Alimento("Banana", simbolo="🍌", valor=2) | |
| print(manzana) | |
| pou.comer(manzana) | |
| print(pou) | |
| pou.jugar() | |
| print(pou) | |
| pou.comer(banana) | |
| print(pou) | |
| pou.comerInventario() | |
| print(pou) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from dataclasses import dataclass | |
| class Tamago: | |
| _vida = 0 | |
| _alimento = 0 | |
| _energia = 0 | |
| _felicidad = 0 | |
| _nombre = "" | |
| _inventario = [] | |
| def __init__(self, nombre): | |
| # Constructor | |
| self._nombre = nombre | |
| self._vida = 10 | |
| self._alimento = 3 | |
| self._energia = 3 | |
| self._felicidad = 3 | |
| self._inventario.append(Alimento("Manzana")) | |
| # Getters y Setters | |
| '''def getAlimento(self): | |
| return self._alimento | |
| def setAlimento(self, alimento): | |
| self._alimento = alimento | |
| ''' | |
| @property | |
| def felicidad(self): # Get Felicidad | |
| return self._felicidad | |
| @felicidad.setter | |
| def felicidad(self, felicidad): # Set Felicidad | |
| if felicidad >= 1 and felicidad <= 5: | |
| self._felicidad = felicidad | |
| else: | |
| raise ValueError("Valor fuera de rango") | |
| @property | |
| def alimento(self): | |
| return self._alimento | |
| @alimento.setter | |
| def alimento(self, alimento): | |
| self._alimento = alimento | |
| @property | |
| def energia(self): | |
| return self._energia | |
| @energia.setter | |
| def energia(self, energia): | |
| self._energia = energia | |
| @property | |
| def nombre(self): | |
| return self._nombre | |
| def comer(self, alimento): | |
| if self._alimento == 5: | |
| self.__hablar("Estoy lleno") | |
| else: | |
| self._alimento += alimento.valor | |
| self.__hablar(f"Estoy comiendo {alimento.nombre}") | |
| def comerInventario(self): | |
| if len(self._inventario) == 0: | |
| self.__hablar("No tengo nada para comer") | |
| else: | |
| alimento = self._inventario.pop() | |
| self._alimento += alimento.valor | |
| self.__hablar(f"Estoy comiendo {alimento.nombre} del inventario") | |
| def jugar(self): | |
| '''if self._felicidad == 5: | |
| self.__hablar("Ahorita no joven") | |
| else: | |
| self._felicidad += 1 | |
| self._energia -= 1 | |
| self._alimento -= 1''' | |
| try: | |
| self.felicidad += 1 | |
| self.energia -= 1 | |
| self.alimento -= 1 | |
| except ValueError as e: | |
| self.__hablar("Ahorita no joven") | |
| def dormir(self): | |
| if self._energia == 5: | |
| self.__hablar("Ando al 100 jefe!") | |
| else: | |
| self._energia += 1 | |
| self._felicidad -= 1 | |
| def __hablar(self, mensaje): | |
| print(f"{self._nombre}: {mensaje}") | |
| def __str__(self): | |
| return f"{self._nombre}: 😊 {self._felicidad}, 🍎 {self._alimento}, 😴 {self._energia}" | |
| # --- Clase Alimento --- | |
| @dataclass | |
| class Alimento: | |
| nombre: str | |
| valor: int = 1 | |
| simbolo: str = "🍎" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment