2017-12-06 11 views
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() 
+2

あなたはほとんどそれを持っているようにそれはあなたが 'あなたのループの先頭に戻って、空のリストにkey_pressed'リセットすることを除いて、見えます。その文をループ外に移動してみてください。 –

答えて

0

が問題です!

while True: 
    key_pressed=[] 

、ここソリューションです:

key_pressed_list = key_pressed 
print(key_pressed_list) 
関連する問題