要求:
动物园里面有10个房间,房间号从1 到 10。 每个房间里面可能是体重200斤的老虎或者体重100斤的羊。 游戏开始后,系统随机在10个房间中放入老虎或者羊。 然后随机给出房间号,要求游戏者选择敲门还是喂食。 如果选择喂食: 喂老虎应该输入单词 meat,喂羊应该输入单词 grass 喂对了,体重加10斤。 喂错了,体重减少10斤 如果选择敲门: 敲房间的门,里面的动物会叫,老虎叫会显示 ‘Wow !!’,羊叫会显示 ‘mie~~’。 动物每叫一次体重减5斤。 游戏者强记每个房间的动物是什么,以便不需要敲门就可以喂正确的食物。 游戏3分钟结束后,显示每个房间的动物和它们的体重。
编程:
# This Python file uses the following encoding: utf-8
from random import randint
import time
class Tiger:#老虎--类
className = 'tiger' #静态属性---类属性--整个类
def __init__(self,weight=100): #初始化方法--构造方法
self.weight = weight
def roar(self):
self.weight -= 5
print('wow!')
def feed(self,food):
if food == 'meat':
self.weight += 10
print('恭喜,体重加10斤!')
else:
self.weight -= 10
print('很遗憾,体重减10斤!')
class Sheep: #羊-类
className = 'sheep'
def __init__(self,weight=100):
self.weight = weight
def roar(self):
self.weight -= 10
print('Mie!')
def feed(self,food):
if food == 'grass':
self.weight += 10
print('恭喜,体重加10斤!')
else:
self.weight -= 10
print('很遗憾,体重减10斤!')
class Room:#房间-类
className = 'room' # 静态属性---类属性--整个类
def __init__(self,num,animal):
self.num = num
self.animal = animal
rooms = []
for one in range(0, 10):
if randint(0, 1):
ani = Tiger()
else:
ani = Sheep()
roomObject = Room(one, ani)
rooms.append(roomObject)
startTime = time.time()
while True:
curTime = time.time()
if (curTime - startTime) > 120:
print('\n\n ********** 游戏结束 ********** \n\n')
for idx, room in enumerate(rooms):
print('房间 :%s' % (idx + 1), room.animal.className, room.animal.weight)
break
roomno = randint(1, 10)
room = rooms[roomno-1]
ch = input('我们来到了房间# %s, 要敲门吗?[y/n]' % roomno)
if ch == 'y':
room.animal.roar()
food = input('请给房间里面的动物喂食:')
room.animal.feed(food.strip())