简体   繁体   English

我如何使用一个变量的值在python中调用另一个变量?

[英]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. 因此,我现在在pygame中工作,确保所有内容都停留在屏幕上正确位置的唯一方法是检查每个可能的位置,直到找到适合正在处理图片的位置为止。 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. 因为pygame每秒以行数运行,所以我的代码需要尽可能短。

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. 我想知道的是,我是否可以接受我的dessert_rand变量,并根据其值直接在语句中使用它来调用某个变量。 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. 这将使我可以将目前需要的12条线缩短到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列表:

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

Then you can just replace the entire if - elseif chain by: 然后,您可以将整个if - elseif链替换为:

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

Note the - 1 : lists in Python are 0-based. 请注意- 1 :Python中的列表基于0。 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.) (顺便说一句, 甜点vs.沙漠 ,这告诉我你的意思是后者。)

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: 如果您有七个图像,并且想要将它们随机分配到七个不同的屏幕位置,请将位置放入列表中的元组,然后使用random.shuffle将它们随机化:

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'): 或更简洁(或更“ Pythonic”):

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

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

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

相关问题 如何使用Python defaultdict将一个值除以另一个? - How can I use a Python defaultdict to divide one value by another? 在python类中,如何在另一个函数中使用一个函数定义的变量? - In python class, how can I use the variable defined by one function in another function? 如何在 python 中使用一个变量从一个 class 到另一个变量? - how do I use a variable from one class into another in python? 如何在不使用python词典的情况下以另一个名称使用变量的值? - How can I use the value of a variable in the name of another without using a dictionary in python? 如何在另一个python文件中的方法中使用变量的值? - How can I use the value of a variable in a method inside another python file? 如何修改一个文件中的变量并在另一个文件中使用它? - How can I modify a variable in one file and use it in another file? 如何使用变量值调用数据框? - How can I use a variable value to call a dataframe? 如何在 Python 中将变量从一个生成器传递到另一个生成器? - How can I pass a variable from one generator to another in Python? 如何将变量从一个 function 导入 Python 中的另一个变量? - How can I import a variable from one function to another in Python? 如何将变量从一个 Python 脚本传递到另一个? - How can I pass a variable from one Python Script to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM