forked from cis3296f23/Project-02-checkers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MusicClass.py
46 lines (38 loc) · 1.54 KB
/
MusicClass.py
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
"""
MusicClass.py
The MusicClass file is used to create a BackgroundMusic class that can be used to play background music.
"""
import pygame
import pygame.mixer
pygame.mixer.init()
class BackgroundMusic:
"""
The BackgroundMusic class is used to play background music, and consists of the functions play music, start music loop, and handle event.
"""
def __init__(self, tracks):
"""
The init function is used to initialize the BackgroundMusic class, and consists of the variables tracks, current_track, and SONG_END.
"""
self.tracks = tracks
self.current_track = 0
self.SONG_END = pygame.USEREVENT + 1
pygame.mixer.music.set_endevent(self.SONG_END)
def play_music(self):
"""
The play music function is used to play music, and consists of the functions load, set volume, play, and current track.
"""
pygame.mixer.music.load(self.tracks[self.current_track])
pygame.mixer.music.set_volume(0.1) # Adjust volume between 0.1 and 1.0
pygame.mixer.music.play()
self.current_track = (self.current_track + 1) % len(self.tracks)
def start_music_loop(self):
"""
The start music loop function is used to start the music loop, and consists of the function play music.
"""
self.play_music()
def handle_event(self, event):
"""
The handle event function is used to handle events, and consists of the function play music.
"""
if event.type == self.SONG_END:
self.play_music()