Python是一种易学易用的编程语言,适用于各种领域。其中,使用Python进行开发的游戏也是十分有趣的。在此,我们将给大家介绍一种Python编写的猜拳游戏。
import random
print("~~~~~~~~~~~~~~~~~~~~~~~~~")
print("欢迎来到Python猜拳游戏!")
print("~~~~~~~~~~~~~~~~~~~~~~~~~")
rounds = int(input("请输入游戏轮数:"))
moves = ["石头","剪刀","布"]
score = [0,0] # 玩家和电脑的得分
for i in range(rounds):
print("============= 第 %d 局 =============" % (i + 1))
print("可供出拳的选项为:")
for j in range(3):
print("【%d】%s" % (j + 1,moves[j]))
player = int(input("请输入你的选择:"))
computer = random.randint(1,3)
print("你出了%s,电脑出了%s" % (moves[player-1],moves[computer-1]))
if player == computer:
print("平局")
elif (player - computer == 1) or (player == 1 and computer == 3):
print("你赢了!")
score[0] += 1
else:
print("电脑赢了!")
score[1] += 1
print("当前得分为:你 %d : %d 电脑" % (score[0],score[1]))
print("游戏结束!最终得分为:你 %d : %d 电脑" % (score[0],score[1]))
这个游戏非常简单,通过输入轮数和出拳选项,就可以和电脑进行猜拳。代码中我们使用了Python内置的随机数生成函数,轻松地实现了电脑的出拳操作。在游戏的过程中,我们还用了循环和条件语句,自动进行方便的计数以及判断胜负。在最终结束时输出你和电脑的得分情况,游戏愉快!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。