I think this branch is newer.

This commit is contained in:
Andreas Zweili 2016-06-28 22:15:09 +02:00
parent 76769c2279
commit b207707fa3
2 changed files with 36 additions and 49 deletions

View File

@ -8,14 +8,15 @@ import os
# imports the pygame library needed to play the sound in the # imports the pygame library needed to play the sound in the
# poking function # poking function
from pygame import mixer from pygame import mixer
import threading
# variables needed for the guessing game # variables needed for the guessing game
secret = randint(1, 10) secret = randint(1, 10)
### a class for controlling the pet ### ### a class for controlling the pet ###
class Pet(object): class Pet(object):
def __init__(self, name, photo, status, health, age, hunger, happiness, poke_count, def __init__(self, name, photo, status, health, age, hunger, happiness, poke_count,
max_health, max_hunger, max_happiness): max_health, max_hunger, max_happiness):
# The pet's stats # The pet's stats
@ -31,7 +32,6 @@ class Pet(object):
self.max_hunger = max_hunger self.max_hunger = max_hunger
self.max_happiness = max_happiness self.max_happiness = max_happiness
# a function which displays the pet's stats in a nice way. # a function which displays the pet's stats in a nice way.
def display_stats(self): def display_stats(self):
os.system('clear') os.system('clear')
@ -43,12 +43,10 @@ class Pet(object):
print("Hunger: " + self.hunger * "*") print("Hunger: " + self.hunger * "*")
print("Happines: " + self.happiness * "") print("Happines: " + self.happiness * "")
# A function which checks if the pet is still alive # A function which checks if the pet is still alive
def is_alive(self): def is_alive(self):
return self.health > 0 return self.health > 0
# A function which changes the status of the pet depending of the age value. # A function which changes the status of the pet depending of the age value.
# Each status has it's own characteristics. # Each status has it's own characteristics.
@ -57,25 +55,21 @@ class Pet(object):
self.max_happiness = 8 self.max_happiness = 8
self.max_hunger = 7 self.max_hunger = 7
def set_adult_stats(self): def set_adult_stats(self):
self.max_health = 10 self.max_health = 10
self.max_happiness = 8 self.max_happiness = 8
self.max_hunger = 7 self.max_hunger = 7
def set_elderly_stats(self): def set_elderly_stats(self):
self.max_health = 7 self.max_health = 7
self.max_happiness = 5 self.max_happiness = 5
self.max_hunger = 10 self.max_hunger = 10
def reset_stats(self): def reset_stats(self):
self.health = self.max_health self.health = self.max_health
self.happiness = self.max_happiness self.happiness = self.max_happiness
self.hunger = self.max_hunger self.hunger = self.max_hunger
def aging(self): def aging(self):
if self.age == 5: if self.age == 5:
self.status = "adult" self.status = "adult"
@ -88,59 +82,52 @@ class Pet(object):
print("Congratulation your pet has become an elderly it needs now less food.") print("Congratulation your pet has become an elderly it needs now less food.")
print("However it's health is worse and it's grumpier than an adult.") print("However it's health is worse and it's grumpier than an adult.")
### Functions to increase and decrease stats ### ### Functions to increase and decrease stats ###
def increase_hunger(self): def increase_hunger(self):
if self.hunger < self.max_hunger: if self.hunger < self.max_hunger:
self.hunger += 1 self.hunger += 1
def increase_poke_count(self): def increase_poke_count(self):
self.poke_count += 1 self.poke_count += 1
def increase_happiness(self): def increase_happiness(self):
if self.happiness < self.max_happiness: if self.happiness < self.max_happiness:
self.happiness += 1 self.happiness += 1
def increase_health(self): def increase_health(self):
if self.health < self.max_health: if self.health < self.max_health:
self.health += 1 self.health += 1
def decrease_hunger(self): def decrease_hunger(self):
if self.hunger > 0: if self.hunger > 0:
self.hunger -= 1 self.hunger -= 1
def decrease_happiness(self): def decrease_happiness(self):
if self.happiness > 0: if self.happiness > 0:
self.happiness -= 1 self.happiness -= 1
def decrease_health(self): def decrease_health(self):
if self.health > 0: if self.health > 0:
self.health -= 1 self.health -= 1
def decrease_poke_count(self): def decrease_poke_count(self):
self.poke_count -= 1 self.poke_count -= 1
# The function to decrease the stats and make the pet "live" needs to # The function to decrease the stats and make the pet "live" needs to
# run in the background. # run in the background.
def decrease_stats(self): def _decrease_stats(self):
while True: while True:
time.sleep(15) time.sleep(2)
self.decrease_hunger() self.decrease_hunger()
if self.hunger <= 0: if self.hunger <= 0:
self.decrease_health() self.decrease_health()
self.decrease_happiness() self.decrease_happiness()
def decrease_stats(self):
threading.Thread(target=self.decrease_stats).start()
### Activities ### ### Activities ###
# A function which simulates stroking it doesn't have any # A function which simulates stroking it doesn't have any
# effect on the pet. # effect on the pet.
@ -151,7 +138,6 @@ class Pet(object):
print("It makes comforting noises and leans against your hand.") print("It makes comforting noises and leans against your hand.")
time.sleep(1) time.sleep(1)
# Increases the pets hungriness by +1 unless the hunger is bigger than # Increases the pets hungriness by +1 unless the hunger is bigger than
# the pet's maximum hunger. In this case the pet will vomit and looses hunger # the pet's maximum hunger. In this case the pet will vomit and looses hunger
# and health. # and health.
@ -162,7 +148,6 @@ class Pet(object):
if feeding_confirmed in ("Y", "y"): if feeding_confirmed in ("Y", "y"):
self.increase_hunger() self.increase_hunger()
# A simple guessing game which increases the pet's happiness # A simple guessing game which increases the pet's happiness
def playing(self): def playing(self):
guess = 0 guess = 0
@ -180,7 +165,6 @@ class Pet(object):
self.increase_happiness() self.increase_happiness()
print("Game over!") print("Game over!")
# let's you poke the pet and it will talk # let's you poke the pet and it will talk
# if you poke it more than 3 times it will get angry at you # if you poke it more than 3 times it will get angry at you
def poking(self): def poking(self):
@ -200,7 +184,6 @@ class Pet(object):
mixer.music.play() mixer.music.play()
time.sleep(3) time.sleep(3)
# A function which let's the pet sleep and regenerates it's stats # A function which let's the pet sleep and regenerates it's stats
def sleeping(self): def sleeping(self):
os.system('clear') os.system('clear')

