出拳小游戏python
时间: 2024-03-06 08:45:37 浏览: 92
python实现猜拳小游戏
5星 · 资源好评率100%
出拳小游戏是一种简单的猜拳游戏,玩家和电脑进行对战,通过选择不同的出拳方式来决定胜负。下面是一个简单的出拳小游戏的Python代码示例:
```python
import random
def get_player_choice():
choice = input("请选择出拳方式(1.剪刀 2.石头 3.布):")
while choice not in ['1', '2', '3']:
choice = input("请选择有效的出拳方式(1.剪刀 2.石头 3.布):")
return int(choice)
def get_computer_choice():
return random.randint(1, 3)
def get_winner(player_choice, computer_choice):
if player_choice == computer_choice:
return "平局"
elif (player_choice == 1 and computer_choice == 3) or \
(player_choice == 2 and computer_choice == 1) or \
(player_choice == 3 and computer_choice == 2):
return "玩家胜利"
else:
return "电脑胜利"
def play_game():
print("欢迎来到出拳小游戏!")
while True:
player_choice = get_player_choice()
computer_choice = get_computer_choice()
print("玩家选择:", player_choice)
print("电脑选择:", computer_choice)
winner = get_winner(player_choice, computer_choice)
print("结果:", winner)
play_again = input("是否再玩一局?(y/n):")
if play_again.lower() != 'y':
break
play_game()
```
这个游戏中,玩家通过输入数字选择出拳方式(1代表剪刀,2代表石头,3代表布),电脑随机选择出拳方式。根据出拳方式的不同,判断胜负关系并输出结果。玩家可以选择是否再玩一局。
阅读全文