1 '' 2 游戲 3 1.選擇人物 4 2.購買武器 金幣 5 3.打仗 贏 得金幣 6 4.選擇刪除武器 7 5.查看游戲 8 6.退出游戲 9 ''' 10 import random 11 print('*'*40) 12 print('\t歡迎來到王者榮耀!') 13 print('*'*40) 14 15 16 role = input('請選擇游戲人物(1.魯班 2.后羿 3.李白 4.孫尚香 5.貂蟬 6.諸葛亮):') 17 18 coins = 1000 19 20 #保存自己的武器 21 weapon_list = [] 22 print('歡迎!%s來到王者峽谷!當前金幣:%d'%(role,coins)) 23 24 while True: 25 choice = int(input('請選擇:\n 1.購買武器\n 2.打仗\n 3.刪除武器\n 4.查看武器\n 5.退出游戲\n')) 26 if choice == 1: 27 #購買武器 28 print('歡迎進入我的商店!') 29 weapons = [['屠龍刀',500],['櫻花槍',400],['98k槍',1000],['手榴彈',800],['碧血劍',700],['羽扇',800]] 30 for weapon in weapons: 31 print(weapon[0],weapon[1],sep =' ') 32 #提示輸入要購買的武器 33 weaponname = input('請輸入要購買的武器名稱:') 34 #1.原來有沒有買過這個武器。2.輸入的武器名是否在武器庫當中 35 if weaponname not in weapon_list: 36 for weapon in weapons: 37 if weaponname in weapon: 38 #購買武器 39 if coins >= weapon[1]: 40 coins -= weapon[1] 41 weapon_list.append(weapon[0]) #添加到自己武器庫里里面 42 print('%s購買%s成功!'%(role,weaponname)) 43 print('%s當前武器:'%(role),weapon_list) 44 break 45 else: 46 print('金幣不足,趕快去戰斗掙金幣吧!') 47 break 48 else: 49 print('輸入武器名稱錯誤!') 50 else: 51 print('已經擁有此武器!!!') 52 elif choice ==2: 53 #打仗 假設有多個武器 54 print('進入戰場...') 55 if len(weapon_list) > 0: 56 #選擇武器 57 print('{}擁有的武器如下:'.format(role)) 58 for weapon in weapon_list: 59 print(weapon) 60 while True: 61 weaponname = input('請選擇:') 62 # 63 if weaponname in weapon_list: 64 #進入戰爭狀態 默認和張飛對戰 65 ran1 = random.randint(1,20) #張飛 66 ran2 = random.randint(1,20) #role 67 68 if ran1 > ran2: 69 print('此局戰爭:張飛勝!!!') 70 elif ran1 < ran2: 71 print('此局戰爭:%s勝'%(role)) 72 coins += 200 73 print('目前金幣:',coins) 74 else: 75 print('此局平局,可以再次對戰') 76 77 break 78 else: 79 print('選擇的武器不存在,請重新選擇:') 80 else: 81 print('趕快使用金幣購買金幣去吧!!!') 82 elif choice == 3: 83 #刪除武器 84 if len(weapon_list) > 0: 85 print('武器太多,背包空間不夠,請丟棄幾個!!!') 86 print('%s擁有的武器如下:'%(role)) 87 for weapon in weapon_list: 88 print(weapon) 89 while True: 90 weaponname = input('請選擇要刪除的武器名稱') 91 if weaponname in weapon_list: 92 #刪除武器 93 weapon_list.remove(weaponname) 94 #歸還金幣 95 #print(weapons) 96 for weapon in weapons: 97 if weaponname in weapon: 98 coins += weapon[1] 99 break 100 break 101 else: 102 print('武器名稱輸入有誤!!!') 103 else: 104 print('背包內沒有武器,快去購買吧!!!') 105 elif choice ==4: 106 #遍歷擁有的武器 107 print('%s擁有的武器如下:'%(role)) 108 for weapon in weapon_list: 109 print(weapon) 110 #查看金幣 111 print('總金幣:',coins) 112 elif choice ==5: 113 answer = input('確定要離開王者榮耀游戲嘛?(y/n):') 114 if answer == 'y': 115 break 116 else: 117 print('輸入錯誤,請重新選擇!')