整数变量不起作用,即使我在它们上使用了 global,它们也不会回来,我什至尝试了 return,但它不起作用。经过无数次尝试错误测试并解决问题后,我找到了问题的根源,但我不知道如何解决它。因为这段代码很长(714),所以我不会把整个事情都写出来。相反,我会提出所需的内容。
def plrcheck():
global pwr
global typ
if prsna in [sf1, sf2, sf3, sa1, sa2, sa3, sw1, sw2, sw3, se1, se2, se3]:
pwr = 5
elif prsna in [sf4, sf5, sa4, sa5, se4, se5, sw4, sw5]:
pwr = 8
elif prsna in [sf6, sa6, sw6, se6]:
pwr = 11
if prsna in [sf1, sf2, sf3, sf4, sf5, sf6]:
typ = 'Fire'
elif prsna in [sw1, sw2, sw3, sw4, sw5, sw6]:
typ = 'Water'
elif prsna in [sa1, sa2, sa3, sa4, sa5, sa6]:
typ = 'Air'
elif prsna in [se1, se2, se3, se4, se5, se6]:
typ = 'Earth'
pygame.display.flip()
def oppcheck():
global optyp
global oppwr
if opp in [sf1, sf2, sf3, sa1, sa2, sa3, sw1, sw2, sw3, se1, se2, se3]:
oppwr = 5
elif opp in [sf4, sf5, sa4, sa5, se4, se5, sw4, sw5]:
oppwr = 8
elif opp in [sf6, sa6, sw6, se6]:
oppwr = 11
if opp in [sf1, sf2, sf3, sf4, sf5, sf6]:
optyp = 'Fire'
elif opp in [sw1, sw2, sw3, sw4, sw5, sw6]:
optyp = 'Water'
elif opp in [sa1, sa2, sa3, sa4, sa5, sa6]:
optyp = 'Air'
elif opp in [se1, se2, se3, se4, se5, se6]:
optyp = 'Earth'
pygame.display.flip()
def atkchk(x):
plrcheck()
oppcheck()
if x == 'opponent':
if optyp == 'Air':
if typ == 'Earth':
oppwr += 2
elif optyp == 'Water':
if typ == 'Fire':
oppwr += 2
elif optyp == 'Fire':
if typ == 'Air':
oppwr += 2
elif optyp == 'Earth':
if typ == 'Water':
oppwr += 2
elif x == 'player':
if typ == 'Air':
if optyp == 'Earth':
pwr += 2
elif typ == 'Water':
if optyp == 'Fire':
pwr += 2
elif typ == 'Fire':
if optyp == 'Air':
pwr += 2
elif typ == 'Earth':
if optyp == 'Water':
pwr += 2
while pwr - oppwr < 0:
discard = int(math.fabs(pwr-oppwr)/2)+1
#Selection Process of Discarding for Player
while pwr - oppwr > -1:
discard = int(math.fabs(pwr-oppwr)/2)+1
#Selection process of discarding for opponent
win()
def game():
while matchLoop:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_x:
plrcheck()
oppcheck()
atkchk('player')
问题出现在 [atkchk(x)] 上,它忘记了 [pwr 和 oppwr] 变量,即使在外面它仍然可以工作。顺便说一句,这不应该需要pygame知识,只需要简单的python知识就足够了。我已经分配了所有其他变量,但这不是问题的一部分(在我添加 [atkchk(x)] 之前它工作得非常好),并且我已经将其缩小到我之前所说的范围内。那么无论如何你知道解决这个问题吗?