如何在循环文件名列表时blit多个图像?

时间:2018-05-23 11:08:09

标签: python-3.x pygame pygame-surface

我试图在动态汽车的多个图像上进行blit,在图像的文件名上使用for循环。但是,它只会绘制屏幕,​​但实际上并不显示/显示图像。我正在使用python3.6。

这是我的代码。

http_code

View of the results.csv

1 个答案:

答案 0 :(得分:0)

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

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

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()
相关问题