Advertisement
shh_algo_PY

Coehn - Synthesizer - Fixed

Apr 8th, 2022
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.12 KB | None | 0 0
  1. import play
  2. import pygame
  3. play.set_backdrop('light blue')
  4.  
  5. text1 = play.new_text("Minecraft piano for fun!", x = -200, y = 250)
  6. text2 = play.new_text("Created in python!", x = -200, y = 200)
  7. text3 = play.new_text("Create your melody by pressing the keys!", x = 0, y = 150)
  8. piano = play.new_image("piano.png", x = 50, y = 225,size = 30)
  9. key_clear_melody = play.new_box(color='blue',border_color = 'gold', x = -200, y = -200, width = 200, height = 100)
  10. kcm = play.new_text(words='clear melody', x=-200, y=-200, font_size=45)
  11. key_play_melody = play.new_box(color='red', border_color = 'gold', x = 200, y = -200, width = 200, height = 100)
  12. kpm = play.new_text(words='play melody', x=200, y=-200, font_size=45)
  13. sound = pygame.mixer.Sound("clear_melody.wav")
  14. b_p = play.new_circle(x=-180, y=-125, radius=10)
  15. txt_p = play.new_text(words='piano', x=-145, y=-125, font_size=20)
  16.  
  17. b_g = play.new_circle(color='blue', x=-80, y=-125, radius=10)
  18. txt_g = play.new_text(words='guitar', x=-45, y=-125, font_size=20)
  19.  
  20. b_v = play.new_circle(color='blue', x=20, y=-125, radius=10)
  21. txt_v = play.new_text(words='violin', x=55, y=-125, font_size=20)
  22.  
  23. b_f = play.new_circle(color='blue', x=120, y=-125, radius=10)
  24. txt_f = play.new_text(words='flute', x=155, y=-125, font_size=20)
  25.  
  26. @play.repeat_forever
  27. def switch ():
  28.     for i in instrument_buttons:
  29.         if i.is_clicked:
  30.             global instrument
  31.             instrument = instrument_buttons.index (i)
  32.             for j in instrument_buttons:
  33.                 j.color = "blue"
  34.             i.color = "black"
  35.  
  36.  
  37. instrument = 0
  38. instrument_buttons = [b_p, b_g, b_v, b_f]
  39.  
  40. keys = []
  41. sounds = [[],[],[],[]]
  42.  
  43. for i in range(8):
  44.  
  45.     key_x = -350 + i * 100
  46.     key = play.new_box(color='white', border_color = 'black', border_width=3, x =key_x, y = 0, width=80, height=200)
  47.     keys.append(key)
  48.     p_sound = pygame.mixer.Sound ("pia" + str(i+1) +'.ogg')
  49.     g_sound = pygame.mixer.Sound ("git" + str(i+1) +'.ogg')
  50.     v_sound = pygame.mixer. Sound ("vio" + str(i+1) +'.ogg')
  51.     f_sound = pygame.mixer.Sound ("fl" + str(i+1) +'.ogg')
  52.     sounds [0].append (p_sound)
  53.     sounds [1].append (g_sound)
  54.     sounds [2].append (v_sound)
  55.     sounds [3].append (f_sound)
  56.    
  57. buttons = ["a", "s", "d", "f", "g", "h", "j", "k"]    
  58. image = []
  59. melody = []
  60.  
  61. i1 = play.new_image("creeper.png", x = -350, y = 0,size = 20)
  62. c = play.new_text("C", x = -350, y = -75)
  63. image.append(i1)
  64.  
  65. i2= play.new_image("skeleton.png", x = -250, y = 0,size = 15)
  66. image.append(i2)
  67. d = play.new_text("D", x = -250, y = -75)
  68.  
  69. i3= play.new_image("enderman.png", x = -150, y = 0,size = 25)
  70. image.append(i3)
  71. E = play.new_text("E", x = -150, y = -75)
  72.  
  73. i4= play.new_image("steve1.png", x = -50, y = 0,size = 45)
  74. image.append(i4)
  75. F = play.new_text("F", x = -50, y = -75)
  76.  
  77. i5= play.new_image("chicken1.png", x = 50, y = 0,size = 20)
  78. image.append(i5)
  79. G = play.new_text("G", x = 50, y = -75)
  80.  
  81. i6= play.new_image("spider.png", x = 150, y = 0,size = 8)
  82. image.append(i6)
  83. A = play.new_text("A", x = 150, y = -75)
  84.  
  85. i7= play.new_image("wither.png", x = 250, y = 0,size = 33)
  86. B = play.new_text("B", x = 250, y = -75)
  87. image.append(i7)
  88.  
  89. i8= play.new_image("dragon.png", x = 350, y = 0,size = 13)
  90. C = play.new_text("C", x = 350, y = -75)
  91. image.append(i8)
  92.  
  93. # You had this twice, one without a 'clear' command
  94. # This is why the recorded sound won't go away!
  95. @key_clear_melody.when_clicked
  96. def play_sound():
  97.     melody.clear()
  98.     sound.play()
  99.  
  100. @play.when_program_starts
  101. def start():
  102.     pygame.mixer_music.load("hello.mp3")
  103.     pygame.mixer_music.play()
  104.  
  105. @key_play_melody.when_clicked
  106. async def play_m():
  107.     for i in range(len(melody)):
  108.         await play.timer(seconds=0.5)
  109.         sounds[instrument][melody[i]].play()
  110.  
  111. @play.repeat_forever
  112. async def play_instrument():
  113.     for i in range(len(keys)):
  114.         if keys[i].is_clicked or play.key_is_pressed(buttons[i]):
  115.             keys[i].color = 'light grey'
  116.             sounds[instrument][i].play()
  117.             await play.timer(seconds=0.3)
  118.             keys[i].color = 'white' #alternately, "white"
  119.             melody.append(i)
  120.  
  121. play.start_program()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement