A few fixes for x-powerbar, more accurately exiting when exit keys are pressed, etc.

This commit is contained in:
Storm Dragon 2024-07-14 03:51:35 -04:00
parent fabf48ff42
commit 0e9c52f5e1

View File

@ -124,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()
@ -154,18 +160,19 @@ def initialize_gui(gameTitle):
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: 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"])]
#lets make a dict with pygame.mixer.Sound() objects {'bottle':<soundobject>}
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: except Exception as e:
return False print("No sounds found.")
speak("No sounds found.", False)
#lets make a dict with pygame.mixer.Sound() objects {'bottle':<soundobject>}
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
def generate_tone(frequency, duration=0.1, sample_rate=44100, volume = 0.2): def generate_tone(frequency, duration=0.1, sample_rate=44100, volume = 0.2):
t = np.linspace(0, duration, int(sample_rate * duration), False) t = np.linspace(0, duration, int(sample_rate * duration), False)
@ -178,23 +185,27 @@ def generate_tone(frequency, duration=0.1, sample_rate=44100, volume = 0.2):
def x_powerbar(): def x_powerbar():
clock = pygame.time.Clock() clock = pygame.time.Clock()
position = -50 position = -50 # Start from the leftmost position
direction = 1 direction = 1 # Move right initially
while True: while True:
frequency = 440 # A4 note frequency = 440 # A4 note
left_volume = (position + 50) / 100 left_volume = (50 - position) / 100
right_volume = (50 - position) / 100 right_volume = (position + 50) / 100
tone = generate_tone(frequency) tone = generate_tone(frequency)
channel = tone.play() channel = tone.play()
channel.set_volume(left_volume, right_volume) channel.set_volume(left_volume, right_volume)
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
channel.stop() channel.stop()
return position return position # This will return a value between -50 and 50
position += direction position += direction
if position >= 50 or position <= -50: if position > 50:
direction *= -1 position = 50
clock.tick(45) # Speed of bar direction = -1
elif position < -50:
position = -50
direction = 1
clock.tick(40) # Speed of bar
def y_powerbar(): def y_powerbar():
clock = pygame.time.Clock() clock = pygame.time.Clock()
@ -211,7 +222,8 @@ def y_powerbar():
power += direction power += direction
if power >= 100 or power <= 0: if power >= 100 or power <= 0:
direction *= -1 # Reverse direction at limits direction *= -1 # Reverse direction at limits
clock.tick(45) check_for_exit()
clock.tick(40)
def cut_scene(sounds, soundName): def cut_scene(sounds, soundName):
pygame.event.clear() pygame.event.clear()
@ -274,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():
@ -343,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: