Jacob Moore
Jacob Moore

Reputation: 277

Trying to get music to play as soon as program starts

Where does my music section belong in this code to play upon starting? If i put it before the loop it only plays the music, if i put it during the loop the program will not run. I think what is happening is that the music doesnt have a chance to play because the loop just keeps restarting. Thanks

pygame.mixer.music.load("doommusic.mp3")
pygame.mixer.music.play()
time.sleep(1090)
pygame.mixer.music.stop()




#imports pygame library
import pygame
import sys
import time

pygame.init()

#Sceen height and width
display_width = 800
display_height = 600

#color variables
white = (255,255,255)

screen = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Operation Python')

screen.fill(white)

Fps = pygame.time.Clock()

man1x = 200
man1y = 200
man=100
man1=100

#main loop
while True:
screen.fill(white)

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

pygame.draw.circle(screen,(9, 44, 100),(man1x,man1y),25)
pygame.draw.circle(screen,(9, 44, 100),(man,man1),75)

pressed = pygame.key.get_pressed()
if pressed[pygame.K_LEFT]:
    man1x -= 1
if pressed[pygame.K_RIGHT]:
    man1x += 1
if pressed[pygame.K_UP]:
    man1y -= 1
if pressed[pygame.K_DOWN]:
    man1y += 1

pygame.display.flip()
Fps.tick(120)

Upvotes: 2

Views: 137

Answers (2)

PRMoureu
PRMoureu

Reputation: 13327

Try with this line, it will wait until the music ends

pygame.mixer.init()
pygame.mixer.music.load("doommusic.mp3")
pygame.mixer.music.play()  

#main loop
while True:
    [...]

Upvotes: 1

DinosauRuss
DinosauRuss

Reputation: 146

Immediately following the pygame.init() you need to add

pygame.mixer.init()
pygame.mixer.music.load("doommusic.mp3")
pygame.mixer.music.play()

Upvotes: 1

Related Questions