如何在遍历文件名列表时 blit 多个图像?

How to blit multiple images while looping over a list of filenames?

我正在尝试对行驶中的汽车的多个图像进行 blit,对图像的文件名使用 for 循环。但是,它只会绘制屏幕而不是 show/blit 图像。我正在使用 python3.6 .

这是我的代码。

import pandas as pd
import pygame

# BLACK = (  0,   0,   0)
# WHITE = (255, 255, 255)
# BLUE =  (  0,   0, 255)
# GREEN = (  0, 255,   0)
# RED =   (255,   0,   0)

df = pd.read_csv('./result.csv')

preds = df['Predicted Angles']
true = df['Actual Angles']
filenames = df['File']

pygame.init()
size = (640, 320)
pygame.display.set_caption("Data viewer")
screen = pygame.display.set_mode(size, pygame.DOUBLEBUF)
myfont = pygame.font.SysFont("monospace", 15)

for i in range(len(list(filenames))):
    img = pygame.image.load(filenames.iloc[i])
    screen.blit(img, (0, 0))
    pygame.display.flip()

View of the results.csv

首先加载所有图像并将它们放入列表或其他数据结构中,然后将当前图像分配给一个变量并在所需的时间间隔后更改它(您可以使用之一)。

我只是在下面的示例中使用了一些彩色 pygame.Surfaces,并借助自定义事件和将事件添加到的 pygame.time.set_timer 函数更改了当前 image/surface指定时间过去后的事件队列。

import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')

images = []
# Three differently colored surfaces for demonstration purposes.
for color in ((0, 100, 200), (200, 100, 50), (100, 200, 0)):
    surface = pg.Surface((200, 100))
    surface.fill(color)
    images.append(surface)

index = 0
image = images[index]
# Define a new event type.
CHANGE_IMAGE_EVENT = pg.USEREVENT + 1
# Add the event to the event queue every 1000 ms.
pg.time.set_timer(CHANGE_IMAGE_EVENT, 1000)

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        elif event.type == CHANGE_IMAGE_EVENT:
            # Increment the index, use modulo len(images)
            # to keep it in the correct range and change
            # the image.
            index += 1
            index %= len(images)
            image = images[index]  # Alternatively load the next image here.

    screen.fill(BG_COLOR)
    # Blit the current image.
    screen.blit(image, (200, 200))
    pg.display.flip()
    clock.tick(30)

pg.quit()