屏幕.blit(图像) 在 pygame 中,图像消失



我正在尝试基于一个事件(一个 midi 输入(在 pygame 窗口中对图像进行 bling 处理,然后根据下一个事件(另一个 midi 输入(对另一个图像进行 blit。这里发生的事情是,当我按下 midi 键盘上的键时,图像只会弹出一秒钟。我需要它留在窗口中,即使我输入另一个输入并点亮另一个图像。发生这种情况的情况是,我的 midi 键盘上的每个键都被记录为一个数字 (1-88( 并添加到NoteList[]然后使用h在窗口中对图像进行 bled,NoteList中的项目作为 x 坐标。在我的实际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()

如果注释应该保留,则需要在 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()

您可以使用enumerate函数来移动位置:

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

我相信问题是每次 while 循环运行时您都会填满屏幕,但您只是在事件发生时在屏幕上将该图像块状......

试试这个:

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

最新更新