import pygame import sys import os import pickle # Initialize Pygame pygame.init() # Screen dimensions screen_width = 1300 # 1000 for level editor + 300 for menu screen_height = 1000 tile_size = 50 # Colors white = (255, 255, 255) black = (0, 0, 0) gray = (200, 200, 200) green = (0, 255, 0) button_green = (0, 200, 0) button_red = (200, 0, 0) # Load background image background_img = pygame.image.load('Images/background/BG2.png') background_img = pygame.transform.scale(background_img, (1000, 1000)) # Load and resize tile images tiles = { 1: pygame.transform.scale(pygame.image.load('Images/tiles2/1.png'), (tile_size, tile_size)), 2: pygame.transform.scale(pygame.image.load('Images/tiles2/2.png'), (tile_size, tile_size)), 3: pygame.transform.scale(pygame.image.load('Images/tiles2/3.png'), (tile_size, tile_size)), 4: pygame.transform.scale(pygame.image.load('Images/tiles2/4.png'), (tile_size, tile_size)), 5: pygame.transform.scale(pygame.image.load('Images/tiles2/5.png'), (tile_size, tile_size)), 6: pygame.transform.scale(pygame.image.load('Images/tiles2/6.png'), (tile_size, tile_size)), 7: pygame.transform.scale(pygame.image.load('Images/tiles2/7.png'), (tile_size, tile_size)), 8: pygame.transform.scale(pygame.image.load('Images/tiles2/8.png'), (tile_size, tile_size)), 9: pygame.transform.scale(pygame.image.load('Images/tiles2/9.png'), (tile_size, tile_size)), 10: pygame.transform.scale(pygame.image.load('Images/tiles2/10.png'), (tile_size, tile_size)), 11: pygame.transform.scale(pygame.image.load('Images/tiles2/11.png'), (tile_size, tile_size)), 12: pygame.transform.scale(pygame.image.load('Images/tiles2/12.png'), (tile_size, tile_size)), 13: pygame.transform.scale(pygame.image.load('Images/tiles2/13.png'), (tile_size, tile_size)), 14: pygame.transform.scale(pygame.image.load('Images/tiles2/14.png'), (tile_size, tile_size)), 15: pygame.transform.scale(pygame.image.load('Images/tiles2/15.png'), (tile_size, tile_size)), 16: pygame.transform.scale(pygame.image.load('Images/tiles2/16.png'), (tile_size, tile_size)), } selected_tile = None # Set up the display screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("Level Editor") # Level data level_data = [[0 for _ in range(20)] for _ in range(20)] # 20x20 grid for 1000x1000 area # Button class class Button: def __init__(self, x, y, width, height, text, color): self.rect = pygame.Rect(x, y, width, height) self.text = text self.font = pygame.font.Font(None, 36) self.color = color self.hover_color = gray def draw(self, screen): mouse_pos = pygame.mouse.get_pos() pygame.draw.rect(screen, self.color, self.rect) if self.rect.collidepoint(mouse_pos): pygame.draw.rect(screen, self.hover_color, self.rect) text_surface = self.font.render(self.text, True, white) screen.blit(text_surface, (self.rect.x + 10, self.rect.y + 10)) def is_clicked(self, event): if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: if self.rect.collidepoint(event.pos): return True return False # Create buttons buttons = [ Button(10, 40, 130, 50, 'Save', button_green), Button(150, 40, 130, 50, 'Load', button_green), Button(10, 100, 270, 50, 'Reset', button_red) ] # Function to draw text def draw_text(text, font, color, surface, x, y): text_obj = font.render(text, True, color) text_rect = text_obj.get_rect() text_rect.topleft = (x, y) surface.blit(text_obj, text_rect) # Function to draw grid def draw_grid(): for x in range(0, 1000, tile_size): pygame.draw.line(screen, gray, (300 + x, 0), (300 + x, 1000)) for y in range(0, 1000, tile_size): pygame.draw.line(screen, gray, (300, y), (1300, y)) # Function to draw level def draw_level(): for row in range(20): for col in range(20): if level_data[row][col] != 0: screen.blit(tiles[level_data[row][col]], (300 + col * tile_size, row * tile_size)) # Function to draw tile inventory def draw_tile_inventory(): for i in range(1, 17): x = 10 + ((i - 1) % 4) * (tile_size + 10) y = 200 + ((i - 1) // 4) * (tile_size + 10) screen.blit(tiles[i], (x, y)) if selected_tile == i: pygame.draw.rect(screen, green, (x, y, tile_size, tile_size), 3) # Function to save level def save_level(): # Get the current level count existing_files = sorted([f for f in os.listdir('Levels') if f.startswith('level_')]) if existing_files: last_level_number = int(existing_files[-1].split('_')[1]) else: last_level_number = 0 new_level_number = last_level_number + 1 level_name = f'level_{new_level_number}' with open(f'Levels/{level_name}', 'wb') as file: pickle.dump(level_data, file) print(f"Level saved as {level_name}") # Function to load level current_level_index = -1 def load_level(): global current_level_index, level_data level_files = sorted([f for f in os.listdir('Levels') if f.startswith('level_')]) if level_files: current_level_index = (current_level_index + 1) % len(level_files) with open(f'Levels/{level_files[current_level_index]}', 'rb') as file: level_data = pickle.load(file) print(f"Level loaded: {level_files[current_level_index]}") # Function to reset level def reset_level(): global level_data level_data = [[0 for _ in range(20)] for _ in range(20)] # Main loop running = True placing_tiles = False removing_tiles = False while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN: mouse_x, mouse_y = event.pos if event.button == 1: # Left click if mouse_x > 300: placing_tiles = True else: for i, button in enumerate(buttons): if button.is_clicked(event): if i == 0: save_level() elif i == 1: load_level() elif i == 2: reset_level() for i in range(1, 17): x = 10 + ((i - 1) % 4) * (tile_size + 10) y = 200 + ((i - 1) // 4) * (tile_size + 10) if pygame.Rect(x, y, tile_size, tile_size).collidepoint(mouse_x, mouse_y): selected_tile = i elif event.button == 3: # Right click if mouse_x > 300: removing_tiles = True elif event.type == pygame.MOUSEBUTTONUP: if event.button == 1: placing_tiles = False elif event.button == 3: removing_tiles = False mouse_x, mouse_y = pygame.mouse.get_pos() if placing_tiles and mouse_x > 300: col = (mouse_x - 300) // tile_size row = mouse_y // tile_size if selected_tile is not None and 0 <= row < 20 and 0 <= col < 20: level_data[row][col] = selected_tile elif removing_tiles and mouse_x > 300: col = (mouse_x - 300) // tile_size row = mouse_y // tile_size if 0 <= row < 20 and 0 <= col < 20: level_data[row][col] = 0 # Fill screen with white screen.fill(white) # Draw the background image screen.blit(background_img, (300, 0)) # Draw the grid draw_grid() # Draw the level draw_level() # Draw the menu area pygame.draw.rect(screen, black, (0, 0, 300, 1000)) # Draw text labels draw_text('MENU BUTTONS', pygame.font.Font(None, 36), white, screen, 10, 10) draw_text('TILES', pygame.font.Font(None, 36), white, screen, 10, 150) # Draw buttons for button in buttons: button.draw(screen) # Draw tile inventory draw_tile_inventory() # Update the display pygame.display.flip() # Quit Pygame pygame.quit() sys.exit()