简体   繁体   中英

How can i use the value of one variable to call another in python?

So i'm working in pygame and right now, the only way to ensure that everything stays in the right spot on the screen is to check every possible location until it finds the right one for the picture im working on. Then it does the same for the next picture until everything is loaded.

Because pygame runs in lines per second, my code needs to be as short as possible.

What im wondering is if i could take my dessert_rand variable, and use it directly in a statement to call a certain variable, based on it's value. For example:

screen.blit(wood, (spot_(wood_rand)x, spot_(wood_rand)y).

I know that formatting isn't correct, but that's the general idea of what i want to do. It would allow me to shorten what is currently taking 12 lines down to 1.

    wood_rand = randint(1,6)

    spot_1x = 0
    spot_1y = 200

    spot_2x = 100
    spot_2y = 350

    spot_3x = 300
    spot_3y = 350

    spot_4x = 400
    spot_4y = 200

    spot_5x = 300
    spot_5y = 50

    spot_6x = 100
    spot_6y = 50

    spot_7x = 200
    spot_7y = 200

    #Wondering if there's a way to make this all shorter...

    #Somthing like this would work.
    #screen.blit(dessert, (spot_(dessert_rand)x, spot_(dessert_rand)y)

    if wood_rand == 1:
        screen.blit(wood, (spot_1x, spot_1y))
    elif wood_rand == 2:
        screen.blit(wood, (spot_2x, spot_2y))
    elif wood_rand == 3:
        screen.blit(wood, (spot_3x, spot_3y))
    elif wood_rand == 4:
        screen.blit(wood, (spot_4x, spot_4y))
    elif wood_rand == 5:
        screen.blit(wood, (spot_5x, spot_5y))
    elif wood_rand == 6:
        screen.blit(wood, (spot_6x, spot_6y))

Do you know about lists? Looks like you could have a spot list containing tuples:

spot = [(0, 200), (100, 350), ...]

Then you can just replace the entire if - elseif chain by:

screen.blit(wood, spot[wood_rand - 1])

Note the - 1 : lists in Python are 0-based. Best to take that into account when randomizing:

wood_rand = randint(0,5)
screen.blit(wood, spot[wood_rand])

(By the way, dessert vs. desert , something tells me you mean the latter.)

If you have seven images and you want to randomly assign them to seven different screen locations, put your locations into tuples in a list then use random.shuffle to randomise them:

import random
spots = [(0, 200), (100, 350), (300, 350), (400, 200), (300, 50), (100, 50), (200, 200)]
spots = random.shuffle(spots)

Then you can simply place the images at their locations one after another:

screen.blit(wood, spots[0]) 
screen.blit(brick, spots[1])
screen.blit(wheat, spots[2])

etc.

Or more concisely (and more 'Pythonic'):

images = [wood, brick, wheat, image4, image5, image6, image7]

for i, image in enumerate(images):
    screen.blit(image, spots[i])

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