Skip to content

Commit 58517b2

Browse files
committed
added sound support
1 parent 58deecf commit 58517b2

File tree

5 files changed

+24
-2
lines changed

5 files changed

+24
-2
lines changed

game.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import menu
1212
import globals_
1313
import status
14+
import sound
1415

1516

1617
class Game(state_manager.State):
@@ -19,6 +20,7 @@ def __init__(self) -> None:
1920
#pygame.font.init() # init fonts, do i have to do it here?
2021
self._cursor = cursor.Cursor() #my cursor :D
2122
self._go_manager = globals_.get_manager()
23+
self._sound = globals_.get_sound() # type: sound.Sound
2224
self._status = status.Status()
2325
self._player = player.Player(self._status) # The one who shoots code snippets
2426
self.mouse_pos = (0, 0)
@@ -65,6 +67,7 @@ def update(self, delta: int, fps: float) -> None:
6567

6668
if self._next_bug_count <= 0:
6769
self._go_manager.add_object(bugs.Bugs(self._status))
70+
self._sound.play(sound.Sounds.BUG, False)
6871
self._next_bug_count = random.randint(*consts.MSEC_BETWEEN_BUGS[min(len(consts.MSEC_BETWEEN_BUGS) - 1, self._status.level)])
6972

7073
if self._next_exception_count <= 0:
@@ -76,9 +79,10 @@ def update(self, delta: int, fps: float) -> None:
7679
self._status.update()
7780

7881
def leave(self, next_: state_manager.StateType) -> None:
79-
pass
82+
self._sound.stop(sound.Sounds.MUSIC)
8083

8184
def enter(self, prev_: state_manager.StateType) -> None:
85+
self._sound.play(sound.Sounds.MUSIC, True)
8286
if prev_ == menu.Menu:
8387
self._go_manager.clear_objects()
8488
self._status.reset()

globals_.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@ def get_manager():
77
if _go_manager is None:
88
_go_manager = go_manager.GoManager()
99
return _go_manager
10+
11+
_sound = None
12+
def get_sound():
13+
import sound
14+
global _sound
15+
if _sound is None:
16+
_sound = sound.Sound()
17+
return _sound

go_manager.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import bugs
77
import collision
88

9+
import globals_
10+
import sound
11+
912
import status
1013
import game_objects
1114
from typing import List
@@ -14,6 +17,7 @@ class GoManager(object):
1417
def __init__(self):
1518
super().__init__()
1619
self._go_list = [] # type: List[GameObjects]
20+
self._sound = globals_.get_sound() # type: sound.Sound
1721

1822
def add_object(self, game_object: game_objects.GameObject):
1923
self._go_list.append(game_object)
@@ -45,6 +49,7 @@ def update(self, delta: int, player_: player.Player, status_: status.Status):
4549
s.delete()
4650
b.delete()
4751
status_.inc_point()
52+
self._sound.play(sound.Sounds.KILL)
4853

4954
# takes only those go to the list which are go._delete_me == true
5055
self._go_list = [go for go in self._go_list if not go._delete_me]

player.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import collision
1010
import status
1111

12+
import sound
13+
import globals_
1214

1315
import random
1416

@@ -28,6 +30,7 @@ def __init__(self, status_: status.Status) -> None:
2830
animations.Surfaces.VIM,
2931
animations.Surfaces.VSCODE]))
3032
self._status = status_
33+
self._sound = globals_.get_sound()
3134
self._pos = (consts.SCREEN_W // 2, consts.SCREEN_H // 2)
3235

3336
def set_pos(self, pos: Tuple[int, int]) -> None:
@@ -44,6 +47,7 @@ def update(self, delta) -> None:
4447
self._progger.update(delta)
4548

4649
def fire(self) -> snippets.Snippet:
50+
self._sound.play(sound.Sounds.FIRE)
4751
return snippets.Snippet(color.POWDERBLUE, self._pos[0])
4852

4953
def try_catch(self, exceptie: exceptions.Excepties) -> None:
@@ -52,6 +56,7 @@ def try_catch(self, exceptie: exceptions.Excepties) -> None:
5256
if collision.collide(self._catcher.surface, cx, cy, exceptie.surface, *exceptie.pos):
5357
exceptie.delete()
5458
no_game_object.NoGameObject(animations.CatchException(), cx, cy)
59+
self._sound.play(sound.Sounds.CATCH)
5560

5661

5762
catcher = property(lambda s: s._catcher)

sound.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def __init__(self):
3232
_sounds = {e: pygame.mixer.Sound(e.get_filename()) for e in Sounds}
3333
m = _sounds.get(Sounds.MUSIC)
3434
if m is not None:
35-
m.set_volume(.25)
35+
m.set_volume(.35)
3636
self._sounds = _sounds
3737

3838
def play(self, sound_: Sounds, loop: bool = False):

0 commit comments

Comments
 (0)