#Imports import pygame import sys import button import csv #Initialize pygame pygame.init() #Timing clock = pygame.time.Clock() FPS = 60 #Screen setup WIDTH = 800 HEIGHT = WIDTH * 0.8 LOWER_MARGIN = 100 SIDE_MARGIN = 300 screen = pygame.display.set_mode((WIDTH + SIDE_MARGIN, HEIGHT + LOWER_MARGIN)) pygame.display.set_caption("Level Editor") #Define game variables ROWS = 16 MAX_COLS = 150 TILE_SIZE = HEIGHT // ROWS TILE_TYPES = 22 level = 0 current_tile = 0 scroll_left = False scroll_right = False scroll = 0 scroll_speed = 1 #Colours GREEN = (144, 201, 120) WHITE = (255, 255, 255) RED = (200, 25, 25) #Define font font = pygame.font.SysFont('Future', 30) #Create empty tile list world_data = [] for row in range(ROWS): r = [-1] * MAX_COLS world_data.append(r) #Create ground for tile in range(0, MAX_COLS): world_data[ROWS - 1][tile] = 0 #Function for outputting text onto screen def draw_text(text, font, text_col, x, y): img = font.render(text, True, text_col) screen.blit(img, (x, y)) #Setup images pine1_img = pygame.image.load('./img/background/pine1.png').convert_alpha() pine2_img = pygame.image.load('./img/background/pine2.png').convert_alpha() sky_img = pygame.image.load('./img/background/sky_cloud.png').convert_alpha() #Store tiles in a list img_list = [] for i in range(TILE_TYPES): img = pygame.image.load(f'./img/tile/{i}.png').convert_alpha() img = pygame.transform.scale(img, (TILE_SIZE, TILE_SIZE)) img_list.append(img) save_img = pygame.image.load('./img/save_btn.png').convert_alpha() load_img = pygame.image.load('./img/load_btn.png').convert_alpha() #Function to draw background def draw_bg(): screen.fill(GREEN) img_width = sky_img.get_width() for x in range(4): screen.blit(sky_img, ((x * img_width) - scroll * 0.5, 0)) screen.blit(pine1_img, ((x * img_width) - scroll * 0.7, HEIGHT - pine1_img.get_height() - 150)) screen.blit(pine2_img, ((x * img_width) - scroll * 0.8, HEIGHT - pine2_img.get_height())) #Draw grid def draw_grid(): #Vertical Lines for c in range(MAX_COLS + 1): pygame.draw.line(screen, WHITE, (c * TILE_SIZE - scroll, 0), (c * TILE_SIZE - scroll, HEIGHT)) #Horizontal Lines for r in range(ROWS + 1): pygame.draw.line(screen, WHITE, (0, r * TILE_SIZE), (WIDTH, r * TILE_SIZE)) #Draw tiles in world def draw_world(): for y, row in enumerate(world_data): for x, tile in enumerate(row): if tile >= 0: screen.blit(img_list[tile], (x * TILE_SIZE - scroll, y * TILE_SIZE)) #Create buttons save_button = button.Button(WIDTH // 2, HEIGHT + LOWER_MARGIN - 75, save_img, 1) load_button = button.Button(WIDTH // 2 + 200, HEIGHT + LOWER_MARGIN - 75, load_img, 1) #Make a button list button_list = [] button_col = 0 button_row = 0 for i in range(len(img_list)): tile_button = button.Button(WIDTH + (75 * button_col) + 50, 75 * button_row + 50, img_list[i], 1) button_list.append(tile_button) button_col += 1 if button_col == 3: button_row += 1 button_col = 0 #Run the game run = True while run: #Event Handler for Event in pygame.event.get(): if Event.type == pygame.QUIT: run = False #Keyboard presses if Event.type == pygame.KEYDOWN: if Event.key == pygame.K_LEFT: scroll_left = True if Event.key == pygame.K_RIGHT: scroll_right = True if Event.key == pygame.K_RSHIFT: scroll_speed = 5 if Event.key == pygame.K_UP: level += 1 if Event.key == pygame.K_DOWN and level > 0: level -= 1 if Event.type == pygame.KEYUP: if Event.key == pygame.K_LEFT: scroll_left = False if Event.key == pygame.K_RIGHT: scroll_right = False if Event.key == pygame.K_RSHIFT: scroll_speed = 1 #Scroll the map if scroll_left == True and scroll > 0: scroll -= 5 * scroll_speed if scroll_right == True and scroll < (MAX_COLS * TILE_SIZE) - WIDTH: scroll += 5 * scroll_speed #Add new tiles to screen #Find Mouse position pos = pygame.mouse.get_pos() x = int((pos[0] + scroll) // TILE_SIZE) y = int((pos[1]) // TILE_SIZE) #Check that mouse coords are within tile area if pos[0] < WIDTH and pos[1] < HEIGHT: #Update tile value if pygame.mouse.get_pressed()[0] == 1: if world_data[y][x] != current_tile: world_data[y][x] = current_tile if pygame.mouse.get_pressed()[2] == 1: if world_data[y][x] != -1: world_data[y][x] = -1 draw_bg() draw_grid() draw_world() draw_text(f"Level: {level + 1}", font, WHITE, 10, HEIGHT + LOWER_MARGIN - 90) draw_text("Press UP or DOWN to change level", font, WHITE, 10, HEIGHT + LOWER_MARGIN - 60) #Save and load data if save_button.draw(screen): #Save level data with open(f'./level{level}_data.csv', 'w', newline='') as csvfile: writer = csv.writer(csvfile, delimiter = ',') for row in world_data: writer.writerow(row) if load_button.draw(screen): #Load in level data #Reset scroll back to start scroll = 0 with open(f'./level{level}_data.csv', newline='') as csvfile: reader = csv.reader(csvfile, delimiter = ',') for x, row in enumerate(reader): for y, tile in enumerate(row): world_data[x][y] = int(tile) #Draw tile panel and tiles pygame.draw.rect(screen, GREEN, (WIDTH, 0, WIDTH + SIDE_MARGIN, HEIGHT)) #Choose a tile button_count = 0 for button_count, b in enumerate(button_list): if b.draw(screen): current_tile = button_count #Highlight selected tile pygame.draw.rect(screen, RED, button_list[current_tile].rect, 3) pygame.display.update() clock.tick(FPS) pygame.QUIT sys.exit