The game is lagging, because the playground is generated in every frame. Create a pygame.Surface
with the size of the playground and draw all the tiles on it:
tileSize = 64
size_y, size_x = len(self.structure), len(self.structure[0])
self.field = pygame.Surface((size_x * tileSize, size_y * tileSize))
for iy, line in enumerate(self.structure):
for ix, sprite in enumerate(line):
tile_surf = self.no_texture
if sprite == 'G':
tile_surf = self.grass
if sprite == 'T':
tile_surf = self.tree
self.field.blit(tile_surf, (ix*tileSize, iy*tileSize))
Blit the playground an area of the playground with the size of the window, on the window, in every frame:
fenetre.blit(self.field, (0, 0), (x, y, screen_widht, screen_height))
Class Level
:
class Level():
def __init__(self):
self.structure = 0
self.map = []
self.grass = pygame.image.load("assets/bloc/normal_blocks/grass.png").convert_alpha()
self.tree = pygame.image.load("assets/bloc/collidables_blocks/tree_grass.png").convert_alpha()
self.no_texture = pygame.image.load("assets/bloc/specials_blocks/no_texture.png").convert_alpha()
def generer(self, map_file_name):
with open(map_file_name, "r") as fichier:
structure_niveau = []
for ligne in fichier:
ligne_niveau = []
for sprite in ligne:
if sprite != '
':
ligne_niveau.append(sprite)
structure_niveau.append(ligne_niveau)
self.structure = structure_niveau
self.createMap()
def createMap(self):
tileSize = 64
size_y, size_x = len(self.structure), len(self.structure[0])
self.field = pygame.Surface((size_x * tileSize, size_y * tileSize))
for iy, line in enumerate(self.structure):
for ix, sprite in enumerate(line):
tile_surf = self.no_texture
if sprite == 'G':
tile_surf = self.grass
if sprite == 'T':
tile_surf = self.tree
self.field.blit(tile_surf, (ix*tileSize, iy*tileSize))
def afficher(self, fenetre, x, y, screen_widht, screen_height):
fenetre.blit(self.field, (0, 0), (x, y, screen_widht, screen_height))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…