# -*- coding: UTF-8 -*-
import random
import threading
from threading import Timer
import os
class Animal_games(threading.Thread):
def __init__(self):
# 调用线程
threading.Thread.__init__(self)
# 字典,用来累计东西
self.dictionaries = {}
# 房间号
self.room = 1
# 老虎初始值体重
self.tiger = 200
# 羊的初始值体重
self.sheep = 100
self.choice = ''
def run(self):
self.Random_generation()
def Random_generation(self, timeout=120):
"""
11为老虎
12为羊
:param timeout: 定义玩游戏时间
:return:
"""
while True:
self.choice = input('一起玩游戏吗?\n请输入玩或不玩:')
if self.choice == '玩':
print('游戏开始倒计时3分钟')
# 设置定时器
self.timer = Timer(timeout, self.countdown)
self.timer.start()
# 重置dictionaries
self.dictionaries.clear()
# 重置1
self.room = 1
for i in range(10):
self.room_add = random.randint(11, 12)
self.dictionaries.update({self.room: self.room_add})
self.room += 1
print('已在十个房间里面添加好动物', self.dictionaries)
while True:
room = random.randint(1, 10)
print('随机房间号:', room)
self.xuanze = input('请选择敲门还是喂食:')
if self.xuanze == '喂食':
while True:
self.choice = input('请选喂食给老虎还是羊:如老虎输入meat,羊输入grass')
if self.choice == 'meat':
print('老虎')
if self.dictionaries[room] == 11:
self.tiger += 10
print('恭喜猜对了,体重增加10斤,目前老虎:', self.tiger, '斤')
# 返回继续循环
break
else:
self.tiger -= 10
print('抱歉猜错了,目前老虎:', self.tiger, '斤')
break
elif self.choice == 'grass':
print('羊')
if self.dictionaries[room] == 12:
self.sheep += 10
print('恭喜猜对了,体重增加10斤,目前羊:', self.sheep, '斤')
# 返回继续循环
break
else:
self.sheep -= 10
print('抱歉猜错了,目前羊:', self.sheep, '斤')
break
else:
print("输入错误~")
elif self.xuanze == '敲门':
if self.dictionaries[room] == 11:
self.tiger -= 5
print('Wow!!')
print('老虎目前体重', self.tiger)
# 返回继续循环
continue
elif self.dictionaries[room] == 12:
self.sheep -= 5
print('mie~~')
print('羊目前体重', self.sheep)
# 返回继续循环
continue
else:
print("输入错误~")
else:
print('输入错误请')
def countdown(self):
"""倒计时"""
print("\n倒计时结束,活动关闭")
print('老虎目前体重', self.sheep)
print('羊目前体重', self.tiger)
# 关闭自身进程
os._exit(0)
if __name__ == '__main__':
thread1 = Animal_games()
thread1.start()
thread1.join()
各位请问如何修改成倒计时结束后重置一切,从头开始问用户玩不玩此游戏