shh_algo_PY

Caden - Synthesizer - Fixed

Apr 8th, 2022
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.17 KB | None | 0 0
  1. import pygame
  2. import play
  3.  
  4.  
  5. # Backdrop colour
  6. play.set_backdrop('light blue')
  7.  
  8. piano = play.new_image("haha.png", x = 0, y = -200, size = 60)
  9.  
  10. introduce1 = play.new_text(words='Piano for fun!', x=0, y=200)
  11.  
  12. introduce2 = play.new_text(words='Create your melody by pressing the keys', x=0, y=150)
  13.  
  14. # BUTTONS
  15. key_play_melody = play.new_box(color='light green', border_color='black', border_width=1, x=-100, y=-170, width=160, height=50)
  16.  
  17. kpm = play.new_text(words='play melody', x=-100, y=-170, font_size=20)
  18.  
  19. key_clear_melody = play.new_box(color='light yellow', border_color='black', border_width=1, x=100, y=-170, width=160, height=50)
  20.  
  21. kcm = play.new_text(words='clear melody', x=100, y=-170, font_size=20)
  22.  
  23. b_p = play.new_circle(x=-180, y=-100, radius=10)
  24. txt_p = play.new_text(words='piano', x=-145, y=-100, font_size=20)
  25.  
  26. b_g = play.new_circle(color='blue', x=-80, y=-100, radius=10)
  27. txt_g = play.new_text(words='guitar', x=-45, y=-100, font_size=20)
  28.  
  29. b_v = play.new_circle(color='blue', x=20, y=-100, radius=10)
  30. txt_v = play.new_text(words='violin', x=55, y=-100, font_size=20)
  31.  
  32. b_f = play.new_circle(color='blue', x=120, y=-100, radius=10)
  33. txt_f = play.new_text(words='flute', x=155, y=-100, font_size=20)
  34.  
  35. instrument = 0
  36.  
  37. instrument_buttons = [b_p, b_g, b_v, b_f]
  38.  
  39. @play.repeat_forever
  40. def switch():
  41.     for i in instrument_buttons:
  42.         if i.is_clicked:
  43.             global instrument
  44.             instrument = instrument_buttons.index(i)
  45.             for j in instrument_buttons:
  46.                 j.color = "blue"
  47.             i.color = "black"
  48.  
  49. keys = []
  50. sounds = [ [], [], [], []]
  51.  
  52. for i in range(8):
  53.     key_x = -180 + i * 50
  54.     key = play.new_box(color='white', border_color='black', border_width=3, x=key_x, y=0, width=40, height=100)
  55.     keys.append(key)     # Adds keys to the keys list
  56.     p_sound = pygame.mixer.Sound ("pia" + str(i+1) +'.ogg')
  57.     g_sound = pygame.mixer.Sound ("git" + str(i+1) +'.ogg')
  58.     v_sound = pygame.mixer.Sound ("vio" + str(i+1) +'.ogg')
  59.     f_sound = pygame.mixer.Sound ("fl" + str(i+1) +'.ogg')
  60.     sounds [0].append (p_sound)
  61.     sounds [1]. append (g_sound)
  62.     sounds [2]. append (v_sound)
  63.     sounds [3]. append (f_sound)
  64.  
  65. melody = []
  66.  
  67. # This is the intro jingle that plays at the start of the program ✨
  68.  
  69. @play.when_program_starts
  70. def start():
  71.     pygame.mixer_music.load('hi-1.mp3')
  72.     pygame.mixer_music.play()
  73.  
  74. # This is clearing the melody - delete all
  75.        
  76. @key_clear_melody.when_clicked
  77. def clear():
  78.     melody.clear()
  79.  
  80. # Processing clicks - when button is clicked, what happens?
  81.  
  82. @key_play_melody.when_clicked
  83. async def play_m():
  84.     for i in range(len(melody)):
  85.         await play.timer(seconds=0.5)
  86.         sounds[instrument][melody[i]].play()
  87.  
  88. # Recording sounds - remembers the melody by adding it to the list
  89.  
  90. @play.repeat_forever
  91. async def play_instrument():
  92.     for i in range(len(keys)):
  93.         if keys[i].is_clicked:
  94.             keys[i].color = 'light grey'
  95.             sounds[instrument][i].play()
  96.             await play.timer(seconds=0.1)
  97.             keys[i].color = 'white' #alternately, "white"
  98.             melody.append(i)
  99.  
  100.  
  101. play.start_program()
Advertisement
Add Comment
Please, Sign In to add comment