python - 第一次调用后的变量丢失
问题描述
所以我正在尝试编写一个基于 UI 的 AK47 纸牌游戏,但我不能看在上帝的份上,为什么它让我选择第一轮,然后取消定义变量。我很茫然。我想我可能在 Draw Class 中定义了一些错误,因为这是构成大部分动作的原因,但是我添加了 print("") 所谓的调试消息,它表明相应的卡片在那里并且变量在它完成最后的动作之后才存在。
import time
import random
import tkinter as tk
suit = ["Club","Heart","Diamond","Spade"]
numb = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]
junkHand = []
data = "true"
#The inital Drawing of 4 cards.
class drawCard:
def __init__(self):
self.cardnr = []
self.Hand = []
self.card = []
#looping until The ai and Player both have 4 cards.
while(len(self.Hand) <= 3):
self.cards = random.choice(suit)
self.cardn = random.choice(numb)
self.card = self.cards+" "+self.cardn
if(self.card in self.Hand):
#putind already drawn cards into the bad pile
junkHand.append(self.card)
else:
junkHand.append(self.card)
#appending the numbers of cards to card number Array.
self.cardnr.append(self.cardn)
#appending the cards to the hand Array.
self.Hand.append(self.card)
playerHand = drawCard()
AIHand = drawCard()
#Discardng a useless card and drawing a new card.
class Draw:
def __init__(self, choiceP, pHand, cardnr, data):
self.card = []
print(pHand)
#poping out the chosen card.
pHand.pop(int(choiceP)-1)
cardnr.pop(int(choiceP)-1)
while(len(pHand) <= 3):
#generating a new card
self.cards = random.choice(suit)
self.cardn = random.choice(numb)
#combining them
self.card = self.cards+" "+self.cardn
#Testing the cards
if(self.card in junkHand):
junkHand.append(self.card)
else:
junkHand.append(self.card)
cardnr.append(self.cardn)
pHand.append(self.card)
print(pHand)
#testing if the AI or Player moved.
if(data == "false"):
hand1Draw['file'] = 'data\\gif\\'+str(playerHand.Hand[0])+'.gif'
hand2Draw['file'] = 'data\\gif\\'+str(playerHand.Hand[1])+'.gif'
hand3Draw['file'] = 'data\\gif\\'+str(playerHand.Hand[2])+'.gif'
hand4Draw['file'] = 'data\\gif\\'+str(playerHand.Hand[3])+'.gif'
GameP2()
elif(data == "true"):
AIDraw()
def AIDraw():
global numb, AIHand, data
AISmort = []
print(AISmort)
#testing if the AIHand has any neccesary cards from AK47.
if(numb[0] in AIHand.cardnr):
AISmort.append(AIHand.cardnr.index(numb[0])+1)
if(numb[3] in AIHand.cardnr):
AISmort.append(AIHand.cardnr.index(numb[3])+1)
if(numb[6] in AIHand.cardnr):
AISmort.append(AIHand.cardnr.index(numb[6])+1)
if(numb[12] in AIHand.cardnr):
AISmort.append(AIHand.cardnr.index(numb[12])+1)
rndChoiceAI = random.randint(1,4)
if(rndChoiceAI in AISmort):
print(AISmort)
AIDraw()
else:
AISmort.clear()
data = "false"
AIHand = Draw(rndChoiceAI, AIHand.Hand, AIHand.cardnr, data)
def GameP2():
global playerHand
print("gameP2")
print(playerHand.Hand)
if(numb[0] in playerHand.cardnr):
if(numb[3] in playerHand.cardnr):
if(numb[6] in playerHand.cardnr):
if(numb[12] in playerHand.cardnr):
print("you won the game :)")
time.sleep(10)
execfile('main.py')
else: pass
else: pass
else: pass
else: pass
if(numb[0] in AIHand.cardnr):
if(numb[3] in AIHand.cardnr):
if(numb[6] in AIHand.cardnr):
if(numb[12] in AIHand.cardnr):
print("you lost the game :(")
time.sleep(10)
execfile('main.py')
else: pass
else: pass
else: pass
else: print(playerHand.Hand)
def C1():
global playerHand
data = "true"
choiceP = 1
playerHand = Draw(choiceP, playerHand.Hand, playerHand.cardnr, data)
def C2():
global playerHand
data = "true"
choiceP = 2
playerHand = Draw(choiceP, playerHand.Hand, playerHand.cardnr, data)
def C3():
global playerHand
data = "true"
choiceP = 3
playerHand = Draw(choiceP, playerHand.Hand, playerHand.cardnr, data)
def C4():
global playerHand
data = "true"
choiceP = 4
playerHand = Draw(choiceP, playerHand.Hand, playerHand.cardnr, data)
cHeight = 768
cWidth = 1366
root = tk.Tk()
root.resizable(False, False)
root.title("Carlos' AK47")
canvas = tk.Canvas(root, height=cHeight, width=cWidth)
canvas.pack()
TopFrame = tk.Frame(root, bg='#252525')
TopFrame.place(relwidth=1, relheight=0.05)
tableFrame = tk.Frame(root, bg='#639D5E')
tableFrame.place(rely=0.05, relwidth=1, relheight=0.95)
hand1Draw = tk.PhotoImage(file='data\\gif\\'+str(playerHand.Hand[0])+'.gif', master=tableFrame)
hand1Button = tk.Button(tableFrame, image=hand1Draw, relief='flat',command=C1)
hand1Button.place(relwidth=0.14, relheight=0.4, relx=0.2, rely=0.65)
hand2Draw = tk.PhotoImage(file='data\\gif\\'+str(playerHand.Hand[1])+'.gif', master=tableFrame)
hand2Button = tk.Button(tableFrame, image=hand2Draw, relief='flat',command=C2)
hand2Button.place(relwidth=0.14, relheight=0.4, relx=0.34, rely=0.65)
hand3Draw = tk.PhotoImage(file='data\\gif\\'+str(playerHand.Hand[2])+'.gif', master=tableFrame)
hand3Button = tk.Button(tableFrame, image=hand3Draw, relief='flat',command=C3)
hand3Button.place(relwidth=0.14, relheight=0.4, relx=0.48, rely=0.65)
hand4Draw = tk.PhotoImage(file='data\\gif\\'+str(playerHand.Hand[3])+'.gif', master=tableFrame)
hand4Button = tk.Button(tableFrame, image=hand4Draw, relief='flat',command=C4)
hand4Button.place(relwidth=0.14, relheight=0.4, relx=0.62, rely=0.65)
root.mainloop()
它在没有 UI 的情况下完美运行,但是一旦我添加了 UI。它引发了很多错误,其中大部分是我修复的,但它们带来了新的错误。即 cussent 的。
Traceback (most recent call last):
File "C:\Users\Predator Backuo\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "c:\Users\Predator Backuo\Desktop\TLN Documents\Code\AK47\ak47.py", line 132, in C2
playerHand = Draw(choiceP, playerHand.Hand, playerHand.cardnr, data)
AttributeError: 'Draw' object has no attribute 'Hand'
PS C:\Users\Predator Backuo\Desktop\TLN Documents\Code\AK47>
谁能告诉我我做错了什么?我不是最擅长python,我只是出于爱好。
解决方案
At first you are assigning a drawCard object to playerHand = drawCard()
, which does contain a Hand
attribute (line 13):
#The inital Drawing of 4 cards.
class drawCard:
def __init__(self):
self.cardnr = []
self.Hand = [] <--- the Hand attribute
self.card = []
...
playerHand = drawCard() <-- add instance of this class to playerHand
But later on in the code (see your C functions), you are overwriting the playerHand
variable with a Draw
instance, which doesn't contain an attribute "Hand":
def C1():
global playerHand
data = "true"
choiceP = 1
playerHand = Draw(choiceP, playerHand.Hand, playerHand.cardnr, data) <-- add instance of another class to playerHand variable
推荐阅读
- php - 使用 Laravel 8 使用动态路由获取错误 500
- django - 如何计算 django 模型中的对象总数
- c - 项目:NodeMCU esp8266-esp12e 连接到另一个 esp8266-esp12e - 社交距离
- python - 运行 WSGI 应用程序时出现错误 NameError: 当我将 DJango 应用程序部署到 pythonanywhere 时?
- php - 我在 AWS linux 2 上设置弹性 bean stalk 时遇到问题。这是一个以 Postgres 作为数据库的 laravel 应用程序。用户密码验证失败
- android - Viewstub:如何使用两种方式数据绑定,文本不变
- redux - 当我们使用 React 在表单中选择单个产品时,如何绑定或填充复选框和单选按钮值
- python - Python 记录器的类型注释
- c++ - 有没有办法初始化动态创建的二维数组
- mql4 - 计算找到利润点。MQL4