50
tamagotchi.py Normal file → Executable file
View File

@ -35,30 +35,14 @@ def beginning():
pet_name = input("How do you want to call your pet?") pet_name = input("How do you want to call your pet?")
tamagotchi.name = pet_name tamagotchi.name = pet_name
tamagotchi.photo = pet_photo tamagotchi.photo = pet_photo
print()
print()
print("Your pet is currently a youngling which means it's needs a lot of attention.")
print("Take good care of it or it will die very soon.")
beginning_finished = True beginning_finished = True
# Present the player with activities to choose from
# Beginning of the main routine which makes up the actual game. def menu():
# thread which runs in the background to cause hunger, etc
t = threading.Thread(target=tamagotchi.decrease_stats())
t.start()
# Only starts if the pet is still alive.
while tamagotchi.is_alive():
if not beginning_finished:
# Let the player choose his pet and skip the beginning from then on.
beginning()
print()
print()
print("Your pet is currently a youngling which means it's needs a lot of attention.")
print("Take good care of it or it will die very soon.")
# checks if the pet has reached a new life stage and updates it accordingly
tamagotchi.aging()
print()
# Each round print the pets stats so that the player can see them.
tamagotchi.display_stats()
print()
# Present the player with activities to choose from
print("What would you like to do?") print("What would you like to do?")
# Start the chosen activity and go back to the activity selector. # Start the chosen activity and go back to the activity selector.
print("1: Feeding, 2: Playing, 3: Stroke Pet,") print("1: Feeding, 2: Playing, 3: Stroke Pet,")
@ -79,4 +63,24 @@ while tamagotchi.is_alive():
tamagotchi.display_stats() tamagotchi.display_stats()
except ValueError: except ValueError:
tamagotchi.display_stats() tamagotchi.display_stats()
print("Your pet died.")
# Beginning of the main routine which makes up the actual game.
# Only starts if the pet is still alive.
def main():
while tamagotchi.is_alive():
if not beginning_finished:
# Let the player choose his pet and skip the beginning from then on.
beginning()
tamagotchi.decrease_stats()
# checks if the pet has reached a new life stage and updates it accordingly
tamagotchi.aging()
print()
# Each round print the pets stats so that the player can see them.
tamagotchi.display_stats()
print()
menu()
print("Your pet died.")
if __name__ == "__main__": main()