Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import play
- # Backdrop colour
- bk = play.new_image(image = 'vsbk.png', size = 175, x = 0, y = 0)
- play.set_backdrop('light sky blue')
- introduce2 = play.new_text(words='Press to play!', x=0, y=190, color = 'medium orchid')
- cat = play.new_image(image = 'hello.png', x = 0, y = -200)
- # 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)
- b_p = play.new_circle(x=-180, y=-100, radius=10)
- txt_p = play.new_text(words='piano', x=-145, y=-100, font_size=20)
- b_g = play.new_circle(color='blue', x=-80, y=-100, radius=10)
- txt_g = play.new_text(words='guitar', x=-45, y=-100, font_size=20)
- b_v = play.new_circle(color='blue', x=20, y=-100, radius=10)
- txt_v = play.new_text(words='violin', x=55, y=-100, font_size=20)
- b_f = play.new_circle(color='blue', x=120, y=-100, radius=10)
- txt_f = play.new_text(words='flute', x=155, y=-100, font_size=20)
- instrument = 0
- instrument_buttons = [b_p, b_g, b_v, b_f]
- @play.repeat_forever
- def switch():
- for i in instrument_buttons:
- if i.is_clicked:
- global instrument
- instrument = instrument_buttons.index(i)
- for j in instrument_buttons:
- j.color = "blue"
- i.color = "black"
- # 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='white', border_width=5, x=key_x, y=20, width=40, height=100)
- keys.append(key) # Adds keys to the keys list
- p_sound = pygame.mixer.Sound("pia" + str(i+1)+ '.ogg')
- g_sound = pygame.mixer.Sound("git" + str(i+1)+ '.ogg')
- v_sound = pygame.mixer.Sound("vio" + str(i+1)+ '.ogg')
- f_sound = pygame.mixer.Sound("fl" + str(i+1)+ '.ogg')
- sounds[0].append(p_sound)
- sounds[1].append(g_sound)
- sounds[2].append(v_sound)
- sounds[3].append(f_sound)
- melody = []
- # This is the intro jingle that plays at the start of the program ✨
- @play.when_program_starts
- def start():
- pygame.mixer_music.load('hi-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()
- # 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[instrument][melody[i]].play()
- # Recording sounds - remembers the melody by adding it to the list
- @play.repeat_forever
- async def play_instrument():
- for i in range(len(keys)):
- if keys[i].is_clicked:
- keys[i].color = 'light grey' # Changes to this
- sounds[instrument][i].play()
- await play.timer(seconds=0.3)
- keys[i].color = 'white' # Go back to starting colour
- melody.append(i)
- play.start_program()
Advertisement
Add Comment
Please, Sign In to add comment