Simple improvement would to put images in a list or dict.
This would stop if block. When more image are added.
This would stop if block. When more image are added.
DISPLAYSURF.blit(tileImage[tile], (centered_x, centered_y)) #display the actual tileMy example. My math could be improve.
import pygame class Square: def __init__(self, color): self.surface = pygame.Surface((64, 64)) self.surface = self.surface.convert_alpha() self.surface.fill((0,0,0,0)) pygame.draw.polygon(self.surface, color, [(32,32), (64,48), (32,64), (0,48)]) def draw(self, surface, pos): surface.blit(self.surface, pos) class Block: def __init__(self, color): self.surface = pygame.Surface((64, 64)).convert_alpha() self.surface.fill((0,0,0,0)) pygame.draw.polygon(self.surface, color, [(32,32), (0,16), (32,0), (64,16)]) c = color.hsla color.hsla = c[0], c[1], c[2] * 0.75, c[3] pygame.draw.polygon(self.surface, color, [(0,16), (32,32), (32,64), (0,48)]) c = color.hsla color.hsla = c[0], c[1], c[2] * 0.75, c[3] pygame.draw.polygon(self.surface, color, [(32,32), (64,16), (64,48), (32,64)]) def draw(self, surface, pos): surface.blit(self.surface, pos) class Scene: def __init__(self): self.rect = pygame.Rect(0, 0, 800, 600) pygame.display.set_caption('Example') self.surface = pygame.display.set_mode(self.rect.size) self.clock = pygame.time.Clock() self.images = [Square(pygame.Color('lawngreen')), Block(pygame.Color('cyan')), Block(pygame.Color('firebrick')) ] self.map = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0, 2, 2, 2, 0], [0, 1, 0, 0, 1, 0, 0, 2, 0, 0], [0, 1, 1, 1, 1, 0, 0, 2, 0, 0], [0, 1, 0, 0, 1, 0, 0, 2, 0, 0], [0, 1, 0, 0, 1, 0, 2, 2, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] def loop(self): self.running = True while self.running: for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False self.surface.fill((0,0,0)) cx = self.rect.centerx // 2 + len(self.map) * 32 cy = self.rect.centery // 2 - len(self.map[0]) * 4 w, h = self.rect.size for row, line in enumerate(self.map): for col, tile in enumerate(line): pos = w - (row * 32 + cx) + col * 32, (col + row) * 16 + cy self.images[tile].draw(self.surface, pos) pygame.display.flip() self.clock.tick(30) pygame.quit() scene = Scene() scene.loop()
99 percent of computer problems exists between chair and keyboard.