So I'm making a piano and I created an animation that would demonstrate that the key has been pressed. I've only made it so that it works when clicking a tile and now I'm trying to link for example, C3 to the key K_s, and when you press the key "s", the animation would play. However, I haven't been able to find a way to do this since I didn't use loops or lists.

import pygame
pygame.init()
pygame.mixer.init()
pygame.mixer.set_num_channels(50)


WIDTH = 1820
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT ), pygame.RESIZABLE)
pygame.display.set_caption("Piano")

fps = 60
timer = pygame.time.Clock()
white_font = pygame.font.Font(None,30)
black_font = pygame.font.Font(None, 20)

class WhiteKeys:
    def __init__(self, fileName, text, width, height, pos, elevation):
        self.sound = pygame.mixer.Sound(fileName)
        self.pressed = False

        self.elevation = elevation
        self.dynamic_elevation = elevation
        self.original_y_pos = pos[1]

        #top rect
        self.top_rect = pygame.Rect(pos, (width, height))
        self.top_color = '#FFFFFF'

        #bottom rect
        self.bottom_rect = pygame.Rect(pos, (width, height))
        self.bottom_color = '#354B5E'

        #text rect
        self.text = white_font.render(text, True, '#000000')
        self.text_rect = self.text.get_rect(bottom = self.top_rect.bottom)

    def drawWhites(self, screen):


        self.top_rect.y = self.original_y_pos - self.dynamic_elevation
        self.text_rect.midbottom = self.top_rect.midbottom

        self.bottom_rect.midtop = self.top_rect.midtop
        self.bottom_rect.height = self.top_rect.height + self.dynamic_elevation

        pygame.draw.rect(screen, self.bottom_color, self.bottom_rect, border_radius=5)
        pygame.draw.rect(screen, self.top_color, self.top_rect, border_radius=5)

        screen.blit(self.text, self.text_rect)
        self.check_click()

    def check_click(self):
        mouse_pos = pygame.mouse.get_pos()
        if self.top_rect.collidepoint(mouse_pos):
            if pygame.mouse.get_pressed()[0] and self.pressed == False and not hover_black:
                self.sound.play(maxtime=700)
                self.top_color = '#DCDCDC'
                self.dynamic_elevation = 0
                self.pressed = True
            elif not pygame.mouse.get_pressed()[0]:
                self.dynamic_elevation = self.elevation
                self.pressed = False
                self.top_color = '#FFFFFF'
        else:
            self.dynamic_elevation = self.elevation
            self.top_color = '#FFFFFF'class WhiteKeys:
    def __init__(self, fileName, text, width, height, pos, elevation):
        self.sound = pygame.mixer.Sound(fileName)
        self.pressed = False

        self.elevation = elevation
        self.dynamic_elevation = elevation
        self.original_y_pos = pos[1]

        #top rect
        self.top_rect = pygame.Rect(pos, (width, height))
        self.top_color = '#FFFFFF'

        #bottom rect
        self.bottom_rect = pygame.Rect(pos, (width, height))
        self.bottom_color = '#354B5E'

        #text rect
        self.text = white_font.render(text, True, '#000000')
        self.text_rect = self.text.get_rect(bottom = self.top_rect.bottom)

    def drawWhites(self, screen):


        self.top_rect.y = self.original_y_pos - self.dynamic_elevation
        self.text_rect.midbottom = self.top_rect.midbottom

        self.bottom_rect.midtop = self.top_rect.midtop
        self.bottom_rect.height = self.top_rect.height + self.dynamic_elevation

        pygame.draw.rect(screen, self.bottom_color, self.bottom_rect, border_radius=5)
        pygame.draw.rect(screen, self.top_color, self.top_rect, border_radius=5)

        screen.blit(self.text, self.text_rect)
        self.check_click()

    def check_click(self):
        mouse_pos = pygame.mouse.get_pos()
        if self.top_rect.collidepoint(mouse_pos):
            if pygame.mouse.get_pressed()[0] and self.pressed == False and not hover_black:
                self.sound.play(maxtime=700)
                self.top_color = '#DCDCDC'
                self.dynamic_elevation = 0
                self.pressed = True
            elif not pygame.mouse.get_pressed()[0]:
                self.dynamic_elevation = self.elevation
                self.pressed = False
                self.top_color = '#FFFFFF'
        else:
            self.dynamic_elevation = self.elevation
            self.top_color = '#FFFFFF'

C3 = WhiteKeys('Sounds/C3.wav', "C3", 35, 230, (576, HEIGHT-235), 6)
D3 = WhiteKeys('Sounds/D3.wav', "D3", 35, 230, (612, HEIGHT-235), 6)
E3 = WhiteKeys('Sounds/E3.wav', "E3", 35, 230, (648, HEIGHT-235), 6)
F3 = WhiteKeys('Sounds/F3.wav', "F3", 35, 230, (684, HEIGHT-235), 6)
G3 = WhiteKeys('Sounds/G3.wav', "G3", 35, 230, (720, HEIGHT-235), 6)
A3 = WhiteKeys('Sounds/A3.wav', "A3", 35, 230, (756, HEIGHT-235), 6)
B3 = WhiteKeys('Sounds/B3.wav', "B3", 35, 230, (792, HEIGHT-235), 6)

run = True
while run:
    timer.tick(fps)
    screen.fill('gray')

    hover_black = False
    for key in black_keys:
        if key.check_hover():
            hover_black = True
            break
    pygame.draw.rect(screen, (36, 37, 38),(30,287,1380,300),0, 7)

    C3.drawWhites(screen)
    D3.drawWhites(screen)
    E3.drawWhites(screen)
    F3.drawWhites(screen)
    G3.drawWhites(screen)
    A3.drawWhites(screen)
    B3.drawWhites(screen)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    pygame.display.flip()

pygame.quit()

I'm gonna be using these keys to play the white notes: S to play C3, D for D3, F for E3, G for F3, H for G3, J for A3 and K for B3

I tried making a def check_key(self): function but the issue is linking a specific key to a specific tile, and then doing that for the other 6 more tiles.

0

There are 0 best solutions below