码迷,mamicode.com
首页 > 其他好文 > 详细

面向对象实现石头剪刀布游戏

时间:2018-07-21 17:04:42      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:computer   ini   code   面向   format   style   统计   com   compare   

  1 import random
  2 import time
  3 
  4 
  5 class Player:
  6     def __init__(self):
  7         self.name = "老张"
  8         self.fist = "你这样会报错"
  9         self.score = 0
 10 
 11     def player_name(self):
 12         name_choice = [喜洋洋, 美洋洋, 懒洋洋]
 13         input_name = input("请选择人物:1喜洋洋 2美洋洋 3懒洋洋(输入exit退出):")
 14         if input_name == "exit":
 15             self.name = "exit"
 16         else:
 17 
 18             if input_name == "1":
 19                 self.name = name_choice[0]
 20             elif input_name == "2":
 21                 self.name = name_choice[1]
 22             elif input_name == "3":
 23                 self.name = name_choice[2]
 24             # self.player_fist()
 25             print("你选择了{}".format(self.name))
 26 
 27     def player_fist(self):
 28         li_fist_p = ["石头", "剪刀", ""]
 29         input_fist = input("请出拳:1石头, 2剪刀, 3布(输入exit退出):\n")
 30         if input_fist == "exit":
 31             self.fist = "exit"
 32         else:
 33             if input_fist == "1":
 34                 self.fist = li_fist_p[0]
 35             if input_fist == "2":
 36                 self.fist = li_fist_p[1]
 37             if input_fist == "3":
 38                 self.fist = li_fist_p[2]
 39             print("你使出九牛二虎之力出了---{}".format(self.fist))
 40 
 41 
 42 class Computer:
 43     def __init__(self):
 44         self.c_name = "慢羊羊"
 45         self.c_fist = "未出拳"
 46         self.c_score = 0
 47 
 48     def computer_name(self):
 49         c_name_choice = ["慢羊羊", "沸羊羊", "暖羊羊"]
 50         self.c_name = random.choice(c_name_choice)
 51         print("敌方派出了超级厉害的---?{}".format(self.c_name))
 52 
 53     def computer_fist(self):
 54         li_fist_c = ["石头", "剪刀", ""]
 55         self.c_fist = random.choice(li_fist_c)
 56         print("{}在千钧一发之际使出了必杀---?{}".format(self.c_name, self.c_fist))
 57 
 58 
 59 class Begin:
 60     n = 1
 61 
 62     def __init__(self, pl, com):
 63         self.player = pl
 64         self.computer = com
 65         self.exit = None
 66 
 67     def play_game(self):
 68         self.player.player_name()
 69         print("正在出招......\n")
 70         time.sleep(1)
 71         print("敌方正在派人与你对战......\n")
 72         time.sleep(1)
 73         self.computer.computer_name()
 74         print("*"*50)
 75 
 76         while self.__class__.n <= 3:
 77             print("----------------第{}回合---------------".format(self.__class__.n))
 78             if self.player.name == "exit":
 79                 break
 80             time.sleep(1)
 81             self.player.player_fist()
 82             if self.player.fist == "exit":
 83                 break
 84             print("{}夜观天象,掐指一算......\n".format(self.computer.c_name))
 85             time.sleep(1)
 86             self.computer.computer_fist()
 87             self.compare()
 88             print("*" * 50)
 89             self.__class__.n += 1
 90             time.sleep(1)
 91         print("正在统计战绩———————?\n")
 92         print("三局定胜负, 游戏结束\n")
 93         print("战绩-------?\n")
 94         time.sleep(1)
 95         print("-------3")
 96         time.sleep(1)
 97         print("-------2")
 98         time.sleep(1)
 99         print("-------1")
100         time.sleep(1)
101         print("-------?\n")
102         print("你的总分为------?{}\n\n\n{}的总分为---?{}\n".format(self.player.score, self.computer.c_name,
103                                                           self.computer.c_score))
104         if self.player.score > self.computer.c_score:
105             print("-----你是神,你赢了!-----")
106         elif self.player.score < self.computer.c_score:
107             print("-----你是笨蛋,你输了!-----")
108         elif self.player.score == self.computer.c_score:
109             print("-----两败俱伤,平局!-----")
110 
111     def score(self):
112         print("你当前分数为:{}   {}分数为:{}".format(self.player.score, self.computer.c_name, self.computer.c_score))
113 
114     def compare(self):
115         if self.player.fist == "石头" and self.computer.c_fist == "剪刀":
116             print("你真厉害----你赢了\n")
117             self.player.score += 1
118             self.score()
119 
120         elif self.player.fist == "石头" and self.computer.c_fist == "":
121             print("命途多舛的人生----你输了\n")
122             self.computer.c_score += 1
123             self.score()
124 
125         elif self.player.fist == "石头" and self.computer.c_fist == "石头":
126             print("激烈的角逐后----打成平局\n")
127             self.score()
128 
129         elif self.player.fist == "剪刀" and self.computer.c_fist == "石头":
130             print("牧童骑黄牛,歌声振林樾----你输了\n")
131             self.computer.c_score += 1
132             self.score()
133 
134         elif self.player.fist == "剪刀" and self.computer.c_fist == "剪刀":
135             print("平平淡淡才是真----平局\n")
136             self.score()
137 
138         elif self.player.fist == "剪刀" and self.computer.c_fist == "":
139             print("我好厉害----你赢了\n")
140             self.player.score += 1
141             self.score()
142 
143         elif self.player.fist == "" and self.computer.c_fist == "剪刀":
144             print("曾经的月亮很圆,你却没有发现----你输了\n")
145             self.computer.c_score += 1
146             self.score()
147 
148         elif self.player.fist == "" and self.computer.c_fist == "石头":
149             print("??????????----你赢了")
150             self.player.score += 1
151             self.score()
152 
153         elif self.player.fist == "" and self.computer.c_fist == "":
154             print("回家----平局")
155             self.score()
156 
157 
158 player = Player()
159 computer = Computer()
160 begin = Begin(player, computer)
161 begin.play_game()

 

面向对象实现石头剪刀布游戏

标签:computer   ini   code   面向   format   style   统计   com   compare   

原文地址:https://www.cnblogs.com/tttzqf/p/9346525.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!