Ajoute d'un systeme de son #34
|
@ -564,10 +564,27 @@ class Sound:
|
||||||
Composant qui une entité emettrant un son.
|
Composant qui une entité emettrant un son.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, name: str) -> None:
|
def __init__(
|
||||||
self.name = name
|
self,
|
||||||
|
name: str,
|
||||||
|
volume: float = 0.5,
|
||||||
|
loop: bool = False,
|
||||||
|
callback: Callable[[World, Entity], object] = lambda _w, _e: None,
|
||||||
|
) -> None:
|
||||||
|
self._name = name
|
||||||
|
self.volume = volume
|
||||||
|
self._loop = loop
|
||||||
self._is_paying = False
|
self._is_paying = False
|
||||||
|
|
||||||
|
self._sound: pygame.mixer.Sound
|
||||||
|
self._chanel: pygame.mixer.Channel
|
||||||
|
|
||||||
|
self.callback = callback
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
self._sound.stop()
|
||||||
|
self._chanel.stop()
|
||||||
|
|
||||||
|
|
||||||
class Scene:
|
class Scene:
|
||||||
"""
|
"""
|
||||||
|
@ -777,9 +794,23 @@ def start_game(
|
||||||
# On verifie si le son est deja actif.
|
# On verifie si le son est deja actif.
|
||||||
if not entity[Sound]._is_paying:
|
if not entity[Sound]._is_paying:
|
||||||
# On charge le son et on le joue.
|
# On charge le son et on le joue.
|
||||||
if os.path.exists("assets/sounds/" + entity[Sound].name):
|
if os.path.exists("assets/sounds/" + entity[Sound]._name):
|
||||||
entity[Sound]._is_paying = True
|
entity[Sound]._is_paying = True
|
||||||
pygame.mixer.Sound("assets/sounds/" + entity[Sound].name).play()
|
entity[Sound]._sound = pygame.mixer.Sound(
|
||||||
|
"assets/sounds/" + entity[Sound]._name
|
||||||
|
)
|
||||||
|
entity[Sound]._sound.set_volume(entity[Sound].volume)
|
||||||
|
entity[Sound]._chanel = entity[Sound]._sound.play(
|
||||||
|
loops=-1 if entity[Sound]._loop else 0
|
||||||
|
)
|
||||||
|
|
||||||
|
else:
|
||||||
|
# si le son est joue.
|
||||||
|
if not entity[Sound]._chanel.get_busy():
|
||||||
|
entity[Sound]._is_paying = False
|
||||||
|
callback = entity[Sound].callback
|
||||||
|
del entity[Sound]
|
||||||
|
callback(world, entity)
|
||||||
|
|
||||||
# Mise a jour de la fenêtre
|
# Mise a jour de la fenêtre
|
||||||
rect = Display._calculate_surface_rect()
|
rect = Display._calculate_surface_rect()
|
||||||
|
|
|
@ -8,6 +8,7 @@ from engine import (
|
||||||
Display,
|
Display,
|
||||||
Game,
|
Game,
|
||||||
HoveredTexture,
|
HoveredTexture,
|
||||||
|
Keyboard,
|
||||||
Order,
|
Order,
|
||||||
Position,
|
Position,
|
||||||
Scene,
|
Scene,
|
||||||
|
@ -45,7 +46,7 @@ def __initialize_world(world: World):
|
||||||
Centered(),
|
Centered(),
|
||||||
Text("Guess The Number"),
|
Text("Guess The Number"),
|
||||||
TextSize(200),
|
TextSize(200),
|
||||||
Sound("pop.ogg"),
|
Sound("pop.ogg", loop=True, callback=lambda _, _a: print("coucou")),
|
||||||
)
|
)
|
||||||
|
|
||||||
scenes_name = ["classique", "menteur", "tricheur", "histoire"]
|
scenes_name = ["classique", "menteur", "tricheur", "histoire"]
|
||||||
|
@ -53,8 +54,17 @@ def __initialize_world(world: World):
|
||||||
__create_button(world, i, name)
|
__create_button(world, i, name)
|
||||||
|
|
||||||
|
|
||||||
|
def get_pressed(world: World):
|
||||||
|
"""
|
||||||
|
Renvoie les touches appuyées.
|
||||||
|
"""
|
||||||
|
if world[Keyboard].is_key_pressed("a"):
|
||||||
|
for entity in world.query(Sound):
|
||||||
|
del entity[Sound]
|
||||||
|
|
||||||
|
|
||||||
SCENE = Scene(
|
SCENE = Scene(
|
||||||
[__initialize_world],
|
[__initialize_world],
|
||||||
[],
|
[get_pressed],
|
||||||
[],
|
[],
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue