我试图创建一个非常简单的python脚本,它将两个字符放在一起,但是在运行脚本时,它执行脚本用来定义两个字符统计信息的前两个函数,但是当它到达第三个函数时,它就挂起了。在
代码如下:
#STAPS: Strength Toughness Agility Perception weapon Skill
#A comparative simulator
import random
#Functions used to define character parameters
#Character 1's parameter function
def char1():
global s1
global t1
global a1
global p1
global dam1
global dt1
global dr1
global ac1
global ws1
s1 = int(input("Char1's Strength? "))
t1 = int(input("Char1's Toughness? "))
a1 = int(input("Char1's Agility? "))
p1 = int(input("Char1's Perception? "))
dam1 = int(input("Char1's Damage? "))
dt1 = int(input("Char1's Damage Threshold? "))
dr1 = int(input("Char1's Damage Resistance? "))
ac1 = int(input("Char1's Armor Class? "))
ws1 = int(input("Char1's Weapon Skill? "))
#Character 2's paramter function
def char2():
global s2
global t2
global a2
global p2
global dam2
global dt2
global dr2
global ac2
global ws2
s2 = int(input("Char2's Strength? "))
t2 = int(input("Char2's Toughness? "))
a2 = int(input("Char2's Agility? "))
p2 = int(input("Char2's Perception? "))
dam2 = int(input("Char2's Damage? "))
dt2 = int(input("Char2's Damage Threshold? "))
dr2 = int(input("Char2's Damage Resistance? "))
ac2 = int(input("Char2's Armor Class? "))
ws2 = int(input("Char2's Weapon Skill? "))
#Main battle function. Ordo Xenos calls this "complex and easy to misuse"
#Jury-rigged way of getting names, why did I include them anyways?
def stapsbatt(c1n,c2n,hp1,hp2):
while hp1 > 0 or hp2 > 0:
#determines original raw acc
char1rawacc = ws1 - ac2
#if statement settles it to minimum 95% acc
if char1rawacc > 95:
char1rawacc = 95
#random int used to determine whether it's a hit or not
char1hitnum = random.randint(0, 100)
if char1rawacc > char1hitnum:
moddam1 = dam1 - dt2
if moddam1 < 0:
moddam1 = 0
rawdam1 = moddam1 * (100 - dr2)
hp2 = hp2 - rawdam1
#Now we move on to doing char2's batt calcs
char2rawacc = ws2 - ac1
if char2rawacc > 95:
char2rawacc = 95
char2hitnum = random.randint(0, 100)
if char2rawacc > char2hitnum:
moddam2 = dam2 - dt1
if moddam2 < 0:
moddam2 = 0
rawdam2 = moddam2 * (100 - dr1)
hp1 = hp1 - rawdam2
if hp1 == 0:
print(c2n, "has won!")
else:
print(c1n, "has won!")
char1()
char2()
stapsbatt("Character 1", "Character 2",400,30)
input("Press enter to exit. ")
是的,这段代码是完全未经编辑的,我知道我的评论不是很好。在
代码中有一些问题导致它挂起。我建议您熟悉python调试器,例如pdb。这将允许您在运行时查看值,逐行逐行执行程序。在
撇开缩进问题不谈,以下是我发现的问题:
while hp1 > 0 and hp2 > 0
作为条件。您不想在这里使用or
,否则它会一直循环,直到两个字符都有<;0hp,所以您应该将其更改为and
。在rawacc
(对于这两个字符),您使用了if char1rawacc > 95
,它实际上对char1rawacc
强制执行一个最大值。你对char2rawacc
做了同样的事情。将它们切换到if char1rawacc < 95
和if char2rawacc < 95
。在希望这能让你走出无限循环!我用这些改变让游戏在我的电脑上运行。在
现在,正如Oz123提到的,你真的在滥用全局变量。相反,您应该研究如何创建和传递对象(参见9000的答案)。例如,您可以为您的角色定义一个类,并创建该类的两个实例(char1和char2),并传递给您的battle函数。这也将使您免于大量的冗余代码。这将需要一些重大的重组,但这是值得的。如果你在这方面遇到了问题,请随意提出一个新问题。在
你可能要找的问题是:
这个循环永远不会结束,因为无论你在里面做什么都不会改变它的状态。可能你想要一个
if
。在剩下的:天哪。看到这个代码很痛苦。让我们做得更好一点。在
不要使用
global
,除非你有一个非常好的理由这样做。现在,把这看作是一个纪律问题;当你作为一个程序员进步时,你会明白为什么分离作用域很重要。在使用函数来描述类似的事情一次。关键在于:去掉重复的部分,用名字代替,用新的“单词”让你的语言更接近你正在解决的问题。在
^{pr2}$使用数据结构。在这种情况下,将一个角色的统计信息合并到一个实体中。在
了解类时,请考虑创建一个自定义类来描述字符:
之后,您的代码可能如下所示:
首先,注释必须与代码处于相同的缩进级别。在
相关问题 更多 >
编程相关推荐