Reputation: 11
I am trying to switch background music when the pictures
hits bottom and back to the first one when it hits the top. When
I try to put it in the while #formula
then it just resets every time
it descends a little and that is not what i want.
Can you help me in figuring how to put this code:
pygame.mixer.music.load('song1')
pygame.mixer.music.play(-1, 0.0)
pygame.mixer.music.load('song2')
pygame.mixer.music.play(-1, 0.0)
inside the code below and make it switch as I explained it here? If I forgot to explain something that you need to know please let me know.
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)
Upvotes: 1
Views: 2006
Reputation: 45
pygame.mixer.music.stop()
pygame.mixer.music.unload()
pygame.mixer.music.load(os.path.abspath('put your music path here'))
pygame.mixer.music.play(loops=-1, start=0.0, fade_ms=750)
*loops = -1 if you want repeat mode on, loops = 0 to turn repeat mode off
Upvotes: 0