Advertisement
shh_algo_PY

Colourful Piano

Mar 19th, 2022 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.02 KB | None | 0 0
  1. # This script is for a piano with rainbow keys!
  2. # It will have a light blue backdrop
  3. # You can select the keys by using clicks or your keyboard
  4. # p stands for play, c stands for clear
  5.  
  6. import play
  7. import pygame
  8.  
  9. play.set_backdrop('light blue')
  10. introduce1 = play.new_text(words='Piano for fun!', x=0, y=200)
  11. introduce2 = play.new_text(words='Create your melody by pressing the keys', x=0, y=150)
  12.  
  13. p = play.new_box(color='green', x=-100, y=-170, width=160, height=50)
  14. p_text = play.new_text(words='play melody', x=-100, y=-170, font_size=20)
  15.  
  16. c = play.new_box(color='yellow',  x=100, y=-170, width=160, height=50)
  17. c_text = play.new_text(words='clear melody', x=100, y=-170, font_size=20)
  18.  
  19. c_sound = pygame.mixer.Sound('clear_melody.wav')
  20.  
  21. keys = []
  22. sounds = []
  23.  
  24. # ADD LISTS TO GROUP COLOURS AND BUTTONS
  25. colors = ["red", "orange", "yellow", "green", "dark green", "blue", "indigo", "purple"]
  26. buttons = ["a", "s", "d", "f", "g", "h", "j", "k"]
  27.  
  28. for i in range(8):
  29.     key_x = -180 + i * 50
  30.     key = play.new_box(color=colors[i], x=key_x, width=40, height=100)
  31.     keys.append(key)
  32.     sound = pygame.mixer.Sound(str(i+1)+'.ogg')
  33.     sounds.append(sound)
  34.  
  35. @play.when_program_starts
  36. def start():
  37.     pygame.mixer_music.load('hello-1.mp3')
  38.     pygame.mixer_music.play()
  39.  
  40. melody = []
  41.  
  42. # When the piano keys are pressed, it goes grey and then back to the original colour
  43.  
  44. @play.repeat_forever
  45. async def play_piano():
  46.     for i in range(len(keys)):
  47.         if keys[i].is_clicked or play.key_is_pressed(buttons[i]): # Add or play.key_is_pressed() if buttons added
  48.             keys[i].color = 'light grey'
  49.             sounds[i].play()
  50.             await play.timer(seconds=0.1)
  51.             keys[i].color = colors[i] # alternately, white or the colour you want
  52.             melody.append(i)
  53.  
  54. @p.when_clicked
  55. async def play_m():
  56.     for i in range(len(melody)):
  57.         await play.timer(seconds=0.5)
  58.         sounds[melody[i]].play()
  59.  
  60. @c.when_clicked
  61. def clear():
  62.     melody.clear()
  63.     c_sound.play()
  64.  
  65. play.start_program()
  66.  
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement