在pygam中,将某些图像快速放到屏幕上是非常缓慢的

2024-06-08 05:14:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试制作一个游戏,但是当我尝试在屏幕上闪烁一些花的时候,它会从60 FPS降到12 FPS。你知道吗

起初,我试图.convert()转换我所有的图像,但后来它们变成了粉红色。所以我改用了.convert_alpha()。使用.convert_alpha()使图像不是粉红色的,但它只使FPS上升了一点点。你知道吗

我的代码的相关部分是

### Creates a random assortment of Flora ###
def createFlora(screenSize, cellSize):
    flower1 = pygame.transform.scale(pygame.image.load("Flower1.PNG"), (64, 64))
    flower1 = flower1.convert_alpha()
    flower2 = pygame.transform.scale(pygame.image.load("Flower2.PNG"), (64, 64))
    flower2 = flower2.convert_alpha()
    flowers = [flower1, flower2]
    flower1ds = pygame.transform.scale(pygame.image.load("Flower1DropShadow.PNG"), (64, 64))
    flower1ds = flower1ds.convert_alpha()
    flower2ds = pygame.transform.scale(pygame.image.load("Flower2DropShadow.PNG"), (64, 64))
    flower2ds = flower2ds.convert_alpha()
    flowersds = [flower1ds, flower2ds]


    flora = []
    for i in range(screenSize[0] // cellSize + 1):
        floraRow = []
        for j in range(screenSize[1] // cellSize + 1):
            if randint(0, len(flowers) * 4) < len(flowers):
                choice = randint(0, len(flowers) - 1)
                floraRow.append((flowers[choice],
                               (i * cellSize, j * cellSize),
                               randint(0, 3) * 90, choice))
            else: floraRow.append(False)
            flora.append(floraRow)

    floraFinal = []
    for i in range(len(flora)):
        for j in range(len(flora[0])):
            if flora[i][j] != False:
                if flora[i][j][3] == 0:
                    if flora[i][j][2] == 0: #depending on the rotation of parent plant, the shadow will be changed.
                        floraFinal.append((pygame.transform.rotate(flowersds[flora[i][j][3]], flora[i][j][2]), flora[i][j][1]))
                    elif flora[i][j][2] == 90:
                        floraFinal.append((pygame.transform.rotate(flowersds[flora[i][j][3]], flora[i][j][2]), (flora[i][j][1][0], flora[i][j][1][1] + 8)))
                    elif flora[i][j][2] == 180:
                        floraFinal.append((pygame.transform.rotate(flowersds[flora[i][j][3]], flora[i][j][2]), (flora[i][j][1][0] + 8, flora[i][j][1][1] + 8)))
                    elif flora[i][j][2] == 270:
                        floraFinal.append((pygame.transform.rotate(flowersds[flora[i][j][3]], flora[i][j][2]), (flora[i][j][1][0] + 8, flora[i][j][1][1])))

                elif flora[i][j][3] == 1: 
                    if flora[i][j][2] == 0:
                        floraFinal.append((flowersds[flora[i][j][3]], flora[i][j][1]))
                    elif flora[i][j][2] == 90:
                        floraFinal.append((flowersds[flora[i][j][3]], (flora[i][j][1][0], flora[i][j][1][1] + 4)))
                    elif flora[i][j][2] == 180:
                        floraFinal.append((flowersds[flora[i][j][3]], (flora[i][j][1][0] + 4, flora[i][j][1][1] + 4)))
                    elif flora[i][j][2] == 270:
                        floraFinal.append((flowersds[flora[i][j][3]], (flora[i][j][1][0] + 4, flora[i][j][1][1])))
                floraFinal.append((pygame.transform.rotate(flora[i][j][0], flora[i][j][2]), flora[i][j][1]))
    return floraFinal

def renderFlora(flora, screen):
    for i in range(len(flora)):
        screen.blit(flora[i][0], flora[i][1])
def main():
    os.environ['SDL_VIDEO_CENTERED'] = '1' #center the screen
    pygame.init()
    screenSize = (1856, 960) #24 x 14 cells
    onTop(pygame.display.get_wm_info()['window']) #move pygame window to the front

    screen = pygame.display.set_mode(screenSize)
    screen.fill((25, 25, 25))

    """ VARS """
    cellSize = 64

    pygame.font.init() 
    myfont = pygame.font.SysFont('Comic Sans MS', 30)

    path = createPath()
    flora = createFlora(screenSize, cellSize)
    tileMap = createTileMap(screenSize, cellSize)

    tileGridOn = False
    currentButton = 0

    buttons = []
    gridButtonImg = pygame.transform.scale(pygame.image.load("GridButton.PNG").convert_alpha(), (48, 48))
    #Grid Button Toggles a grid so you can see where Tiles are easier
    buttons.append(Button((20, screenSize[1] - 64), False, gridButtonImg, screen, 48))


    pathSds = pygame.transform.scale(pygame.image.load("StraightPathDropShadow.PNG").convert_alpha(), (64, 64))
    pathCds = pygame.transform.scale(pygame.image.load("CornerPathDropShadow.PNG").convert_alpha(), (64, 64))

    #==# MAIN LOOP #==#
    clock = pygame.time.Clock()
    while True:
        screen.fill((25, 25, 25))
        for evt in pygame.event.get():
            if evt.type == pygame.QUIT:
                pygame.quit()
                quit()

        #Draw Background Tiles
        renderBackground(tileMap, screen)

        #Draw Midground Items/Tiles
        renderFlora(flora, screen)

        #Draw Foreground Items/Tiles
        renderPath(path, screen, pathSds, pathCds)

        textsurface = myfont.render(str(round(clock.get_fps() * 10) / 10), False, (0, 0, 0))
        screen.blit(textsurface,(15, 15))

        ### Buttons ###
        for i in range(len(buttons)):
            buttons[i].update(screen)
            if buttons[i].getCTF():
                currentButton = i
            #Toggle Grid
        tileGridOn = buttons[0].getValue()
        if tileGridOn: drawGrid(screenSize, cellSize, screen)

        pygame.display.flip()
        clock.tick(60)
main()

flora是在主循环之前创建的,所以我不是每个循环都创建图像,我只是通过render函数传递一个列表,这个函数只会将图像blit到屏幕上。你知道吗

如果我注释掉renderFlora()方法,它会变成60帧/秒,但是如果我保留它,它会显著下降,我不知道为什么,因为我已经为平铺背景创建了更多的图像。方法。你知道吗


Tags: imagealphaconvertiftransformloadscreenpygame

热门问题