diff --git a/cromulant/audio/March of the Cyber Ants.mp3 b/cromulant/audio/March of the Cyber Ants.mp3 new file mode 100644 index 0000000..29a4215 Binary files /dev/null and b/cromulant/audio/March of the Cyber Ants.mp3 differ diff --git a/cromulant/config.py b/cromulant/config.py index 0028c48..f882557 100644 --- a/cromulant/config.py +++ b/cromulant/config.py @@ -36,6 +36,7 @@ class Config: hit_icon: str = "🎃" triumph_message: str = "Scored a triumph" hit_message: str = "Took a hit" + song_path: Path @staticmethod def prepare() -> None: @@ -53,3 +54,4 @@ class Config: Config.names_json = Config.here / "data" / "names.json" Config.font_path = Config.here / "fonts" / "NotoSans-Regular.ttf" Config.emoji_font_path = Config.here / "fonts" / "NotoEmoji-Regular.ttf" + Config.song_path = Config.here / "audio" / "March of the Cyber Ants.mp3" diff --git a/cromulant/game.py b/cromulant/game.py index 8780b96..2f46b2e 100644 --- a/cromulant/game.py +++ b/cromulant/game.py @@ -22,6 +22,7 @@ from .window import Window class Game: timer: QTimer + playing_song: bool = False @staticmethod def prepare() -> None: @@ -122,6 +123,7 @@ class Game: rgb = Utils.get_rgb(color) image_label.setStyleSheet(f"border: 2px solid {rgb};") + image_label.mousePressEvent = lambda event: Game.toggle_song() return image_label @staticmethod @@ -210,3 +212,13 @@ class Game: Window.info.setText(Config.info_separator.join(text)) Window.info.adjustSize() + + @staticmethod + def toggle_song() -> None: + if Game.playing_song: + Window.stop_audio() + else: + path = str(Config.song_path) + Window.play_audio(path) + + Game.playing_song = not Game.playing_song diff --git a/cromulant/utils.py b/cromulant/utils.py index 68e641b..04a4f63 100644 --- a/cromulant/utils.py +++ b/cromulant/utils.py @@ -1,10 +1,10 @@ from __future__ import annotations - import random import colorsys import time from typing import ClassVar + from fontTools.ttLib import TTFont # type: ignore from .config import Config diff --git a/cromulant/window.py b/cromulant/window.py index 68c3024..d6dbc15 100644 --- a/cromulant/window.py +++ b/cromulant/window.py @@ -21,6 +21,9 @@ from PySide6.QtGui import QMouseEvent from PySide6.QtGui import QIcon from PySide6.QtCore import Qt # type: ignore from PySide6.QtCore import Signal +from PySide6.QtCore import QUrl +from PySide6.QtMultimedia import QMediaPlayer # type: ignore +from PySide6.QtMultimedia import QAudioOutput from .config import Config @@ -46,6 +49,8 @@ class Window: info: SpecialButton font: str emoji_font: str + player: QMediaPlayer + audio: QAudioOutput @staticmethod def prepare() -> None: @@ -102,9 +107,11 @@ class Window: btn_hatch.middleClicked.connect(lambda: Ants.hatch_burst()) btn_terminate = SpecialButton("Terminate") + btn_terminate.setToolTip( "Terminate a random ant\nMiddle Click to terminate all" ) + btn_terminate.clicked.connect(lambda e: Ants.terminate()) btn_terminate.middleClicked.connect(lambda: Ants.terminate_all()) @@ -215,3 +222,16 @@ class Window: container.addWidget(Window.info) root.setLayout(container) Window.root.addWidget(root) + + @staticmethod + def play_audio(path: str) -> None: + Window.player = QMediaPlayer() + Window.audio = QAudioOutput() + Window.player.setAudioOutput(Window.audio) + Window.player.setSource(QUrl.fromLocalFile(path)) + Window.audio.setVolume(100) + Window.player.play() + + @staticmethod + def stop_audio() -> None: + Window.player.stop()