Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # import pygame library - which allows sounds
- import pygame
- import play
- # Backdrop colour
- 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)
- # BUTTONS
- key_play_melody = play.new_box(color='light green', border_color='black', border_width=1, x=-100, y=-170, width=160, height=50)
- kpm = play.new_text(words='play melody', x=-100, y=-170, font_size=20)
- key_clear_melody = play.new_box(color='light yellow', border_color='black', border_width=1, x=100, y=-170, width=160, height=50)
- kcm = play.new_text(words='clear melody', x=100, y=-170, font_size=20)
- sound_clear_melody = pygame.mixer.Sound('clear_melody.wav') # DELETE THIS IF YOU DON'T HAVE THE FILE
- # Activate the for loop + lists
- keys = []
- sounds = []
- for i in range(8):
- key_x = -180 + i * 50
- key = play.new_box(color='white', border_color='black', border_width=3, x=key_x, y=0, width=40, height=100)
- sound = pygame.mixer.Sound(str(i+1)+'.ogg') # Adds the sound file: 1.ogg, 2.ogg, ...
- keys.append(key) # Adds keys to the keys list
- sounds.append(sound) # Adds sounds to the sound list
- melody = []
- # This is the intro jingle that plays at the start of the program ✨
- @play.when_program_starts
- def start():
- pygame.mixer_music.load('hello-1.mp3')
- pygame.mixer_music.play()
- # This is clearing the melody - delete all
- @key_clear_melody.when_clicked
- def clear():
- melody.clear()
- sound_clear_melody.play() # DELETE IF YOU DON'T HAVE IT
- # Processing clicks - when button is clicked, what happens?
- @key_play_melody.when_clicked
- async def play_m():
- for i in range(len(melody)):
- await play.timer(seconds=0.5)
- sounds[melody[i]].play()
- # Recording sounds - remembers the melody by adding it to the list
- @play.repeat_forever
- async def play_piano():
- for i in range(len(keys)):
- if keys[i].is_clicked:
- keys[i].color = 'light grey' # Changes to this
- sounds[i].play()
- await play.timer(seconds=0.3)
- keys[i].color = 'white' # Go back to starting colour
- melody.append(i)
- play.start_program()
Add Comment
Please, Sign In to add comment