Python:使用代码编写代码-对于初学者

Python:使用代码编写代码-对于初学者,python,pygame,getattr,Python,Pygame,Getattr,我现在遇到的问题是在Pygame中加载一堆声音文件作为它们自己的对象。使用以下语法加载声音: sound1 = pygame.mixer.Sound('file.wav') 假设我有七个文件,我希望加载它们并命名为sound1-sound7。我不想单独加载它们。如果我不知道它有缺陷,我会尝试以下方法: for i in range(1, 8): new = 'sound' + str(i) new = pygame.mixer.Sound(str(new) + 'wav')

我现在遇到的问题是在Pygame中加载一堆声音文件作为它们自己的对象。使用以下语法加载声音:

sound1 = pygame.mixer.Sound('file.wav')
假设我有七个文件,我希望加载它们并命名为sound1-sound7。我不想单独加载它们。如果我不知道它有缺陷,我会尝试以下方法:

for i in range(1, 8):
    new = 'sound' + str(i)
    new = pygame.mixer.Sound(str(new) + 'wav')
我该如何让“new”成为自己的变量,而不是字符串?我读过关于getattr的文章,但它令人困惑。我真的很想知道如何使用函数和循环来动态创建代码,但到目前为止,我找不到任何对我这样的初学者有用的东西。以此为例,是否有人愿意用一种简单的方式来解释如何在代码内部创建代码,并将字符串转换为可用的变量/对象

谢谢大家!

sounds = [] # list
for i in range(1, 8):
    sounds.append(pygame.mixer.Sound('sound' + str(i) + 'wav'))

起初,似乎您使用的字典方法与列表方法相同,例如发音[1]发音[2]等等,但您也可以这样做:

sounds = {} # dictionary
for i in range(1, 8):
    sounds['sound' + str(i)] = pygame.mixer.Sound('sound' + str(i) + 'wav')

例如,现在声音[“sound1”]等都可以工作。

您可以使用数组来实现这一点:

sound = []
for i in range(1,8):
    sound.append (pygame.mixer.Sound("sound%d.wav" % i))
# Now use sound[0..6] to reference sound[1..7].wav

这将通过
sound8.wav
加载文件
sound1.wav
——如果文件名称不同,只需更改范围和/或字符串格式。

python中有两种循环for循环和while循环。for循环用于重复n次。while循环用于重复,直到发生某些事情。For循环对于游戏编程非常有用,因为它们经常处理游戏显示的帧。每帧通过一个循环运行一次。存储for循环的方式是使用列表。下面是一个您可以熟悉的基本循环示例:

he_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']

# this first kind of for-loop goes through a list
for number in the_count:
    print "This is count %d" % number

# same as above
for fruit in fruits:
    print "A fruit of type: %s" % fruit

# also we can go through mixed lists too
# notice we have to use %r since we don't know what's in it
for i in change:
    print "I got %r" % i

# we can also build lists, first start with an empty one
elements = []

# then use the range function to do 0 to 5 counts
for i in range(0, 6):
    print "Adding %d to the list." % i
    # append is a function that lists understand
    elements.append(i)

# now we can print them out too
for i in elements:
    print "Element was: %d" % i
您可以在此处了解有关python循环和游戏编程的更多信息:

您需要查看数组-然后您可以参考例如
声音[2]
仅供参考,字符串格式通常比字符串串联更好:
'sound'+str(i)==='sound%s'%i
我认为这是一个很好的初学者问题的好例子-虽然您很新,但您尝试了一些方法,做了一些研究,并解释了你在找什么。尽管如此,我还是建议您一直学习一些Python教程,这些教程将向您介绍列表和字典实际上,这里不需要字典,但在一个用例中它会非常好:
sounds{“ding”}=pygame.mixer.Sound(“ding.wav”)
-然后您可以使用声音名称来查找声音,类似于
的声音{“ding”}.play()
。对,如果你只是按数字对它进行索引,它基本上做了相同的事情,但是如果你更喜欢按其他东西(如声音的名称)进行索引,这是一个很大的改进
he_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']

# this first kind of for-loop goes through a list
for number in the_count:
    print "This is count %d" % number

# same as above
for fruit in fruits:
    print "A fruit of type: %s" % fruit

# also we can go through mixed lists too
# notice we have to use %r since we don't know what's in it
for i in change:
    print "I got %r" % i

# we can also build lists, first start with an empty one
elements = []

# then use the range function to do 0 to 5 counts
for i in range(0, 6):
    print "Adding %d to the list." % i
    # append is a function that lists understand
    elements.append(i)

# now we can print them out too
for i in elements:
    print "Element was: %d" % i