Compare commits

...

2 Commits

Author SHA1 Message Date
Storm Dragon
0e9c52f5e1 A few fixes for x-powerbar, more accurately exiting when exit keys are pressed, etc. 2024-07-14 03:51:35 -04:00
Storm Dragon
fabf48ff42 Powerbars added and working. 2024-07-13 03:03:32 -04:00

View File

@ -29,6 +29,7 @@ except ImportError:
print("No other speech providers found.") print("No other speech providers found.")
exit() exit()
import math import math
import numpy as np
import time import time
localConfig = configparser.ConfigParser() localConfig = configparser.ConfigParser()
@ -123,6 +124,12 @@ def speak(text, interupt = True):
s.speak(text, interrupt=True) s.speak(text, interrupt=True)
def check_for_exit():
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key in [pygame.K_ESCAPE, pygame.K_q]:
exit_game()
pygame.event.pump()
def exit_game(): def exit_game():
if speechProvider == "speechd": spd.close() if speechProvider == "speechd": spd.close()
pygame.mixer.music.stop() pygame.mixer.music.stop()
@ -148,12 +155,17 @@ def initialize_gui(gameTitle):
pygame.display.set_mode((320, 200)) pygame.display.set_mode((320, 200))
pygame.display.set_caption(gameTitle) pygame.display.set_caption(gameTitle)
# Set 32 channels for sound by default # Set 32 channels for sound by default
pygame.mixer.pre_init(44100, -16, 2, 1024)
pygame.mixer.init() pygame.mixer.init()
pygame.mixer.set_num_channels(32) pygame.mixer.set_num_channels(32)
# Reserve the cut scene channel # Reserve the cut scene channel
pygame.mixer.set_reserved(0) pygame.mixer.set_reserved(0)
# Load sounds from the sound directory and creates a list like that {'bottle': 'bottle.ogg'} # Load sounds from the sound directory and creates a list like {'bottle': 'bottle.ogg'}
try:
soundFiles = [f for f in listdir("sounds/") if isfile(join("sounds/", f)) and (f.split('.')[1].lower() in ["ogg","wav"])] soundFiles = [f for f in listdir("sounds/") if isfile(join("sounds/", f)) and (f.split('.')[1].lower() in ["ogg","wav"])]
except Exception as e:
print("No sounds found.")
speak("No sounds found.", False)
#lets make a dict with pygame.mixer.Sound() objects {'bottle':<soundobject>} #lets make a dict with pygame.mixer.Sound() objects {'bottle':<soundobject>}
soundData = {} soundData = {}
for f in soundFiles: for f in soundFiles:
@ -162,6 +174,57 @@ def initialize_gui(gameTitle):
time.sleep(soundData['game-intro'].get_length()) time.sleep(soundData['game-intro'].get_length())
return soundData return soundData
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 # Start from the leftmost position
direction = 1 # Move right initially
while True:
frequency = 440 # A4 note
left_volume = (50 - position) / 100
right_volume = (position + 50) / 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 # This will return a value between -50 and 50
position += direction
if position > 50:
position = 50
direction = -1
elif position < -50:
position = -50
direction = 1
clock.tick(40) # 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
check_for_exit()
clock.tick(40)
def cut_scene(sounds, soundName): def cut_scene(sounds, soundName):
pygame.event.clear() pygame.event.clear()
pygame.mixer.stop() pygame.mixer.stop()
@ -223,6 +286,22 @@ def obj_stop(x):
except: except:
return x return x
def play_ambiance(sounds, soundNames, probability, randomLocation = False):
# Check if any of the sounds in the list is already playing
for soundName in soundNames:
if pygame.mixer.find_channel(True) and pygame.mixer.find_channel(True).get_busy():
return
if random.randint(1, 100) > probability:
return
# Choose a random sound from the list
ambianceSound = random.choice(soundNames)
channel = sounds[ambianceSound].play()
if randomLocation and channel:
left_volume = random.random()
right_volume = random.random()
channel.set_volume(left_volume, right_volume)
return channel # Return the channel object for potential further manipulation
def play_random(sounds, soundName, pause = False, interrupt = False): def play_random(sounds, soundName, pause = False, interrupt = False):
key = [] key = []
for i in sounds.keys(): for i in sounds.keys():
@ -292,7 +371,7 @@ def learn_sounds(sounds):
loop = True loop = True
pygame.mixer.music.pause() pygame.mixer.music.pause()
i = 0 i = 0
soundFiles = [f for f in listdir("sounds/") if isfile(join("sounds/", f)) and (f.split('.')[1].lower() in ["ogg","wav"]) and (f.split('.')[0].lower() not in ["game-intro", "music_menu"])] soundFiles = [f for f in listdir("sounds/") if isfile(join("sounds/", f)) and (f.split('.')[1].lower() in ["ogg","wav"]) and (f.split('.')[0].lower() not in ["game-intro", "music_menu"]) and (not f.lower().startswith("_"))]
# j keeps track of last spoken index so it isn't voiced on key up. # j keeps track of last spoken index so it isn't voiced on key up.
j = -1 j = -1
while loop == True: while loop == True:
@ -327,9 +406,12 @@ def game_menu(sounds, *options):
if pygame.mixer.music.get_busy(): if pygame.mixer.music.get_busy():
pygame.mixer.music.unpause() pygame.mixer.music.unpause()
else: else:
try:
pygame.mixer.music.load("sounds/music_menu.ogg") pygame.mixer.music.load("sounds/music_menu.ogg")
pygame.mixer.music.set_volume(0.75) pygame.mixer.music.set_volume(0.75)
pygame.mixer.music.play(-1) pygame.mixer.music.play(-1)
except:
pass
i = 0 i = 0
# j keeps track of last spoken index so it isn't voiced on key up. # j keeps track of last spoken index so it isn't voiced on key up.
j = -1 j = -1