python猜拳游戏

前言

初学写的猜拳游戏,几十行代码搞定。比较玩家和电脑之间使用数字替代,最终记录获胜者的得分,并且显示出来。
电脑使用random库,随机生成1-3之间的数字。

一、代码

#猜拳游戏
import random
player_score = 0
computer_score = 0
#
print('''
***********欢迎来到4399小游戏平台*************
                    石头          剪刀           布
**********************************************
''')
player_name=input('请输入玩家姓名:')
print('1.猴子  2.猪八戒  3唐僧')
choice=eval(input('请输入电脑玩家:'))
if choice == 1:
    computer_name = '猴子'
elif choice == 2:
    computer_name = '猪八戒'
elif choice == '3':
    computer_name = '唐僧'
else:
    computer_name = '匿名'
print(player_name ,'VS' ,computer_name)
while True:
    #玩家出拳
    player_first = eval(input('----------请出拳:1.石头   2.剪刀  3.布---------------'))
    if player_first == 1:
        player_fist_name = '石头'
    elif player_first == 2:
        player_fist_name = '剪刀'
    elif player_name == 3:
        player_fist_name = '布'
    else:
        player_fist_name = '石头'
        player_first = 1
    #电脑出拳
    computer_first = random.randint(1, 3)
    if computer_first==1:
        computer_fist_name = '石头'
    elif computer_first == 2:
        computer_fist_name = '剪刀'
    elif computer_first ==3:
        computer_fist_name = '布'
    print(player_name, '出拳', player_fist_name)
    print(computer_name, '出拳', computer_fist_name)
    if player_fist_name == computer_fist_name:
        print('平局')
    elif player_fist_name>=computer_fist_name:
        print(player_name, '赢')
        player_score+=1
    else:
        print(computer_name, '赢')
        computer_score+=1
    answer = input('再来一局不?y/n')
    if answer != 'y':
        break

#最终积分
print('----------------------------------------')
print(player_name, '总共得', player_score)
print(computer_name, '总共得', computer_score)
print('----------------------------------------')

if player_score>computer_score:
    print('玩家', player_name, '获胜')
elif computer_score>player_score:
    print('电脑', computer_name, '获胜')
else:
    print('平局')

总结

总结自己对python编程语言的理解,熟练掌握python中的各项功能,为以后的学习奠定基础。

你可能感兴趣的:(python,游戏,开发语言)