Commit 0e4dba85 authored by santiago duque's avatar santiago duque

now working with pygame keyboard

parent b0e4be43
import os
from os.path import isfile, join
import pygame
import time
import signal
import keyboard
from storage_media import USBDeviceHandler
# from storage_media import USBDeviceHandler
# import time
# import signal
# import keyboard
pygame.init()
# window = pygame.display.set_mode((300,300))
clock = pygame.time.Clock()
dir_path = os.getcwd()
sound_dir = os.getcwd() + "/sounds"
......@@ -14,7 +20,9 @@ cut_current_sound = False
channel = False
current_sound_index = -1
usb_handler = USBDeviceHandler()
contentlist = sorted(contentlist)
# usb_handler = USBDeviceHandler()
def setup_sounds():
for content in contentlist:
......@@ -22,6 +30,7 @@ def setup_sounds():
if content.endswith(".mp3") or content.endswith(".wav"):
sound = pygame.mixer.Sound(join(sound_dir, content))
sound_array.append(sound)
print(content)
print(sound_array)
......@@ -36,48 +45,49 @@ def play_sound(index):
while channel.get_busy():
pygame.time.wait(100) # ms
print("Playing...")
pygame.event.clear()
print("Finished.")
""" todo
make sounds stop when new key pressed
else:
if current_sound_index == index:
current_sound_index = index
channel = sound_array[current_sound_index].play()
while channel.get_busy():
pygame.time.wait(100) # ms
print("Playing...")
print("Playing...pygame.init()")
print("Finished.")
else:
print("STOP.")
"""
pygame.init()
setup_sounds()
run = True
while run:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
while True:
try: # used try so that if user pressed other than the given key error will not be shown
if keyboard.is_pressed("q"):
print("You Pressed A Key!")
if event.type == pygame.KEYDOWN:
print("you pressed " + pygame.key.name(event.key))
if event.key == pygame.K_q:
play_sound(0)
elif keyboard.is_pressed("w"):
print("You Pressed A Key!")
elif event.key == pygame.K_w:
play_sound(1)
elif keyboard.is_pressed("e"):
print("You Pressed A Key!")
elif event.key == pygame.K_e:
play_sound(2)
elif keyboard.is_pressed("r"):
print("You Pressed A Key!")
elif event.key == pygame.K_r:
play_sound(3)
except:
break # if user pressed a key other than the given key the loop will break
pygame.quit()
exit()
# time.sleep(5.5)
# signal.pause()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment