import pygame
from functools import lru_cache
# Предположим, что у нас есть некий класс Tile для представления тайла
class Tile:
def __init__(self, image_path):
self.image = pygame.image.load(image_path)
# Кэшируем функцию отрисовки тайла
@lru_cache(maxsize=None)
def draw_tile(tile):
return tile.image
# Создаем предварительное кэширование всех тайлов
tile_cache = {}
def preload_tiles(tile_paths):
for path in tile_paths:
tile_cache[path] = Tile(path)
# Функция, чтобы отрисовать ряд тайлов на экране
def draw_tiles(surface, tile_paths, position):
for index, tile_path in enumerate(tile_paths):
tile_image = draw_tile(tile_cache[tile_path])
surface.blit(tile_image, (position[0] + index * tile_image.get_width(), position[1]))
# Пример использования
pygame.init()
screen = pygame.display.set_mode((800, 600))
# Предзагрузка тайлов
tile_paths = ['tile1.png', 'tile2.png', 'tile3.png']
preload_tiles(tile_paths)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 0))
draw_tiles(screen, tile_paths, (50, 50))
pygame.display.flip()
pygame.quit()