有没有简单的方法可以压缩此代码?

时间:2019-10-18 20:03:53

标签: python-3.x

这是我目前正在编程/测试的恐龙狩猎风格游戏的代码片段。到目前为止,这是代码中最可怕的低效率部分,我对是否有很多需要适当冷凝的区域很感兴趣。由此生成的统计信息只需传递到类结构中即可创建实际的生物。随着拟议的奖杯3以后的生物的统计数据将继续上升(以非线性方式),我认为没有简单的数学方法可以压缩代码,尽管我认为自己是效率低下的Python程序员,但我希望这样做可以在一些帮助下凝结下来。

Trophy1 = ["Compsognathus", "Coelophysis", "Troodon"]
Trophy2 = ["Edmontosaurus", "Iguanadon", "Parasaurolophus"]
Trophy3 = ["Deinonychus", "Utahraptor", "Novaraptor"]
Trophy4 = ["Protoceratops", "Avimimmus", "Gallimumus"]
Trophy5 = ["Allosaurus", "Carnotaurus", "Baryonyx"]
Trophy6 = ["Stegosarus", "Arrhinoceratops", "Triceratops"]
Trophy7 = ["Ankylosaurus", "Minmi", "Euplocephalus"]
Trophy8 = ["Abelisaurus", "Albertosaurus", "Gorgosarus", "Megalosaurus"]
Trophy9 = ["Apatasaurus", "Brachiosaurus", "Brontosaurus"]
Trophy10 = ["Tyrannosaurus Rex","Giganotosaurus","Spinosaurus"]
Trophy11 = ["Indominous Rex"]
if Trophy == 1:
    Name = random.choice(Trophy1)
    EHealth = random.randint(50,100)
    EAttack = random.randint(20,50)
    Resistance = random.randint(5,10)
    Weight = random.randint(100,150)
    Carnivore = 1
elif Trophy == 2:
    Name = random.choice(Trophy2)
    EHealth = random.randint(100,250)
    EAttack = random.randint(30,75)
    Resistance = random.randint(5,15)
    Weight = random.randint(500,1000)
    Carnivore = 1
elif Trophy == 3:
    Name = random.choice(Trophy3)
    EHealth = random.randint(100,200)
    EAttack = random.randint(50,100)
    Resistance = random.randint(5,10)
    Weight = random.randint(250,500)
    Carnivore = 2
elif Trophy == 4:
    Name = random.choice(Trophy4)
    EHealth = random.randint(50,100)
    EAttack = random.randint(20,50)
    Resistance = random.randint(5,10)
    Weight = random.randint(100,150)
    Carnivore = 1
elif Trophy == 5:
    Name = random.choice(Trophy5)
    EHealth = random.randint(50,100)
    EAttack = random.randint(20,50)
    Resistance = random.randint(5,10)
    Weight = random.randint(100,150)
    Carnivore = 2
elif Trophy == 6:
    Name = random.choice(Trophy6)
    EHealth = random.randint(50,100)
    EAttack = random.randint(20,50)
    Resistance = random.randint(5,10)
    Weight = random.randint(100,150)
    Carnivore = 1
elif Trophy == 7:
    Name = random.choice(Trophy7)
    EHealth = random.randint(50,100)
    EAttack = random.randint(20,50)
    Resistance = random.randint(5,10)
    Weight = random.randint(100,150)
    Carnivore = 1
elif Trophy == 8:
    Name = random.choice(Trophy8)
    EHealth = random.randint(50,100)
    EAttack = random.randint(20,50)
    Resistance = random.randint(5,10)
    Weight = random.randint(100,150)
    Carnivore = 2
elif Trophy == 9:
    Name = random.choice(Trophy9)
    EHealth = random.randint(50,100)
    EAttack = random.randint(20,50)
    Resistance = random.randint(5,10)
    Weight = random.randint(100,150)
    Carnivore = 1
elif Trophy == 10:
    Name = random.choice(Trophy10)
    EHealth = random.randint(50,100)
    EAttack = random.randint(20,50)
    Resistance = random.randint(5,10)
    Weight = random.randint(100,150)
    Carnivore = 3
elif Trophy == 11:
    Name = random.choice(Trophy11)
    EHealth = random.randint(50,100)
    EAttack = random.randint(20,50)
    Resistance = random.randint(5,10)
    Weight = random.randint(100,150)
    Carnivore = 4

1 个答案:

答案 0 :(得分:0)

将所有这些数据放入一个数组中,然后对其进行索引(不要忘记减去Trophy中的一个以获得适当的索引)。或者,您可以创建类似DinosaurDataDescriptor的类,然后将该对象放入数组中。