Skip to content

Commit e521f61

Browse files
authored
Merge pull request #64 from cis3296f23/FixMusic
Fix music
2 parents 3ebb8d4 + 2adce32 commit e521f61

File tree

6 files changed

+182
-2
lines changed

6 files changed

+182
-2
lines changed

MusicClass.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
MusicClass.py
3+
The MusicClass file is used to create a BackgroundMusic class that can be used to play background music.
4+
"""
5+
6+
7+
import pygame
8+
import pygame.mixer
9+
10+
pygame.mixer.init()
11+
class BackgroundMusic:
12+
"""
13+
The BackgroundMusic class is used to play background music, and consists of the functions play music, start music loop, and handle event.
14+
"""
15+
def __init__(self, tracks):
16+
"""
17+
The init function is used to initialize the BackgroundMusic class, and consists of the variables tracks, current_track, and SONG_END.
18+
"""
19+
self.tracks = tracks
20+
self.current_track = 0
21+
self.SONG_END = pygame.USEREVENT + 1
22+
23+
pygame.mixer.music.set_endevent(self.SONG_END)
24+
25+
def play_music(self):
26+
"""
27+
The play music function is used to play music, and consists of the functions load, set volume, play, and current track.
28+
"""
29+
pygame.mixer.music.load(self.tracks[self.current_track])
30+
pygame.mixer.music.set_volume(0.1) # Adjust volume between 0.1 and 1.0
31+
pygame.mixer.music.play()
32+
self.current_track = (self.current_track + 1) % len(self.tracks)
33+
34+
def start_music_loop(self):
35+
"""
36+
The start music loop function is used to start the music loop, and consists of the function play music.
37+
"""
38+
self.play_music()
39+
40+
def handle_event(self, event):
41+
"""
42+
The handle event function is used to handle events, and consists of the function play music.
43+
"""
44+
if event.type == self.SONG_END:
45+
self.play_music()
46+

SecondMenu.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
from constants import RED, SQUARE_SIZE, WHITE
1111
from game import Game
1212
from computer import minimax
13+
from MusicClass import BackgroundMusic
14+
from SharedObjects import background_music
15+
1316

1417
Width, Height = 1000, 700
1518
background_image = pygame.image.load("checkers.jpg")
@@ -37,11 +40,18 @@ class SecondMenu:
3740
The SecondMenu class consists of a String color, which represents the color of the board chosen by the user.
3841
The class also has three functions, start_game_menu, start_game_vs_player, and start_game_vs_computer.
3942
"""
43+
44+
def __init__(self, track):
45+
self.selected_music_track = track
46+
self.background_music = BackgroundMusic([track])
47+
4048
color = RED
4149
def start_game_menu(self):
4250
"""
4351
The start game menu function displays the second menu of the game, which allows the user to choose between playing against another player or against the computer.
4452
"""
53+
54+
4555
global player1_name, player2_name
4656
start_game_screen = pygame.display.set_mode([Width, Height])
4757

@@ -174,6 +184,8 @@ def start_game_menu(self):
174184
score_manager.save_scores()
175185
return
176186
# score_manager.save_scores() # now inside elif so scores are updated before returning to main
187+
elif event.type == self.background_music.SONG_END:
188+
self.background_music.handle_event(event)
177189

178190
def start_game_vs_player(self, screen):
179191
"""
@@ -216,6 +228,9 @@ def start_game_vs_player(self, screen):
216228
pos = pygame.mouse.get_pos()
217229
row, col = get_row_col_from_mouse(pos)
218230
game.select(row, col)
231+
# Check for background music event
232+
if event.type == background_music.SONG_END:
233+
background_music.handle_event(event)
219234

220235
game.update()
221236

@@ -260,5 +275,8 @@ def start_game_vs_computer(self, screen):
260275
pos = pygame.mouse.get_pos()
261276
row, col = get_row_col_from_mouse(pos)
262277
game.select(row, col)
278+
279+
if event.type == background_music.SONG_END:
280+
background_music.handle_event(event)
263281

264282
game.update()

SharedObjects.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
SharedObjects.py
3+
The SharedObjects file is used to create a shared instance of the BackgroundMusic class.
4+
"""
5+
6+
from MusicClass import BackgroundMusic
7+
import pygame
8+
9+
# Initialize Pygame
10+
pygame.init()
11+
12+
# Initialize the mixer
13+
pygame.mixer.init()
14+
15+
# Initial tracks
16+
initial_tracks = ["music/Track1.mp3", "music/Track2.mp3", "music/Track3.mp3", "music/Track4.mp3", "music/Track5.mp3", "music/Track6.mp3", "music/Track7.mp3", "music/Track8.mp3"]
17+
18+
# Create a shared instance of BackgroundMusic with the initial tracks
19+
background_music = BackgroundMusic(initial_tracks)
20+
background_music.start_music_loop()

