-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainMusic.py
More file actions
58 lines (43 loc) · 1.51 KB
/
mainMusic.py
File metadata and controls
58 lines (43 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os, sys
# Ensure Python can import your sibling .py files
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from mainSetting import *
import pygame
pygame.init()
pygame.mixer.init()
MUSIC_ENDED = pygame.USEREVENT
pygame.mixer.music.set_endevent(MUSIC_ENDED)
pygame.display.set_mode((width,height))
songs=[]
song_index=0
# Define base directory and sound path relative to this file
BASE_DIR = os.path.dirname(__file__)
SOUND_PATH = os.path.join(BASE_DIR, MUSIC_PATH)
def load_music(path):
global songs
for filename in os.listdir(path):
if filename.endswith('.xoht'):
songs.append(os.path.join(path, filename))
return songs
def runP():
pygame.mixer.init()
# load_music should accept a directory path
songs = load_music(path=SOUND_PATH)
global song_index # The current song to load
pygame.mixer.music.load(songs[song_index])
pygame.mixer.music.play()
song_index += 1
def whi(event):
if event.type == MUSIC_ENDED:
global song_index,songs
song_index = (song_index + 1) % len(songs) # Go to the next song (or first if at last).
pygame.mixer.music.load(songs[song_index])
pygame.mixer.music.play()
pygame.display.update()
def bgmusic():
pygame.mixer.music.load(os.path.join(SOUND_PATH, 'music.mp3'))
pygame.mixer.music.play()
# Play an alternate background music track
def bgmus():
pygame.mixer.music.load(os.path.join(SOUND_PATH, 'coolbeansbgmusic.mp3'))
pygame.mixer.music.play()