-
Notifications
You must be signed in to change notification settings - Fork 4
/
animal.py
61 lines (55 loc) · 2.56 KB
/
animal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# import necessary packages
import random
import time
import sys
# setup the game by storing animals in a list and their dietry habits in a dictionary
animal_list = ["dog", "cat", "porcupine", "whale", "rat", "rabbit", "pig", "horse","lion"]
animal_food = {"dog":["beef","chicken","rice"], "cat":["mice","eggs","cereal"],
"porcupine": ["worms","beetles","ants"] ,
"whale":["prawns", "fish","seals"],
"rat":["corn","fruit","biscuits"],
"rabbit":["carrots","lettuce","nuts"],
"pig":["potatoes","turnips","cabbage"],
"horse":["hay", "sugar cubes"],
"lion":["deer", "buffalo","zebra"]}
health_bar = 100 # size of heath bar which is printed to console
# start the game by randomly selecting an animal and a health score
random_animal = random.choice(animal_list)
random_health = random.randint(1,100)
# tell the user what their animal is and ask to name the animal
print(f"Your animal is a {random_animal}. It has a health score of {random_health}")
animal_name = input("Give your animal a name : ")
# keeps asking the user to select a food and adjusts the health score and displays the health bar
while random_health > 0:
decoy_animal = random.choice(animal_list)
print(f"{animal_name} is currently fine")
mixed_list = [item for pair in zip(animal_food[random_animal], animal_food[decoy_animal]) for item in pair]
chosen_food = input(f"What would you like to feed {animal_name}, choose from {mixed_list}?\n")
chosen_food = chosen_food.lower().replace(" ","")
if chosen_food not in mixed_list:
print(f"Sorry {chosen_food} is not a valid food")
continue
else:
if chosen_food in animal_food[random_animal]:
random_health += 10
print(f"{animal_name} enjoyed that. Their score is now {random_health}")
else:
random_health -= 10
print(f"{animal_name} didn't like that. Their score is now {random_health}")
# health bar code
sys.stdout.write("[%s]" % (" " * health_bar))
sys.stdout.flush()
sys.stdout.write("\b" * (health_bar+1)) # return to start of line, after '['
Health_width = random_health
for i in range(Health_width):
time.sleep(0.1)
sys.stdout.write("-")
sys.stdout.flush()
Sick_width = health_bar-Health_width
for i in range(Sick_width):
time.sleep(0.01)
sys.stdout.write("0")
sys.stdout.flush()
sys.stdout.write("]\n") # this ends the health bar
# final messgae to game player
print(f"sorry {animal_name} is dead :( ")