snakeware

Python-based Linux distro for x86-64 and RPi
git clone git://squid-tech.com/snakeware.git
Log | Files | Refs | README

commit 70fe40e451439cb8247a040bf6ab511da37ab04a
parent 434b264ae6143ee1a0ae2bf92845c382f5d8a5ea
Author: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Date:   Wed, 10 Jun 2020 20:33:48 +0000

fixup! Format Python code with psf/black push

Diffstat:
Msnakewm/apps/fun/piano/piano.py | 87++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
1 file changed, 60 insertions(+), 27 deletions(-)

diff --git a/snakewm/apps/fun/piano/piano.py b/snakewm/apps/fun/piano/piano.py @@ -17,16 +17,26 @@ BLUE = 0, 0, 255 YELLOW = 255, 255, 0 BACKGROUND = 33, 41, 46 GRAY = 76, 80, 82 -LW = 10 # outline width -BKW = 22 # black key width -BKH = 200 # black key height -H = 350 # white key height +LW = 10 # outline width +BKW = 22 # black key width +BKH = 200 # black key height +H = 350 # white key height bkeys = {1: 1, 2: 3, 4: 6, 5: 8, 6: 10} wkeys = {0: 0, 1: 2, 2: 4, 3: 5, 4: 7, 5: 9, 6: 11, 7: 12} RECT_NORMAL = pygame.Rect((236, 432, 130, 55)) # button rects RECT_SIMON = pygame.Rect((400, 432, 150, 55)) NOTELEN = 0.6 -SIMONCOL = (RED, ORANGE, GREEN, ORANGE, BLUE, ORANGE, ORANGE, YELLOW) # colors when in Simon mode +SIMONCOL = ( + RED, + ORANGE, + GREEN, + ORANGE, + BLUE, + ORANGE, + ORANGE, + YELLOW, +) # colors when in Simon mode + class Piano(pygame_gui.elements.UIWindow): def __init__(self, pos, manager): @@ -67,61 +77,80 @@ class Piano(pygame_gui.elements.UIWindow): def process_event(self, event): super().process_event(event) r = super().get_abs_rect() - if event.type == pygame.QUIT: self.running = False + if event.type == pygame.QUIT: + self.running = False if event.type == pygame.MOUSEBUTTONDOWN: x, y = pygame.mouse.get_pos() x -= r[0] + 16 y -= r[1] + 40 - p = self.pos2key(x,y) + p = self.pos2key(x, y) if p != None: self.play(p) if RECT_NORMAL.collidepoint(x, y) and self.simon: self.simon = False - super().set_display_title('piano') + super().set_display_title("piano") if RECT_SIMON.collidepoint(x, y) and not self.simon: self.simon = True self.simonseq = [random.choice([0, 4, 7, 12]) for q in range(100)] self.simonlen = 1 self.simonplay = True self.simonstart = time.time() - super().set_display_title('simon') + super().set_display_title("simon") if event.type == pygame.KEYDOWN: - if event.key == pygame.K_s or event.key == pygame.K_1 or event.key == pygame.K_LEFT: + if ( + event.key == pygame.K_s + or event.key == pygame.K_1 + or event.key == pygame.K_LEFT + ): self.play(0) if event.key == pygame.K_d: self.play(2) - if event.key == pygame.K_f or event.key == pygame.K_2 or event.key == pygame.K_DOWN: + if ( + event.key == pygame.K_f + or event.key == pygame.K_2 + or event.key == pygame.K_DOWN + ): self.play(4) if event.key == pygame.K_g: self.play(5) - if event.key == pygame.K_h or event.key == pygame.K_3 or event.key == pygame.K_RIGHT: + if ( + event.key == pygame.K_h + or event.key == pygame.K_3 + or event.key == pygame.K_RIGHT + ): self.play(7) if event.key == pygame.K_j: self.play(9) if event.key == pygame.K_k: self.play(11) - if event.key == pygame.K_l or event.key == pygame.K_4 or event.key == pygame.K_UP: + if ( + event.key == pygame.K_l + or event.key == pygame.K_4 + or event.key == pygame.K_UP + ): self.play(12) if event.key == pygame.K_e: self.play(1) if event.key == pygame.K_r: self.play(3) - if event.key == pygame.K_y or event.key == pygame.K_z: # QWERTY/QWERTZ layout + if ( + event.key == pygame.K_y or event.key == pygame.K_z + ): # QWERTY/QWERTZ layout self.play(6) if event.key == pygame.K_u: self.play(8) if event.key == pygame.K_i: self.play(10) - def play(self, k, user = True): + def play(self, k, user=True): "Play a note" self.audio[k].play() self.playnote = k self.playtime = time.time() if self.simon and user == True: self.userseq.append(k) - if self.userseq == self.simonseq[:self.simonlen]: + if self.userseq == self.simonseq[: self.simonlen]: print(self.simonlen, "CORRECT!") self.update(NOTELEN) time.sleep(NOTELEN) @@ -132,32 +161,37 @@ class Piano(pygame_gui.elements.UIWindow): self.simonlen += 1 self.simonplay = True self.simonstart = time.time() - super().set_display_title('simon (%u notes correct)' % (self.simonlen - 1)) - if self.userseq and self.userseq[-1] != self.simonseq[len(self.userseq) - 1]: + super().set_display_title( + "simon (%u notes correct)" % (self.simonlen - 1) + ) + if ( + self.userseq + and self.userseq[-1] != self.simonseq[len(self.userseq) - 1] + ): print("WRONG!") time.sleep(NOTELEN) self.audio["buzz"].play() - time.sleep(.8) + time.sleep(0.8) self.simonplay = True self.simonstart = time.time() self.userseq = [] def draw_wkey(self, k, c): "Draw white key" - pygame.draw.rect(self.win, BLACK, (100*k, 0, 100, H)) - pygame.draw.rect(self.win, c, (int(100*k+LW/2), LW, 100-LW, H-2*LW)) + pygame.draw.rect(self.win, BLACK, (100 * k, 0, 100, H)) + pygame.draw.rect(self.win, c, (int(100 * k + LW / 2), LW, 100 - LW, H - 2 * LW)) def draw_bkey(self, k, c): "Draw black key" - pygame.draw.rect(self.win, c, (100*k-BKW, 0, 2*BKW, BKH)) + pygame.draw.rect(self.win, c, (100 * k - BKW, 0, 2 * BKW, BKH)) def pos2key(self, x, y): "Get key num from mouse click position" for k in 1, 2, 4, 5, 6: - if y < BKH and (100*k-BKW < x < 100*k+BKW): + if y < BKH and (100 * k - BKW < x < 100 * k + BKW): return bkeys[k] for k in range(8): - if y < H and 100*k < x < 100*(k+1): + if y < H and 100 * k < x < 100 * (k + 1): return wkeys[k] def update(self, delta): @@ -170,14 +204,14 @@ class Piano(pygame_gui.elements.UIWindow): self.lastplayed = -1 self.userseq = [] elif n > self.lastplayed: - self.play(self.simonseq[n], user = False) + self.play(self.simonseq[n], user=False) self.lastplayed = n self.win.fill(BACKGROUND) if not self.simon: if time.time() - self.playtime > 0.2: self.playnote = None else: - if time.time() - self.playtime > .85 * NOTELEN: + if time.time() - self.playtime > 0.85 * NOTELEN: self.playnote = None for k in range(8): c = WHITE @@ -204,4 +238,3 @@ class Piano(pygame_gui.elements.UIWindow): self.win.blit(self.overlay, (0, 0)) self.dsurf.image.blit(self.win, (0, 0)) -