简体   繁体   English

PYTHON - 随机生成器

[英]PYTHON - Random Generator

I am building a random class generator for Call of Duty classes.我正在为使命召唤课程构建一个随机 class 生成器。 Hit 'generate', and it will generate a random class.点击“生成”,它将生成一个随机的 class。

I am running into an issue when it comes to kill streaks.当涉及到杀死条纹时,我遇到了一个问题。 Essentially, if you get 3 kills in a row, you can use your selected kill streak.本质上,如果您连续获得 3 次击杀,则可以使用您选择的连杀数。 There are, however, multiple kill streaks you can earn at 3 kills, 4 kills, 5 kills, etc.但是,您可以在 3 杀、4 杀、5 杀等情况下获得多次连杀。

So, if I just build a list of all the kill streaks and use the random import, it is likely it will generate a few of the same valued streaks;所以,如果我只是建立一个包含所有连续击杀的列表并使用随机导入,它很可能会生成一些具有相同价值的连续击杀; which you cannot use in the game.你不能在游戏中使用。 They must be separate values.它们必须是单独的值。

How can I counter this?我该如何应对? Thanks in advance: Here is the code I have thus far:在此先感谢:这是我到目前为止的代码:

#this portion will generate the primary weapon

import random

primaryGun_list = ['AK-47', 'AN-94', 'AS VAL', 'CR-56 AMAX', 'FAL',
                   'FR 5.56', 'Grau 5.56', 'Kilo 141', 'M13', 'M4A1',
                   'Oden', 'FN SCAR 17', 'RAM-7', 'AUG', 'P90', 'MP5',
                   'Uzi', 'PP19 Bizon', 'MP7', 'Striker 45', 'Fennec',
                   'ISO', 'Model 680', 'R9-0', '725', 'Origin 12', 'VLK Rogue',
                   'JAK-12', 'PKM', 'SA87', 'M91', 'MG34', 'Holger-26', 'Bruen Mk9',
                    'FiNN LMG', 'Dragunov', 'HDR', 'AX-50', 'Rytec AMR', 'EBR',
                   'MK2 Carbine', 'Kar98k', 'Crossbow', 'SKS']

print("Primary Weapon:", random.choice(primaryGun_list))



#this portion will generate the secondary weapon

import random

secondaryGun_list = ['PILA', 'Strela-P', 'JOKR', 'RPG-7', 'X16', '1911',
                     '.357', 'M19', '.50 GS', 'Renetti', 'Riot Shield',
                     'Combat Knife', 'Kali Sticks', 'Dual Kodachis']

print("Secondary Weapon:", random.choice(secondaryGun_list))

      

#this portion will generate 'Perk 1'

import random

perk1_list = ['Double Time', 'Kill Chain', 'Scavenger', 'E.O.D.',
              'Cold-Blooded', 'Quick Fix']

print("Perk 1:", random.choice(perk1_list))



#this portion will generate 'Perk 2'

import random

perk2_list = ['Restock', 'Hardline', 'High Alert', 'Ghost', 'Pointman', 'Overkill']

print("Perk 2:", random.choice(perk2_list))



#this portion will generate 'Perk 3'

import random

perk3_list = ['Tune Up', 'Amped', 'Shrapnel', 'Battle Hardened', 'Spotter', 'Tracker']

print("Perk 3:", random.choice(perk3_list))



#this portion will generate the Lethal

import random

lethal_list = ['Claymore', 'Frag Grenade', 'Molotov Cocktail', 'C4', 'Semtex',
               'Throwing Knife', 'Proximity Mine', 'Thermite', 'Flaming Throwing Knife',
               'EHV Throwing Knife', 'Phlebotomist']

print("Lethal:", random.choice(lethal_list))



#this portion will generate the Tactical

import random

tactical_list = ['Flash Grenade', 'Stun Grenade', 'Smoke Grenade', 'Snapshot Grenade',
                 'Heartbeat Sensor', 'Stim', 'Decoy Grenade', 'Gas Grenade']

print("Tactical:", random.choice(tactical_list))



#this portion will generate the Field Upgrade(s)

import random

fieldUpgrade_list = ['Munitions Box', 'Recon Drone', 'Dead Silence', 'Stopping Rounds',
                     'Trophy System', 'Deployable Cover', 'Tactical Insertion', 'EMP Drone',
                     'Weapon Drop']

print("Field Upgrades:", random.sample(fieldUpgrade_list, 2))



#this portion will generate the 3 Killstreaks

#import random

#killstreak_list = ['

Firstly, you only need to import random once at the start of the program.首先,您只需要在程序开始时随机导入一次。 Secondly it is much better to use subroutines and external files.txt files.其次,最好使用子程序和外部 files.txt 文件。 These files have to be in the same folder as the program and look like this:这些文件必须与程序位于同一文件夹中,如下所示:

AK-47
AN-94
etc.

This needs to be done for all of the parts of the list.这需要对列表的所有部分完成。 Create a new file for each list.为每个列表创建一个新文件。 This all put together makes your code much shorter and easier to understand.所有这些放在一起使您的代码更短且更易于理解。 The code is shown below:代码如下所示:

import random
from random import choice

#Makes the lists
def file_to_list(filename):
    thefile = open(filename,"r")
    rows = [line for line in thefile]
    thefile.close()
    for i,r in enumerate(rows):
        rows[i] = r.replace("\n","")
    return rows

#Using the subroutine
primary = file_to_list("primary.txt")
secondary = file_to_list("secondary.txt")
perk1 = file_to_list("perk1.txt")
perk2 = file_to_list("perk2.txt")
perk3 = file_to_list("perk3.txt")
lethal = file_to_list("lethal.txt")
tactical = file_to_list("tactical.txt")
field_upgrade = file_to_list("field.txt")
kill = file_to_list("killstreak.txt")

#Chooses the options for the class
p = choice(primary)
s = choice(secondary)
p1 = choice(perk1)
p2 = choice(perk2)
p3 = choice(perk3)
l = choice(lethal)
t = choice(tactical)
f = choice(field_upgrade)
for i in range(3):
    k = choice(kill)
    print("Killstreak:",k)

#Output
print("Primary Weapon:", p)
print("Secondary Weapon:", s)
print("Perk 1:", p1)
print("Perk 2:", p2)
print("Perk 3:", p3)
print("Lethal:", l)
print("Tactical:", t)
print("Field Upgrades:", f)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM