the sound plays right now for eternity and I only want it to play once when the game ends. I've looked it up everywhere but I really cannot find any good and helpful solution to this problem.
I use pygame zero. Here is my code for my game:
import pgzrun
from random import randint
'''
music
music = pygame.mixer.music.load('music.mp3')
pygame.mixer.music.play(-1)
'''
#mixer
music.play("music")
music.set_volume(0.3)
sounds.effect1.set_volume(0.2)
sounds.effect2.set_volume(0.3)
BREDD = 400
HOJD = 400
summa = 0
spelet_slut = False
rav = Actor("fox")
rav.pos = 100,100
mynt = Actor("coin")
mynt.pos = 200,200
apple = Actor("apple")
apple.pos = 300,300
def draw():
screen.fill("blue")
rav.draw()
mynt.draw()
apple.draw()
screen.draw.text("Summa: " + str(summa), color="black", topleft=(10,10))
if spelet_slut:
music.stop()
screen.fill("red")
screen.draw.text("Slutsumma: " + str(summa), topleft=(10,10), fontsize=60)
sounds.lose.play()
def placera_mynt():
mynt.x = randint(20, (BREDD - 20))
mynt.y = randint(20, (HOJD - 20))
def placera_apple():
apple.x = randint(20, (BREDD - 20))
apple.y = randint(20, (HOJD - 20))
def tiden_slut():
global spelet_slut
spelet_slut = True
def update():
global summa
if keyboard.left:
rav.x = rav.x - 4
elif keyboard.right:
rav.x = rav.x + 4
elif keyboard.up:
rav.y = rav.y - 4
elif keyboard.down:
rav.y = rav.y + 4
insamlade_mynt = rav.colliderect(mynt)
insamlade_apple = rav.colliderect(apple)
if insamlade_mynt:
summa = summa + 10
sounds.effect1.play()
placera_mynt()
elif insamlade_apple:
summa = summa + 10
sounds.effect2.play()
placera_apple()
clock.schedule(tiden_slut, 5)
placera_mynt()
placera_apple()
pgzrun.go()