admin 管理员组文章数量: 1086019
2024年2月18日发(作者:python基础训练题及答案)
python小游戏代码
import random
def display_instructions():
print("欢迎来到石头剪刀布游戏!")
print("游戏规则:")
print("1. 石头:普通攻击")
print("2. 剪刀:剪切攻击,可以打败石头")
print("3. 布:防护,可以打败剪刀")
print("每一轮游戏,电脑会随机选择石头、剪刀或布。")
print("如果你想退出游戏,请输入'退出'。")
def get_user_choice():
user_choice = input("请选择(石头、剪刀或布):")
while user_choice not in ['石头', '剪刀', '布'] and
user_choice != '退出':
print("无效的选择,请重新选择(石头、剪刀或布)或输入'退出':user_choice = input("请选择(石头、剪刀或布):")
return user_choice
def get_computer_choice():
choices = ['石头', '剪刀', '布']
return (choices)
")
def determine_winner(user_choice, computer_choice):
if user_choice == '退出':
print("游戏结束,你选择了退出。")
elif user_choice == computer_choice:
print("平局!")
elif (user_choice == '石头' and computer_choice == '剪刀') or
(user_choice == '剪刀' and computer_choice == '布') or
(user_choice == '布' and computer_choice == '石头'):
print("你赢了!")
else:
print("你输了!")
return
def play_game():
display_instructions()
while True:
user_choice = get_user_choice()
if user_choice == '退出':
break
computer_choice = get_computer_choice()
print("电脑选择了:", computer_choice)
determine_winner(user_choice, computer_choice)
print("谢谢游玩!")
play_game()
这个游戏的流程是:首先,电脑会显示游戏说明。然后,用户会被要求输入他们的选择(石头、剪刀或布)。用户的输入会被检查,如果输入不是“石头”、“剪刀”或“布”,则会被要求重新输入。然后,电脑会随机选择一种攻击方式。接着,游戏会确定胜者并输出结果。如果用户想退出游戏,可以输入“退出”,游戏将结束。游戏的循环会一直进行下去,直到用户选择退出。
版权声明:本文标题:python小游戏代码 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/b/1708249368a517730.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论