some first changes to make the pet a class

This commit is contained in:
Andreas Zweili 2015-08-31 06:42:03 +02:00
parent 612896caff
commit 3cb88fe198
2 changed files with 153 additions and 178 deletions

View File

@ -1,5 +1,3 @@
# imports the global variables
import pet_variables
# imports the time library needed to delay certain functions # imports the time library needed to delay certain functions
import time import time
# imports the random library needed to generate a random number for # imports the random library needed to generate a random number for
@ -11,206 +9,206 @@ import os
# poking function # poking function
from pygame import mixer from pygame import mixer
# 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 ###
### Functions providing the basic function of the programme ### class Pet(object):
# a function which displays the pet's stats in a nice way. def __init__(self, name, photo, status, health, age, hunger, happiness, poke_count,
def pet_stats(): max_health, max_hunger, max_happiness):
os.system('clear') # The pet's stats
print(pet_variables.pet_name) self.name = name
print(pet_variables.pet_photo) self.photo = photo
print("Status: " + pet_variables.pet_status) self.status = status
print("Age: " + str(pet_variables.pet_age)) self.health = health
print("Health: " + pet_variables.pet_health * "") self.age = age
print("Hunger: " + pet_variables.pet_hunger * "*") self.hunger = hunger
print("Happines: " + pet_variables.pet_happiness * "") self.happiness = happiness
self.poke_count = poke_count
self.max_health = max_health
self.max_hunger = max_hunger
self.max_happiness = max_happiness
# A function which checks if the pet is still alive # a function which displays the pet's stats in a nice way.
def is_alive(): def display_stats(self):
return pet_variables.pet_health > 0 os.system('clear')
print(self.name)
print(self.photo)
print("Status: " + self.status)
print("Age: " + str(self.age))
print("Health: " + self.health * "")
print("Hunger: " + self.hunger * "*")
print("Happines: " + self.happiness * "")
# A function which let's the player choose his pet. # A function which checks if the pet is still alive
def beginning(): def is_alive(self):
print("Which pet do you want to look after?") return self.health > 0
print("1: Cat, 2: Mouse, 3: Fish, 4: Owl")
chosen_pet = int(input("Choose your pet:"))
if chosen_pet == 1:
pet_variables.pet_photo = pet_variables.cat
elif chosen_pet == 2:
pet_variables.pet_photo = pet_variables.mouse
elif chosen_pet == 3:
pet_variables.pet_photo = pet_variables.fish
elif chosen_pet == 4:
pet_variables.pet_photo = pet_variables.owl
pet_variables.pet_name = input("How do you want to call your pet?")
# 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.
def set_youngling_stats(): def set_youngling_stats(self):
pet_variables.max_health = 10 self.max_health = 10
pet_variables.max_happiness = 8 self.max_happiness = 8
pet_variables.max_hunger = 7 self.max_hunger = 7
def set_adult_stats(): def set_adult_stats(self):
pet_variables.max_health = 10 self.max_health = 10
pet_variables.max_happiness = 8 self.max_happiness = 8
pet_variables.max_hunger = 7 self.max_hunger = 7
def set_elderly_stats(): def set_elderly_stats(self):
pet_variables.max_health = 7 self.max_health = 7
pet_variables.max_happiness = 5 self.max_happiness = 5
pet_variables.max_hunger = 10 self.max_hunger = 10
def reset_stats(): def reset_stats(self):
pet_variables.pet_health = pet_variables.max_health self.health = self.max_health
pet_variables.pet_happiness = pet_variables.max_happiness self.happiness = self.max_happiness
pet_variables.pet_hunger = pet_variables.max_hunger self.hunger = self.max_hunger
def aging(): def aging(self):
if pet_variables.pet_age == 5: if self.age == 5:
pet_variables.pet_status = "adult" self.status = "adult"
set_adult_stats() self.set_adult_stats()
print("Congratulation your pet has become an adult. It needs less food now") print("Congratulation your pet has become an adult. It needs less food now")
print("and it's health has improved however it's grumpier than a youngling.") print("and it's health has improved however it's grumpier than a youngling.")
elif pet_variables.pet_age == 15: elif self.age == 15:
pet_variables.pet_status = "elderly" self.status = "elderly"
set_elderly_stats() self.set_elderly_stats()
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(): def increase_hunger(self):
if pet_variables.pet_hunger < pet_variables.max_hunger: if self.hunger < self.max_hunger:
pet_variables.pet_hunger += 1 self.hunger += 1
def increase_poke_count(): def increase_poke_count(self):
pet_variables.poke_count += 1 self.poke_count += 1
def increase_happiness(): def increase_happiness(self):
if pet_variables.pet_happiness < pet_variables.max_happiness: if self.happiness < self.max_happiness:
pet_variables.pet_happiness += 1 self.happiness += 1
def increase_health(): def increase_health(self):
if pet_variables.pet_health < pet_variables.max_health: if self.health < self.max_health:
pet_variables.pet_health += 1 self.health += 1
def decrease_hunger(): def decrease_hunger(self):
if pet_variables.pet_hunger > 0: if self.hunger > 0:
pet_variables.pet_hunger -= 1 self.hunger -= 1
def decrease_happiness(): def decrease_happiness(self):
if pet_variables.pet_happiness > 0: if self.happiness > 0:
pet_variables.pet_happiness -= 1 self.happiness -= 1
def decrease_health(): def decrease_health(self):
if pet_variables.pet_health > 0: if self.health > 0:
pet_variables.pet_health -= 1 self.health -= 1
def decrease_poke_count(): def decrease_poke_count(self):
pet_variables.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(): def decrease_stats(self):
while True: while True:
time.sleep(pet_variables.day) time.sleep(15)
decrease_hunger() self.decrease_hunger()
if pet_variables.pet_hunger <= 0: if self.hunger <= 0:
decrease_health() decrease_health()
decrease_happiness() decrease_happiness()
### 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.
def stroking(): def stroking(self):
os.system('clear') os.system('clear')
print() print()
print("You're stroking the back of your pet gently.") print("You're stroking the back of your pet gently.")
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.
def feeding(): def feeding(self):
os.system('clear') os.system('clear')
print("Hungriness of " + pet_variables.pet_name + ": " + pet_variables.pet_hunger * "*") print("Hungriness of " + self.name + ": " + self.hunger * "*")
feeding_confirmed = input("Do you want to feed your pet?") feeding_confirmed = input("Do you want to feed your pet?")
if feeding_confirmed in ("Y", "y"): if feeding_confirmed in ("Y", "y"):
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(): def playing(self):
guess = 0 guess = 0
os.system('clear') os.system('clear')
while guess != secret: while guess != secret:
g = input("Guess the Number") g = input("Guess the Number")
guess = int(g) guess = int(g)
if guess == secret: if guess == secret:
print("You win!") print("You win!")
else:
if guess > secret:
print("Your guess is too high")
else: else:
print("Too low") if guess > secret:
increase_happiness() print("Your guess is too high")
print("Game over!") else:
print("Too low")
self.increase_happiness()
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(): def poking(self):
os.system('clear') os.system('clear')
if pet_variables.poke_count < 3: if self.poke_count < 3:
print("You poke " + pet_variables.pet_name + " and it starts to speak.") print("You poke " + self.name + " and it starts to speak.")
increase_poke_count() self.increase_poke_count()
mixer.init() mixer.init()
mixer.music.load('happy.mp3') mixer.music.load('happy.mp3')
mixer.music.play() mixer.music.play()
time.sleep(5) time.sleep(5)
else: else:
print("You annoyed " + pet_variables.pet_name + "." + " It got angry at you.") print("You annoyed " + self.name + "." + " It got angry at you.")
decrease_happiness() self.decrease_happiness()
mixer.init() mixer.init()
mixer.music.load('angry.mp3') mixer.music.load('angry.mp3')
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(): def sleeping(self):
os.system('clear') os.system('clear')
print("Your pet is sleeping now.") print("Your pet is sleeping now.")
time.sleep(10) time.sleep(10)
if pet_variables.max_hunger / pet_variables.pet_hunger > 0.5: if self.max_hunger / self.hunger > 0.5:
reset_stats() self.reset_stats()
print("Your pet woke up feeling rested and in a good mood.") print("Your pet woke up feeling rested and in a good mood.")
else: else:
print("Your pet has woken up.") print("Your pet has woken up.")
time.sleep(3) time.sleep(3)

View File

@ -1,25 +1,2 @@
# The pet's stats
pet_name = "Fluffy"
pet_photo = "<`)))><"
pet_status = "youngling"
pet_health = 5
pet_age = 0
pet_hunger = 5
pet_happiness = 5
pet_stomach = 0
# age based max values
max_health = 5
max_hunger = 5
max_happiness = 10
# Pictures and symbols used ingame
cat = "(=^o.o^=)__"
mouse = "<:3 )~~~~"
fish = "<`)))><"
owl = "(^0M0^)"
# programme variables # programme variables
beginning_finished = False
poke_count = 0
day = 15