def pysocket(): global lmusic global server_socket mnow = "" lnow = [] pygame.init() clock = pygame.time.Clock() fps = 60 size = [480, 410] bg = [250, 250, 162] x1 = 80 xsize = 50 y = 80 ysize = 50 x2 = 260 note = Music("") nowb = "" screen = pygame.display.set_mode(size) pygame.display.set_caption("The main player") button_1a = pygame.Rect(x1, y, xsize, ysize) button_1l = pygame.Rect(x1 + 50, y, xsize + 40, ysize) button_2a = pygame.Rect(x1, y + 70, xsize, ysize) button_2l = pygame.Rect(x1 + xsize, y + 70, xsize + 40, ysize) button_3a = pygame.Rect(x1, y + 140, xsize, ysize) button_3l = pygame.Rect(x1 + xsize, y + 140, xsize + 40, ysize) button_4a = pygame.Rect(x1, y + 210, xsize, ysize) button_4l = pygame.Rect(x1 + xsize, y + 210, xsize + 40, ysize) button_5a = pygame.Rect(x2, y, xsize, ysize) button_5l = pygame.Rect(x2 + 50, y, xsize + 40, ysize) button_6a = pygame.Rect(x2, y + 70, xsize, ysize) button_6l = pygame.Rect(x2 + 50, y + 70, xsize + 40, ysize) button_7a = pygame.Rect(x2, y + 140, xsize, ysize) button_7l = pygame.Rect(x2 + 50, y + 140, xsize + 40, ysize) button_8a = pygame.Rect(x2, y + 210, xsize, ysize) button_8l = pygame.Rect(x2 + 50, y + 210, xsize + 40, ysize) button_melody = pygame.Rect(400, 10, 2 * xsize - 20, ysize) button_file_selection = pygame.Rect(320, 10, 2 * xsize - 20, ysize) button_revers = pygame.Rect(240, 10, 2 * xsize - 20, ysize) button_save = pygame.Rect(160, 10, 2 * xsize - 20, ysize) button_clear = pygame.Rect(80, 10, 2 * xsize - 20, ysize) button_delete_note = pygame.Rect(0, 10, 2 * xsize - 20, ysize) button_your_notes = pygame.Rect(320, 350, 2 * xsize - 20, ysize) button_enter = pygame.Rect(400, 350, 2 * xsize - 20, ysize) button_clear_sections = pygame.Rect(240, 350, 2 * xsize - 20, ysize) button_random = pygame.Rect(160, 350, 2 * xsize - 20, ysize) button_delete_last_added = pygame.Rect(80, 350, 2 * xsize - 20, ysize) button_open_piano = pygame.Rect(0, 350, 2 * xsize - 20, ysize) font = pygame.font.SysFont('Lucida Handwriting', 20) font2 = pygame.font.SysFont('Parchment', 62) font3 = pygame.font.SysFont('Parchment', 72) font4 = pygame.font.SysFont('Parchment', 56) font5 = pygame.font.SysFont('Parchment', 42) font6 = pygame.font.SysFont('Bauhaus 93', 32) textx = x1 - 15 texty = 100 colortext = [0, 0, 255] colortext2 = [0, 0, 53] colortext3 = [117, 249, 134] text1 = font.render('1)', True, colortext) textRect1 = text1.get_rect() textRect1.center = (textx, texty) text2 = font.render('2)', True, colortext) textRect2 = text2.get_rect() textRect2.center = (textx, texty + 70) text3 = font.render('3)', True, colortext) textRect3 = text3.get_rect() textRect3.center = (textx, texty + 140) text4 = font.render('4)', True, colortext) textRect4 = text4.get_rect() textRect4.center = (textx, texty + 210) text5 = font.render('(5', True, colortext) textRect5 = text5.get_rect() textRect5.center = (textx + 345, texty) text6 = font.render('(6', True, colortext) textRect6 = text6.get_rect() textRect6.center = (textx + 345, texty + 70) text7 = font.render('(7', True, colortext) textRect7 = text7.get_rect() textRect7.center = (textx + 345, texty + 140) text8 = font.render('(8', True, colortext) textRect8 = text8.get_rect() textRect8.center = (textx + 345, texty + 210) text9 = font.render('add', True, colortext3) textRect91 = text9.get_rect() textRect91.center = (x1 + 25, y + 25) textRect92 = text9.get_rect() textRect92.center = (x1 + 25, y + 95) textRect93 = text9.get_rect() textRect93.center = (x1 + 25, y + 165) textRect94 = text9.get_rect() textRect94.center = (x1 + 25, y + 235) textRect95 = text9.get_rect() textRect95.center = (x2 + 25, y + 25) textRect96 = text9.get_rect() textRect96.center = (x2 + 25, y + 95) textRect97 = text9.get_rect() textRect97.center = (x2 + 25, y + 165) textRect98 = text9.get_rect() textRect98.center = (x2 + 25, y + 235) text10 = font.render('listen', True, colortext2) textRect101 = text10.get_rect() textRect101.center = (x1 + 95, y + 25) textRect102 = text10.get_rect() textRect102.center = (x1 + 95, y + 95) textRect103 = text10.get_rect() textRect103.center = (x1 + 95, y + 165) textRect104 = text10.get_rect() textRect104.center = (x1 + 95, y + 235) textRect105 = text10.get_rect() textRect105.center = (x2 + 95, y + 25) textRect106 = text10.get_rect() textRect106.center = (x2 + 95, y + 95) textRect107 = text10.get_rect() textRect107.center = (x2 + 95, y + 165) textRect108 = text10.get_rect() textRect108.center = (x2 + 95, y + 235) text11 = font3.render('play', True, [255, 255, 255]) textRect11 = text11.get_rect() textRect11.center = (440, 30) text12 = font3.render('revers', True, [255, 255, 255]) textRect12 = text12.get_rect() textRect12.center = (280, 30) text13 = font3.render('save', True, [255, 255, 255]) textRect13 = text13.get_rect() textRect13.center = (200, 30) text14 = font3.render('clear', True, [255, 255, 255]) textRect14 = text14.get_rect() textRect14.center = (120, 30) text151 = font2.render('delete', True, [255, 255, 255]) text152 = font2.render('note', True, [255, 255, 255]) textRect151 = text151.get_rect() textRect151.center = (40, 23) textRect152 = text152.get_rect() textRect152.center = (40, 45) text16 = font3.render('enter', True, [255, 255, 255]) textRect16 = text16.get_rect() textRect16.center = (440, 370) text171 = font2.render('clear', True, [255, 255, 255]) text172 = font4.render('sections', True, [255, 255, 255]) textRect171 = text171.get_rect() textRect171.center = (280, 360) textRect172 = text172.get_rect() textRect172.center = (280, 385) text18 = font4.render('random', True, [255, 255, 255]) textRect18 = text18.get_rect() textRect18.center = (200, 370) text191 = font2.render('delete', True, [255, 255, 255]) text192 = font5.render('last added', True, [255, 255, 255]) textRect191 = text191.get_rect() textRect191.center = (120, 362) textRect192 = text192.get_rect() textRect192.center = (120, 388) text201 = font2.render('open', True, [255, 255, 255]) text202 = font3.render('piano', True, [255, 255, 255]) textRect201 = text201.get_rect() textRect201.center = (40, 355) textRect202 = text202.get_rect() textRect202.center = (40, 375) text21 = font3.render('select', True, [255, 255, 255]) textRect21 = text21.get_rect() textRect21.center = (360, 30) text222 = font3.render('your', True, [255, 255, 255]) text221 = font3.render('notes', True, [255, 255, 255]) textRect221 = text221.get_rect() textRect221.center = (352, 355) textRect222 = text222.get_rect() textRect222.center = (363, 380) # creates a rect object # The rect method is similar to a list but with a few added perks # for example if you want the position of the button you can simpy type # button.x or button.y or if you want size you can type button.width or # height. you can also get the top, left, right and bottom of an object # with button.right, left, top, and bottom while True: for event in pygame.event.get(): if note.bool == True: nowb = "" if event.type == pygame.QUIT: lmusic = ["False"] server_socket.close() return False if event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = event.pos # gets mouse position # checks if mouse position is over the button list1 = [] for k in lnow: str2 = "" klist = k.split() for j in klist: if not (j != "do" and j != "re" and j != "mi" and j != "fa" and j != "sol" and j != "la" and j != "ci" and j != "do2" and j != "#c" and j != "#d" and j != "#f" and j != "#g" and j != "#a"): str2 += " " + j if str2.replace(" ", "") != "": list1.append(str2) lnow = list1 if button_1l.collidepoint(mouse_pos): if (len(lmusic) >= 1): if nowb == "l1" and note.bool == False: note.bre = False print(note.bre) elif note.bool == True: note = Music(lmusic[0]) t2 = threading.Thread(target=note.play_music) t2.start() nowb = "l1" else: print("1l") elif button_1a.collidepoint(mouse_pos): if (len(lmusic) >= 1): lnow.append(lmusic[0]) #note.play_click() else: print("1a") elif button_2l.collidepoint(mouse_pos): if (len(lmusic) >= 2): if nowb == "l2" and note.bool == False: note.bre = False print(note.bre) elif note.bool == True: note = Music(lmusic[1]) t2 = threading.Thread(target=note.play_music) t2.start() nowb = "l2" else: print("21") elif button_2a.collidepoint(mouse_pos): if (len(lmusic) >= 2): lnow.append(lmusic[1]) #note.play_click() else: print("2a") elif button_3l.collidepoint(mouse_pos): if (len(lmusic) >= 3): if nowb == "l3" and note.bool == False: note.bre = False print(note.bre) elif note.bool == True: note = Music(lmusic[2]) t2 = threading.Thread(target=note.play_music) t2.start() nowb = "l3" else: print("31") elif button_3a.collidepoint(mouse_pos): if (len(lmusic) >= 3): lnow.append(lmusic[2]) #note.play_click() else: print("3a") elif button_4l.collidepoint(mouse_pos): if (len(lmusic) >= 4): if nowb == "l4" and note.bool == False: note.bre = False print(note.bre) elif note.bool == True: note = Music(lmusic[3]) t2 = threading.Thread(target=note.play_music) t2.start() nowb = "l4" else: print("41") elif button_4a.collidepoint(mouse_pos): if (len(lmusic) >= 4): lnow.append(lmusic[3]) #note.play_click() else: print("4a") elif button_5l.collidepoint(mouse_pos): if (len(lmusic) >= 5): if nowb == "l5" and note.bool == False: note.bre = False print(note.bre) elif note.bool == True: note = Music(lmusic[4]) t2 = threading.Thread(target=note.play_music) t2.start() nowb = "l5" else: print("51") elif button_5a.collidepoint(mouse_pos): if (len(lmusic) >= 5): lnow.append(lmusic[4]) #note.play_click() else: print("5a") elif button_6l.collidepoint(mouse_pos): if (len(lmusic) >= 6): if nowb == "l6" and note.bool == False: note.bre = False print(note.bre) elif note.bool == True: note = Music(lmusic[5]) t2 = threading.Thread(target=note.play_music) t2.start() nowb = "l6" else: print("61") elif button_6a.collidepoint(mouse_pos): if (len(lmusic) >= 6): lnow.append(lmusic[5]) #note.play_click() else: print("6a") elif button_7l.collidepoint(mouse_pos): if (len(lmusic) >= 7): if nowb == "l7" and note.bool == False: note.bre = False print(note.bre) elif note.bool == True: note = Music(lmusic[6]) t2 = threading.Thread(target=note.play_music) t2.start() nowb = "l7" else: print("71") elif button_7a.collidepoint(mouse_pos): if (len(lmusic) >= 7): lnow.append(lmusic[6]) #note.play_click() else: print("7a") elif button_8l.collidepoint(mouse_pos): if (len(lmusic) >= 8): if nowb == "l8" and note.bool == False: note.bre = False print(note.bre) elif note.bool == True: note = Music(lmusic[7]) t2 = threading.Thread(target=note.play_music) t2.start() nowb = "l8" else: print("81") elif button_8a.collidepoint(mouse_pos): if (len(lmusic) >= 8): lnow.append(lmusic[7]) #note.play_click() else: print("8a") elif button_melody.collidepoint(mouse_pos): if nowb == "play" and note.bool == False: note.bre = False elif note.bool == True: note = Music( str(lnow).replace("[", "").replace("]", "").replace( "'", "").replace(",", "")) t2 = threading.Thread(target=note.play_music) t2.start() nowb = "play" elif note.bool == False: break elif button_revers.collidepoint(mouse_pos): lnow2 = [] for i in lnow: lnow2.append( str(i.split()[::-1]).replace("[", "").replace( "]", "").replace("'", "").replace(",", "")) lnow = lnow2[::-1] note = Music( str(lnow).replace("[", "").replace("]", "").replace( "'", "").replace(",", "")) #note.play_click() elif button_save.collidepoint(mouse_pos): root2 = tk.Tk() root2.withdraw( ) # we don't want a full GUI, so keep the root window from appearing pygame.quit() filename = askopenfilename( ) # show an "Open" dialog box and return the path to the selected file screen = pygame.display.set_mode(size) root2.destroy() print(filename) if not (str(filename) == "" or str(filename)[::-1][0:4][::-1] != ".txt"): input_file = open(r'' + filename, 'a') input_file.write((str(lnow).replace("[", "").replace( "]", "").replace("'", "").replace(",", "").replace( " ", " ")) + '\n') input_file.close() print("save") #note.play_click() elif button_clear.collidepoint(mouse_pos): lnow = [] #note.play_click() elif button_delete_note.collidepoint(mouse_pos): print(lnow) if (len(lnow) > 0): lnow[-1] = str(lnow[-1]).split() del (lnow[-1])[-1] lnow[-1] = str(lnow[-1]).replace("[", "").replace( "]", "").replace("'", "").replace(",", "") print(lnow) #note.play_click() elif button_enter.collidepoint(mouse_pos): ROOT = tk.Tk() ROOT.withdraw() pygame.quit() enter_notes = simpledialog.askstring(title="notes", prompt="enter notes") ROOT.destroy() screen = pygame.display.set_mode(size) if str(enter_notes) != "None": print(str(enter_notes)) lnow.append(enter_notes.lower()) print("enter") elif button_clear_sections.collidepoint(mouse_pos): n = 0 while len(lmusic) != 0 and n < 8: n += 1 del lmusic[0] print("clear sections") elif button_random.collidepoint(mouse_pos): lnow2 = [] if lnow != []: nrandom = random.randint(0, len(lnow) - 1) while lnow != []: lnow2.append(lnow.pop(nrandom)) if (len(lnow) != 0): nrandom = random.randint(0, len(lnow) - 1) lnow = lnow2 #note.play_click() print("random") elif button_delete_last_added.collidepoint(mouse_pos): if len(lnow) != 0: del (lnow)[-1] #note.play_click() print("delete last added") elif button_open_piano.collidepoint(mouse_pos): pygame.quit() exec(open("piano clinet.py").read()) screen = pygame.display.set_mode(size) print("open piano") elif button_file_selection.collidepoint(mouse_pos): root3 = tk.Tk() root3.withdraw( ) # we don't want a full GUI, so keep the root window from appearing pygame.quit() filename = askopenfilename( ) # show an "Open" dialog box and return the path to the selected file screen = pygame.display.set_mode(size) root3.destroy() print(filename) string = "" if not (str(filename) == "" or str(filename)[::-1][0:4][::-1] != ".txt"): input_file = open(r'' + filename, 'r') notes = input_file.read().replace('\n', " ").split() for j in notes: if not (j != "do" and j != "re" and j != "mi" and j != "fa" and j != "sol" and j != "la" and j != "ci" and j != "do2" and j != "#c" and j != "#d" and j != "#f" and j != "#g" and j != "#a"): string += " " + j input_file.close() lnow.append(string) elif button_your_notes.collidepoint(mouse_pos): root = tk.Tk() pygame.quit() S = tk.Scrollbar(root) T = tk.Text(root, height=4, width=50) S.pack(side=tk.RIGHT, fill=tk.Y) T.pack(side=tk.LEFT, fill=tk.Y) S.config(command=T.yview) T.config(yscrollcommand=S.set) T.insert( tk.END, str(lnow).replace("]", '').replace("[", "").replace( "'", "").replace(", ", " ")) tk.mainloop() screen = pygame.display.set_mode(size) print("your_notes") print(lnow) screen.fill(bg) colorb1 = [17, 6, 70] colorb2 = [133, 252, 113] pygame.draw.rect(screen, colorb1, button_1a) # draw button pygame.draw.rect(screen, colorb2, button_1l) # draw button pygame.draw.rect(screen, colorb1, button_2a) # draw button pygame.draw.rect(screen, colorb2, button_2l) # draw button pygame.draw.rect(screen, colorb1, button_3a) # draw button pygame.draw.rect(screen, colorb2, button_3l) # draw button pygame.draw.rect(screen, colorb1, button_4a) # draw button pygame.draw.rect(screen, colorb2, button_4l) # draw button pygame.draw.rect(screen, colorb1, button_5a) # draw button pygame.draw.rect(screen, colorb2, button_5l) # draw button pygame.draw.rect(screen, colorb1, button_6a) # draw button pygame.draw.rect(screen, colorb2, button_6l) # draw button pygame.draw.rect(screen, colorb1, button_7a) # draw button pygame.draw.rect(screen, colorb2, button_7l) # draw button pygame.draw.rect(screen, colorb1, button_8a) # draw button pygame.draw.rect(screen, colorb2, button_8l) # draw button pygame.draw.rect(screen, [0, 0, 0], button_melody) # draw button pygame.draw.rect(screen, [40, 40, 40], button_file_selection) # draw button pygame.draw.rect(screen, [80, 80, 80], button_revers) # draw button pygame.draw.rect(screen, [130, 130, 130], button_save) # draw button pygame.draw.rect(screen, [170, 170, 170], button_clear) # draw button pygame.draw.rect(screen, [200, 200, 200], button_delete_note) # draw button pygame.draw.rect(screen, [0, 0, 0], button_enter) # draw button pygame.draw.rect(screen, [40, 40, 40], button_your_notes) # draw button pygame.draw.rect(screen, [80, 80, 80], button_clear_sections) # draw button pygame.draw.rect(screen, [130, 130, 130], button_random) # draw button pygame.draw.rect(screen, [170, 170, 170], button_delete_last_added) # draw button pygame.draw.rect(screen, [200, 200, 200], button_open_piano) # draw button screen.blit(text1, textRect1) screen.blit(text2, textRect2) screen.blit(text3, textRect3) screen.blit(text4, textRect4) screen.blit(text5, textRect5) screen.blit(text6, textRect6) screen.blit(text7, textRect7) screen.blit(text8, textRect8) screen.blit(text9, textRect91) screen.blit(text9, textRect92) screen.blit(text9, textRect93) screen.blit(text9, textRect94) screen.blit(text9, textRect95) screen.blit(text9, textRect96) screen.blit(text9, textRect97) screen.blit(text9, textRect98) screen.blit(text10, textRect101) screen.blit(text10, textRect102) screen.blit(text10, textRect103) screen.blit(text10, textRect104) screen.blit(text10, textRect105) screen.blit(text10, textRect106) screen.blit(text10, textRect107) screen.blit(text10, textRect108) screen.blit(text11, textRect11) screen.blit(text12, textRect12) screen.blit(text13, textRect13) screen.blit(text14, textRect14) screen.blit(text151, textRect151) screen.blit(text152, textRect152) screen.blit(text16, textRect16) screen.blit(text171, textRect171) screen.blit(text172, textRect172) screen.blit(text18, textRect18) screen.blit(text191, textRect191) screen.blit(text192, textRect192) screen.blit(text201, textRect201) screen.blit(text202, textRect202) screen.blit(text21, textRect21) screen.blit(text222, textRect221) screen.blit(text221, textRect222) pygame.display.update() clock.tick(fps) pygame.quit() sys.exit
def pysocket(): #פונקציה האחראית על מסך הpygame global lmusic global server_socket lnow = [] #רשימת קטע המוזיקה של השרת ltimenow = [] #רשימת זמני המעברים של הקטע של השרצ pygame.init() clock = pygame.time.Clock() fps = 60 size = [480, 410] #גודל מסך bg = [250, 250, 162] #צבע מסך x1 = 80 xsize = 50 y = 80 ysize = 50 x2 = 260 note = Music() nowb = "" #איזה קטע מושמע עכשיו screen = pygame.display.set_mode(size) pygame.display.set_caption("The main player") #שם הדף #מיקומי וגודל כפתורים button_1a = pygame.Rect(x1, y, xsize, ysize) #a = add l = listen button_1l = pygame.Rect(x1 + 50, y, xsize + 40, ysize) button_2a = pygame.Rect(x1, y + 70, xsize, ysize) button_2l = pygame.Rect(x1 + xsize, y + 70, xsize + 40, ysize) button_3a = pygame.Rect(x1, y + 140, xsize, ysize) button_3l = pygame.Rect(x1 + xsize, y + 140, xsize + 40, ysize) button_4a = pygame.Rect(x1, y + 210, xsize, ysize) button_4l = pygame.Rect(x1 + xsize, y + 210, xsize + 40, ysize) button_5a = pygame.Rect(x2, y, xsize, ysize) button_5l = pygame.Rect(x2 + 50, y, xsize + 40, ysize) button_6a = pygame.Rect(x2, y + 70, xsize, ysize) button_6l = pygame.Rect(x2 + 50, y + 70, xsize + 40, ysize) button_7a = pygame.Rect(x2, y + 140, xsize, ysize) button_7l = pygame.Rect(x2 + 50, y + 140, xsize + 40, ysize) button_8a = pygame.Rect(x2, y + 210, xsize, ysize) button_8l = pygame.Rect(x2 + 50, y + 210, xsize + 40, ysize) button_melody = pygame.Rect(400, 10, 2 * xsize - 20, ysize) button_file_selection = pygame.Rect(320, 10, 2 * xsize - 20, ysize) button_revers = pygame.Rect(240, 10, 2 * xsize - 20, ysize) button_save = pygame.Rect(160, 10, 2 * xsize - 20, ysize) button_clear = pygame.Rect(80, 10, 2 * xsize - 20, ysize) button_delete_note = pygame.Rect(0, 10, 2 * xsize - 20, ysize) button_your_notes = pygame.Rect(320, 350, 2 * xsize - 20, ysize) button_enter = pygame.Rect(400, 350, 2 * xsize - 20, ysize) button_clear_sections = pygame.Rect(240, 350, 2 * xsize - 20, ysize) button_random = pygame.Rect(160, 350, 2 * xsize - 20, ysize) button_delete_last_added = pygame.Rect(80, 350, 2 * xsize - 20, ysize) button_open_piano = pygame.Rect(0, 350, 2 * xsize - 20, ysize) font = pygame.font.SysFont('Lucida Handwriting', 20) #סוגי הפונטים והגדלים השונים font2 = pygame.font.SysFont('Parchment', 62) font3 = pygame.font.SysFont('Parchment', 72) font4 = pygame.font.SysFont('Parchment', 56) font5 = pygame.font.SysFont('Parchment', 47) font6 = pygame.font.SysFont('Bauhaus 93', 32) textx = x1 - 15 texty = 100 colortext = [0, 0, 255] colortext2 = [0, 0, 53] colortext3 = [117, 249, 134] #יצרית הכתב על המסך text1 = font.render('1)', True, colortext) textRect1 = text1.get_rect() textRect1.center = (textx, texty) text2 = font.render('2)', True, colortext) textRect2 = text2.get_rect() textRect2.center = (textx, texty + 70) text3 = font.render('3)', True, colortext) textRect3 = text3.get_rect() textRect3.center = (textx, texty + 140) text4 = font.render('4)', True, colortext) textRect4 = text4.get_rect() textRect4.center = (textx, texty + 210) text5 = font.render('(5', True, colortext) textRect5 = text5.get_rect() textRect5.center = (textx + 345, texty) text6 = font.render('(6', True, colortext) textRect6 = text6.get_rect() textRect6.center = (textx + 345, texty + 70) text7 = font.render('(7', True, colortext) textRect7 = text7.get_rect() textRect7.center = (textx + 345, texty + 140) text8 = font.render('(8', True, colortext) textRect8 = text8.get_rect() textRect8.center = (textx + 345, texty + 210) text9 = font.render('add', True, colortext3) textRect91 = text9.get_rect() textRect91.center = (x1 + 25, y + 25) textRect92 = text9.get_rect() textRect92.center = (x1 + 25, y + 95) textRect93 = text9.get_rect() textRect93.center = (x1 + 25, y + 165) textRect94 = text9.get_rect() textRect94.center = (x1 + 25, y + 235) textRect95 = text9.get_rect() textRect95.center = (x2 + 25, y + 25) textRect96 = text9.get_rect() textRect96.center = (x2 + 25, y + 95) textRect97 = text9.get_rect() textRect97.center = (x2 + 25, y + 165) textRect98 = text9.get_rect() textRect98.center = (x2 + 25, y + 235) text10 = font.render('listen', True, colortext2) textRect101 = text10.get_rect() textRect101.center = (x1 + 95, y + 25) textRect102 = text10.get_rect() textRect102.center = (x1 + 95, y + 95) textRect103 = text10.get_rect() textRect103.center = (x1 + 95, y + 165) textRect104 = text10.get_rect() textRect104.center = (x1 + 95, y + 235) textRect105 = text10.get_rect() textRect105.center = (x2 + 95, y + 25) textRect106 = text10.get_rect() textRect106.center = (x2 + 95, y + 95) textRect107 = text10.get_rect() textRect107.center = (x2 + 95, y + 165) textRect108 = text10.get_rect() textRect108.center = (x2 + 95, y + 235) text11 = font3.render('play', True, [255, 255, 255]) textRect11 = text11.get_rect() textRect11.center = (440, 30) text12 = font3.render('revers', True, [255, 255, 255]) textRect12 = text12.get_rect() textRect12.center = (280, 30) text13 = font3.render('save', True, [255, 255, 255]) textRect13 = text13.get_rect() textRect13.center = (200, 30) text14 = font3.render('clear', True, [255, 255, 255]) textRect14 = text14.get_rect() textRect14.center = (120, 30) text151 = font2.render('delete', True, [255, 255, 255]) text152 = font2.render('note', True, [255, 255, 255]) textRect151 = text151.get_rect() textRect151.center = (40, 23) textRect152 = text152.get_rect() textRect152.center = (40, 45) text16 = font3.render('enter', True, [255, 255, 255]) textRect16 = text16.get_rect() textRect16.center = (440, 370) text171 = font2.render('clear', True, [255, 255, 255]) text172 = font4.render('sections', True, [255, 255, 255]) textRect171 = text171.get_rect() textRect171.center = (280, 360) textRect172 = text172.get_rect() textRect172.center = (280, 385) text18 = font4.render('random', True, [255, 255, 255]) textRect18 = text18.get_rect() textRect18.center = (200, 370) text191 = font5.render('delete last', True, [255, 255, 255]) text192 = font2.render('section', True, [255, 255, 255]) textRect191 = text191.get_rect() textRect191.center = (120, 362) textRect192 = text192.get_rect() textRect192.center = (120, 380) text201 = font2.render('open', True, [255, 255, 255]) text202 = font3.render('piano', True, [255, 255, 255]) textRect201 = text201.get_rect() textRect201.center = (40, 355) textRect202 = text202.get_rect() textRect202.center = (40, 375) text21 = font3.render('select', True, [255, 255, 255]) textRect21 = text21.get_rect() textRect21.center = (360, 30) text222 = font3.render('your', True, [255, 255, 255]) text221 = font3.render('notes', True, [255, 255, 255]) textRect221 = text221.get_rect() textRect221.center = (352, 355) textRect222 = text222.get_rect() textRect222.center = (363, 380) # creates a rect object # The rect method is similar to a list but with a few added perks # for example if you want the position of the button you can simpy type # button.x or button.y or if you want size you can type button.width or # height. you can also get the top, left, right and bottom of an object # with button.right, left, top, and bottom while True: for event in pygame.event.get(): if note.bool == True: #מאפס את הערך שנעיד איזה קטע מושמע עכשיו nowb = "" if event.type == pygame.QUIT: lmusic = ["False"] server_socket.close() return False if event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = event.pos # gets mouse position # checks if mouse position is over the button str2 = "" strtime2 = "" if len(lnow) != 0 and lnow[ -1] == '': #מוחק קטעים רקים מהקטע של השרת del lnow[-1] if len(lnow) != 0 and len(lnow) != len( ltimenow): #מוסיף מעברים עם חסר(בגלל ההוספות הידניות) noteend = lnow[-1].split() for j in noteend: if not (j != "do" and j != "re" and j != "mi" and j != "fa" and j != "sol" and j != "la" and j != "ci" and j != "do2" and j != "#c" and j != "#d" and j != "#f" and j != "#g" and j != "#a"): str2 += " " + j strtime2 += " " + str(0.8) if str2.replace(" ", "") != "": lnow[-1] = str2 ltimenow.append(strtime2) print(lnow) print(ltimenow) if button_1l.collidepoint(mouse_pos): pygame.draw.rect(screen, [0, 255, 0], button_1l) # draw button pygame.display.update() clock.tick(fps) if (len(lmusic) >= 1): if nowb == "l1" and note.bool == False: note.bre = False #אם נלחץ פעם נוספת אותו כפתור print(note.bre) #עוצר את ההשמעה elif note.bool == True: #אם אין כלום מושמע note = Music(lmusic[0].split("stop")[0], lmusic[0].split("stop")[1]) t2 = threading.Thread(target=note.play_music) t2.start() #הרצת השמעה במקביל למסך nowb = "l1" else: print("1l") elif button_1a.collidepoint(mouse_pos): pygame.draw.rect(screen, [0, 255, 0], button_1a) # draw button pygame.display.update() clock.tick(fps) if (len(lmusic) >= 1 and note.bool == True): #אם יש ערך שהתקבל lnow.append( lmusic[0].split("stop")[0]) #הוסף אותו לשרת ltimenow.append(lmusic[0].split("stop")[1]) else: print("1a") elif button_2l.collidepoint(mouse_pos): pygame.draw.rect(screen, [0, 255, 0], button_2l) # draw button pygame.display.update() clock.tick(fps) if (len(lmusic) >= 2): if nowb == "l2" and note.bool == False: note.bre = False print(note.bre) elif note.bool == True: note = Music(lmusic[1].split("stop")[0], lmusic[0].split("stop")[1]) t2 = threading.Thread(target=note.play_music) t2.start() nowb = "l2" else: print("21") elif button_2a.collidepoint(mouse_pos): pygame.draw.rect(screen, [0, 255, 0], button_2a) # draw button pygame.display.update() clock.tick(fps) if (len(lmusic) >= 2 and note.bool == True): lnow.append(lmusic[1].split("stop")[0]) ltimenow.append(lmusic[1].split("stop")[1]) else: print("2a") elif button_3l.collidepoint(mouse_pos): pygame.draw.rect(screen, [0, 255, 0], button_3l) # draw button pygame.display.update() clock.tick(fps) if (len(lmusic) >= 3): if nowb == "l3" and note.bool == False: note.bre = False print(note.bre) elif note.bool == True: note = Music(lmusic[2].split("stop")[0], lmusic[0].split("stop")[1]) t2 = threading.Thread(target=note.play_music) t2.start() nowb = "l3" else: print("31") elif button_3a.collidepoint(mouse_pos): pygame.draw.rect(screen, [0, 255, 0], button_3a) # draw button pygame.display.update() clock.tick(fps) if (len(lmusic) >= 3 and note.bool == True): lnow.append(lmusic[2].split("stop")[0]) ltimenow.append(lmusic[2].split("stop")[1]) else: print("3a") elif button_4l.collidepoint(mouse_pos): pygame.draw.rect(screen, [0, 255, 0], button_4l) # draw button pygame.display.update() clock.tick(fps) if (len(lmusic) >= 4): if nowb == "l4" and note.bool == False: note.bre = False print(note.bre) elif note.bool == True: note = Music(lmusic[3].split("stop")[0], lmusic[0].split("stop")[1]) t2 = threading.Thread(target=note.play_music) t2.start() nowb = "l4" else: print("41") elif button_4a.collidepoint(mouse_pos): pygame.draw.rect(screen, [0, 255, 0], button_4a) # draw button pygame.display.update() clock.tick(fps) if (len(lmusic) >= 4 and note.bool == True): lnow.append(lmusic[3].split("stop")[0]) ltimenow.append(lmusic[3].split("stop")[1]) else: print("4a") elif button_5l.collidepoint(mouse_pos): pygame.draw.rect(screen, [0, 255, 0], button_5l) # draw button pygame.display.update() clock.tick(fps) if (len(lmusic) >= 5): if nowb == "l5" and note.bool == False: note.bre = False print(note.bre) elif note.bool == True: note = Music(lmusic[4].split("stop")[0], lmusic[0].split("stop")[1]) t2 = threading.Thread(target=note.play_music) t2.start() nowb = "l5" else: print("51") elif button_5a.collidepoint(mouse_pos): pygame.draw.rect(screen, [0, 255, 0], button_5a) # draw button pygame.display.update() clock.tick(fps) if (len(lmusic) >= 5 and note.bool == True): lnow.append(lmusic[4].split("stop")[0]) ltimenow.append(lmusic[4].split("stop")[1]) else: print("5a") elif button_6l.collidepoint(mouse_pos): pygame.draw.rect(screen, [0, 255, 0], button_6l) # draw button pygame.display.update() clock.tick(fps) if (len(lmusic) >= 6): if nowb == "l6" and note.bool == False: note.bre = False print(note.bre) elif note.bool == True: note = Music(lmusic[5].split("stop")[0], lmusic[0].split("stop")[1]) t2 = threading.Thread(target=note.play_music) t2.start() nowb = "l6" else: print("61") elif button_6a.collidepoint(mouse_pos): pygame.draw.rect(screen, [0, 255, 0], button_6a) # draw button pygame.display.update() clock.tick(fps) if (len(lmusic) >= 6 and note.bool == True): lnow.append(lmusic[5].split("stop")[0]) ltimenow.append(lmusic[5].split("stop")[1]) else: print("6a") elif button_7l.collidepoint(mouse_pos): pygame.draw.rect(screen, [0, 255, 0], button_7l) # draw button pygame.display.update() clock.tick(fps) if (len(lmusic) >= 7): if nowb == "l7" and note.bool == False: note.bre = False print(note.bre) elif note.bool == True: note = Music(lmusic[6].split("stop")[0], lmusic[0].split("stop")[1]) t2 = threading.Thread(target=note.play_music) t2.start() nowb = "l7" else: print("71") elif button_7a.collidepoint(mouse_pos): pygame.draw.rect(screen, [0, 255, 0], button_7a) # draw button pygame.display.update() clock.tick(fps) if (len(lmusic) >= 7 and note.bool == True): lnow.append(lmusic[6].split("stop")[0]) ltimenow.append(lmusic[6].split("stop")[1]) else: print("7a") elif button_8l.collidepoint(mouse_pos): pygame.draw.rect(screen, [0, 255, 0], button_8l) # draw button pygame.display.update() clock.tick(fps) if (len(lmusic) >= 8 and note.bool == True): if nowb == "l8" and note.bool == False: note.bre = False print(note.bre) elif note.bool == True: note = Music(lmusic[7].split("stop")[0], lmusic[0].split("stop")[1]) t2 = threading.Thread(target=note.play_music) t2.start() nowb = "l8" else: print("81") elif button_8a.collidepoint(mouse_pos): pygame.draw.rect(screen, [0, 255, 0], button_8a) # draw button pygame.display.update() clock.tick(fps) if (len(lmusic) >= 8): lnow.append(lmusic[7].split("stop")[0]) ltimenow.append(lmusic[7].split("stop")[1]) else: print("8a") elif button_melody.collidepoint(mouse_pos): if nowb == "play" and note.bool == False: #אם נלחץ פעם נוספת note.bre = False #עוצר את ההשמעה elif note.bool == True: pygame.draw.rect(screen, [0, 255, 0], button_melody) # draw button pygame.display.update() clock.tick(fps) note = Music( str(lnow).replace("[", "").replace("]", "").replace( "'", "").replace(",", ""), str(ltimenow).replace("[", "").replace( "]", "").replace("'", "").replace(",", "")) t2 = threading.Thread(target=note.play_music) t2.start() #הרצת ההשמעה במקביל למסך nowb = "play" elif note.bool == False: #מונע לחיצת כפתורים בזמן השמעת קטע break elif button_revers.collidepoint(mouse_pos): pygame.draw.rect(screen, [0, 255, 0], button_revers) # draw button pygame.display.update() clock.tick(fps) lnow2 = [] ltimenow2 = [0.0] for i in lnow: lnow2.append( str(i.split()[::-1]).replace("[", "").replace( "]", "").replace("'", "").replace( ",", "")) #הפיכת סדר התווים בתוך כל איבר ברשימה for j in ltimenow: ltimenow2.append( str(j.split()[::-1]).replace("[", "").replace( "]", "").replace("'", "").replace( ",", "")) #הפיכת סדר הזמנים בתוך כל איבר ברשימה lnow = lnow2[::-1] #הפיכת סדר האיברים ltimenow = ltimenow2[::-1] #הפיכת סדר האיברים if len(ltimenow) != 0: #שמירת כמות זמני מעבר תקינה del ltimenow[-1] note = Music( str(lnow).replace("[", "").replace("]", "").replace( "'", "").replace(",", ""), str(ltimenow).replace("[", "").replace("]", "").replace( "'", "").replace(",", "")) elif button_save.collidepoint(mouse_pos): root2 = tk.Tk() root2.withdraw( ) # we don't want a full GUI, so keep the root window from appearing pygame.quit() filename = askopenfilename( ) # show an "Open" dialog box and return the path to the selected file screen = pygame.display.set_mode(size) root2.destroy() print(filename) if not (str(filename) == "" or str(filename)[::-1][0:4][::-1] != ".txt" ): #בדיקה שהקובץ תקין input_file = open(r'' + filename, 'a') #פתיחת קובץ לעריכה input_file.write((str(lnow).replace("[", "").replace( "]", "").replace("'", "").replace(",", "").replace( " ", " ")) + '\n' + str(ltimenow).replace( "[", "").replace("]", "").replace("'", ""). replace(",", "").replace(" ", " ")) input_file.close() #סגירת הקובץ לעריכה print("save") elif button_clear.collidepoint(mouse_pos): pygame.draw.rect(screen, [0, 255, 0], button_clear) # draw button pygame.display.update() clock.tick(fps) lnow = [] #איפוס משתנים ltimenow = [] elif button_delete_note.collidepoint(mouse_pos): pygame.draw.rect(screen, [0, 255, 0], button_delete_note) # draw button pygame.display.update() clock.tick(fps) print(lnow) if (len(lnow) > 0): lnow[-1] = str(lnow[-1]).split() del (lnow[-1])[-1] #מחיקת תו האחרון באיבר האחרון lnow[-1] = str(lnow[-1]).replace("[", "").replace( "]", "").replace("'", "").replace(",", "") ltimenow[-1] = str(ltimenow[-1]).split() del (ltimenow[-1])[-1] #מחיקת מספר אחרון באיבר האחרון ltimenow[-1] = str(ltimenow[-1]).replace( "[", "").replace("]", "").replace("'", "").replace(",", "") print(ltimenow) elif button_enter.collidepoint(mouse_pos): #פתיחת קובץ tkinter עם תיבת כתיבה ROOT = tk.Tk() ROOT.withdraw() pygame.quit() enter_notes = simpledialog.askstring(title="notes", prompt="enter notes") ROOT.destroy() screen = pygame.display.set_mode(size) if str(enter_notes) != "None": #בדיקה אם התקבל ערך print(str(enter_notes)) lnow.append(enter_notes.lower() ) #הוספת המתקבל לרשימת הנגינה של השרצ print("enter") elif button_clear_sections.collidepoint(mouse_pos): pygame.draw.rect(screen, [0, 255, 0], button_clear_sections) # draw button pygame.display.update() clock.tick(fps) n = 0 while len( lmusic ) != 0 and n < 8: #מחיקת 8 האיברים הראשונים ברשימת קטעים שהתקבלו מהלקוחות n += 1 del lmusic[0] print("clear sections") elif button_random.collidepoint(mouse_pos): pygame.draw.rect(screen, [0, 255, 0], button_random) # draw button pygame.display.update() clock.tick(fps) lnow2 = [] ltimenow2 = [] if lnow != []: nrandom = random.randint(0, len(lnow) - 1) #בחירת מספר רנדומלי while lnow != []: lnow2.append( lnow.pop(nrandom) ) #הכנסת האיבר עם האינדקס של הרנדום לרשימה חדשה ltimenow2.append(ltimenow.pop(nrandom)) if (len(lnow) != 0): nrandom = random.randint(0, len(lnow) - 1) #בחירת מספר רנדומלי lnow = lnow2 #החזרה למשתנים המקוריים ltimenow = ltimenow2 print("random") elif button_delete_last_added.collidepoint(mouse_pos): pygame.draw.rect(screen, [0, 255, 0], button_delete_last_added) # draw button pygame.display.update() clock.tick(fps) if len(lnow) != 0: del (lnow)[-1] #מחיקת איבר אחרון del (ltimenow)[-1] print("delete last added") elif button_open_piano.collidepoint(mouse_pos): pygame.quit() exec(open("piano clinet.py").read()) #פתיחת פסנתרר (לקוח) screen = pygame.display.set_mode(size) #פתיחה חזרה של השרת print("open piano") elif button_file_selection.collidepoint(mouse_pos): #פתיחת חלון בחירת קובץ root3 = tk.Tk() root3.withdraw( ) # we don't want a full GUI, so keep the root window from appearing pygame.quit() filename = askopenfilename( ) # show an "Open" dialog box and return the path to the selected file screen = pygame.display.set_mode(size) root3.destroy() print(filename) string = "" strtime = "" if not (str(filename) == "" or str(filename)[::-1][0:4][::-1] != ".txt" ): #האם הקובץ מסוג תקין input_file = open(r'' + filename, 'r') #פתיחת הקובץ לעריכה notes = input_file.read().replace('\n', " ").split() for j in notes: if not (j != "do" and j != "re" and j != "mi" and j != "fa" and j != "sol" and j != "la" and j != "ci" and j != "do2" and j != "#c" and j != "#d" and j != "#f" and j != "#g" and j != "#a"): string += " " + j else: num2 = "" point = True for i in j: #מציאת מספרים בקובץ if (i == "0" or i == "1" or i == "2" or i == "3" or i == "4" or i == "5" or i == "6" or i == "7" or i == "8" or i == "9"): num2 += i elif i == "." and point: num2 += i point = False if (num2 != ""): strtime += num2 + " " #טיפול במצבים בהם מספר הממעברים לא תואם למספר התווים שהוכנסו if len(strtime.split()) < len(string.split()): while len(strtime.split()) != len(string.split()): strtime += "0.8 " elif len(strtime.split()) > len(string.split()): strtime = strtime.split() while len(strtime) != len(string.split()): del strtime[-1] strtime = str(strtime).replace("[", "").replace( "]", "").replace("'", "").replace(",", "") input_file.close() #סגירת עריכת קובץ lnow.append(string) ltimenow.append(strtime) elif button_your_notes.collidepoint(mouse_pos): root = tk.Tk() pygame.quit() S = tk.Scrollbar(root) T = tk.Text(root, height=4, width=50) S.pack(side=tk.RIGHT, fill=tk.Y) T.pack(side=tk.LEFT, fill=tk.Y) S.config(command=T.yview) T.config(yscrollcommand=S.set) T.insert( tk.END, str(lnow).replace("]", '').replace("[", "").replace( "'", "").replace(", ", " ")) tk.mainloop() #הרצת חלון tkinter screen = pygame.display.set_mode(size) print("your_notes") print(lnow) screen.fill(bg) colorb1 = [17, 6, 70] colorb2 = [133, 252, 113] #הרצת הכפתורים pygame.draw.rect(screen, colorb1, button_1a) # draw button pygame.draw.rect(screen, colorb2, button_1l) # draw button pygame.draw.rect(screen, colorb1, button_2a) # draw button pygame.draw.rect(screen, colorb2, button_2l) # draw button pygame.draw.rect(screen, colorb1, button_3a) # draw button pygame.draw.rect(screen, colorb2, button_3l) # draw button pygame.draw.rect(screen, colorb1, button_4a) # draw button pygame.draw.rect(screen, colorb2, button_4l) # draw button pygame.draw.rect(screen, colorb1, button_5a) # draw button pygame.draw.rect(screen, colorb2, button_5l) # draw button pygame.draw.rect(screen, colorb1, button_6a) # draw button pygame.draw.rect(screen, colorb2, button_6l) # draw button pygame.draw.rect(screen, colorb1, button_7a) # draw button pygame.draw.rect(screen, colorb2, button_7l) # draw button pygame.draw.rect(screen, colorb1, button_8a) # draw button pygame.draw.rect(screen, colorb2, button_8l) # draw button pygame.draw.rect(screen, [0, 0, 0], button_melody) # draw button pygame.draw.rect(screen, [40, 40, 40], button_file_selection) # draw button pygame.draw.rect(screen, [80, 80, 80], button_revers) # draw button pygame.draw.rect(screen, [130, 130, 130], button_save) # draw button pygame.draw.rect(screen, [170, 170, 170], button_clear) # draw button pygame.draw.rect(screen, [200, 200, 200], button_delete_note) # draw button pygame.draw.rect(screen, [0, 0, 0], button_enter) # draw button pygame.draw.rect(screen, [40, 40, 40], button_your_notes) # draw button pygame.draw.rect(screen, [80, 80, 80], button_clear_sections) # draw button pygame.draw.rect(screen, [130, 130, 130], button_random) # draw button pygame.draw.rect(screen, [170, 170, 170], button_delete_last_added) # draw button pygame.draw.rect(screen, [200, 200, 200], button_open_piano) # draw button #הרצת הכתב screen.blit(text1, textRect1) screen.blit(text2, textRect2) screen.blit(text3, textRect3) screen.blit(text4, textRect4) screen.blit(text5, textRect5) screen.blit(text6, textRect6) screen.blit(text7, textRect7) screen.blit(text8, textRect8) screen.blit(text9, textRect91) screen.blit(text9, textRect92) screen.blit(text9, textRect93) screen.blit(text9, textRect94) screen.blit(text9, textRect95) screen.blit(text9, textRect96) screen.blit(text9, textRect97) screen.blit(text9, textRect98) screen.blit(text10, textRect101) screen.blit(text10, textRect102) screen.blit(text10, textRect103) screen.blit(text10, textRect104) screen.blit(text10, textRect105) screen.blit(text10, textRect106) screen.blit(text10, textRect107) screen.blit(text10, textRect108) screen.blit(text11, textRect11) screen.blit(text12, textRect12) screen.blit(text13, textRect13) screen.blit(text14, textRect14) screen.blit(text151, textRect151) screen.blit(text152, textRect152) screen.blit(text16, textRect16) screen.blit(text171, textRect171) screen.blit(text172, textRect172) screen.blit(text18, textRect18) screen.blit(text191, textRect191) screen.blit(text192, textRect192) screen.blit(text201, textRect201) screen.blit(text202, textRect202) screen.blit(text21, textRect21) screen.blit(text222, textRect221) screen.blit(text221, textRect222) pygame.display.update() clock.tick(fps) pygame.quit() sys.exit
def main(): string = "" #מחרוזת תווים strtime = "" #מחרוזת זמנים pygame.init() clock = pygame.time.Clock() fps = 60 size = [600, 210] #גודל המסך bg = [200, 200, 255] #צבע המסך font = pygame.font.Font('freesansbold.ttf', 27) #סוגי הפונטים והגדלים השונים font2 = pygame.font.SysFont('Parchment', 82) font3 = pygame.font.Font('freesansbold.ttf', 23) bool1 = False #שולט על אילו כפתורים יפעלו (האדומים והכתומים או הפסנתר) note = Music() color = [255, 0, 0] #משתנה האחראי על צבע כפתור play/ stop screen = pygame.display.set_mode(size) pygame.display.set_caption("Piano") #שם מסך white = [255, 255, 255] black = [0, 0, 0] green = [0, 255, 0] #הגדרת מיקום וגודל כפתורים button_c = pygame.Rect(10, 10, 50, 150) button_d = pygame.Rect(70, 10, 50, 150) button_e = pygame.Rect(130, 10, 50, 150) button_f = pygame.Rect(190, 10, 50, 150) button_g = pygame.Rect(250, 10, 50, 150) button_a = pygame.Rect(310, 10, 50, 150) button_b = pygame.Rect(370, 10, 50, 150) button_c2 = pygame.Rect(430, 10, 50, 150) button_db = pygame.Rect(50, 10, 30, 110) button_eb = pygame.Rect(110, 10, 30, 110) button_gb = pygame.Rect(230, 10, 30, 110) button_ab = pygame.Rect(290, 10, 30, 110) button_bb = pygame.Rect(350, 10, 30, 110) button_send = pygame.Rect(500, 10, 95, 35) button_clear = pygame.Rect(500, 50, 95, 25) button_play = pygame.Rect(500, 80, 95, 25) button_revers = pygame.Rect(500, 110, 95, 25) button_file_selection = pygame.Rect(500, 140, 95, 25) button_play_stop = pygame.Rect(500, 170, 95, 35) #הגדרת הכתבים cw = font.render('C', True, black) cRect = cw.get_rect() cRect.center = (30, 130) dw = font.render('D', True, black) dRect = dw.get_rect() dRect.center = (95, 130) ew = font.render('E', True, black) eRect = ew.get_rect() eRect.center = (155, 130) fw = font.render('F', True, black) fRect = fw.get_rect() fRect.center = (215, 130) gw = font.render('G', True, black) gRect = gw.get_rect() gRect.center = (275, 130) aw = font.render('A', True, black) aRect = aw.get_rect() aRect.center = (335, 130) bw = font.render('B', True, black) bRect = bw.get_rect() bRect.center = (395, 130) cRect2 = cw.get_rect() cRect2.center = (455, 130) send = font.render('send', True, white) sendRect = send.get_rect() sendRect.center = (547, 30) clear = font.render('clear', True, white) clearRect = clear.get_rect() clearRect.center = (547, 65) play = font.render('play', True, white) playRect = play.get_rect() playRect.center = (547, 92) revers = font.render('revers', True, white) reversRect = revers.get_rect() reversRect.center = (547, 123) file_selection = font.render('select', True, white) file_selectionRect = file_selection.get_rect() file_selectionRect.center = (547, 155) play_stop1 = font3.render('play/', True, white) play_stopRect1 = play_stop1.get_rect() play_stopRect1.center = (540, 180) play_stop2 = font3.render('stop', True, white) play_stopRect2 = play_stop2.get_rect() play_stopRect2.center = (560, 195) db = font2.render('c#', True, white) dbRect = db.get_rect() dbRect.center = (65, 90) eb = font2.render('d#', True, white) ebRect = eb.get_rect() ebRect.center = (126, 90) gb = font2.render('f#', True, white) gbRect = gb.get_rect() gbRect.center = (245, 90) ab = font2.render('g#', True, white) abRect = ab.get_rect() abRect.center = (307, 90) bb = font2.render('a#', True, white) bbRect = bb.get_rect() bbRect.center = (366, 90) # creates a rect object # The rect method is similar to a list but with a few added perks # for example if you want the position of the button you can simpy type # button.x or button.y or if you want size you can type button.width or # height. you can also get the top, left, right and bottom of an object # with button.right, left, top, and bottom start_time = time.time() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: #עצירת הרצת המסך return False if event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = event.pos # gets mouse position # checks if mouse position is over the button if button_play.collidepoint(mouse_pos) and not bool1: pygame.draw.rect(screen, green, button_play) #שינוי צבע כפתור בזמן לחיצה pygame.display.update() clock.tick(fps) if note.bool == False: #אם נלחץ הכפתור פעם שנייה note.bre = False #נעצר הרצת המוזיקה על ידי המחלקה else: note = Music(string, strtime) t1 = threading.Thread( target=note.play_music ) #הרצת השמעת המוזיקה במקביל להצגת המסך t1.start() elif note.bool == False: #מניעת לחיצת כפתורים בזמן שמיעת מוזיקה break elif button_db.collidepoint(mouse_pos) and bool1: string += " " + "#c" #הוספת תו למחרוזת תווים שנוגנו strtime += str(time.time() - start_time ) + " " #הוספת זמן מרווח למחרוזת הזמנים start_time = time.time() #איפוס זמן התחלת המרווח note.play_db() #השמעת צליל elif button_eb.collidepoint(mouse_pos) and bool1: string += " " + "#d" strtime += str(time.time() - start_time) + " " start_time = time.time() note.play_eb() elif button_gb.collidepoint(mouse_pos) and bool1: string += " " + "#f" strtime += str(time.time() - start_time) + " " start_time = time.time() note.play_gb() elif button_ab.collidepoint(mouse_pos) and bool1: string += " " + "#g" strtime += str(time.time() - start_time) + " " start_time = time.time() note.play_ab() elif button_bb.collidepoint(mouse_pos) and bool1: string += " " + "#a" note.play_bb() elif button_c.collidepoint(mouse_pos) and bool1: # prints current location of mouse string += " " + "do" strtime += str(time.time() - start_time) + " " start_time = time.time() note.play_do() elif button_d.collidepoint(mouse_pos) and bool1: string += " " + "re" strtime += str(time.time() - start_time) + " " start_time = time.time() note.play_re() elif button_e.collidepoint(mouse_pos) and bool1: string += " " + "mi" strtime += str(time.time() - start_time) + " " start_time = time.time() note.play_mi() elif button_f.collidepoint(mouse_pos) and bool1: string += " " + "fa" strtime += str(time.time() - start_time) + " " start_time = time.time() note.play_fa() elif button_g.collidepoint(mouse_pos) and bool1: string += " " + "sol" strtime += str(time.time() - start_time) + " " start_time = time.time() note.play_sol() elif button_a.collidepoint(mouse_pos) and bool1: string += " " + "la" strtime += str(time.time() - start_time) + " " start_time = time.time() note.play_la() elif button_b.collidepoint(mouse_pos) and bool1: string += " " + "ci" strtime += str(time.time() - start_time) + " " start_time = time.time() note.play_ci() elif button_c2.collidepoint(mouse_pos) and bool1: string += " " + "do2" strtime += str(time.time() - start_time) + " " start_time = time.time() note.play_do2() elif button_send.collidepoint(mouse_pos) and not bool1: #התקשרות לשרת my_socket = socket.socket() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ipsocket = ('192.168.1.16', 1729) result = sock.connect_ex(ipsocket) if result == 0: #בדיקה עם השרת פועל my_socket.connect(ipsocket) listtime = strtime.split() if len(listtime) != 0: listtime[ 0] = "0.0" #איפוס הזמן הראשון (שיתחיל את הנגינה) strtime = str(listtime).replace("[", "").replace( "]", "").replace("'", "").replace(",", "") string += " stop " + strtime #חיבור 2 המחרוזות למחרוזת אחת. שמפריד בניהן המילה stop my_socket.send(string.encode()) #שליחת המחרוזות לשרת string = "" #איפוס המחרוזות strtime = "" pygame.draw.rect(screen, green, button_send) # draw button pygame.display.update() clock.tick(fps) elif button_clear.collidepoint(mouse_pos) and not bool1: pygame.draw.rect(screen, green, button_clear) # draw button pygame.display.update() clock.tick(fps) string = "" #איפוס המחרוזות strtime = "" elif button_revers.collidepoint(mouse_pos) and not bool1: pygame.draw.rect(screen, green, button_revers) # draw button pygame.display.update() clock.tick(fps) listnote = string.split() listnote = listnote[::-1] #היפוך התווים string = str(listnote).replace("[", "").replace( "]", "").replace("'", "").replace(",", "") listtime = strtime.split() listtime = listtime[::-1] #היפוך הזמנים strtime = str(listtime).replace("[", "").replace( "]", "").replace("'", "").replace(",", "") elif button_file_selection.collidepoint( mouse_pos) and not bool1: Tk().withdraw( ) # we don't want a full GUI, so keep the root window from appearing pygame.quit() filename = askopenfilename( ) # show an "Open" dialog box and return the path to the selected file print(filename) screen = pygame.display.set_mode(size) if not (str(filename) == "" or str(filename)[::-1][0:4][::-1] != ".txt" ): #בדיקה שסוג הקובץ תקין input_file = open(r'' + filename, 'r') #פתיחת קובץ לקריאה notes = input_file.read().replace('\n', " ").split() if len(strtime.split()) != len(string.split()): strtime += "0.0 " for j in notes: if not (j != "do" and j != "re" and j != "mi" and j != "fa" and j != "sol" and j != "la" and j != "ci" and j != "do2" and j != "#c" and j != "#d" and j != "#f" and j != "#g" and j != "#a"): string += " " + j else: num2 = "" point = True for i in j: #מציאת מספרים בקובץ if (i == "0" or i == "1" or i == "2" or i == "3" or i == "4" or i == "5" or i == "6" or i == "7" or i == "8" or i == "9"): num2 += i elif i == "." and point: num2 += i point = False if (num2 != ""): strtime += num2 + " " #טיפול במצבים בהם מספר הממעברים לא תואם למספר התווים שהוכנסו if len(strtime.split()) < len(string.split()): while len(strtime.split()) != len(string.split()): strtime += "0.8 " elif len(strtime.split()) > len(string.split()): strtime = strtime.split() while len(strtime) != len(string.split()): del strtime[-1] strtime = str(strtime).replace("[", "").replace( "]", "").replace("'", "").replace(",", "") input_file.close() #סגירת עריכת קובץ strtime += " " print(strtime) print(string) elif button_play_stop.collidepoint(mouse_pos): bool1 = not bool1 start_time = time.time() if bool1: #שינוי צבע הכפתור color = [0, 255, 0] else: color = [255, 0, 0] screen.fill(bg) #הרצת הכפתורים pygame.draw.rect(screen, white, button_c) # draw button pygame.draw.rect(screen, white, button_d) # draw button pygame.draw.rect(screen, white, button_e) # draw button pygame.draw.rect(screen, white, button_f) # draw button pygame.draw.rect(screen, white, button_g) # draw button pygame.draw.rect(screen, white, button_a) # draw button pygame.draw.rect(screen, white, button_b) # draw button pygame.draw.rect(screen, white, button_c2) # draw button pygame.draw.rect(screen, black, button_db) # draw button pygame.draw.rect(screen, black, button_eb) # draw button pygame.draw.rect(screen, black, button_gb) # draw button pygame.draw.rect(screen, black, button_ab) # draw button pygame.draw.rect(screen, black, button_bb) # draw button pygame.draw.rect(screen, [255, 100, 0], button_send) # draw button pygame.draw.rect(screen, [255, 170, 0], button_clear) # draw button pygame.draw.rect(screen, [255, 170, 0], button_play) # draw button pygame.draw.rect(screen, [255, 170, 0], button_revers) # draw button pygame.draw.rect(screen, [255, 170, 0], button_file_selection) # draw button pygame.draw.rect(screen, color, button_play_stop) # draw button #הרצת הכתבים screen.blit(cw, cRect) screen.blit(dw, dRect) screen.blit(ew, eRect) screen.blit(fw, fRect) screen.blit(gw, gRect) screen.blit(aw, aRect) screen.blit(bw, bRect) screen.blit(cw, cRect2) screen.blit(send, sendRect) screen.blit(clear, clearRect) screen.blit(play, playRect) screen.blit(revers, reversRect) screen.blit(file_selection, file_selectionRect) screen.blit(db, dbRect) screen.blit(eb, ebRect) screen.blit(gb, gbRect) screen.blit(ab, abRect) screen.blit(bb, bbRect) screen.blit(play_stop1, play_stopRect1) screen.blit(play_stop2, play_stopRect2) pygame.display.update() clock.tick(fps) #סגירת הרשתות והpygame my_socket.close() pygame.quit() sys.exit
def main(): string = "" pygame.init() clock = pygame.time.Clock() fps = 60 size = [600, 200] bg = [200, 200, 255] font = pygame.font.Font('freesansbold.ttf', 27) font2 = pygame.font.SysFont('Parchment', 82) note = Music(size) screen = pygame.display.set_mode(size) pygame.display.set_caption("Piano") button_c = pygame.Rect(10, 10, 50, 150) button_d = pygame.Rect(70, 10, 50, 150) button_e = pygame.Rect(130, 10, 50, 150) button_f = pygame.Rect(190, 10, 50, 150) button_g = pygame.Rect(250, 10, 50, 150) button_a = pygame.Rect(310, 10, 50, 150) button_b = pygame.Rect(370, 10, 50, 150) button_c2 = pygame.Rect(430, 10, 50, 150) button_db = pygame.Rect(50, 10, 30, 110) button_eb = pygame.Rect(110, 10, 30, 110) button_gb = pygame.Rect(230, 10, 30, 110) button_ab = pygame.Rect(290, 10, 30, 110) button_bb = pygame.Rect(350, 10, 30, 110) button_send = pygame.Rect(500, 10, 95, 45) button_clear = pygame.Rect(500, 60, 95, 30) button_play = pygame.Rect(500, 95, 95, 30) button_revers = pygame.Rect(500, 130, 95, 30) button_file_selection = pygame.Rect(500, 165, 95, 30) cw = font.render('C', True, [0, 0, 0]) cRect = cw.get_rect() cRect.center = (30, 130) dw = font.render('D', True, [0, 0, 0]) dRect = dw.get_rect() dRect.center = (95, 130) ew = font.render('E', True, [0, 0, 0]) eRect = ew.get_rect() eRect.center = (155, 130) fw = font.render('F', True, [0, 0, 0]) fRect = fw.get_rect() fRect.center = (215, 130) gw = font.render('G', True, [0, 0, 0]) gRect = gw.get_rect() gRect.center = (275, 130) aw = font.render('A', True, [0, 0, 0]) aRect = aw.get_rect() aRect.center = (335, 130) bw = font.render('B', True, [0, 0, 0]) bRect = bw.get_rect() bRect.center = (395, 130) cRect2 = cw.get_rect() cRect2.center = (455, 130) send = font.render('send', True, [255, 255, 255]) sendRect = send.get_rect() sendRect.center = (547, 35) clear = font.render('clear', True, [255, 255, 255]) clearRect = clear.get_rect() clearRect.center = (547, 75) play = font.render('play', True, [255, 255, 255]) playRect = play.get_rect() playRect.center = (547, 110) revers = font.render('revers', True, [255, 255, 255]) reversRect = revers.get_rect() reversRect.center = (547, 145) file_selection = font.render('select', True, [255, 255, 255]) file_selectionRect = file_selection.get_rect() file_selectionRect.center = (547, 180) db = font2.render('c#', True, [255, 255, 255]) dbRect = db.get_rect() dbRect.center = (65, 90) eb = font2.render('d#', True, [255, 255, 255]) ebRect = eb.get_rect() ebRect.center = (126, 90) gb = font2.render('f#', True, [255, 255, 255]) gbRect = gb.get_rect() gbRect.center = (245, 90) ab = font2.render('g#', True, [255, 255, 255]) abRect = ab.get_rect() abRect.center = (307, 90) bb = font2.render('a#', True, [255, 255, 255]) bbRect = bb.get_rect() bbRect.center = (366, 90) # creates a rect object # The rect method is similar to a list but with a few added perks # for example if you want the position of the button you can simpy type # button.x or button.y or if you want size you can type button.width or # height. you can also get the top, left, right and bottom of an object # with button.right, left, top, and bottom while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return False if event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = event.pos # gets mouse position # checks if mouse position is over the button if button_play.collidepoint(mouse_pos): if note.bool == False: note.bre = False else: note = Music(string) t1 = threading.Thread(target=note.play_music) t1.start() elif note.bool == False: break elif button_db.collidepoint(mouse_pos): string += " " + "#c" note.play_db() elif button_eb.collidepoint(mouse_pos): string += " " + "#d" note.play_eb() elif button_gb.collidepoint(mouse_pos): string += " " + "#f" note.play_gb() elif button_ab.collidepoint(mouse_pos): string += " " + "#g" note.play_ab() elif button_bb.collidepoint(mouse_pos): string += " " + "#a" note.play_bb() elif button_c.collidepoint(mouse_pos): # prints current location of mouse string += " " + "do" note.play_do() elif button_d.collidepoint(mouse_pos): string += " " + "re" note.play_re() elif button_e.collidepoint(mouse_pos): string += " " + "mi" note.play_mi() elif button_f.collidepoint(mouse_pos): string += " " + "fa" note.play_fa() elif button_g.collidepoint(mouse_pos): string += " " + "sol" note.play_sol() elif button_a.collidepoint(mouse_pos): string += " " + "la" note.play_la() elif button_b.collidepoint(mouse_pos): string += " " + "ci" note.play_ci() elif button_c2.collidepoint(mouse_pos): string += " " + "do2" note.play_do2() elif button_send.collidepoint(mouse_pos): my_socket = socket.socket() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ipsocket = ('192.168.1.16', 1729) result = sock.connect_ex(ipsocket) if result == 0: my_socket.connect(ipsocket) my_socket.send(string.encode()) string = "" #note.play_click() elif button_clear.collidepoint(mouse_pos): string = "" #note.play_click() elif button_revers.collidepoint(mouse_pos): listnote = string.split() listnote = listnote[::-1] string = str(listnote).replace("[", "").replace( "]", "").replace("'", "").replace(",", "") #note.play_click() elif button_file_selection.collidepoint(mouse_pos): Tk().withdraw( ) # we don't want a full GUI, so keep the root window from appearing pygame.quit() filename = askopenfilename( ) # show an "Open" dialog box and return the path to the selected file print(filename) screen = pygame.display.set_mode(size) if not (str(filename) == "" or str(filename)[::-1][0:4][::-1] != ".txt"): input_file = open(r'' + filename, 'r') notes = input_file.read().replace('\n', " ").split() for j in notes: if not (j != "do" and j != "re" and j != "mi" and j != "fa" and j != "sol" and j != "la" and j != "ci" and j != "do2" and j != "#c" and j != "#d" and j != "#f" and j != "#g" and j != "#a"): string += " " + j input_file.close() screen.fill(bg) pygame.draw.rect(screen, [255, 255, 255], button_c) # draw button pygame.draw.rect(screen, [255, 255, 255], button_d) # draw button pygame.draw.rect(screen, [255, 255, 255], button_e) # draw button pygame.draw.rect(screen, [255, 255, 255], button_f) # draw button pygame.draw.rect(screen, [255, 255, 255], button_g) # draw button pygame.draw.rect(screen, [255, 255, 255], button_a) # draw button pygame.draw.rect(screen, [255, 255, 255], button_b) # draw button pygame.draw.rect(screen, [255, 255, 255], button_c2) # draw button pygame.draw.rect(screen, [0, 0, 0], button_db) # draw button pygame.draw.rect(screen, [0, 0, 0], button_eb) # draw button pygame.draw.rect(screen, [0, 0, 0], button_gb) # draw button pygame.draw.rect(screen, [0, 0, 0], button_ab) # draw button pygame.draw.rect(screen, [0, 0, 0], button_bb) # draw button pygame.draw.rect(screen, [255, 0, 0], button_send) # draw button pygame.draw.rect(screen, [255, 120, 0], button_clear) # draw button pygame.draw.rect(screen, [255, 120, 0], button_play) # draw button pygame.draw.rect(screen, [255, 120, 0], button_revers) # draw button pygame.draw.rect(screen, [255, 120, 0], button_file_selection) # draw button screen.blit(cw, cRect) screen.blit(dw, dRect) screen.blit(ew, eRect) screen.blit(fw, fRect) screen.blit(gw, gRect) screen.blit(aw, aRect) screen.blit(bw, bRect) screen.blit(cw, cRect2) screen.blit(send, sendRect) screen.blit(clear, clearRect) screen.blit(play, playRect) screen.blit(revers, reversRect) screen.blit(file_selection, file_selectionRect) screen.blit(db, dbRect) screen.blit(eb, ebRect) screen.blit(gb, gbRect) screen.blit(ab, abRect) screen.blit(bb, bbRect) pygame.display.update() clock.tick(fps) my_socket.close() pygame.quit() sys.exit