Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
- 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"
- keys = []
- sounds = [[], [], [], []]
- 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)
- 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)
- @play.when_program_starts
- def start():
- pygame.mixer_music.load('hi-1.mp3')
- pygame.mixer_music.play()
- melody = []
- @play.repeat_forever
- async def play_instrument():
- for i in range(len(keys)):
- if keys[i].is_clicked or play.key_is_pressed(buttons[i]):
- keys[i].color = 'light grey'
- sounds[instrument][i].play()
- await play.timer(seconds=0.1)
- keys[i].color = colors[i] #alternately, "white"
- melody.append(i)
- @p.when_clicked
- async def play_m():
- for i in range(len(melody)):
- await play.timer(seconds=0.5)
- sounds[instrument][melody[i]].play()
- @c.when_clicked
- def clear():
- melody.clear()
- play.start_program()
Add Comment
Please, Sign In to add comment