# editor.py import sys # Importing system-specific parameters and functions import pygame # Importing the pygame library for game development from game import load_images, Tilemap, load_image # Importing load_images and Tilemap from the game module RENDER_SCALE = 2.0 # Scale factor for rendering class Editor: def __init__(self): pygame.init() # Initialize all imported pygame modules pygame.display.set_caption('editor') # Set the window caption self.screen = pygame.display.set_mode((640, 480)) # Set the display window size self.display = pygame.Surface((320, 240)) # Create a surface for rendering, smaller than the display window self.clock = pygame.time.Clock() # Create a clock object to control the frame rate # Load different types of tiles into assets dictionary self.assets = { 'decor': load_images('tiles/decor'), 'grass': load_images('tiles/grass'), 'large_decor': load_images('tiles/large_decor'), 'stone': load_images('tiles/stone'), 'spawners': load_images('tiles/spawners'), 'key': [load_image('entities/key.png')], 'door' : [load_image('entities/door.png')], } self.movement = [False, False, False, False] # List to track movement directions (left, right, up, down) self.tilemap = Tilemap(self, tile_size=16) # Create a Tilemap object with a tile size of 16 try: self.tilemap.load('map.json') # Try to load an existing map except FileNotFoundError: pass # If the file is not found, do nothing self.scroll = [0, 0] # Initialize scroll position self.tile_list = list(self.assets) # List of tile types self.tile_group = 0 # Index of the current tile group self.tile_variant = 0 # Index of the current tile variant self.clicking = False # Boolean to track left mouse button clicks self.right_clicking = False # Boolean to track right mouse button clicks self.shift = False # Boolean to track if the shift key is pressed self.ongrid = True # Boolean to toggle on-grid placement of tiles def run(self): while True: # Main loop self.display.fill((0, 0, 0)) # Fill the display surface with black color # Update scroll position based on movement directions self.scroll[0] += (self.movement[1] - self.movement[0]) * 2 self.scroll[1] += (self.movement[3] - self.movement[2]) * 2 render_scroll = (int(self.scroll[0]), int(self.scroll[1])) # Convert scroll values to integers self.tilemap.render(self.display, offset=render_scroll) # Render the tilemap with the current scroll offset # Get the current tile image and set its transparency current_tile_img = self.assets[self.tile_list[self.tile_group]][self.tile_variant].copy() current_tile_img.set_alpha(100) mpos = pygame.mouse.get_pos() # Get the current mouse position mpos = (mpos[0] / RENDER_SCALE, mpos[1] / RENDER_SCALE) # Adjust mouse position based on render scale tile_pos = (int((mpos[0] + self.scroll[0]) // self.tilemap.tile_size), int((mpos[1] + self.scroll[1]) // self.tilemap.tile_size)) # Calculate tile position if self.ongrid: # Blit (draw) the current tile image at the tile position, adjusted for scroll self.display.blit(current_tile_img, (tile_pos[0] * self.tilemap.tile_size - self.scroll[0], tile_pos[1] * self.tilemap.tile_size - self.scroll[1])) else: # Blit the current tile image at the mouse position self.display.blit(current_tile_img, mpos) if self.clicking and self.ongrid: # If left-clicking and ongrid is True, place the tile on the grid self.tilemap.tilemap[str(tile_pos[0]) + ';' + str(tile_pos[1])] = {'type': self.tile_list[self.tile_group], 'variant': self.tile_variant, 'pos': tile_pos} if self.right_clicking: # If right-clicking, remove the tile at the tile position tile_loc = str(tile_pos[0]) + ';' + str(tile_pos[1]) if tile_loc in self.tilemap.tilemap: del self.tilemap.tilemap[tile_loc] for tile in self.tilemap.offgrid_tiles.copy(): tile_img = self.assets[tile['type']][tile['variant']] tile_r = pygame.Rect(tile['pos'][0] - self.scroll[0], tile['pos'][1] - self.scroll[1], tile_img.get_width(), tile_img.get_height()) if tile_r.collidepoint(mpos): self.tilemap.offgrid_tiles.remove(tile) # Blit the current tile image at a fixed position on the display self.display.blit(current_tile_img, (5, 5)) for event in pygame.event.get(): # Event handling if event.type == pygame.QUIT: pygame.quit() # Quit pygame sys.exit() # Exit the program if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: self.clicking = True # Start left-clicking if not self.ongrid: # If not ongrid, add tile to off-grid tiles list self.tilemap.offgrid_tiles.append({'type': self.tile_list[self.tile_group], 'variant': self.tile_variant, 'pos': (mpos[0] + self.scroll[0], mpos[1] + self.scroll[1])}) if event.button == 3: self.right_clicking = True # Start right-clicking if self.shift: if event.button == 4: # Scroll through tile variants if shift is pressed self.tile_variant = (self.tile_variant - 1) % len(self.assets[self.tile_list[self.tile_group]]) if event.button == 5: self.tile_variant = (self.tile_variant + 1) % len(self.assets[self.tile_list[self.tile_group]]) else: if event.button == 4: # Scroll through tile groups if shift is not pressed self.tile_group = (self.tile_group - 1) % len(self.tile_list) self.tile_variant = 0 if event.button == 5: self.tile_group = (self.tile_group + 1) % len(self.tile_list) self.tile_variant = 0 if event.type == pygame.MOUSEBUTTONUP: if event.button == 1: self.clicking = False # Stop left-clicking if event.button == 3: self.right_clicking = False # Stop right-clicking if event.type == pygame.KEYDOWN: if event.key == pygame.K_a: self.movement[0] = True # Move left if event.key == pygame.K_d: self.movement[1] = True # Move right if event.key == pygame.K_w: self.movement[2] = True # Move up if event.key == pygame.K_s: self.movement[3] = True # Move down if event.key == pygame.K_g: self.ongrid = not self.ongrid # Toggle ongrid if event.key == pygame.K_t: self.tilemap.autotile() # Autotile the map if event.key == pygame.K_o: self.tilemap.save('map.json') # Save the map if event.key == pygame.K_LSHIFT: self.shift = True # Shift key down if event.type == pygame.KEYUP: if event.key == pygame.K_a: self.movement[0] = False # Stop moving left if event.key == pygame.K_d: self.movement[1] = False # Stop moving right if event.key == pygame.K_w: self.movement[2] = False # Stop moving up if event.key == pygame.K_s: self.movement[3] = False # Stop moving down if event.key == pygame.K_LSHIFT: self.shift = False # Shift key up # Scale the display surface to the screen size and update the display self.screen.blit(pygame.transform.scale(self.display, self.screen.get_size()), (0, 0)) pygame.display.update() # Update the full display surface to the screen self.clock.tick(60) # Limit the frame rate to 60 frames per second Editor().run() # Create an instance of Editor and run the editor