-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathStep4.py
More file actions
151 lines (132 loc) · 5.49 KB
/
Step4.py
File metadata and controls
151 lines (132 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import pygame
import piano_lists as pl
from pygame import mixer
pygame.init()
pygame.mixer.set_num_channels(50)
font = pygame.font.Font('assets/Terserah.ttf', 48)
medium_font = pygame.font.Font('assets/Terserah.ttf', 28)
small_font = pygame.font.Font('assets/Terserah.ttf', 16)
real_small_font = pygame.font.Font('assets/Terserah.ttf', 10)
fps = 60
timer = pygame.time.Clock()
WIDTH = 52 * 35
HEIGHT = 400
screen = pygame.display.set_mode([WIDTH, HEIGHT])
pygame.display.set_caption('My Python Piano')
active_whites = []
active_blacks = []
white_sounds = []
black_sounds = []
left_hand = pl.left_hand
right_hand = pl.right_hand
piano_notes = pl.piano_notes
white_notes = pl.white_notes
black_notes = pl.black_notes
black_labels = pl.black_labels
for i in range(len(white_notes)):
white_sounds.append(mixer.Sound(f'assets\\notes\\{white_notes[i]}.wav'))
for i in range(len(black_notes)):
black_sounds.append(mixer.Sound(f'assets\\notes\\{black_notes[i]}.wav'))
left_oct = 4
right_oct = 5
def draw_piano(whites, blacks):
white_rects = []
for i in range(52):
rect = pygame.draw.rect(screen, 'white', [i * 35, HEIGHT - 300, 35, 300], 0, 2)
white_rects.append(rect)
pygame.draw.rect(screen, 'black', [i * 35, HEIGHT - 300, 35, 300], 2, 2)
key_label = small_font.render(white_notes[i], True, 'black')
screen.blit(key_label, (i * 35 + 3, HEIGHT - 20))
skip_count = 0
last_skip = 2
skip_track = 2
black_rects = []
for i in range(36):
rect = pygame.draw.rect(screen, 'black', [23 + (i * 35) + (skip_count * 35), HEIGHT - 300, 24, 200], 0, 2)
for q in range(len(blacks)):
if blacks[q][0] == i:
if blacks[q][1] > 0:
pygame.draw.rect(screen, 'green', [23 + (i * 35) + (skip_count * 35), HEIGHT - 300, 24, 200], 2, 2)
blacks[q][1] -= 1
key_label = real_small_font.render(black_labels[i], True, 'white')
screen.blit(key_label, (25 + (i * 35) + (skip_count * 35), HEIGHT - 120))
black_rects.append(rect)
skip_track += 1
if last_skip == 2 and skip_track == 3:
last_skip = 3
skip_track = 0
skip_count += 1
elif last_skip == 3 and skip_track == 2:
last_skip = 2
skip_track = 0
skip_count += 1
for i in range(len(whites)):
if whites[i][1] > 0:
j = whites[i][0]
pygame.draw.rect(screen, 'green', [j * 35, HEIGHT - 100, 35, 100], 2, 2)
whites[i][1] -= 1
return white_rects, black_rects, whites, blacks
run = True
while run:
left_dict = {'Z': f'C{left_oct}',
'S': f'C#{left_oct}',
'X': f'D{left_oct}',
'D': f'D#{left_oct}',
'C': f'E{left_oct}',
'V': f'F{left_oct}',
'G': f'F#{left_oct}',
'B': f'G{left_oct}',
'H': f'G#{left_oct}',
'N': f'A{left_oct}',
'J': f'A#{left_oct}',
'M': f'B{left_oct}'}
right_dict = {'R': f'C{right_oct}',
'5': f'C#{right_oct}',
'T': f'D{right_oct}',
'6': f'D#{right_oct}',
'Y': f'E{right_oct}',
'U': f'F{right_oct}',
'8': f'F#{right_oct}',
'I': f'G{right_oct}',
'9': f'G#{right_oct}',
'O': f'A{right_oct}',
'0': f'A#{right_oct}',
'P': f'B{right_oct}'}
timer.tick(fps)
screen.fill('gray')
white_keys, black_keys, active_whites, active_blacks = draw_piano(active_whites, active_blacks)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
black_key = False
for i in range(len(black_keys)):
if black_keys[i].collidepoint(event.pos):
black_sounds[i].play(0, 1000)
black_key = True
active_blacks.append([i, 30])
for i in range(len(white_keys)):
if white_keys[i].collidepoint(event.pos) and not black_key:
white_sounds[i].play(0, 1000)
active_whites.append([i, 30])
if event.type == pygame.TEXTINPUT:
if event.text.upper() in left_dict:
if left_dict[event.text.upper()][1] == '#':
index = black_labels.index(left_dict[event.text.upper()])
black_sounds[index].play(0, 1000)
active_blacks.append([index, 30])
else:
index = white_notes.index(left_dict[event.text.upper()])
white_sounds[index].play(0, 1000)
active_whites.append([index, 30])
if event.text.upper() in right_dict:
if right_dict[event.text.upper()][1] == '#':
index = black_labels.index(right_dict[event.text.upper()])
black_sounds[index].play(0, 1000)
active_blacks.append([index, 30])
else:
index = white_notes.index(right_dict[event.text.upper()])
white_sounds[index].play(0, 1000)
active_whites.append([index, 30])
pygame.display.flip()
pygame.quit()