summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorViljami Ilola <+@hix.fi>2024-04-27 01:29:45 +0300
committerViljami Ilola <+@hix.fi>2024-04-27 01:29:45 +0300
commit7a33e5020f9b1e7cf89bc532e2ddcf681f5bcf9c (patch)
tree544e868288f05213452dec9b7b789bd1eb208191
parenta4794437d561a01c8bea12d59502e4cc72acfbea (diff)
volume control
-rw-r--r--src/sliceitoff/game/game.py4
-rw-r--r--src/sliceitoff/game/settings.py17
-rw-r--r--src/sliceitoff/screens/settings.py10
-rw-r--r--src/sliceitoff/settings/settings.py15
-rw-r--r--src/sliceitoff/sfx/sfx.py27
5 files changed, 59 insertions, 14 deletions
diff --git a/src/sliceitoff/game/game.py b/src/sliceitoff/game/game.py
index f85a544..57f2c56 100644
--- a/src/sliceitoff/game/game.py
+++ b/src/sliceitoff/game/game.py
@@ -21,7 +21,7 @@ from .mainmenu import Mainmenu, MenuItems
from .level import Level
from .show import Show
from .initials import Initials
-from .settings import Settings
+from .settings import SettingsMenu
class Game:
""" This is the whole game. """
@@ -84,7 +84,7 @@ class Game:
def settings(self):
""" settings menu where one can adjust volume for example """
- menu = Settings()
+ menu = SettingsMenu()
while menu.active:
menu.update(dt = self.clock.tick())
self.display.update([menu])
diff --git a/src/sliceitoff/game/settings.py b/src/sliceitoff/game/settings.py
index 6a709a4..53d0362 100644
--- a/src/sliceitoff/game/settings.py
+++ b/src/sliceitoff/game/settings.py
@@ -1,4 +1,4 @@
-""" game.mainmenu - Let's user choose """
+""" game.settings - Settings dialog """
from enum import IntEnum
import pygame
@@ -11,16 +11,16 @@ from .explodeout import ExplodeOutGroup
MOUSE_TRESHOLD = 100
class MenuItems(IntEnum):
- """ Items in the menu. Should match mainmenu_screen """
+ """ Items in the menu. Should match settings_screen """
SFX = 0
MUSIC = 1
BACK = 2
-class Settings(ExplodeOutGroup):
+class SettingsMenu(ExplodeOutGroup):
""" sprite group with imputs to make selection """
def __init__(self):
super().__init__()
- self.add(settings_screen(0))
+ self.add(settings_screen(0,0,0))
self.selection = 0
self.mousey = 0
@@ -29,6 +29,10 @@ class Settings(ExplodeOutGroup):
match self.selection:
case MenuItems.BACK:
self.do_fadeout()
+ case MenuItems.SFX:
+ sfx.sfx_up()
+ case MenuItems.MUSIC:
+ sfx.music_up()
def update(self, dt = 0, **kwargs):
""" Does it all. Reads keyboard and updates screen """
@@ -49,7 +53,10 @@ class Settings(ExplodeOutGroup):
elif event.type == pygame.MOUSEMOTION:
self.process_mouse_motion()
self.empty()
- self.add(settings_screen(self.selection))
+ self.add(settings_screen(
+ self.selection,
+ sfx.sfx_volume,
+ sfx.music_volume))
def process_mouse_motion(self):
""" Mouse movement up or down moves menu selection """
diff --git a/src/sliceitoff/screens/settings.py b/src/sliceitoff/screens/settings.py
index cb0549a..c987126 100644
--- a/src/sliceitoff/screens/settings.py
+++ b/src/sliceitoff/screens/settings.py
@@ -2,19 +2,19 @@
from random import randrange
from sliceitoff.text import TextPage
-def settings_screen(selection):
+def settings_screen(selection, sfx_vol, music_vol):
""" Screen where current selection is flashing """
active = randrange(0xe9,0xf0)
inactive = 0xe7
text = (
f" Settings:\n"
f"\n\n"
- f"\xe7SFX Volume:\n"
+ f"\xe7SFX: "
f"{chr(active if selection == 0 else inactive)}"
- f" [ ]\n\n"
- f"\xe7Music Volume:\n"
+ f"[{'#' * sfx_vol}{' ' * (10 - sfx_vol)}]\n\n"
+ f"\xe7BGM: "
f"{chr(active if selection == 1 else inactive)}"
- f" [ ]\n\n"
+ f"[{'#' * music_vol}{' ' * (10 - music_vol)}]\n\n"
f"{chr(active if selection == 2 else inactive)}"
f"Back.")
return TextPage(
diff --git a/src/sliceitoff/settings/settings.py b/src/sliceitoff/settings/settings.py
index cf588b7..19cca7d 100644
--- a/src/sliceitoff/settings/settings.py
+++ b/src/sliceitoff/settings/settings.py
@@ -4,6 +4,7 @@ from pathlib import Path
from .static import DEFAULT_SETTINGS
class Settings:
+ """ Handles loading and saving settings from config file"""
def __init__(self, filename = None):
self.settings=[]
if filename:
@@ -26,6 +27,7 @@ class Settings:
self.settings.append(entry)
def validate_line(self, line):
+ """ Validates and splits config line """
if not line or line[0] == '#':
return None
data = line.split('=')
@@ -34,41 +36,50 @@ class Settings:
return data[0], data[1]
def get_values(self, option):
+ """ Gets all values for certain option """
return [x[1] for x in self.settings if x[0]==option]
def put_values(self, option, values):
+ """ Puts multiple values with same option name """
for x in values:
self.put_value(option, x)
def remove_values(self, option):
+ """ Removes all values with given option name """
for x in self.settings[:]:
if x[0]==option:
self.settings.remove(x)
def replace_values(self, option, values):
+ """ After replacement only given values are attached to the option """
self.remove_values(option)
self.put_values(option, values)
def get_value(self, option):
+ """ Gets first value of the option """
v = self.get_values(option)
if not v:
return None
- return v
+ return v[0]
def get_value_or_default(self, option):
- v = self.get_values(option)
+ """ Gets first value if found otherwise default """
+ v = self.get_value(option)
if v:
return v
return [x[1] for x in DEFAULT_SETTINGS if x[0]==option][1]
def put_value(self, option, value):
+ """ Puts single value with option name """
self.settings.append((option, value))
def replace_value(self, option, value):
+ """ Replaces even multiple values from option just to add one """
self.remove_values(option)
self.put_value(option, value)
def save(self):
+ """ Saves options to config file """
with open(self.config_filename, 'w', encoding="utf-8") as config_file:
for option, value in self.settings:
config_file.write(f"{option}={value}\n")
diff --git a/src/sliceitoff/sfx/sfx.py b/src/sliceitoff/sfx/sfx.py
index fd494b8..8f3371e 100644
--- a/src/sliceitoff/sfx/sfx.py
+++ b/src/sliceitoff/sfx/sfx.py
@@ -1,8 +1,11 @@
""" sfx.sfx - pygame.mixer initialization and sound effects handling """
import os
+from random import choice
from pathlib import Path
import pygame
+from sliceitoff.settings import settings
+
DEBUG = os.getenv("DEBUG")
class Sfx:
@@ -12,6 +15,14 @@ class Sfx:
self.sound = {}
self.bgm = None
try:
+ self.sfx_volume = int(
+ settings.get_value_or_default("sfx_volume"))
+ self.music_volume = int(
+ settings.get_value_or_default("music_volume"))
+ except ValueError:
+ self.sfx_volume = 10
+ self.music_volume = 10
+ try:
pygame.mixer.pre_init(channels=2, buffer=512, frequency=48000)
except pygame.error:
pass
@@ -29,9 +40,24 @@ class Sfx:
except pygame.error:
pass
+ def sfx_up(self):
+ """ Turn volume up. If its turned to 11 it resets back to 0. """
+ self.sfx_volume += 1
+ self.sfx_volume %= 11
+ settings.replace_value("sfx_volume", self.sfx_volume)
+ self.play(choice(("baby", "laser", "glass")))
+
+ def music_up(self):
+ """ Turn volume up. If its turned to 11 it resets back to 0. """
+ self.music_volume += 1
+ self.music_volume %= 11
+ settings.replace_value("music_volume", self.music_volume)
+ self.sound[self.bgm].set_volume(self.music_volume / 10)
+
def play(self, sample):
""" Just plays named sample loaded from assets directory """
if self.initialized:
+ self.sound[sample].set_volume(self.sfx_volume / 10)
self.sound[sample].play()
def music(self, music):
@@ -44,6 +70,7 @@ class Sfx:
self.sound[self.bgm].fadeout(500)
self.bgm = music
if self.bgm:
+ self.sound[self.bgm].set_volume(self.music_volume / 10)
self.sound[self.bgm].play()
# Initialize only one time