import pygame import csv import os def run_level_editor(): pygame.init() # Screen setup SCREEN_WIDTH = 800 SCREEN_HEIGHT = 640 LOWER_MARGIN = 100 SIDE_MARGIN = 300 screen = pygame.display.set_mode((SCREEN_WIDTH + SIDE_MARGIN, SCREEN_HEIGHT + LOWER_MARGIN)) pygame.display.set_caption('Level Editor') # Game variables ROWS = 16 MAX_COLS = 150 TILE_SIZE = SCREEN_HEIGHT // ROWS scroll_left = False scroll_right = False scroll = 0 scroll_speed = 1 TILE_TYPES = 5 # You can increase this if needed # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) # Tile buttons (placeholder tiles as rectangles) tile_colors = [(100, 100, 255), (255, 100, 100), (100, 255, 100), (200, 200, 50), (150, 0, 150)] # Tile selection buttons button_list = [] button_col = 0 button_row = 0 BUTTON_SIZE = 50 current_tile = 0 for i in range(TILE_TYPES): x = SCREEN_WIDTH + 50 + (button_col * (BUTTON_SIZE + 10)) y = 50 + (button_row * (BUTTON_SIZE + 10)) rect = pygame.Rect(x, y, BUTTON_SIZE, BUTTON_SIZE) button_list.append((rect, tile_colors[i])) button_col += 1 if button_col == 3: button_row += 1 button_col = 0 # World data world_data = [[-1 for _ in range(MAX_COLS)] for _ in range(ROWS)] current_level = 0 # Save/load functions def save_level(level): if not os.path.exists('levels'): os.makedirs('levels') with open(f'levels/level_{level}.csv', 'w', newline='') as file: writer = csv.writer(file) writer.writerows(world_data) def load_level(level): nonlocal world_data try: with open(f'levels/level_{level}.csv', 'r') as file: reader = csv.reader(file) world_data = [[int(cell) for cell in row] for row in reader] except FileNotFoundError: world_data = [[-1 for _ in range(MAX_COLS)] for _ in range(ROWS)] # Drawing functions def draw_grid(): for c in range(MAX_COLS + 1): pygame.draw.line(screen, WHITE, (c * TILE_SIZE - scroll, 0), (c * TILE_SIZE - scroll, SCREEN_HEIGHT)) for r in range(ROWS + 1): pygame.draw.line(screen, WHITE, (0, r * TILE_SIZE), (SCREEN_WIDTH, r * TILE_SIZE)) def draw_world(): for y, row in enumerate(world_data): for x, tile in enumerate(row): if tile >= 0: pygame.draw.rect(screen, tile_colors[tile], (x * TILE_SIZE - scroll, y * TILE_SIZE, TILE_SIZE, TILE_SIZE)) def draw_buttons(): for i, (rect, color) in enumerate(button_list): pygame.draw.rect(screen, color, rect) if i == current_tile: pygame.draw.rect(screen, WHITE, rect, 3) # Main loop run = True while run: pygame.time.Clock().tick(60) screen.fill(BLACK) draw_grid() draw_world() draw_buttons() pygame.draw.rect(screen, WHITE, (SCREEN_WIDTH, 0, SIDE_MARGIN, SCREEN_HEIGHT + LOWER_MARGIN)) # Handle mouse input mouse_x, mouse_y = pygame.mouse.get_pos() if pygame.mouse.get_pressed()[0]: if mouse_x < SCREEN_WIDTH and mouse_y < SCREEN_HEIGHT: x = (mouse_x + scroll) // TILE_SIZE y = mouse_y // TILE_SIZE if x < MAX_COLS and y < ROWS: world_data[y][x] = current_tile else: for i, (rect, _) in enumerate(button_list): if rect.collidepoint((mouse_x, mouse_y)): current_tile = i if pygame.mouse.get_pressed()[2]: if mouse_x < SCREEN_WIDTH and mouse_y < SCREEN_HEIGHT: x = (mouse_x + scroll) // TILE_SIZE y = mouse_y // TILE_SIZE if x < MAX_COLS and y < ROWS: world_data[y][x] = -1 # Scroll map if scroll_left and scroll > 0: scroll -= 5 * scroll_speed if scroll_right: scroll += 5 ::contentReference[oaicite:0]{index=0}