html_documentation/MusicClass.html

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Python: module MusicClass</title>
6+
</head><body>
7+
8+
<table class="heading">
9+
<tr class="heading-text decor">
10+
<td class="title">&nbsp;<br><strong class="title">MusicClass</strong></td>
11+
<td class="extra"><a href=".">index</a><br><a href="file:c%3A%5Cusers%5Cjtsta%5Conedrive%5Cdesktop%5Ccode%5Csoftware%20design%5Cgithub%20basics%20assignment%5Cproject-02-checkers%5Cmusicclass.py">c:\users\jtsta\onedrive\desktop\code\software design\github basics assignment\project-02-checkers\musicclass.py</a></td></tr></table>
12+
<p><span class="code">MusicClass.py<br>
13+
The&nbsp;MusicClass&nbsp;file&nbsp;is&nbsp;used&nbsp;to&nbsp;create&nbsp;a&nbsp;<a href="#BackgroundMusic">BackgroundMusic</a>&nbsp;class&nbsp;that&nbsp;can&nbsp;be&nbsp;used&nbsp;to&nbsp;play&nbsp;background&nbsp;music.</span></p>
14+
<p>
15+
<table class="section">
16+
<tr class="decor pkg-content-decor heading-text">
17+
<td class="section-title" colspan=3>&nbsp;<br><strong class="bigsection">Modules</strong></td></tr>
18+
19+
<tr><td class="decor pkg-content-decor"><span class="code">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></td><td>&nbsp;</td>
20+
<td class="singlecolumn"><table><tr><td class="multicolumn"><a href="pygame.html">pygame</a><br>
21+
</td><td class="multicolumn"></td><td class="multicolumn"></td><td class="multicolumn"></td></tr></table></td></tr></table><p>
22+
<table class="section">
23+
<tr class="decor index-decor heading-text">
24+
<td class="section-title" colspan=3>&nbsp;<br><strong class="bigsection">Classes</strong></td></tr>
25+
26+
<tr><td class="decor index-decor"><span class="code">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></td><td>&nbsp;</td>
27+
<td class="singlecolumn"><dl>
28+
<dt class="heading-text"><a href="builtins.html#object">builtins.object</a>
29+
</dt><dd>
30+
<dl>
31+
<dt class="heading-text"><a href="MusicClass.html#BackgroundMusic">BackgroundMusic</a>
32+
</dt></dl>
33+
</dd>
34+
</dl>
35+
<p>
36+
<table class="section">
37+
<tr class="decor title-decor heading-text">
38+
<td class="section-title" colspan=3>&nbsp;<br><a name="BackgroundMusic">class <strong>BackgroundMusic</strong></a>(<a href="builtins.html#object">builtins.object</a>)</td></tr>
39+
40+
<tr><td class="decor title-decor" rowspan=2><span class="code">&nbsp;&nbsp;&nbsp;</span></td>
41+
<td class="decor title-decor" colspan=2><span class="code"><a href="#BackgroundMusic">BackgroundMusic</a>(tracks)<br>
42+
&nbsp;<br>
43+
The&nbsp;<a href="#BackgroundMusic">BackgroundMusic</a>&nbsp;class&nbsp;is&nbsp;used&nbsp;to&nbsp;play&nbsp;background&nbsp;music,&nbsp;and&nbsp;consists&nbsp;of&nbsp;the&nbsp;functions&nbsp;play&nbsp;music,&nbsp;start&nbsp;music&nbsp;loop,&nbsp;and&nbsp;handle&nbsp;event.<br>&nbsp;</span></td></tr>
44+
<tr><td>&nbsp;</td>
45+
<td class="singlecolumn">Methods defined here:<br>
46+
<dl><dt><a name="BackgroundMusic-__init__"><strong>__init__</strong></a>(self, tracks)</dt><dd><span class="code">The&nbsp;init&nbsp;function&nbsp;is&nbsp;used&nbsp;to&nbsp;initialize&nbsp;the&nbsp;<a href="#BackgroundMusic">BackgroundMusic</a>&nbsp;class,&nbsp;and&nbsp;consists&nbsp;of&nbsp;the&nbsp;variables&nbsp;tracks,&nbsp;current_track,&nbsp;and&nbsp;SONG_END.</span></dd></dl>
47+
48+
<dl><dt><a name="BackgroundMusic-handle_event"><strong>handle_event</strong></a>(self, event)</dt><dd><span class="code">The&nbsp;handle&nbsp;event&nbsp;function&nbsp;is&nbsp;used&nbsp;to&nbsp;handle&nbsp;events,&nbsp;and&nbsp;consists&nbsp;of&nbsp;the&nbsp;function&nbsp;play&nbsp;music.</span></dd></dl>
49+
50+
<dl><dt><a name="BackgroundMusic-play_music"><strong>play_music</strong></a>(self)</dt><dd><span class="code">The&nbsp;play&nbsp;music&nbsp;function&nbsp;is&nbsp;used&nbsp;to&nbsp;play&nbsp;music,&nbsp;and&nbsp;consists&nbsp;of&nbsp;the&nbsp;functions&nbsp;load,&nbsp;set&nbsp;volume,&nbsp;play,&nbsp;and&nbsp;current&nbsp;track.</span></dd></dl>
51+
52+
<dl><dt><a name="BackgroundMusic-start_music_loop"><strong>start_music_loop</strong></a>(self)</dt><dd><span class="code">The&nbsp;start&nbsp;music&nbsp;loop&nbsp;function&nbsp;is&nbsp;used&nbsp;to&nbsp;start&nbsp;the&nbsp;music&nbsp;loop,&nbsp;and&nbsp;consists&nbsp;of&nbsp;the&nbsp;function&nbsp;play&nbsp;music.</span></dd></dl>
53+
54+
<hr>
55+
Data descriptors defined here:<br>
56+
<dl><dt><strong>__dict__</strong></dt>
57+
<dd><span class="code">dictionary&nbsp;for&nbsp;instance&nbsp;variables</span></dd>
58+
</dl>
59+
<dl><dt><strong>__weakref__</strong></dt>
60+
<dd><span class="code">list&nbsp;of&nbsp;weak&nbsp;references&nbsp;to&nbsp;the&nbsp;object</span></dd>
61+
</dl>
62+
</td></tr></table></td></tr></table>
63+
</body></html>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Python: module SharedObjects</title>
6+
</head><body>
7+
8+
<table class="heading">
9+
<tr class="heading-text decor">
10+
<td class="title">&nbsp;<br><strong class="title">SharedObjects</strong></td>
11+
<td class="extra"><a href=".">index</a><br><a href="file:c%3A%5Cusers%5Cjtsta%5Conedrive%5Cdesktop%5Ccode%5Csoftware%20design%5Cgithub%20basics%20assignment%5Cproject-02-checkers%5Csharedobjects.py">c:\users\jtsta\onedrive\desktop\code\software design\github basics assignment\project-02-checkers\sharedobjects.py</a></td></tr></table>
12+
<p><span class="code">SharedObjects.py<br>
13+
The&nbsp;SharedObjects&nbsp;file&nbsp;is&nbsp;used&nbsp;to&nbsp;create&nbsp;a&nbsp;shared&nbsp;instance&nbsp;of&nbsp;the&nbsp;BackgroundMusic&nbsp;class.</span></p>
14+
<p>
15+
<table class="section">
16+
<tr class="decor pkg-content-decor heading-text">
17+
<td class="section-title" colspan=3>&nbsp;<br><strong class="bigsection">Modules</strong></td></tr>
18+
19+
<tr><td class="decor pkg-content-decor"><span class="code">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></td><td>&nbsp;</td>
20+
<td class="singlecolumn"><table><tr><td class="multicolumn"><a href="pygame.html">pygame</a><br>
21+
</td><td class="multicolumn"></td><td class="multicolumn"></td><td class="multicolumn"></td></tr></table></td></tr></table><p>
22+
<table class="section">
23+
<tr class="decor data-decor heading-text">
24+
<td class="section-title" colspan=3>&nbsp;<br><strong class="bigsection">Data</strong></td></tr>
25+
26+
<tr><td class="decor data-decor"><span class="code">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></td><td>&nbsp;</td>
27+
<td class="singlecolumn"><strong>background_music</strong> = &lt;MusicClass.BackgroundMusic object&gt;<br>
28+
<strong>initial_tracks</strong> = ['music/Track1.mp3', 'music/Track2.mp3', 'music/Track3.mp3', 'music/Track4.mp3', 'music/Track5.mp3', 'music/Track6.mp3', 'music/Track7.mp3', 'music/Track8.mp3']</td></tr></table>
29+
</body></html>

