Python Forum

Full Version: Pygame, how to run an if statement only once, help!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey guys.

I have been struggling with this problem for some time now, so i have to ask for help.
I got this game i am developing. You play as a spaceship, where your mission is to kill aliens, you get 1 point/score at each alien kill. I want to make a sound when you got a score of 10 only once. I got the score function working good and it is counting + 1 every time you kill an alien, but when the sound starts at 10 points it is playing that sound continually if i still got the score of 10. When i get 11 points it stops. I have tried with the pygame.mixer.stop after the yeah.play(), but then it isn't playing at all. Any suggestions?

My code look like this:

if score_value == 10:
yeah = pygame.mixer.Sound("PointsSound.wav")
yeah.set_volume(0.1)
yeah.play()
said_yeah = False
if score_value == 10 and said_yeah == False:
   yeah = pygame.mixer.Sound("PointsSound.wav")
   yeah.set_volume(0.1)
   yeah.play()
   said_yeah = True
Thank you for the reply Michael. I have tried with this too, it is still the same :/
said_yeah = False  #<------- put before game loop starts.

if score_value == 10 and said_yeah == False:
   yeah = pygame.mixer.Sound("PointsSound.wav")
   yeah.set_volume(0.1)
   yeah.play()
   said_yeah = True
Ok, sorry. The said_yeah = False needs to be before the game loop starts or it will keep resetting to False on each frame. Should have stated that.
we need to see more of your code to fix it
ahh ofc, it has to be out of the game loop, stupid me! :P
It works now.

Thank you Michael! :D