简体   繁体   中英

PYTHON - Random Generator

I am building a random class generator for Call of Duty classes. Hit 'generate', and it will generate a random 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. There are, however, multiple kill streaks you can earn at 3 kills, 4 kills, 5 kills, etc.

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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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