如何使用 Python 通过 if/else 触发对不同音频通道的更改?

问题描述 投票:0回答:1

请原谅我,我对 Python(以及一般的编码)非常陌生,但老实说我很困惑。

本质上,我想做的是尝试在我正在开发的这个猜谜游戏中触发音乐的变化,当有人得到答案时,他们会得到嗯......rickrolled

问题是,每当我尝试取消暂停音乐或播放音乐时,一旦他们猜对了,它就不起作用。

我尝试过利用 pygame.mixer.music.play()、pygame.mixer.music.pause() 等......但没有成功。

这是代码...

# 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!')

python python-3.x pygame
1个回答
0
投票

稍微修改了你的代码。

# 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!")

说明

  • guess != secret_word
    时循环起作用。因此,如果
    guess
    secret_word
    相同,则循环退出。因此,当猜测匹配时,我们需要添加 rickroll 音频。你的逻辑是正确的,它会在循环结束后,也就是循环退出时执行的代码。

  • current_input
    接收您的下一次击键。如果您按
    Enter
    current_input
    的值为
    None
    ,这将帮助您退出此循环。否则,你会继续被打滚! :)

如果有任何不清楚的地方,请要求澄清。

© www.soinside.com 2019 - 2024. All rights reserved.