Ssnakes 发表于 2022-4-15 12:32

【Python源码】Python写的.石头.剪刀.布.小游戏源码

首先声明,我是Python小白,写的不好,希望前辈们不吝指教,谢谢~{:1_893:}{:1_893:}
源码如下:
import random
player_score=0
computer_score=0   
print ('''
*************小小游戏***********

         石头    剪刀   布

**********************************
''')
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_fist=eval(input("请出拳:1,石头 2,剪刀 3,布\n"))
    if player_fist==1:
      player_fist_name="石头"
    elif player_fist==2:
      player_fist_name="剪刀"
    elif player_fist==3:
      player_fist_name="布"
    else:
      player_fist_name="石头"
      player_fist=1
    computer_fist=random.randint(1,3)
    if computer_fist==1:
      computer_fist_name="石头"
    elif computer_fist==2:
      computer_fist_name="剪刀"
    else:
      computer_fist_name="布"
    print (player_name,"出拳",player_fist_name)
    print(computer_name,"出拳",computer_fist_name)
    if player_fist==computer_fist:
      print("哇塞,打成了平局!")
    elif player_fist==1 and computer_fist==2:
      print("玩家:",player_name,"赢🦅")
      player_score+=1
    elif player_fist==2 and computer_fist==3:
      print("玩家:",player_name,"赢🦅")
      player_score+=1
    elif player_fist==3 and computer_fist==1:
      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,"完胜",computer_name)
elif player_score<computer_score:
    print(computer_name,"完胜",player_name)   
else:
    print("双方实力不分伯仲,打成平局!")

程序说明:其中有些判断输出语句还可以用更精简的语句缩短代码,用or链接会让代码更简洁,但为了看起来逻辑上更符合人类大脑,未做简化。 :loveliness::loveliness:

iawyxkdn8 发表于 2022-4-15 15:30

这么麻烦,还不如二只手一伸来得直接!{:1_918:}

大大泡泡糖121 发表于 2022-4-15 15:46

厉害了,之前看B站上有拿python写植物大战僵尸的,佩服程序员!

大大泡泡糖121 发表于 2022-4-15 15:47

游戏促进变成发展!哈哈

Ssnakes 发表于 2022-4-17 10:40

大大泡泡糖121 发表于 2022-4-15 15:47
游戏促进变成发展!哈哈

游戏是推动发展的一大动力

Ssnakes 发表于 2022-4-17 10:42

大大泡泡糖121 发表于 2022-4-15 15:46
厉害了,之前看B站上有拿python写植物大战僵尸的,佩服程序员!

编程考验的是逻辑思维,很锻炼人,那些大神也是我学习的对象{:1_918:}

Ssnakes 发表于 2022-4-17 10:43

iawyxkdn8 发表于 2022-4-15 15:30
这么麻烦,还不如二只手一伸来得直接!

哈哈哈哈~务实的坛友~:lol
页: [1]
查看完整版本: 【Python源码】Python写的.石头.剪刀.布.小游戏源码