2017-08-11 536 views
1

我試圖在基於一個事件,一個MIDI輸入pygame的窗口位塊傳輸的圖像,然後根據的blit的下一個事件,另一個MIDI輸入另一個圖像。這裏發生的是當我按下midi鍵盤上的按鍵時,圖像彈出一秒鐘。我需要它留在窗口中,即使在我輸入另一個輸入並輸出另一個圖像之後。如何處理此情況是,我的MIDI鍵盤上的每個按鍵被記錄爲數字(1-88),並加入到NoteList[]那麼圖像是使用h,項目blit的窗口中NoteList x座標。在我的實際pygame中的h變量也將通過一組函數,我只是想弄清楚這部分以簡單的pygame的窗口。screen.blit(圖像)pygame的,圖像消失

going = True 
while going: 
    screen.fill(white) 
    events = event_get() 
    NoteList=[] 
    for e in events: 
     if e.type in [QUIT]: 
      going = False 
     if e.type in [KEYDOWN]: 
      going = False 
      events = pygame.event.get() 
     if e.type in [pygame.midi.MIDIIN]: 
      print(str(e.data1)) 
      NoteList.append(int(e.data1-20)) 
    for h in NoteList: 
      screen.blit(EthnoteIMG, (int(h), 100)) 
    pygame.display.update() 

回答

0

您需要定義NoteList外while循環,如果票據應該堅持的,否則你創建一個新的空單每一次迭代。

NoteList = [] 

going = True 
while going: 
    for e in pygame.event.get(): 
     if e.type == pygame.QUIT: 
      going = False 
     elif e.type == pygame.midi.MIDIIN: 
      print(str(e.data1)) 
      NoteList.append(int(e.data1-20)) 

    screen.fill(white) 

    for h in NoteList: 
     screen.blit(EthnoteIMG, (int(h), 100)) 

    pygame.display.update() 

可以使用enumerate函數來移動位置:

for g, h in enumerate(NoteList): 
    screen.blit(EthnoteIMG, (g*12, int(h))) 
+0

我還建議添加一個時鐘'時鐘= pygame.time.Clock()',然後限制幀的通過在while循環中調用'clock.tick(30)#或60'來提高速度。 – skrx

+0

好的,謝謝。你知道我會怎麼做這樣的while循環內:G = 0,而g>的8:G + = 1個screen.blit(IMG,(G * 12),INT(H)),它只有通過去循環一次,我每次輸入的東西通過MIDI –

+0

我已經添加了一個段落的答案。那是你要的嗎? – skrx

0

我相信這個問題是你每次充滿屏幕,而循環運行,但你剛纔的blit在屏幕上的形象在事件發生時...

試試這個:

screen.fill(white) 
going = True 
while going: 
    events = event_get() 
    NoteList=[] 
    for e in events: 
     if e.type in [QUIT]: 
      going = False 
     if e.type in [KEYDOWN]: 
      going = False 
      events = pygame.event.get() 
     if e.type in [pygame.midi.MIDIIN]: 
      print(str(e.data1)) 
      NoteList.append(int(e.data1-20)) 
    for h in NoteList: 
     screen.fill(white) 
     screen.blit(EthnoteIMG, (int(h), 100)) 
    pygame.display.update()