Pygame - 是否可以根据时间更改背景图像?

Pygame - Is it possible to change the background image in relation to time?

我正在使用 pygame 来创建一种动画。我的想法是让一系列背景图像随着我启动游戏后的时间而变化。我想出了这个代码来做到这一点:

while True:

    DISPLAYSURF.fill(black)  #fills the displaysurf with black

    for time in pygame.time.get_ticks():

            if time == 2:
                img_1 = img_2
                DISPLAYSURF.blit(img_2, (0, 0)) #copy, pastes the surface object to fixed point
            if time == 4:
                img_2 = img_3
                DISPLAYSURF.blit(img_3, (0, 0))
            if time == 8:
                img_3 = img_4
                DISPLAYSURF.blit(img_4, (0, 0))
            if time == 10:
                img_4 = img_5
                DISPLAYSURF.blit(img_5, (0, 0))
            if time == 12:
                img_5 = img_6
                DISPLAYSURF.blit(img_6, (0, 0))
            if time == 14:
                img_6 = img_7
                DISPLAYSURF.blit(img_7, (0, 0))
            if time == 16:
                img_7 = img_8
                DISPLAYSURF.blit(img_8, (0, 0))

    pygame.display.flip()
    clock.tick(FPS)

当我 运行 该程序时收到的反馈是“'int' 对象不可迭代”,这让我觉得我可能无法按照我的想法去做,因为我拥有的图像在 Pygame 中被归类为表面对象。我在想两件事:

-->是否可以创建一个函数,通过某种方式转换表面对象的类型来重新上传与时间相关的图像?

-->我的代码是否反映了我想要它做什么?

请让我知道并拒绝批评!我对编码还很陌生,所以任何反馈都是有帮助的!

您收到错误 int object is not iterable 的原因是因为 pygame.time.get_ticks() 是一个 returns 整数的函数,除非您在 range() 函数与 for 循环结合使用。一个更好的主意可能是简单地用 time = pygame.time.get_ticks()

替换 for time in pygame.time.get_ticks()

@Aggragoth 已经介绍了错误消息,所以我不会再说了。

定期更改背景的一种方法是保留一个计时器,并根据预定义的周期调整背景。

import pygame
import time

# Window size
WINDOW_WIDTH  = 200
WINDOW_HEIGHT = 200

# background colour
SKY_BLUE      = (161, 255, 254)
WHITE         = (255, 255, 255)
BLUE          = (  5,  55, 255)

### MAIN
pygame.init()
surface_type = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE
window       = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), surface_type )
pygame.display.set_caption("Background Change")

# Variables to manage the background change
background_delay = 1500  # milliseconds
background_time  = 0     # when the background last changed
backgrounds      = [ WHITE, SKY_BLUE, WHITE, SKY_BLUE, BLUE ]
background_index = 0     # index of the currently used background

# Main loop
clock = pygame.time.Clock()
done = False
while not done:

    # Handle user-input
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            done = True

    # Re-draw the screen background from the list after a delay
    time_now = pygame.time.get_ticks()
    if ( time_now > background_time + background_delay ):
        # switch to the next background
        background_time = time_now
        background_index += 1
        # if we're out of backgrounds, start back at the head of the list
        if ( background_index >= len( backgrounds ) ):
            background_index = 0

    # Draw the background
    window.fill( backgrounds[ background_index ] )

    pygame.display.flip()
    # Update the window, but not more than 60fps
    clock.tick_busy_loop( 60 )

pygame.quit()

代码的主要部分是保留我们上次更改背景的时间。如果从那时起(从pygame.time.get_ticks()开始)经过的时间大于last-change-time加上一个延迟,则切换到下一个背景。

在这个例子中,我只使用了颜色,但 backgrounds[] 列表也可以包含图像,并与 window.blit() 一起使用。