"""A tile map from text, pre-rendered once, with a camera that follows the player.""" import pygame TILE = 32 WIDTH, HEIGHT = 640, 480 LEVEL = """ ############################################## #............................................# #.....................ooo....................# #....................#####...........ooo.....# #...................................#####....# #............................................# #..........ooo...............................# #.........#####..........ooo......########...# #........................###.................# #....######..........................ooo.....# #..........................#######..#####....# #...........ooo......#.......................# #..P.......#####....##..................ooo..# #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~# ############################################## """.strip().splitlines() COLORS = {"#": (110, 120, 150), "~": (70, 140, 90)} def build_level(rows): walls, coins, start = [], [], (TILE, TILE) surface = pygame.Surface((len(rows[0]) * TILE, len(rows) * TILE)) surface.fill((24, 32, 56)) for r, row in enumerate(rows): for c, ch in enumerate(row): rect = pygame.Rect(c * TILE, r * TILE, TILE, TILE) if ch in COLORS: walls.append(rect) pygame.draw.rect(surface, COLORS[ch], rect) pygame.draw.rect(surface, (40, 50, 80), rect, 1) elif ch == "o": coins.append(rect.inflate(-16, -16)) elif ch == "P": start = rect.topleft return surface, walls, coins, start pygame.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) clock = pygame.time.Clock() font = pygame.font.Font(None, 28) level_surface, walls, coins, start = build_level(LEVEL) # drawn once, blitted every frame player = pygame.Rect(start[0] + 4, start[1] + 4, 24, 24) camera = pygame.Vector2(0, 0) level_rect = level_surface.get_rect() running = True while running: dt = clock.tick(60) / 1000 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False keys = pygame.key.get_pressed() move = pygame.Vector2(keys[pygame.K_RIGHT] - keys[pygame.K_LEFT], keys[pygame.K_DOWN] - keys[pygame.K_UP]) * 220 * dt # Move on each axis separately and push out of any wall we hit. player.x += round(move.x) for w in walls: if player.colliderect(w): player.right = w.left if move.x > 0 else player.right player.left = w.right if move.x < 0 else player.left player.y += round(move.y) for w in walls: if player.colliderect(w): player.bottom = w.top if move.y > 0 else player.bottom player.top = w.bottom if move.y < 0 else player.top coins = [c for c in coins if not player.colliderect(c)] # Camera: centre on the player, clamped to the level edges. camera.x = max(0, min(player.centerx - WIDTH / 2, level_rect.width - WIDTH)) camera.y = max(0, min(player.centery - HEIGHT / 2, level_rect.height - HEIGHT)) offset = (-round(camera.x), -round(camera.y)) screen.fill((0, 0, 0)) screen.blit(level_surface, offset) for c in coins: pygame.draw.ellipse(screen, (244, 197, 49), c.move(offset)) pygame.draw.rect(screen, (91, 147, 230), player.move(offset), border_radius=4) screen.blit(font.render(f"Coins left: {len(coins)} Arrow keys to move", True, (230, 236, 247)), (12, 10)) pygame.display.flip() pygame.quit()