Python实现的空灵鼓模拟器源代码,可定制曲谱,简单易学

科技   科技   2023-06-09 20:59   云南  

Python实现的空灵鼓模拟器源代码,可定制乐谱,简单易学,单靠鼠标就可以演奏一着曲子

核心代码

import pygameimport sysimport mathfrom pygame.locals import *pygame.init()clock = pygame.time.Clock()# 定义一个自定义事件,去添加music_symbolADD_MUSIC_SYMBOL = pygame.USEREVENT + 1# 记录自定义事件的开始时间,800后面的单位是mspygame.time.set_timer(ADD_MUSIC_SYMBOL, 800)all_music_symbols = pygame.sprite.Group()

# class MusicTextGroup:# def __init__(self):# self.all_music_text = []# self.all_text_count = 0# self.all_display_music_text = []# self.move_pos_x = 0# self.dx = 3# self.ONE_TEXT_WIDTH = 50## def add_all_music_text (self, music_text_str):# for one_text_str in music_text_str:# self.all_music_text.append(MusicText(one_text_str, 1080, 20))# self.all_text_count += 1# for i in range(90):# self.move()## def add_one_to_display(self):# if self.all_text_count > 0:# self.all_text_count -= 1# self.all_display_music_text.append(self.all_music_text[0])# del self.all_music_text[0]## def move(self):# for music_text in self.all_display_music_text:# music_text.move(self.dx)# self.move_pos_x += self.dx# if self.move_pos_x >= self.ONE_TEXT_WIDTH:# self.move_pos_x = 0# self.add_one_to_display()## def update(self):# for music_text in self.all_display_music_text:# music_text.update()

class MusicText(pygame.sprite.Sprite): def __init__(self, num_str, pos_x, pos_y): super(MusicText, self).__init__() self.num_str = num_str self.pos_x = pos_x self.pos_y = pos_y self.dx = 4 self.display = True self.font_size = 60 self.font = pygame.font.Font(None, self.font_size)
def move(self, dx=3, stop=False): self.dx = dx if stop == False: if self.pos_x > 0: self.pos_x -= self.dx if 550 < self.pos_x < 600: self.font_size = 80 else: self.font_size = 60 if self.pos_x <= 500: self.kill()
self.font = pygame.font.Font(None, self.font_size)
def update(self): # screen.blit() if self.display: image_text = self.font.render(self.num_str, True, "red") # print(self.pos_x, self.pos_y) screen.blit(image_text, (self.pos_x, self.pos_y)) # pass

class MusicDance: def __init__(self, music_symbol_image): self.image = pygame.image.load("./images/" + music_symbol_image) self.display = False self.display_count = 0 self.DISPLAY_MAX = 60 self.dy = 5 self.pos_x = 0 self.pos_y = 0
def start_display(self, start_x, start_y): self.pos_x = start_x - 30 self.pos_y = start_y self.display = True self.display_count = 0
def update(self): if self.display: self.pos_y -= self.dy self.display_count += 1 # temp_image = self.image temp_image = pygame.transform.scale(self.image, (self.image.get_width()//self.display_count, self.image.get_height()//self.display_count)) screen.blit(temp_image, (self.pos_x, self.pos_y))
if self.display_count >= self.DISPLAY_MAX: self.display = False

class Num(pygame.sprite.Sprite): def __init__(self, image_name, pos): super(Num, self).__init__() self.label = image_name self.image_list = [] self.image_list.append(pygame.image.load("./images/" + image_name + ".png")) self.image_list.append(pygame.image.load("./images/" + image_name + "_flip.png")) self.image_index = 0 self.pos_x = pos[0] self.pos_y = pos[1] rect = self.image_list[0].get_rect() self.circle_center = rect.center self.radius = math.sqrt(rect.width**2 + rect.height**2) // 2 self.sound = pygame.mixer.Sound("./sounds/" + self.label + ".mp3") # pygame.draw.circle(Surface, color, pos, radius, width=0)
def update_index(self): if self.image_index == 0: self.image_index = 1 else: self.image_index = 0
def play(self): self.sound.play()
def draw(self): self.rect = pygame.draw.circle(screen, "black", ((self.circle_center[0]-5) + self.pos_x, (self.circle_center[1]-5) + self.pos_y), self.radius, width=1) # temp = self.image_list[self.image_index].get_rect() # self.rect = pygame.Rect() # temp.x = self.pos_x # temp.y = self.pos_y # screen.fill("green", self.rect) # screen.fill("yellow", temp) screen.blit(self.image_list[self.image_index], (self.pos_x, self.pos_y))

