目录
1、控制台输入数字代表石头剪刀布,用随机数随机石头剪刀布
2、对比控制台输入和随机到的结果
3、设置输出颜色
4、记录胜利、平局、失败次数
5、输入不在设定范围内提示输入有误
6、退出游戏告知胜率
1、类的定义,用来设定输出颜色
2、判断if elif else 的使用
3、在死循环中退出循环 break
4、随机函数 random
5、字符串相等 ==
6、and or
- """
- * @Author: xiaofang
- * @software: PyCharm
- * @Description: 猜拳小游戏
- """
- import random
-
-
- class BColors:
- HEADER = '\033[95m'
- OKBLUE = '\033[94m'
- OKGREEN = '\033[92m'
- WARNING = '\033[93m'
- FAIL = '\033[91m'
- ENDC = '\033[0m'
- BOLD = '\033[1m'
- UNDERLINE = '\033[4m'
-
-
- lose = 0
- win = 0
- ping = 0
- while True:
- print(BColors.HEADER + '**************************欢迎来猜拳*******************' + BColors.ENDC)
- print('1 剪刀 2 石头 3 布 4 退出游戏')
- print(BColors.UNDERLINE + '赢:%s 平:%s 输:%s' % (win, ping, lose) + BColors.ENDC)
- robot = random.choice(['剪刀', '布', '石头'])
- h = input(BColors.BOLD + '请出: ' + BColors.ENDC)
- if (h == '1' and robot == '布') or (h == '2' and robot == '剪刀') or (h == '3' and robot == '石头'):
- win += 1
- print(BColors.OKGREEN + '很高兴,你赢了' + BColors.ENDC)
- elif (h == '1' and robot == '剪刀') or (h == '2' and robot == '石头') or (h == '3' and robot == '布'):
- ping += 1
- print(BColors.OKBLUE + '很高兴,平局' + BColors.ENDC)
- elif (h == '1' and robot == '石头') or (h == '2' and robot == '布') or (h == '3' and robot == '剪刀'):
- lose += 1
- print(BColors.FAIL + '很高兴,它赢了' + BColors.ENDC)
- elif h == '4':
- print('已退出游戏,游戏结果如下:')
- print(BColors.UNDERLINE + '赢:%s 平:%s 输:%s' % (win, ping, lose) + BColors.ENDC)
- print('获胜率:', str(win * 100 / (win + ping + lose)) + '%')
- break
- else:
- print(BColors.WARNING + '输入错误' + BColors.ENDC)