简体   繁体   English

pygame中的screen.blit(image),图像消失

[英]screen.blit(image) in pygame, image disappears

I'm trying to blit an image in a pygame window based on one event, a midi input, and then blit another image based on the next event, another midi input. 我正在尝试根据一个事件(MIDI输入)在pygame窗口中对齐图像,然后根据下一个事件(另一个Midi输入)对另一图像进行对齐。 What happens here is that the image pops up just for a second when I press a key on the midi keyboard. 这里发生的是,当我按下Midi键盘上的一个键时,图像仅弹出一秒钟。 I need it to stay in the window, even after I enter another input and blit another image. 即使在我输入其他输入并遮住另一张图像后,我也需要将其保留在窗口中。 What happens with this is that each key on my midi keyboard gets recorded as a number (1-88) and added to NoteList[] then an image is blit in the window using h , an item in NoteList as the x coordinate. 这样做的结果是,我的Midi键盘上的每个键都被记录为数字( NoteList[] )并添加到NoteList[]然后使用h (在NoteList中的NoteList作为x坐标)在窗口中将NoteList In my actual pygame the h variable will also go through a bunch of functions, I am just trying to figure out this part in a simpler pygame window. 在我实际的pygame中, h变量还将通过一堆函数,我只是想在一个更简单的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()

I belive that the problem is that you fill the screen every time the while loop runs but you just blit that image on the screen when the event occurs... 我相信问题是每次while循环运行时您都会填满屏幕,但是当事件发生时您只是在屏幕上抹掉了图像...

Try this: 尝试这个:

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()

You need to define the NoteList outside of the while loop if the notes should persist, otherwise you create a new empty list each iteration. 如果要保留注释,则需要在while循环之外定义NoteList ,否则,每次迭代都创建一个新的空列表。

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()

You can use the enumerate function to shift the position: 您可以使用enumerate函数移动位置:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM