当前位置:   article > 正文

【Python小游戏示例:猜拳游戏】

【Python小游戏示例:猜拳游戏】

当然可以!以下是一个简单的Python小游戏示例:猜拳游戏。在这个游戏中,玩家将与计算机进行猜拳(石头、剪刀、布)。

import random

def get_computer_choice():
    choices = ['石头', '剪刀', '布']
    return random.choice(choices)

def get_player_choice():
    choice = input("请输入你的选择(石头、剪刀、布):")
    while choice not in ['石头', '剪刀', '布']:
        choice = input("输入无效,请重新输入你的选择(石头、剪刀、布):")
    return choice

def determine_winner(player_choice, computer_choice):
    if player_choice == computer_choice:
        return "平局!"
    elif (player_choice == "石头" and computer_choice == "剪刀") or \
         (player_choice == "剪刀" and computer_choice == "布") or \
         (player_choice == "布" and computer_choice == "石头"):
        return "你赢了!"
    else:
        return "你输了!"

def play_game():
    player_choice = get_player_choice()
    computer_choice = get_computer_choice()
    print(f"\n你的选择是:{player_choice}")
    print(f"计算机的选择是:{computer_choice}")
    result = determine_winner(player_choice, computer_choice)
    print(result)

if __name__ == "__main__":
    play_game()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

在这个游戏中,玩家输入他们的选择(石头、剪刀或布),计算机随机生成一个选择,然后程序会判断并宣布赢家。你可以运行这段代码,并根据提示来玩猜拳游戏。

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号