Reputation: 375
I want to create a string from 2 strings that I use in a function. It is the zz
that I use in the function geraeusche.mach_geraeusche(2000, "hupen")
but I can't create that. I get the following error message:
descriptor 'play' for 'Sound' objects doesn't apply to a 'str' object
My code:
class Geraeusche(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.startzeit = 0
self.zaehler = 0
self.martinshorn = pygame.mixer.Sound("Bilder/martinshorn.mp3")
self.hupen = pygame.mixer.Sound("Bilder/horn.wav")
def mach_geraeusche(self,warten,auswahl):
if self.zaehler < 10:
aktuelle_zeit = pygame.time.get_ticks()
if aktuelle_zeit - self.startzeit > warten:
self.startzeit = aktuelle_zeit
self.zaehler +=1
zz=str("self." + auswahl)
pygame.mixer.Sound.play(zz)
while True:
clock.tick(60)
geraeusche.mach_geraeusche(2000,"hupen")
Upvotes: 0
Views: 66
Reputation: 2086
I can see two issues here.
First, the way you are trying to access the class attribute will not work - zz will just be the string ‘self.hupen’ and will not actually point to that attribute. The correct way to do this is:
zz = getattr(self, auswahl)
as per Python: access class property from string.
Second, this doesn’t appear to be the correct way to initialise and play sounds in pygame. It should be:
my_sound = pygame.mixer.Sound(zz)
pygame.mixer.Sound.play(my_sound)
as per https://pythonprogramming.net/adding-sounds-music-pygame/
Upvotes: 1