0
キーを押すとコンピュータで生成されたサウンドが再現されるコードがありますが、懸念事項はどのようにキーをリスト内で押された順番で保存して印刷できるのですか?ここでキーボード入力をリストに保存できますか?
はコードです:ここでは
from array import array
import pygame
from pygame.mixer import Sound, get_init, pre_init
class Note(pygame.mixer.Sound):
def __init__(self, frequency, volume=.1):
self.frequency = frequency
Sound.__init__(self, self.build_samples())
self.set_volume(volume)
def build_samples(self):
period = int(round(get_init()[0]/self.frequency))
samples = array("h", [0] * period)
amplitude = 2 ** (abs(get_init()[1]) - 1) - 1
for time in range(period):
if time < period/2:
samples[time] = amplitude
else:
samples[time] = -amplitude
return samples
pre_init(44100, -16, 1, 1024)
pygame.init()
screen = pygame.display.set_mode([640, 480], 0)
sounds = {}
keymap = {pygame.K_p: 880, pygame.K_r: 440}
while True:
key_pressed=[]
evt = pygame.event.wait()
if evt.type == pygame.QUIT:
break
elif evt.type == pygame.KEYDOWN:
if evt.key in keymap:
note = Note(keymap[evt.key])
note.play(-1)
sounds[evt.key] = note
key_pressed.append(note)
elif evt.type == pygame.KEYUP:
if evt.key in sounds:
sounds.pop(evt.key).stop()
あなたはほとんどそれを持っているようにそれはあなたが 'あなたのループの先頭に戻って、空のリストにkey_pressed'リセットすることを除いて、見えます。その文をループ外に移動してみてください。 –