main.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from SecondMenu import SecondMenu
88
from constants import BLUE, YELLOW, RED, GREEN
99
from ScoreManager import ScoreManager
10+
from SecondMenu import SecondMenu
11+
1012

1113
pygame.init()
1214
pygame.mixer.init() # initialize pygame mixer for music
@@ -21,6 +23,9 @@
2123
tracks = ["music/Track1.mp3", "music/Track2.mp3", "music/Track3.mp3", "music/Track4.mp3", "music/Track5.mp3", "music/Track6.mp3", "music/Track7.mp3", "music/Track8.mp3"] # can add more or delete tracks if we do not like them
2224
current_track = 0
2325
SONG_END = pygame.USEREVENT + 1
26+
second_menu = SecondMenu(tracks)
27+
28+
2429
def music_loop():
2530
"""
2631
The music loop function loops through the music tracks in the tracks list.
@@ -57,8 +62,7 @@ def music_loop():
5762
credits_text2 = credits_font.render(credits2, True, (255, 255, 255))
5863
credits_rect2 = credits_text2.get_rect(center=(Width // 2, 670))
5964

60-
second_menu_instance = SecondMenu()
61-
65+
second_menu_instance = SecondMenu(tracks)
6266
def main():
6367
"""
6468
The main function is the main menu of the game. It displays the title, message, and credits, and holds user interaction with buttons.

0 commit comments

Comments
 (0)