pygame.init()WIDTH = 1280HEIGHT = 720screen = pygame.display.set_mode((WIDTH, HEIGHT))boot_image = pygame.image.load("./images/boot_image.png")bg_image = pygame.image.load("./images/bg_image.png")stage = 0Num_info_dict = {"1": (930, 521), "2": (665, 510), "3": (1005, 430), "4": (590, 420), "5": (1024, 318), "6": (595, 309), "7": (1000, 225), "1dot": (607, 209), "2dot": (929, 133), "3dot": (694, 122), "5dot": (805, 312), "6dot": (805, 530), "7dot": (821, 107)}# Num_info_dict = {"1": (930, 521)}Num_obj_list = []for num_label, num_pos in Num_info_dict.items(): # print(num_pos, num_label) Num_obj = Num(num_label, num_pos) Num_obj_list.append(Num_obj)
# sys.exit()bg_color = ["red", "orange", "yellow", "blue", "green", "cyan", "purple"]bg_color_index = 0
music_dance = MusicDance("music_symbol.png")small_rect = pygame.Rect(507, 17, 50, 45)big_rect = pygame.Rect(555, 9, 70, 55)mid_rect = pygame.Rect(621, 15, 649, 45)small_rect_img = pygame.image.load("./images/small_rect.png")big_rect_img = pygame.image.load("./images/big_rect.png")mid_rect_img = pygame.image.load("./images/mid_rect.png")

# music_num = MusicText("1", 1080, 20)music_text_list = ["3", "5", "5","6", "5", "3","1", "1","2", "3", "3", "2", "1", "2"," ",\ "3", "5", "5","6", "5", "3","1", "1","2", "3", "3", "2", "2", "1",]MUSIC_TEXT_LEN = len(music_text_list)music_text_index = 0stop = False# music_text_group = MusicTextGroup()# music_text_group.add_all_music_text(["3", "5", "5","6", "5", "3","1", "1","2", "3", "3", "2", "1", "2"," ",\# "3", "5", "5","6", "5", "3","1", "1","2", "3", "3", "2", "2", "1",])while True: for event in pygame.event.get(): if event.type == QUIT: sys.exit() if event.type == KEYUP: if event.key == K_SPACE: stop = not stop if not stop: pygame.time.set_timer(ADD_MUSIC_SYMBOL, 800) elif event.type == ADD_MUSIC_SYMBOL: if not stop: music_symbol = MusicText(music_text_list[music_text_index], 1120, 20) # symbol_inex all_music_symbols.add(music_symbol) music_text_index += 1 if music_text_index == MUSIC_TEXT_LEN: music_text_index = 0 elif event.type == MOUSEBUTTONDOWN: if stage == 1: mouse_x, mouse_y = event.pos for num_obj in Num_obj_list: if num_obj.rect.collidepoint(mouse_x, mouse_y): num_obj.update_index() music_dance.start_display(num_obj.pos_x, num_obj.pos_y) num_obj.play() # print("down") elif event.type == MOUSEBUTTONUP: if stage == 1: mouse_x, mouse_y = event.pos for num_obj in Num_obj_list: if num_obj.rect.collidepoint(mouse_x, mouse_y): # print(num_obj.label) bg_color_index += 1 if bg_color_index >= len(bg_color): bg_color_index = 0 num_obj.update_index() break
if stage == 0: screen.blit(boot_image, (0, 0)) stage = 1 if stage == 1: # fill back color screen.fill(bg_color[bg_color_index]) # screen.fill("pink", small_rect) screen.blit(small_rect_img, (507, 17)) screen.blit(big_rect_img, (553, 9)) screen.blit(mid_rect_img, (621, 15)) # screen.fill("gold", big_rect) # screen.fill("gray", mid_rect) # blit drump # for num ; display all nums screen.blit(bg_image, (0, 0)) for num_obj in Num_obj_list: num_obj.draw()
for music_symbol in all_music_symbols: music_symbol.move(stop=stop) music_symbol.update() # music_text_group.move() # music_text_group.update() music_dance.update() pygame.display.update() clock.tick(20)

完整代码下载地址:

https://download.csdn.net/download/weixin_42756970/87264145


Python代码大全
Python源程序、源代码、源码分享,Python代码大全,Python源代码学习,Python入门,Python基础教程。
 最新文章