python小游戏代码

python小游戏代码


2023年12月27日发(作者:)

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()

这个游戏的流程是:首先,电脑会显示游戏说明。然后,用户会被要求输入他们的选择(石头、剪刀或布)。用户的输入会被检查,如果输入不是“石头”、“剪刀”或“布”,则会被要求重新输入。然后,电脑会随机选择一种攻击方式。接着,游戏会确定胜者并输出结果。如果用户想退出游戏,可以输入“退出”,游戏将结束。游戏的循环会一直进行下去,直到用户选择退出。


发布者:admin,转转请注明出处:http://www.yc00.com/news/1703622658a1306521.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信