Advertisement
doubledare704

Untitled

May 24th, 2022
766
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame
  2. from pygame import mixer
  3. pygame.init()
  4.  
  5. black = (0, 0, 0)
  6. white = (255, 255, 255)
  7. gray = (128, 128, 128)
  8. dark_gray = (50, 50, 50)
  9. light_gray = (170, 170, 170)
  10. blue = (0, 255, 255)
  11. red = (255, 0, 0)
  12. green = (0, 255, 0)
  13. gold = (212, 175, 55)
  14. WIDTH = 1400
  15. HEIGHT = 800
  16. active_length = 0
  17. active_beat = 0
  18.  
  19. # sounds
  20. '''
  21. hi_hat = mixer.Sound('sounds\kit2\hi hat.wav')
  22. snare = mixer.Sound('sounds\kit2\snare.wav')
  23. kick = mixer.Sound('sounds\kit2\kick.wav')
  24. crash = mixer.Sound('sounds\kit2\crash.wav')
  25. clap = mixer.Sound('sounds\kit2\clap.wav')
  26. tom = mixer.Sound("sounds\kit2\\tom.wav")
  27. '''
  28. hi_hat = mixer.Sound('sounds\hi hat.wav')
  29. snare = mixer.Sound('sounds\snare.wav')
  30. kick = mixer.Sound('sounds\kick.wav')
  31. crash = mixer.Sound('sounds\crash.wav')
  32. clap = mixer.Sound('sounds\clap.wav')
  33. tom = mixer.Sound("sounds\\tom.wav")
  34.  
  35. screen = pygame.display.set_mode([WIDTH, HEIGHT])
  36. pygame.display.set_caption('The Beat Maker')
  37. label_font = pygame.font.Font('Roboto-Bold.ttf', 32)
  38. medium_font = pygame.font.Font('Roboto-Bold.ttf', 24)
  39. beat_changed = True
  40. timer = pygame.time.Clock()
  41. fps = 60
  42. beats = 8
  43. bpm = 240
  44. instruments = 6
  45. playing = True
  46. clicked = [[-1 for _ in range(beats)] for _ in range(instruments)]
  47. active_list = [1 for _ in range(instruments)]
  48. pygame.mixer.set_num_channels(instruments * 3)
  49. save_menu = False
  50. load_menu = False
  51. saved_beats = []
  52. file = open('saved_beats.txt', 'r')
  53. for line in file:
  54.     saved_beats.append(line)
  55. beat_name = ''
  56. typing = False
  57. index = 100
  58.  
  59.  
  60. def draw_grid(clicks, beat, actives):
  61.     boxes = []
  62.     left_box = pygame.draw.rect(screen, gray, [0, 0, 200, HEIGHT - 200], 5)
  63.     bottom_box = pygame.draw.rect(screen, gray, [0, HEIGHT - 200, WIDTH, 200], 5)
  64.     for i in range(instruments + 1):
  65.         pygame.draw.line(screen, gray, (0, i * 100), (200, i * 100), 3)
  66.     colors = [gray, white, gray]
  67.     hi_hat_text = label_font.render('Hi Hat', True, colors[actives[0]])
  68.     screen.blit(hi_hat_text, (30, 30))
  69.     snare_text = label_font.render('Snare', True, colors[actives[1]])
  70.     screen.blit(snare_text, (30, 130))
  71.     kick_text = label_font.render('Bass Drum', True, colors[actives[2]])
  72.     screen.blit(kick_text, (30, 230))
  73.     crash_text = label_font.render('Crash', True, colors[actives[3]])
  74.     screen.blit(crash_text, (30, 330))
  75.     clap_text = label_font.render('Clap', True, colors[actives[4]])
  76.     screen.blit(clap_text, (30, 430))
  77.     tom_text = label_font.render('Floor Tom', True, colors[actives[5]])
  78.     screen.blit(tom_text, (30, 530))
  79.     for i in range(beats):
  80.         for j in range(instruments):
  81.             if clicks[j][i] == -1:
  82.                 color = gray
  83.             else:
  84.                 if actives[j] == 1:
  85.                     color = green
  86.                 else:
  87.                     color = dark_gray
  88.             rect = pygame.draw.rect(screen, color,
  89.                                     [i * ((WIDTH - 200) // beats) + 205, (j * 100) + 5, ((WIDTH - 200) // beats) - 10,
  90.                                      90], 0, 3)
  91.             pygame.draw.rect(screen, gold, [i * ((WIDTH - 200) // beats) + 200, j * 100, ((WIDTH - 200) // beats), 100],
  92.                              5, 5)
  93.             pygame.draw.rect(screen, black,
  94.                              [i * ((WIDTH - 200) // beats) + 200, j * 100, ((WIDTH - 200) // beats), 100],
  95.                              2, 5)
  96.             boxes.append((rect, (i, j)))
  97.     active = pygame.draw.rect(screen, blue,
  98.                               [beat * ((WIDTH - 200) // beats) + 200, 0, ((WIDTH - 200) // beats), instruments * 100],
  99.                               5, 3)
  100.     return boxes
  101.  
  102.  
  103. def play_notes():
  104.     for i in range(len(clicked)):
  105.         if clicked[i][active_beat] == 1 and active_list[i] == 1:
  106.             if i == 0:
  107.                 hi_hat.play()
  108.             if i == 1:
  109.                 snare.play()
  110.             if i == 2:
  111.                 kick.play()
  112.             if i == 3:
  113.                 crash.play()
  114.             if i == 4:
  115.                 clap.play()
  116.             if i == 5:
  117.                 tom.play()
  118.  
  119.  
  120. def draw_save_menu(beat_name, typing):
  121.     pygame.draw.rect(screen, black, [0, 0, WIDTH, HEIGHT])
  122.     menu_text = label_font.render('SAVE MENU: Enter a Name for this beat', True, white)
  123.     screen.blit(menu_text, (400, 40))
  124.     exit_btn = pygame.draw.rect(screen, gray, [WIDTH - 200, HEIGHT - 100, 180, 90], 0, 5)
  125.     exit_text = label_font.render('Close', True, white)
  126.     screen.blit(exit_text, (WIDTH - 160, HEIGHT - 70))
  127.     saving_btn = pygame.draw.rect(screen, gray, [WIDTH // 2 - 100, HEIGHT * 0.75, 200, 100], 0, 5)
  128.     saving_text = label_font.render('Save Beat', True, white)
  129.     screen.blit(saving_text, (WIDTH // 2 - 70, HEIGHT * 0.75 + 30))
  130.     if typing:
  131.         pygame.draw.rect(screen, dark_gray, [400, 200, 600, 200], 0, 5)
  132.     entry_rect = pygame.draw.rect(screen, gray, [400, 200, 600, 200], 5, 5)
  133.     entry_text = label_font.render(f'{beat_name}', True, white)
  134.     screen.blit(entry_text, (430, 250))
  135.     return exit_btn, saving_btn, beat_name, entry_rect
  136.  
  137.  
  138. def draw_load_menu(index):
  139.     loaded_clicked = []
  140.     loaded_beats = 0
  141.     loaded_bpm = 0
  142.     pygame.draw.rect(screen, black, [0, 0, WIDTH, HEIGHT])
  143.     menu_text = label_font.render('LOAD MENU: Select a beat to load in', True, white)
  144.     screen.blit(menu_text, (400, 40))
  145.     exit_btn = pygame.draw.rect(screen, gray, [WIDTH - 200, HEIGHT - 100, 180, 90], 0, 5)
  146.     exit_text = label_font.render('Close', True, white)
  147.     screen.blit(exit_text, (WIDTH - 160, HEIGHT - 70))
  148.     loading_btn = pygame.draw.rect(screen, gray, [WIDTH // 2 - 100, HEIGHT * 0.87, 200, 100], 0, 5)
  149.     loading_text = label_font.render('Load Beat', True, white)
  150.     screen.blit(loading_text, (WIDTH // 2 - 70, HEIGHT * 0.87 + 30))
  151.     delete_btn = pygame.draw.rect(screen, gray, [WIDTH // 2 - 400, HEIGHT * 0.87, 200, 100], 0, 5)
  152.     delete_text = label_font.render('Delete Beat', True, white)
  153.     screen.blit(delete_text, (WIDTH // 2 - 385, HEIGHT * 0.87 + 30))
  154.     if 0 <= index < len(saved_beats):
  155.         pygame.draw.rect(screen, light_gray, [190, 100 + index*50, 1000, 50])
  156.     for beat in range(len(saved_beats)):
  157.         if beat < 10:
  158.             beat_clicked = []
  159.             row_text = medium_font.render(f'{beat + 1}', True, white)
  160.             screen.blit(row_text, (200, 100 + beat * 50))
  161.             name_index_start = saved_beats[beat].index('name: ') + 6
  162.             name_index_end = saved_beats[beat].index(', beats:')
  163.             name_text = medium_font.render(saved_beats[beat][name_index_start:name_index_end], True, white)
  164.             screen.blit(name_text, (240, 100 + beat * 50))
  165.         if 0 <= index < len(saved_beats) and beat == index:
  166.             beats_index_end = saved_beats[beat].index(', bpm:')
  167.             loaded_beats = int(saved_beats[beat][name_index_end + 8:beats_index_end])
  168.             bpm_index_end = saved_beats[beat].index(', selected:')
  169.             loaded_bpm = int(saved_beats[beat][beats_index_end + 6:bpm_index_end])
  170.             loaded_clicks_string = saved_beats[beat][bpm_index_end + 14: -3]
  171.             loaded_clicks_rows = list(loaded_clicks_string.split("], ["))
  172.             for row in range(len(loaded_clicks_rows)):
  173.                 loaded_clicks_row = (loaded_clicks_rows[row].split(', '))
  174.                 for item in range(len(loaded_clicks_row)):
  175.                     if loaded_clicks_row[item] == '1' or loaded_clicks_row[item] == '-1':
  176.                         loaded_clicks_row[item] = int(loaded_clicks_row[item])
  177.                 beat_clicked.append(loaded_clicks_row)
  178.                 loaded_clicked = beat_clicked
  179.     loaded_info = [loaded_beats, loaded_bpm, loaded_clicked]
  180.     entry_rect = pygame.draw.rect(screen, gray, [190, 90, 1000, 600], 5, 5)
  181.     return exit_btn, loading_btn, entry_rect, delete_btn, loaded_info
  182.  
  183.  
  184. run = True
  185. while run:
  186.     timer.tick(fps)
  187.     screen.fill(black)
  188.     boxes = draw_grid(clicked, active_beat, active_list)
  189.     # drawing lower menu
  190.     play_pause = pygame.draw.rect(screen, gray, [50, HEIGHT - 150, 200, 100], 0, 5)
  191.     play_text = label_font.render('Play/Pause', True, white)
  192.     screen.blit(play_text, (70, HEIGHT - 130))
  193.     if playing:
  194.         play_text2 = medium_font.render('Playing', True, dark_gray)
  195.     else:
  196.         play_text2 = medium_font.render('Paused', True, dark_gray)
  197.     screen.blit(play_text2, (70, HEIGHT - 100))
  198.     # beats per minute buttons
  199.     bpm_rect = pygame.draw.rect(screen, gray, [300, HEIGHT - 150, 200, 100], 5, 5)
  200.     bpm_text = medium_font.render('Beats Per Minute', True, white)
  201.     screen.blit(bpm_text, (308, HEIGHT - 130))
  202.     bpm_text2 = label_font.render(f'{bpm}', True, white)
  203.     screen.blit(bpm_text2, (370, HEIGHT - 100))
  204.     bpm_add_rect = pygame.draw.rect(screen, gray, [510, HEIGHT - 150, 48, 48], 0, 5)
  205.     bpm_sub_rect = pygame.draw.rect(screen, gray, [510, HEIGHT - 100, 48, 48], 0, 5)
  206.     add_text = medium_font.render('+5', True, white)
  207.     screen.blit(add_text, (520, HEIGHT - 140))
  208.     sub_text = medium_font.render('-5', True, white)
  209.     screen.blit(sub_text, (520, HEIGHT - 90))
  210.     # beats per loop buttons
  211.     beats_rect = pygame.draw.rect(screen, gray, [600, HEIGHT - 150, 200, 100], 5, 5)
  212.     beats_text = medium_font.render('Beats In Loop', True, white)
  213.     screen.blit(beats_text, (612, HEIGHT - 130))
  214.     beats_text2 = label_font.render(f'{beats}', True, white)
  215.     screen.blit(beats_text2, (670, HEIGHT - 100))
  216.     beats_add_rect = pygame.draw.rect(screen, gray, [810, HEIGHT - 150, 48, 48], 0, 5)
  217.     beats_sub_rect = pygame.draw.rect(screen, gray, [810, HEIGHT - 100, 48, 48], 0, 5)
  218.     add_text2 = medium_font.render('+1', True, white)
  219.     screen.blit(add_text2, (820, HEIGHT - 140))
  220.     sub_text2 = medium_font.render('-1', True, white)
  221.     screen.blit(sub_text2, (820, HEIGHT - 90))
  222.     # clear board button
  223.     clear = pygame.draw.rect(screen, gray, [1150, HEIGHT - 150, 200, 100], 0, 5)
  224.     play_text = label_font.render('Clear Board', True, white)
  225.     screen.blit(play_text, (1160, HEIGHT - 130))
  226.     # save and load buttons
  227.     save_button = pygame.draw.rect(screen, gray, [900, HEIGHT - 150, 200, 48], 0, 5)
  228.     save_text = label_font.render('Save Beat', True, white)
  229.     screen.blit(save_text, (920, HEIGHT - 140))
  230.     load_button = pygame.draw.rect(screen, gray, [900, HEIGHT - 98, 200, 48], 0, 5)
  231.     load_text = label_font.render('Load Beat', True, white)
  232.     screen.blit(load_text, (920, HEIGHT - 90))
  233.     # instrument rectangles
  234.     instrument_rects = []
  235.     for i in range(instruments):
  236.         rect = pygame.rect.Rect((0, i * 100), (200, 100))
  237.         instrument_rects.append(rect)
  238.     if beat_changed:
  239.         play_notes()
  240.         beat_changed = False
  241.     if save_menu:
  242.         exit_button, saving_button, beat_name, entry_rect = draw_save_menu(beat_name, typing)
  243.     elif load_menu:
  244.         exit_button, loading_button, entry_rect, delete_button, loaded_information = draw_load_menu(index)
  245.     for event in pygame.event.get():
  246.         if event.type == pygame.QUIT:
  247.             run = False
  248.         if event.type == pygame.MOUSEBUTTONDOWN and not save_menu and not load_menu:
  249.             for i in range(len(boxes)):
  250.                 if boxes[i][0].collidepoint(event.pos):
  251.                     coords = boxes[i][1]
  252.                     clicked[coords[1]][coords[0]] *= -1
  253.         if event.type == pygame.MOUSEBUTTONUP and not save_menu and not load_menu:
  254.             if play_pause.collidepoint(event.pos) and playing:
  255.                 playing = False
  256.             elif play_pause.collidepoint(event.pos) and not playing:
  257.                 playing = True
  258.                 active_beat = 0
  259.                 active_length = 0
  260.             if beats_add_rect.collidepoint(event.pos):
  261.                 beats += 1
  262.                 for i in range(len(clicked)):
  263.                     clicked[i].append(-1)
  264.             elif beats_sub_rect.collidepoint(event.pos):
  265.                 beats -= 1
  266.                 for i in range(len(clicked)):
  267.                     clicked[i].pop(-1)
  268.             if bpm_add_rect.collidepoint(event.pos):
  269.                 bpm += 5
  270.             elif bpm_sub_rect.collidepoint(event.pos):
  271.                 bpm -= 5
  272.             if clear.collidepoint(event.pos):
  273.                 clicked = [[-1 for _ in range(beats)] for _ in range(instruments)]
  274.             for i in range(len(instrument_rects)):
  275.                 if instrument_rects[i].collidepoint(event.pos):
  276.                     active_list[i] *= -1
  277.             if save_button.collidepoint(event.pos):
  278.                 save_menu = True
  279.             if load_button.collidepoint(event.pos):
  280.                 load_menu = True
  281.                 playing = False
  282.         elif event.type == pygame.MOUSEBUTTONUP:
  283.             if exit_button.collidepoint(event.pos):
  284.                 save_menu = False
  285.                 load_menu = False
  286.                 playing = True
  287.                 typing = False
  288.                 beat_name = ''
  289.             if entry_rect.collidepoint(event.pos):
  290.                 if save_menu:
  291.                     if typing:
  292.                         typing = False
  293.                     else:
  294.                         typing = True
  295.                 if load_menu:
  296.                     index = (event.pos[1] - 100) // 50
  297.             if save_menu:
  298.                 if saving_button.collidepoint(event.pos):
  299.                     file = open('saved_beats.txt', 'w')
  300.                     saved_beats.append(f'\nname: {beat_name}, beats: {beats}, bpm: {bpm}, selected: {clicked}')
  301.                     for i in range(len(saved_beats)):
  302.                         file.write(str(saved_beats[i]))
  303.                     file.close()
  304.                     save_menu = False
  305.                     load_menu = False
  306.                     playing = True
  307.                     typing = False
  308.                     beat_name = ''
  309.             if load_menu:
  310.                 if delete_button.collidepoint(event.pos):
  311.                     if 0 <= index < len(saved_beats):
  312.                         saved_beats.pop(index)
  313.                 if loading_button.collidepoint(event.pos):
  314.                     if 0 <= index < len(saved_beats):
  315.                         beats = loaded_information[0]
  316.                         bpm = loaded_information[1]
  317.                         clicked = loaded_information[2]
  318.                         index = 100
  319.                         save_menu = False
  320.                         load_menu = False
  321.                         playing = True
  322.                         typing = False
  323.         if event.type == pygame.TEXTINPUT and typing:
  324.             beat_name += event.text
  325.         if event.type == pygame.KEYDOWN:
  326.             if event.key == pygame.K_BACKSPACE and len(beat_name) > 0:
  327.                 beat_name = beat_name[:-1]
  328.  
  329.     beat_length = 3600 // bpm
  330.  
  331.     if playing:
  332.         if active_length < beat_length:
  333.             active_length += 1
  334.         else:
  335.             active_length = 0
  336.             if active_beat < beats - 1:
  337.                 active_beat += 1
  338.                 beat_changed = True
  339.             else:
  340.                 active_beat = 0
  341.                 beat_changed = True
  342.  
  343.     pygame.display.flip()
  344.  
  345. file = open('saved_beats.txt', 'w')
  346. for i in range(len(saved_beats)):
  347.     file.write(str(saved_beats[i]))
  348. file.close()
  349. pygame.quit()
  350.  
Advertisement
RAW Paste Data Copied
Advertisement