0

I'd like to create a function to make it easier for me to load and scale images. I have a lot of images in one folder which I use the same path to get to, and they all have the same names as the PNG files. How would I create a function where I could imput a list of file names, and have Python create variables with said names, and then scale them accordingly?

Here is the code I have right now:

Ocean = p.image.load("textures/tiles/Ocean.png")
Grassland = p.image.load("textures/tiles/Grassland.png")
Plains = p.image.load("textures/tiles/Plains.png")
FGrassland = p.image.load("textures/tiles/FGrassland.png")
FPlains = p.image.load("textures/tiles/FPlains.png")
HGrassland = p.image.load("textures/tiles/HGrassland.png")
HPlains = p.image.load("textures/tiles/HPlains.png")
MGrassland = p.image.load("textures/tiles/MGrassland.png")
MPlains =p.image.load("textures/tiles/MPlains.png")
Desert = p.image.load("textures/tiles/Desert.png")
HDesert = p.image.load("textures/tiles/HDesert.png")
Tundra = p.image.load("textures/tiles/Tundra.png")
FTundra = p.image.load("textures/tiles/FTundra.png")
HTundra = p.image.load("textures/tiles/HTundra.png")
MTundra = p.image.load("textures/tiles/MTundra.png")
Undiscovered = p.image.load("Undiscovered.png")
#########################################
Ocean = p.transform.scale(Ocean,(EFFECTIVE_SCALE,EFFECTIVE_SCALE))
Grassland = p.transform.scale(Grassland, (EFFECTIVE_SCALE, EFFECTIVE_SCALE))
FGrassland = p.transform.scale(FGrassland, (EFFECTIVE_SCALE, EFFECTIVE_SCALE))
HGrassland = p.transform.scale(HGrassland, (EFFECTIVE_SCALE, EFFECTIVE_SCALE))
MGrassland = p.transform.scale(MGrassland, (EFFECTIVE_SCALE, EFFECTIVE_SCALE))
Plains = p.transform.scale(Plains, (EFFECTIVE_SCALE, EFFECTIVE_SCALE))
Desert = p.transform.scale(Desert, (EFFECTIVE_SCALE, EFFECTIVE_SCALE))
Tundra = p.transform.scale(Tundra, (EFFECTIVE_SCALE, EFFECTIVE_SCALE ))
FTundra = p.transform.scale(FTundra, (EFFECTIVE_SCALE, EFFECTIVE_SCALE ))
Undiscovered = p.transform.scale(Undiscovered, (EFFECTIVE_SCALE, EFFECTIVE_SCALE))
HTundra = p.transform.scale(HTundra, (EFFECTIVE_SCALE, EFFECTIVE_SCALE))
MTundra = p.transform.scale(MTundra, (EFFECTIVE_SCALE, EFFECTIVE_SCALE))

I'd like to create something like this:

def LoadTextures(self,textureList):
    for t in range textureList:
        texturepath = str("textures/tiles/" + textureList[t] + ".png")
        (create a new variable with the name of the png) = p.image.load("texturepath")
        (use same variable name as the above line) = p.transform.scale(textureList[t],(EFFECTIVE_SCALE,EFFECTIVE_SCALE))

Anyone have any ideas how I could do this?

  • 2
    the amount of copy-pasting here is really worrying. You're right in thinking that there should be a better way. The trick is, whenever you need to create these many variables, you should be thinking of a datastructure instead "one level up". So, a dict for example. – Paritosh Singh Feb 20 '20 at 02:13
  • How would I code something like that? I've just never messed with them before, and I'm not sure how I would code one. – Adventurer32 Feb 20 '20 at 02:17
  • 1
    take a look at `os.listdir` as well – Paritosh Singh Feb 20 '20 at 02:28
  • @Adventurer32 _I’ve just never messed with them before_ Messed with what? – AMC Feb 20 '20 at 02:31
  • Datastructures, but then I looked up dictionaries and I realized I did actually remember them from one program I did forever ago in my comp sci class I could never imagine a use for at the time. – Adventurer32 Feb 20 '20 at 02:42

0 Answers0