From fabf48ff426283b50f0db100aafaf3be9431f10a Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Sat, 13 Jul 2024 03:03:32 -0400 Subject: [PATCH] Powerbars added and working. --- __init__.py | 76 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 11 deletions(-) diff --git a/__init__.py b/__init__.py index 5107341..3a5e696 100755 --- a/__init__.py +++ b/__init__.py @@ -29,6 +29,7 @@ except ImportError: print("No other speech providers found.") exit() import math +import numpy as np import time localConfig = configparser.ConfigParser() @@ -148,19 +149,69 @@ def initialize_gui(gameTitle): pygame.display.set_mode((320, 200)) pygame.display.set_caption(gameTitle) # Set 32 channels for sound by default + pygame.mixer.pre_init(44100, -16, 2, 1024) pygame.mixer.init() pygame.mixer.set_num_channels(32) # Reserve the cut scene channel pygame.mixer.set_reserved(0) # Load sounds from the sound directory and creates a list like that {'bottle': 'bottle.ogg'} - soundFiles = [f for f in listdir("sounds/") if isfile(join("sounds/", f)) and (f.split('.')[1].lower() in ["ogg","wav"])] - #lets make a dict with pygame.mixer.Sound() objects {'bottle':} - soundData = {} - for f in soundFiles: - soundData[f.split('.')[0]] = pygame.mixer.Sound("sounds/" + f) - soundData['game-intro'].play() - time.sleep(soundData['game-intro'].get_length()) - return soundData + try: + soundFiles = [f for f in listdir("sounds/") if isfile(join("sounds/", f)) and (f.split('.')[1].lower() in ["ogg","wav"])] + #lets make a dict with pygame.mixer.Sound() objects {'bottle':} + soundData = {} + for f in soundFiles: + soundData[f.split('.')[0]] = pygame.mixer.Sound("sounds/" + f) + soundData['game-intro'].play() + time.sleep(soundData['game-intro'].get_length()) + return soundData + except Exception as e: + return False + +def generate_tone(frequency, duration=0.1, sample_rate=44100, volume = 0.2): + t = np.linspace(0, duration, int(sample_rate * duration), False) + tone = np.sin(2 * np.pi * frequency * t) + stereo_tone = np.vstack((tone, tone)).T # Create a 2D array for stereo + stereo_tone = (stereo_tone * 32767).astype(np.int16) + stereo_tone = (stereo_tone * 32767 * volume).astype(np.int16) # Apply volume + stereo_tone = np.ascontiguousarray(stereo_tone) # Ensure C-contiguous array + return pygame.sndarray.make_sound(stereo_tone) + +def x_powerbar(): + clock = pygame.time.Clock() + position = -50 + direction = 1 + while True: + frequency = 440 # A4 note + left_volume = (position + 50) / 100 + right_volume = (50 - position) / 100 + tone = generate_tone(frequency) + channel = tone.play() + channel.set_volume(left_volume, right_volume) + for event in pygame.event.get(): + if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: + channel.stop() + return position + position += direction + if position >= 50 or position <= -50: + direction *= -1 + clock.tick(45) # Speed of bar + +def y_powerbar(): + clock = pygame.time.Clock() + power = 0 + direction = 1 # 1 for increasing, -1 for decreasing + while True: + frequency = 220 + (power * 5) # Adjust these values to change the pitch range + tone = generate_tone(frequency) + channel = tone.play() + for event in pygame.event.get(): + if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: + channel.stop() + return power + power += direction + if power >= 100 or power <= 0: + direction *= -1 # Reverse direction at limits + clock.tick(45) def cut_scene(sounds, soundName): pygame.event.clear() @@ -327,9 +378,12 @@ def game_menu(sounds, *options): if pygame.mixer.music.get_busy(): pygame.mixer.music.unpause() else: - pygame.mixer.music.load("sounds/music_menu.ogg") - pygame.mixer.music.set_volume(0.75) - pygame.mixer.music.play(-1) + try: + pygame.mixer.music.load("sounds/music_menu.ogg") + pygame.mixer.music.set_volume(0.75) + pygame.mixer.music.play(-1) + except: + pass i = 0 # j keeps track of last spoken index so it isn't voiced on key up. j = -1