需要帮助切换我的pygame代码上的音乐背景

时间:2015-04-16 14:11:33

标签: python audio pygame

我正在尝试在拍照时切换背景音乐 当它击中顶部时击中底部​​并返回第一个。什么时候 我试着把它放在while #formula然后它每次都重置 它下降了一点,这不是我想要的。 你能帮我弄清楚如何编写这段代码:

pygame.mixer.music.load('song1')
pygame.mixer.music.play(-1, 0.0)

pygame.mixer.music.load('song2')
pygame.mixer.music.play(-1, 0.0)

在下面的代码中,并按照我在这里解释的那样切换它? 如果我忘了解释你需要知道的事情 请告诉我。

import pygame, sys
from pygame.locals import *

pygame.init()

FPS = 200
fpsClock = pygame.time.Clock()

DISPLAYSURF = pygame.display.set_mode((400, 400), 0, 0)
pygame.display.set_caption('Verkefni 21')

WHITE = (255, 255, 255)
clownx = 10
clowny = 10
direction = 'right'
stefna = 'niður'

while True:
DISPLAYSURF.fill(WHITE)
if stefna == 'niður':
    if direction == 'right':
        clownImg = pygame.image.load('sarah.jpg')
        clownx += 5
        if clownx == 320:
            clowny += 10
            direction = 'left'
            clownImg = pygame.image.load('john.jpg')
        if clowny == 300:
            stefna = 'up'
            soundObj = pygame.mixer.Sound('bomb.wav')
            soundObj.play()
            import time
            time.sleep(2) # wait and let the sound play for 1 second
            soundObj.stop()

    elif direction == 'left':
        clownImg = pygame.image.load('john.jpg')
        clownx -= 5
        if clownx == 10:
            clowny += 10
            direction = 'right'
            clownImg = pygame.image.load('sarah.jpg')
        if clowny == 300:
            stefna = 'up'
            soundObj = pygame.mixer.Sound('bomb.wav')
            soundObj.play()
            import time
            time.sleep(2) # wait and let the sound play for 1 second
            soundObj.stop()
elif stefna == 'up':
    if direction == 'right':
        clownImg = pygame.image.load('arnold.jpg')
        clownx += 5
        if clownx == 320:
            clowny -= 10
            direction = 'left'
            clownImg = pygame.image.load('terminator.jpg')
        if clowny == 0:
            stefna = 'niður'
            soundObj = pygame.mixer.Sound('bomb.wav')
            soundObj.play()
            import time
            time.sleep(2) # wait and let the sound play for 1 second
            soundObj.stop()
    elif direction == 'left':
        clownImg = pygame.image.load('terminator.jpg')
        clownx -= 5
        if clownx == 0:
            clowny -=10
            direction = 'right'
            clownImg = pygame.image.load('arnold.jpg')
        if clowny == 0:
            stefna = 'niður'
            soundObj = pygame.mixer.Sound('bomb.wav')
            soundObj.play()
            import time
            time.sleep(2) # wait and let the sound play for 1 second
            soundObj.stop()

DISPLAYSURF.blit(clownImg, (clownx, clowny))

for event in pygame.event.get():
    if event.type == QUIT:
        pygame.quit()
        sys.exit()

pygame.display.update()
fpsClock.tick(FPS)

1 个答案:

答案 0 :(得分:0)

切换歌曲时放置pygame.mixer.music.stop()