Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This script is for a piano with rainbow keys!
- # It will have a light blue backdrop
- # You can select the keys by using clicks or your keyboard
- # p stands for play, c stands for clear
- import play
- import pygame
- play.set_backdrop('light blue')
- introduce1 = play.new_text(words='Piano for fun!', x=0, y=200)
- introduce2 = play.new_text(words='Create your melody by pressing the keys', x=0, y=150)
- p = play.new_box(color='green', x=-100, y=-170, width=160, height=50)
- p_text = play.new_text(words='play melody', x=-100, y=-170, font_size=20)
- c = play.new_box(color='yellow', x=100, y=-170, width=160, height=50)
- c_text = play.new_text(words='clear melody', x=100, y=-170, font_size=20)
- c_sound = pygame.mixer.Sound('clear_melody.wav')
- keys = []
- sounds = []
- # ADD LISTS TO GROUP COLOURS AND BUTTONS
- colors = ["red", "orange", "yellow", "green", "dark green", "blue", "indigo", "purple"]
- buttons = ["a", "s", "d", "f", "g", "h", "j", "k"]
- for i in range(8):
- key_x = -180 + i * 50
- key = play.new_box(color=colors[i], x=key_x, width=40, height=100)
- keys.append(key)
- sound = pygame.mixer.Sound(str(i+1)+'.ogg')
- sounds.append(sound)
- @play.when_program_starts
- def start():
- pygame.mixer_music.load('hello-1.mp3')
- pygame.mixer_music.play()
- melody = []
- # When the piano keys are pressed, it goes grey and then back to the original colour
- @play.repeat_forever
- async def play_piano():
- for i in range(len(keys)):
- if keys[i].is_clicked or play.key_is_pressed(buttons[i]): # Add or play.key_is_pressed() if buttons added
- keys[i].color = 'light grey'
- sounds[i].play()
- await play.timer(seconds=0.1)
- keys[i].color = colors[i] # alternately, white or the colour you want
- melody.append(i)
- @p.when_clicked
- async def play_m():
- for i in range(len(melody)):
- await play.timer(seconds=0.5)
- sounds[melody[i]].play()
- @c.when_clicked
- def clear():
- melody.clear()
- c_sound.play()
- play.start_program()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement