shh_algo_PY

Xin Yen - Synthesizer - Fixed

Apr 8th, 2022
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.27 KB | None | 0 0
  1. import pygame
  2. import play
  3.  
  4. # Backdrop colour
  5. bk = play.new_image(image = 'vsbk.png', size = 175, x = 0, y = 0)
  6.  
  7. play.set_backdrop('light sky blue')
  8.  
  9.  
  10. introduce2 = play.new_text(words='Press to play!', x=0, y=190, color = 'medium orchid')
  11.  
  12. cat = play.new_image(image = 'hello.png', x = 0, y = -200)
  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.  
  25. txt_p = play.new_text(words='piano', x=-145, y=-100, font_size=20)
  26.  
  27. b_g = play.new_circle(color='blue', x=-80, y=-100, radius=10)
  28.  
  29. txt_g = play.new_text(words='guitar', x=-45, y=-100, font_size=20)
  30.  
  31. b_v = play.new_circle(color='blue', x=20, y=-100, radius=10)
  32.  
  33. txt_v = play.new_text(words='violin', x=55, y=-100, font_size=20)
  34.  
  35. b_f = play.new_circle(color='blue', x=120, y=-100, radius=10)
  36.  
  37. txt_f = play.new_text(words='flute', x=155, y=-100, font_size=20)
  38.  
  39. instrument = 0
  40. instrument_buttons = [b_p, b_g, b_v, b_f]
  41.  
  42. @play.repeat_forever
  43. def switch():
  44.     for i in instrument_buttons:
  45.         if i.is_clicked:
  46.             global instrument
  47.             instrument = instrument_buttons.index(i)
  48.             for j in instrument_buttons:
  49.                 j.color = "blue"
  50.             i.color = "black"
  51.  
  52.  
  53. # Activate the for loop + lists
  54.  
  55. keys = []
  56. sounds = [[], [], [], []]
  57.  
  58. for i in range(8):
  59.     key_x = -180 + i * 50
  60.     key = play.new_box(color='white', border_color='white', border_width=5, x=key_x, y=20, width=40, height=100)
  61.     keys.append(key)     # Adds keys to the keys list
  62.     p_sound = pygame.mixer.Sound("pia" + str(i+1)+ '.ogg')
  63.     g_sound = pygame.mixer.Sound("git" + str(i+1)+ '.ogg')
  64.     v_sound = pygame.mixer.Sound("vio" + str(i+1)+ '.ogg')
  65.     f_sound = pygame.mixer.Sound("fl" + str(i+1)+ '.ogg')
  66.  
  67.     sounds[0].append(p_sound)
  68.     sounds[1].append(g_sound)
  69.     sounds[2].append(v_sound)
  70.     sounds[3].append(f_sound)
  71.  
  72.  
  73.  
  74. melody = []
  75.  
  76. # This is the intro jingle that plays at the start of the program ✨
  77.  
  78. @play.when_program_starts
  79. def start():
  80.     pygame.mixer_music.load('hi-1.mp3')
  81.     pygame.mixer_music.play()
  82.  
  83. # This is clearing the melody - delete all
  84.        
  85. @key_clear_melody.when_clicked
  86. def clear():
  87.     melody.clear()
  88.     sound_clear_melody.play()
  89.  
  90. # Processing clicks - when button is clicked, what happens?
  91.  
  92. @key_play_melody.when_clicked
  93. async def play_m():
  94.     for i in range(len(melody)):
  95.         await play.timer(seconds=0.5)
  96.         sounds[instrument][melody[i]].play()
  97.  
  98. # Recording sounds - remembers the melody by adding it to the list
  99.  
  100. @play.repeat_forever
  101. async def play_instrument():
  102.     for i in range(len(keys)):
  103.         if keys[i].is_clicked:
  104.             keys[i].color = 'light grey'    # Changes to this
  105.             sounds[instrument][i].play()
  106.             await play.timer(seconds=0.3)
  107.             keys[i].color = 'white'         # Go back to starting colour
  108.             melody.append(i)
  109.  
  110. play.start_program()
Advertisement
Add Comment
Please, Sign In to add comment