Python编写石头剪刀布的游戏

石头剪刀布游戏该如何编写呢?首先,我们需要使用python的random库来实现电脑随机出拳的效果,time库为装饰。

import random

import time

我们为了让电脑出拳,就需要建立一个列表,里面存储出拳的内容:

choices = ["石头", "布", "剪刀"]

 然后我们需要用random库的shuffle命令来打乱列表里面的内容:

computer = random.choice(choices)

我们需要让玩家出拳,所以需要用input()命令

player = input("输入剪刀,石头布(输入999退出游戏):").capitalize()

接下来是判断胜负的代码:

if player == computer:
        time.sleep(0.5)
        print()
        print("平局")
        time.sleep(0.5)
        print('玩家出拳:',player)
        time.sleep(0.5)
        print('CPU 出拳:',computer)
elif player == "石头":
    if computer == "布":
        time.sleep(0.5)
        print()
        print("你输啦! CPU出拳:",computer)
        time.sleep(0.7)
        print('你出拳:',player)
        cpu_score+=1
    else:
        time.sleep(0.5)
        print()
        print("你赢了! 你出拳:", player)
        time.sleep(0.7)
        print('CPU 出拳:',computer)
        player_score+=1
elif player == "布":
    if computer == "剪刀":
        time.sleep(0.5)
        print()
        print("你输啦! CPU出拳:",computer)
        time.sleep(0.7)
        print('你出拳:',player)
    else:
        time.sleep(0.5)
        print()
        print("你赢了! 你出拳:", player)
        time.sleep(0.7)
        print('CPU 出拳:',computer)
        player_score+=1
elif player == "剪刀":
    if computer == "石头":
        time.sleep(0.5)
        print()
        print("你输啦! CPU出拳:",computer)
        time.sleep(0.7)
        print('你出拳:',player)
    else:
        time.sleep(0.5)
        print()
        print("你赢了! 你出拳:", player)
        time.sleep(0.7)
        print('CPU 出拳:',computer)
        player_score+=1

然后是输入999退出的代码:

if player=='999':
    if player_score>cpu_score:
        time.sleep(0.5)
        print()
        print("胜利者为:",a)
        time.sleep(0.4)
        print('玩家分数为:',player_score)
        time.sleep(0.4)
        print('电脑分数为:',cpu_score)
        time.sleep(1)
        print()
        print('游戏结束')
        time.sleep(1)
        print('+=============================================+')
        break
    if cpu_score>player_score:
        time.sleep(0.5)
        print()
        print("胜利者为: CPU")
        time.sleep(0.4)
        print('玩家分数为:',player_score)
        time.sleep(0.4)
        print('电脑分数为:',cpu_score)
        time.sleep(1)
        print()
        print('游戏结束')
        time.sleep(1)
        print('+=============================================+')
        break
    if cpu_score==player_score:
        time.sleep(0.5)
        print()
        print("本次游戏结果为: 平局")
        time.sleep(0.4)
        print('玩家分数为:',player_score)
        time.sleep(0.4)
        print('电脑分数为:',cpu_score)
        time.sleep(1)
        print()
        print('游戏结束')
        time.sleep(1)
        print('+=============================================+')
        break

接下来就是全部代码了:

import random
import time
choices = ["石头", "布", "剪刀"]
computer = random.choice(choices)
player = False
cpu_score = 0
player_score = 0
print('+=============================================+')
print('+                  猜拳游戏                   +')
print('+=============================================+')
time.sleep(0.4)
a=input('请输入你的姓名:')

while True:
    time.sleep(1)
    print()
    player = input("输入剪刀,石头布(输入999退出游戏):").capitalize()
    if player == computer:
        time.sleep(0.5)
        print()
        print("平局")
        time.sleep(0.5)
        print('玩家出拳:',player)
        time.sleep(0.5)
        print('CPU 出拳:',computer)
    elif player == "石头":
        if computer == "布":
            time.sleep(0.5)
            print()
            print("你输啦! CPU出拳:",computer)
            time.sleep(0.7)
            print('你出拳:',player)
            cpu_score+=1
        else:
            time.sleep(0.5)
            print()
            print("你赢了! 你出拳:", player)
            time.sleep(0.7)
            print('CPU 出拳:',computer)
            player_score+=1
    elif player == "布":
        if computer == "剪刀":
            time.sleep(0.5)
            print()
            print("你输啦! CPU出拳:",computer)
            time.sleep(0.7)
            print('你出拳:',player)
        else:
            time.sleep(0.5)
            print()
            print("你赢了! 你出拳:", player)
            time.sleep(0.7)
            print('CPU 出拳:',computer)
            player_score+=1
    elif player == "剪刀":
        if computer == "石头":
            time.sleep(0.5)
            print()
            print("你输啦! CPU出拳:",computer)
            time.sleep(0.7)
            print('你出拳:',player)
        else:
            time.sleep(0.5)
            print()
            print("你赢了! 你出拳:", player)
            time.sleep(0.7)
            print('CPU 出拳:',computer)
            player_score+=1
    if player=='999':
        if player_score>cpu_score:
            time.sleep(0.5)
            print()
            print("胜利者为:",a)
            time.sleep(0.4)
            print('玩家分数为:',player_score)
            time.sleep(0.4)
            print('电脑分数为:',cpu_score)
            time.sleep(1)
            print()
            print('游戏结束')
            time.sleep(1)
            print('+=============================================+')
            break
        if cpu_score>player_score:
            time.sleep(0.5)
            print()
            print("胜利者为: CPU")
            time.sleep(0.4)
            print('玩家分数为:',player_score)
            time.sleep(0.4)
            print('电脑分数为:',cpu_score)
            time.sleep(1)
            print()
            print('游戏结束')
            time.sleep(1)
            print('+=============================================+')
            break
        if cpu_score==player_score:
            time.sleep(0.5)
            print()
            print("本次游戏结果为: 平局")
            time.sleep(0.4)
            print('玩家分数为:',player_score)
            time.sleep(0.4)
            print('电脑分数为:',cpu_score)
            time.sleep(1)
            print()
            print('游戏结束')
            time.sleep(1)
            print('+=============================================+')
            break
    
            
    computer = random.choice(choices)

运行结果:

Python编写石头剪刀布的游戏_第1张图片

 

本文内容就到这里,感谢阅读和观看,谢谢!

 

 

你可能感兴趣的:(python,游戏)