From faef3d2b52ad79ca4ded3e8d504ebae76f5d72c6 Mon Sep 17 00:00:00 2001 From: CoCo_Sol Date: Sun, 29 Oct 2023 11:58:09 +0100 Subject: [PATCH 01/11] sound component --- src/engine.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/engine.py b/src/engine.py index 57b4de8..2a53889 100644 --- a/src/engine.py +++ b/src/engine.py @@ -3,6 +3,7 @@ Un moteur de jeu inspiré de bevy. """ +from genericpath import exists import glob import json import math @@ -559,6 +560,15 @@ class Clickable: self.callback = callback +class Sound: + """ + Composant qui une entité emettrant un son. + """ + + def __init__(self, name: str) -> None: + self.name = name + + class Scene: """ Une scène dans le jeu. From 651855f2faa99978da59380cb0fcaf79ce93a625 Mon Sep 17 00:00:00 2001 From: CoCo_Sol Date: Sun, 29 Oct 2023 11:59:07 +0100 Subject: [PATCH 02/11] fix import --- src/engine.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/engine.py b/src/engine.py index 2a53889..ea071c6 100644 --- a/src/engine.py +++ b/src/engine.py @@ -3,7 +3,6 @@ Un moteur de jeu inspiré de bevy. """ -from genericpath import exists import glob import json import math From cda7cbcc595e053fe70f99dd830a916ba0de9ba4 Mon Sep 17 00:00:00 2001 From: CoCo_Sol Date: Sun, 29 Oct 2023 12:27:21 +0100 Subject: [PATCH 03/11] play sound --- src/engine.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/engine.py b/src/engine.py index ea071c6..dfe59ab 100644 --- a/src/engine.py +++ b/src/engine.py @@ -566,6 +566,7 @@ class Sound: def __init__(self, name: str) -> None: self.name = name + self._is_paying = False class Scene: @@ -771,6 +772,15 @@ def start_game( ), ) + # On met le son + for entity in world.query(Sound): + # On verifie si le son est deja actif. + if not entity[Sound]._is_paying: + # On charge le son et on le joue. + if os.path.exists("assets/sounds/" + entity[Sound].name): + entity[Sound]._is_paying = True + pygame.mixer.Sound("assets/sounds/" + entity[Sound].name).play() + # Mise a jour de la fenêtre rect = Display._calculate_surface_rect() pygame.transform.set_smoothscale_backend("MMX") From c398f139ef3410aa4015dec08385a1f8982af203 Mon Sep 17 00:00:00 2001 From: CoCo_Sol Date: Sun, 29 Oct 2023 12:28:06 +0100 Subject: [PATCH 04/11] exemple sound --- assets/sounds/pop.ogg | Bin 0 -> 8578 bytes src/scenes/menu.py | 2 ++ 2 files changed, 2 insertions(+) create mode 100644 assets/sounds/pop.ogg diff --git a/assets/sounds/pop.ogg b/assets/sounds/pop.ogg new file mode 100644 index 0000000000000000000000000000000000000000..72c934568d16a58066d09647ea989771b088de7b GIT binary patch literal 8578 zcmeG?d05lOwv&YrAkqK{h5$BUGeGbr5Gb1(!WsmG5HN&d37dq)%H|cVHb8+?;(_VxAM@7?!(_j~`n`6lztoH;XR<}5Sk z%rEOVZ3+iS@F6WXTB=-{eRl-00 z7M~`{PDE%eQMToHMG__$yQFTSp zFezTi9Qm!Fuo8#>pn-&@Q`&fsn{v@@6KQ2>W}TZj#PtwGTM>4{O=s{YoA}KweJ!b* zFWOpTmqlqot^sxcRYu@xia0s7kOYg}W|^bZL;Evz?ON;dbsa}31woDvoA!jbKWuF* zb$>+bo8;&@^i2l_F=r#=51+)ue35n0?f(7`h0fwWC~z$tj7b8XgVjtgw{5A;gi|eT zivlEYo4~)E!0jQ7_n2LnvReOw9%ke`<>VXA4~~E-CuYOe#LHWAE^pl#RV|A?x-;si zEP7rR^RrAuD*TY&?EL;`O~ieN%dH7+!7^-Ag{keshO;K zdBIJ;=5AdZp?2nU5x;ijv9{{kMG2-|G`S6BX*;%WxNhHMP1Z#&uBLgZmTOq6-PJY? zVJ7$Y>yMa-Af9--M!Ri60LMm6*4%47Mu4@VirSU^$M{S0eNc+BfGN$LX4)^UOu}$K7Y}-Z&=IV!AWb z;*$P*2eS^l1*A6j&k60DD|msn%}W*qg-S^%^TL=c8cAVw=9ujvXcd1H+;gi51M{gr zg2Rb1Q^cGZK89nTW*Mt%(Z*iP1-PMh$#0m?g9M{&LPAEJtG^7H*}t161h$w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xTp+H7&+P_P`$LfH;^PL|&=G&i_-FZ<-B`ZSW6h}BSSlJXc7R5Wh z>e5<3B9sB>%pLwk3pr$ZV>9t&$F4ldHq>{f$yA0ueM@U}21kxxDYhw%bacwnMFfDY zh52IBzu2{i?OW;!PZHny%tSs7-hqhjai$|1TH^%52gzsprZ}?-u_CGCSo_iJ#(R1= zNdhI1M={xgzd0!}@VklSzqUu2;Uq8+kh2@GYtPZIPkkZ@L zpIErRP?cG0|B5HJ(N6YwatI?>U5Rcvg~CWu22)V9A&<(OlZ>l~uJp|*JDt) zpiutFC(cKgy8^ZQa&TsqHn$hq4A~N7ych-;tHrUm^wLhy63L91y<;Wor4 zLBo&Q6O8SPnL41gKBd1WuAy2ktZAqkP&0yd^bpqdADK<|uop%63?FxnZiTMQ!&r-U z$qf#=9v$Gil|;gJXE+@+%#OLgHRV|E+1;=8 zYL35`edd}X(UzY!=ESA}bngk!b;92A*@hT}BgOJFUGCSwb&a3d{Rb$pa1d`d$GmlP z+h5p2uL?tZe%NGxy2HqAu*t70f?B1H}E8iIp04w5I$mY?fxlab1 zn!nh0%4#fLc7yWl zE;dWA4>3MmU6DNrpFd9}e)sIcC&zC4kDNVT;c$EVvr}C2wT^385!0qKA!$EZ)K2{6<%kg>xw!i%Sgy?tI?}eo16wc!`%o=hd>@+O&aletJQA2i&t$ z`p8`kcAJ4cZgAKUWa2SBbkrPK+KZ#ibjX_>5)uS_->$>$?Y(lX_Br Date: Sun, 29 Oct 2023 13:06:22 +0100 Subject: [PATCH 05/11] del sound --- src/engine.py | 39 +++++++++++++++++++++++++++++++++++---- src/scenes/menu.py | 14 ++++++++++++-- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/engine.py b/src/engine.py index dfe59ab..e0b49e4 100644 --- a/src/engine.py +++ b/src/engine.py @@ -564,10 +564,27 @@ class Sound: Composant qui une entité emettrant un son. """ - def __init__(self, name: str) -> None: - self.name = name + def __init__( + 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._sound: pygame.mixer.Sound + self._chanel: pygame.mixer.Channel + + self.callback = callback + + def __del__(self): + self._sound.stop() + self._chanel.stop() + class Scene: """ @@ -777,9 +794,23 @@ def start_game( # On verifie si le son est deja actif. if not entity[Sound]._is_paying: # 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 - 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 rect = Display._calculate_surface_rect() diff --git a/src/scenes/menu.py b/src/scenes/menu.py index 722442a..58c683e 100644 --- a/src/scenes/menu.py +++ b/src/scenes/menu.py @@ -8,6 +8,7 @@ from engine import ( Display, Game, HoveredTexture, + Keyboard, Order, Position, Scene, @@ -45,7 +46,7 @@ def __initialize_world(world: World): Centered(), Text("Guess The Number"), TextSize(200), - Sound("pop.ogg"), + Sound("pop.ogg", loop=True, callback=lambda _, _a: print("coucou")), ) scenes_name = ["classique", "menteur", "tricheur", "histoire"] @@ -53,8 +54,17 @@ def __initialize_world(world: World): __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( [__initialize_world], - [], + [get_pressed], [], ) From ed6ea65d5150202d25cffe6a8a4d34250ee8d720 Mon Sep 17 00:00:00 2001 From: CoCo_Sol Date: Sun, 29 Oct 2023 13:08:24 +0100 Subject: [PATCH 06/11] import Sound --- src/scenes/menu.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/scenes/menu.py b/src/scenes/menu.py index f59edc1..fba8a65 100644 --- a/src/scenes/menu.py +++ b/src/scenes/menu.py @@ -12,6 +12,7 @@ from engine import ( Order, Position, Scene, + Sound, Texture, World, ) From 609a00ea11692c4410e13e759a9996c8a98ef3cc Mon Sep 17 00:00:00 2001 From: CoCo_Sol Date: Sun, 29 Oct 2023 14:20:56 +0100 Subject: [PATCH 07/11] sound system --- src/engine.py | 74 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 27 deletions(-) diff --git a/src/engine.py b/src/engine.py index 89a5da3..1eb5fc4 100644 --- a/src/engine.py +++ b/src/engine.py @@ -283,6 +283,7 @@ class Assets: # Cache des ressources self.__textures: dict[str, pygame.Surface] = {} self.__fonts: dict[int, pygame.font.Font] = {} + self.__sounds: dict[str, pygame.mixer.Sound] = {} def get_texture(self, name: str) -> pygame.Surface: """ @@ -346,6 +347,22 @@ class Assets: """ return Vec2(*self.get_font(size).size(text)) + def get_sound(self, name: str) -> pygame.mixer.Sound: + """ + Renvoie le son qui correspond au nom *name*. + + Paramètres: + name: Le nom du son. + + Retourne: + Le son qui correspond au nom *name*. + """ + sound = self.__sounds.get(name) + if sound is None: + sound = pygame.mixer.Sound(f"assets/sounds/{name}") + self.__sounds[name] = sound + return sound + class Time(float): """ @@ -578,15 +595,8 @@ class Sound: self._loop = loop 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: """ @@ -633,6 +643,9 @@ def start_game( # Chargements des assets assets = Assets(surface) + # creation des channels pour les sons + channels: dict[Entity, pygame.mixer.Channel] = {} + # On récupère la première scène scene = scenes.get(start_scene) @@ -791,28 +804,35 @@ def start_game( ), ) - # On met le son + # On update assets._sound en fonction des nouvelles entité. for entity in world.query(Sound): - # On verifie si le son est deja actif. - if not entity[Sound]._is_paying: - # On charge le son et on le joue. - if os.path.exists("assets/sounds/" + entity[Sound]._name): - entity[Sound]._is_paying = True - 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 - ) + channel = channels.get(entity) + if channel is None: + sound = assets.get_sound(entity[Sound]._name) + channel: Optional[pygame.mixer.Channel] = sound.play( + loops=-1 if entity[Sound]._loop else 0 + ) + if channel is None: # type: ignore + continue + channels[entity] = channel - 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) + channel.set_volume(entity[Sound].volume) + + # On verifie si le son est terminé + if not channel.get_busy(): + entity[Sound]._is_paying = False + callback = entity[Sound].callback + del entity[Sound] + del channels[entity] + callback(world, entity) + + entities_to_delete: list[Entity] = [] + for entity, channel in channels.items(): + if Sound not in entity: + channel.stop() + entities_to_delete.append(entity) + for entity in entities_to_delete: + del channels[entity] # Mise a jour de la fenêtre rect = Display._calculate_surface_rect() From 56dba9a493ab4347355f2b4efeaa81453ca2118a Mon Sep 17 00:00:00 2001 From: CoCo_Sol Date: Sun, 29 Oct 2023 14:24:05 +0100 Subject: [PATCH 08/11] remove pop --- assets/sounds/pop.ogg | Bin 8578 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 assets/sounds/pop.ogg diff --git a/assets/sounds/pop.ogg b/assets/sounds/pop.ogg deleted file mode 100644 index 72c934568d16a58066d09647ea989771b088de7b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8578 zcmeG?d05lOwv&YrAkqK{h5$BUGeGbr5Gb1(!WsmG5HN&d37dq)%H|cVHb8+?;(_VxAM@7?!(_j~`n`6lztoH;XR<}5Sk z%rEOVZ3+iS@F6WXTB=-{eRl-00 z7M~`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+@+%#OLgHRV|E+1;=8 zYL35`edd}X(UzY!=ESA}bngk!b;92A*@hT}BgOJFUGCSwb&a3d{Rb$pa1d`d$GmlP z+h5p2uL?tZe%NGxy2HqAu*t70f?B1H}E8iIp04w5I$mY?fxlab1 zn!nh0%4#fLc7yWl zE;dWA4>3MmU6DNrpFd9}e)sIcC&zC4kDNVT;c$EVvr}C2wT^385!0qKA!$EZ)K2{6<%kg>xw!i%Sgy?tI?}eo16wc!`%o=hd>@+O&aletJQA2i&t$ z`p8`kcAJ4cZgAKUWa2SBbkrPK+KZ#ibjX_>5)uS_->$>$?Y(lX_Br Date: Sun, 29 Oct 2023 14:31:27 +0100 Subject: [PATCH 09/11] exemple --- assets/sounds/click.wav | Bin 0 -> 1148 bytes src/main.py | 1 + src/scenes/menu.py | 23 +++++++++++------------ 3 files changed, 12 insertions(+), 12 deletions(-) create mode 100644 assets/sounds/click.wav diff --git a/assets/sounds/click.wav b/assets/sounds/click.wav new file mode 100644 index 0000000000000000000000000000000000000000..2797a50096f93fee07c27da6c964a041ca9837f6 GIT binary patch literal 1148 zcmW-heN2~C6vw~k{y;$99pEcStu~!=F0t7bu?%t9R*ElCQ&d2SWJD^g&CHe4k@ZIl zm$Nb~Z31d;HJ8q)WSW5H(DeqsFbqiN7#NWFgWo;h4(zkDo%=lZe$RQ%x#wB2bjgy6 zFd%Qy%Q?km73m281P+T&014kZgdqVN3n~hh+n#BdVKyDfMv~bSTTMoak%UyFjr$W3 ziEslLr8A{i#F&QQA%h&@D2F&;44M}-e~`m`zz{vh&Uo{N$KMPSwu(d)CfMm%)0|<^ z;*e-c%4mBYV?-euGx6wn22(KAa+rp6BNNY{)AhNg3B3~*M68Th6(|mb5o`}NhZwpa zTFM-LDYf#C8+3`1B5$#hpKw1P`aa(gx)OTCC;OK|S)najrZ>=mR5!~NxpH@c#~H5b zhi%_2y{!YbV^Hy=-85welbwVcUR&XRdDC|;hHEIEhsXhJjYv!A(qk#(li zi+*gA?J`}` z9^f+MqM!G8O)snHL;t*H=|x^-k-R45ZikC=$!?mYN+)h(7pf32>Wr`OHA+~_Mm?iF z+NF7x)o1*aM|hY)9njCUT5qwP8S<1QNTSs982{9(+OK!DRhx9|8#>U1GI>)4>k= zomy%29`g76CS9l1+{d@E1IKU@Iab%P-k!k^xFKCK;=%)K0;PdLr*4IN)qNn<@&G}E zNwi$BI;UU~F0q++ji1@Tct$c$bM-gh;!AwK-{OmXlsf%J>vU9yG?o*Y&BffxGTYt2 zD(uBdStIrGogA_rZ Date: Sun, 29 Oct 2023 15:43:48 +0100 Subject: [PATCH 10/11] =?UTF-8?q?Meilleur=20gestion=20de=20l'audio=20lors?= =?UTF-8?q?=20du=20changement=20de=20sc=C3=A8nes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/engine.py | 69 +++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/src/engine.py b/src/engine.py index 1eb5fc4..0c61496 100644 --- a/src/engine.py +++ b/src/engine.py @@ -589,13 +589,13 @@ class Sound: volume: float = 0.5, loop: bool = False, callback: Callable[[World, Entity], object] = lambda _w, _e: None, + stop_on_remove: bool = False, ) -> None: - self._name = name + self.name = name self.volume = volume - self._loop = loop - self._is_paying = False - + self.loop = loop self.callback = callback + self.stop_on_remove = stop_on_remove class Scene: @@ -644,7 +644,7 @@ def start_game( assets = Assets(surface) # creation des channels pour les sons - channels: dict[Entity, pygame.mixer.Channel] = {} + channels: dict[Entity, tuple[bool, pygame.mixer.Channel]] = {} # On récupère la première scène scene = scenes.get(start_scene) @@ -741,6 +741,35 @@ def start_game( for system in scene.update_systems: system(world) + # Gestion des sons en cours + sound_entities = world.query(Sound) + entities_to_delete: list[Entity] = [] + for entity, (stop_on_remove, channel) in channels.items(): + if Sound in entity: + entity_sound = entity[Sound] + channel.set_volume(entity_sound.volume) + if not channel.get_busy(): + entities_to_delete.append(entity) + del entity[Sound] + entity_sound.callback(world, entity) + continue + if stop_on_remove and entity not in sound_entities: + entities_to_delete.append(entity) + channel.stop() + for entity in entities_to_delete: + del channels[entity] + + # Ajout des sons non gérés + for entity in sound_entities: + if entity not in channels: + entity_sound = entity[Sound] + sound = assets.get_sound(entity_sound.name) + channel = sound.play(loops=-1 if entity_sound.loop else 0) + if channel is None: # type: ignore + continue + channel.set_volume(entity_sound.volume) + channels[entity] = entity_sound.stop_on_remove, channel + # Mise à jour des animations for entity in world.query(Animation): animation = entity[Animation] @@ -804,36 +833,6 @@ def start_game( ), ) - # On update assets._sound en fonction des nouvelles entité. - for entity in world.query(Sound): - channel = channels.get(entity) - if channel is None: - sound = assets.get_sound(entity[Sound]._name) - channel: Optional[pygame.mixer.Channel] = sound.play( - loops=-1 if entity[Sound]._loop else 0 - ) - if channel is None: # type: ignore - continue - channels[entity] = channel - - channel.set_volume(entity[Sound].volume) - - # On verifie si le son est terminé - if not channel.get_busy(): - entity[Sound]._is_paying = False - callback = entity[Sound].callback - del entity[Sound] - del channels[entity] - callback(world, entity) - - entities_to_delete: list[Entity] = [] - for entity, channel in channels.items(): - if Sound not in entity: - channel.stop() - entities_to_delete.append(entity) - for entity in entities_to_delete: - del channels[entity] - # Mise a jour de la fenêtre rect = Display._calculate_surface_rect() pygame.transform.set_smoothscale_backend("MMX") From 1b00dc4be6fa98a62b8225c473eb915d14982d59 Mon Sep 17 00:00:00 2001 From: Tipragot Date: Sun, 29 Oct 2023 15:45:02 +0100 Subject: [PATCH 11/11] Pourquoi? --- src/main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main.py b/src/main.py index 0b4f1ae..1754bc0 100644 --- a/src/main.py +++ b/src/main.py @@ -10,7 +10,6 @@ from scenes import menu start_game( { "menu": menu.SCENE, - "classique": menu.SCENE, }, "menu", title="Guess The Number",