python开发简单猜拳游戏
规则
1 :石头, 2 : 剪刀, 3 : 布, 0 : 退出
定义三个类
- 启动器
- 主体
- 积分器
启动器
def __init__(self): self.run() # 启动器 @staticmethod def run(): computer = random.randint(1, 3) try: user = int(input("> 1 : 石头 > 2 : 剪刀 > 3 : 布 > input 1 - 3 ")) if user == 0: sys.exit(0) if user > 3 or user < 1: user = random.randint(1, 3) except Exception as e: print(e) user = random.randint(1, 3) GameSet = TheFingerGuessingGame() GameSet.user = user GameSet.computer = computer GameSet.resultShow()
主体
规则定制
# 规则设定 @staticmethod def explain(count): if count == 1: return "石头" elif count == 2: return "剪刀" elif count == 3: return "布" else: return
判断胜负
# 判断胜负 @staticmethod def JudgeTheOutcome(compute, use): WeightRatio = compute - use if WeightRatio == 0: return 0 elif (WeightRatio == -1) or (WeightRatio == 2): return -1 elif (WeightRatio == 1) or (WeightRatio == -2): return 1
结果展示
# 结果展示 def resultShow(self): print("> Computer : ", self.explain(self.computer), " > You : ", self.explain(self.user)) if self.JudgeTheOutcome(self.computer, self.user) == 0: Integrator.user_score -= 2 Integrator.computer_score -= 2 print("> 平局 : Play Even") Integrator.ShowScore() elif self.JudgeTheOutcome(self.computer, self.user) == -1: print("> 输 : You Lose : ") Integrator.computer_score -= 1 Integrator.ShowScore() elif self.JudgeTheOutcome(self.computer, self.user) == 1: Integrator.user_score += 1 Integrator.ShowScore() print(f"> 赢 : You Win ")
积分器
初始化积分
def __init__(self): self.computer_score = 0 self.user_score = 0
展示当前积分
@staticmethod def ShowScore(): print(f">> you score : {Integrator.user_score}" f" >> computer score : {Integrator.computer_score} ")
设置为无限循环
if __name__ == __main__: Integrator = Integrator() while True: Launcher()