Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I trigger a change to a different audio channel via if/else using Python?

Forgive me here, I’m very new to Python (and coding in general) but I’m honestly stumped.

Essentially what I’m trying to do is I’m attempting to trigger a change in music on this guessing game I’m working on, when someone gets the answer they get um… rickrolled.

The problem is that whenever I try to unpause the music or play the music once they guess correctly it won’t work.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’ve tried utilizing pygame.mixer.music.play(), pygame.mixer.music.pause(), etc… to no avail.

This is the the code…

# Adding rickroll Audio via pygame

import pygame

pygame.init()
main_channel = pygame.mixer.Channel(0)
pause_channel = pygame.mixer.Channel(1)
main_channel.play(pygame.mixer.Sound('quiztime.ogg'), loops=-1, fade_ms=5000)
pause_channel.play(pygame.mixer.Sound('rickroll.ogg'), loops=-1, fade_ms=5000)
pause_channel.pause()

# Secret Word
secret_word = 'rickroll'

# Created a 'guess' variable using the input statement

guess = input('Can you guess the secret word?: ')

# Added a guess count to implement hint system

guess_count = 0

# Condition using the While keyword

while guess != secret_word:
    guess_count += 1
    print('Almost... Try Again! ')
    guess = input('New Guess here plz: ')
    if guess_count == 3:
        print('Tis an old meme good friend...')
        guess = input('New Guess here plz: ')
    elif guess_count == 5:
        print('Think of a ginger man with soul~')
        guess = input('New Guess here plz: ')
    elif guess_count == 10:
        print("I wouldn't give you up ;)")
        guess = input('New Guess here plz: ')
    else:
        main_channel.pause()
        pause_channel.unpause()
        print("We're no strangers to loooooove~")
        # If done correctly should play rickroll upon correct guess.

print('Press ENTER to exit!')

>Solution :

Modified your code a bit.

# Adding rickroll Audio via pygame

import pygame

pygame.init()
main_channel = pygame.mixer.Channel(0)
pause_channel = pygame.mixer.Channel(1)
main_channel.play(pygame.mixer.Sound("quiztime.ogg"), loops=-1, fade_ms=5000)
pause_channel.play(pygame.mixer.Sound("rickroll.ogg"), loops=-1, fade_ms=5000)
pause_channel.pause()

# Secret Word
secret_word = "rickroll"

# Created a 'guess' variable using the input statement

guess = input("Can you guess the secret word?: ")

# Added a guess count to implement hint system

guess_count = 0

# Condition using the While keyword

while guess != secret_word:
    guess_count += 1
    print("Almost... Try Again! ")
    guess = input("New Guess here plz: ")
    if guess_count == 3:
        print("Tis an old meme good friend...")
        guess = input("New Guess here plz: ")
    elif guess_count == 5:
        print("Think of a ginger man with soul~")
        guess = input("New Guess here plz: ")
    elif guess_count == 10:
        print("I wouldn't give you up ;)")
        guess = input("New Guess here plz: ")

main_channel.pause()
pause_channel.unpause()
print("We're no strangers to loooooove~")

# If press enter exit, else
current_input = input("Press ENTER to exit!")
while current_input:
    current_input = input("Press ENTER to exit!")

Explanation

  • When guess != secret_word the loop works. So, if guess becomes the same as secret_word, the loop exits. Thus, we need to add the rickroll audio when guess matches. Your logic is correct, it will be just after the loop ends, which is the code that will be executed when loop exits.

  • current_input takes in your next keystroke. If you press Enter, the value of current_input is None, which will help you exit this loop. Otherwise, you will continue getting rickrolled! 🙂

Do ask for any clarifications in case anything is unclear.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading