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

Posted tttzqf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了面向对象实现石头剪刀布游戏相关的知识,希望对你有一定的参考价值。

  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退出):
")
 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("正在出招......
")
 70         time.sleep(1)
 71         print("敌方正在派人与你对战......
")
 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("{}夜观天象,掐指一算......
".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("正在统计战绩———————?
")
 92         print("三局定胜负, 游戏结束
")
 93         print("战绩-------?
")
 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("-------?
")
102         print("你的总分为------?{}


{}的总分为---?{}
".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("你真厉害----你赢了
")
117             self.player.score += 1
118             self.score()
119 
120         elif self.player.fist == "石头" and self.computer.c_fist == "":
121             print("命途多舛的人生----你输了
")
122             self.computer.c_score += 1
123             self.score()
124 
125         elif self.player.fist == "石头" and self.computer.c_fist == "石头":
126             print("激烈的角逐后----打成平局
")
127             self.score()
128 
129         elif self.player.fist == "剪刀" and self.computer.c_fist == "石头":
130             print("牧童骑黄牛,歌声振林樾----你输了
")
131             self.computer.c_score += 1
132             self.score()
133 
134         elif self.player.fist == "剪刀" and self.computer.c_fist == "剪刀":
135             print("平平淡淡才是真----平局
")
136             self.score()
137 
138         elif self.player.fist == "剪刀" and self.computer.c_fist == "":
139             print("我好厉害----你赢了
")
140             self.player.score += 1
141             self.score()
142 
143         elif self.player.fist == "" and self.computer.c_fist == "剪刀":
144             print("曾经的月亮很圆,你却没有发现----你输了
")
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()

 

以上是关于面向对象实现石头剪刀布游戏的主要内容,如果未能解决你的问题,请参考以下文章

Linux脚本shell编程通过数组实现石头剪刀布小游戏

python-编写石头剪刀布小游戏

ZKP应用:石头剪刀布游戏

C#简易人机对抗“石头剪刀布”游戏

石头剪刀布游戏

石头剪刀布游戏