Python卡牌游戏开发:如何将失败者卡牌移至胜利者牌组末尾?
实现卡牌转移功能的解决方案
首先得先修正你代码里的一个小问题:Read函数创建Ram实例时少传了一个参数——Ram构造函数需要9个参数,但你只传了i[0]到i[7],漏掉了对应fat的i[8],不修正的话运行会直接报错。
接下来就是核心的卡牌转移逻辑,其实思路很直白:
- 胜负判定后,把双方打出的顶牌(列表索引0的元素)取出来
- 胜利者把这两张牌追加到自己牌组的末尾(顺序可以按规则调整,比如先放自己的牌再放对手的)
- 因为已经用
pop取出了顶牌,失败者的牌组会自动移除这张牌,不用额外操作
下面是修改后的完整代码,我标注了关键改动的地方:
import random class Ram(): def __init__(self, name, weight, milk, wool, offs, thigh, fert, meat, fat): self.name = name self.weight = weight self.milk = milk self.wool = wool self.offs = offs self.thigh = thigh self.fert = fert self.meat = meat self.fat = fat def __repr__(self): return f"{self.name,self.weight,self.milk,self.wool,self.offs,self.thigh,self.fert,self.meat,self.fat}" def Read(txtfile): datalist=[] f = open(txtfile, "r", encoding="utf-8") for line in f: # 去掉每行末尾的换行符,避免参数带多余换行 line = line.strip() datalist.append(line.split(',')) f.close() cardlist = [] for i in datalist: # 修正:补充fat对应的i[8]参数 cardlist.append(Ram(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7], i[8])) return cardlist def RandomChoice(): l=["User","Computer"] return random.choice(l) cardlist=Read("hrutaspil.txt") random.shuffle(cardlist) split = len(cardlist) // 2 user_deck = cardlist[:split] computer_deck = cardlist[split:] game=input("Do you want to play?(y/n)") if game == "y": if RandomChoice() == "User": print("The user chooses first") print("Your top card is:",user_deck[0].name) print("1 = weight =",user_deck[0].weight) print("2 = milk =", user_deck[0].milk) print("3 = wool =", user_deck[0].wool) print("4 = offs =", user_deck[0].offs) print("5 = thigh =", user_deck[0].thigh) print("6 = fert =", user_deck[0].fert) print("7 = meat =", user_deck[0].meat) print("8 = fat =", user_deck[0].fat) choice=input("Choose stat: ") # 先取出双方顶牌,避免后续操作干扰 user_card = user_deck.pop(0) computer_card = computer_deck.pop(0) if choice == ("1"): user_stat = float(user_card.weight) comp_stat = float(computer_card.weight) print(f"Weight is: {user_stat}") print(f"compared with computer: {comp_stat}") if comp_stat > user_stat: print("Computer wins.") # 电脑获胜,将两张牌加到自己牌组末尾 computer_deck.append(computer_card) computer_deck.append(user_card) elif comp_stat < user_stat: print("You win.") # 用户获胜,将两张牌加到自己牌组末尾 user_deck.append(user_card) user_deck.append(computer_card) else: # 处理平局情况,这里示例为双方收回自己的牌 print("It's a tie! Both players take back their cards.") user_deck.append(user_card) computer_deck.append(computer_card) # 可以继续扩展其他属性的判断逻辑,和weight的逻辑一致 else: print("The computer chooses first") # 补充电脑选属性的逻辑,比如随机选一个属性 comp_choice = random.choice(["1","2","3","4","5","6","7","8"]) stat_names = { "1": "weight", "2": "milk", "3": "wool", "4": "offs", "5": "thigh", "6": "fert", "7": "meat", "8": "fat" } print(f"Computer chooses {stat_names[comp_choice]}") user_card = user_deck.pop(0) computer_card = computer_deck.pop(0) user_stat = float(getattr(user_card, stat_names[comp_choice])) comp_stat = float(getattr(computer_card, stat_names[comp_choice])) print(f"Your {stat_names[comp_choice]}: {user_stat}") print(f"Computer's {stat_names[comp_choice]}: {comp_stat}") if comp_stat > user_stat: print("Computer wins.") computer_deck.append(computer_card) computer_deck.append(user_card) elif comp_stat < user_stat: print("You win.") user_deck.append(user_card) user_deck.append(computer_card) else: print("It's a tie! Both players take back their cards.") user_deck.append(user_card) computer_deck.append(computer_card)
关键细节说明:
- 用
pop(0)取出顶牌:这个方法会移除列表第一个元素并返回它,完美契合“打出当前顶牌”的需求。 - 平局处理:我加了平局的逻辑示例,你可以根据游戏规则改成其他方式,比如双方再各出几张牌进行新一轮比拼(也就是常见的“战争”机制)。
- 电脑选属性的逻辑:补充了电脑先选的流程,让游戏更完整,用
getattr可以动态获取卡牌的属性值,不用写重复的判断代码。
内容的提问来源于stack exchange,提问作者Mateusz Kuzniewski
相关产品推荐
相关产品推荐

