[英]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.