Skip to content

Commit

Permalink
preload the sounds in the main menu
Browse files Browse the repository at this point in the history
  • Loading branch information
LupaDevStudio committed Oct 26, 2023
1 parent bdf4ec8 commit 5c33eff
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

### Python imports ###

import os
import platform
os_name = platform.system()
if os_name == "Windows":
import os
os.environ['KIVY_TEXT'] = 'pil'

### Kivy imports ###
Expand Down
23 changes: 19 additions & 4 deletions screens/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,25 @@
from tools.path import (
PATH_TEXT_FONT,
PATH_IMAGES,
PATH_SCREENS
PATH_MUSICS,
PATH_SOUNDS
)
from tools import (
music_mixer,
sound_mixer,
game,
USER_DATA
)
from tools.kivy_tools import (
ImprovedScreen
)
from tools.game_tools import (
load_sounds
)
from tools.constants import (
MUSIC_LIST,
SOUND_LIST
)


#############
Expand Down Expand Up @@ -100,16 +109,22 @@ def __init__(self, **kw):

def preload(self, *_):

# Load the kv content
# Builder.load_file(PATH_SCREENS + "game.kv", encoding="utf-8")

# Load the night camp background
self.night_camp_background = Loader.image(
PATH_IMAGES + "night_camp.png")

# Load the day camp background
self.day_camp_background = Loader.image(
PATH_IMAGES + "day_camp.png")

# Load the musics and sounds
new_musics = load_sounds(
MUSIC_LIST, PATH_MUSICS, USER_DATA.music_volume)
new_sounds = load_sounds(SOUND_LIST, PATH_SOUNDS,
USER_DATA.sound_effects_volume)
music_mixer.add_sounds(new_musics)
sound_mixer.add_sounds(new_sounds)

# Preload the class
super().preload()

Expand Down
7 changes: 4 additions & 3 deletions tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
from tools.constants import (
USER_DATA,
MUSIC_LIST,
SOUND_LIST
SOUND_LIST,
START_MUSIC_LIST
)

from tools.postrias import Game
Expand All @@ -39,8 +40,8 @@
###############

# Load the dictionnaries
MUSIC_DICT = load_sounds(MUSIC_LIST, PATH_MUSICS, USER_DATA.music_volume)
SOUND_DICT = load_sounds(SOUND_LIST, PATH_SOUNDS,
MUSIC_DICT = load_sounds(START_MUSIC_LIST, PATH_MUSICS, USER_DATA.music_volume)
SOUND_DICT = load_sounds([], PATH_SOUNDS,
USER_DATA.sound_effects_volume)

# Create the mixer
Expand Down
4 changes: 2 additions & 2 deletions tools/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,6 @@ def change_language(self, language):
TEXT_FONT_COLOR = (50 / 255, 50 / 255, 50 / 255, 1)

### Musics ###
MUSIC_LIST = ["cinematic_dramatic.mp3",
"game_music.mp3", "time_of_the_apocalypse.mp3"]
MUSIC_LIST = ["game_music.mp3", "time_of_the_apocalypse.mp3"]
SOUND_LIST = ["decision.wav", "decree.wav", "guillotine.wav"]
START_MUSIC_LIST = ["cinematic_dramatic.mp3",]
13 changes: 12 additions & 1 deletion tools/game_tools/sound.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from kivy.core.audio import SoundLoader

from tools.constants import FPS, MUSIC_LIST
from tools.constants import FPS


###############
Expand Down Expand Up @@ -63,6 +63,17 @@ def __init__(self, dict_music, volume):
self.dico_frame_state = dico_frame_state
self.volume = volume

def add_sounds(self, sound_dict):
for sound_name in sound_dict:
self.dico_frame_state[sound_name] = 0
self.musics[sound_name] = sound_dict[sound_name]
self.musics[sound_name].volume = self.volume

def add_sound(self, sound, sound_name):
self.dico_frame_state[sound_name] = 0
self.musics[sound_name] = sound
self.musics[sound_name].volume = self.volume

def fade_out(self, name, duration, mode="linear"):
if mode == "exp":
self.instructions.append(("exp_fade_out", name, duration))
Expand Down
2 changes: 2 additions & 0 deletions tools/kivy_tools/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ def on_pre_enter(self, *args):
if not self.is_loaded:
self.preload()

self.update_font_ratio()

def on_enter(self, *args):
"""
Initialize the screen when it is opened.
Expand Down

0 comments on commit 5c33eff

Please sign in to comment.