# 8:45 ep 4 import pygame import button from button import Button pygame.init() clock = pygame.time.Clock() FPS = 60 #game window SCREEN_WIDTH = 800 SCREEN_HEIGHT = 640 LOWER_MARGIN = 100 SIDE_MARGIN = 300 # creates screen, adds screen size and the margins screen = pygame.display.set_mode((SCREEN_WIDTH + SIDE_MARGIN, SCREEN_HEIGHT + LOWER_MARGIN)) pygame.display.set_caption('Level Editor') #define game variables ROWS = 16 MAX_COLS = 150 TILE_SIZE = SCREEN_HEIGHT // ROWS TILE_TYPES = 18 current_tile = 0 scroll_left = False scroll_right = False scroll = 0 scroll_speed = 1 # indicates which background to use first bgnum = 1 #load images bg1_img = pygame.image.load('bg1.jpg') bg2_img = pygame.image.load('bg2.jpg') title_img = pygame.image.load('title.png') # store tiles in a list img_list = [] for x in range(TILE_TYPES): img = pygame.image.load(f'tiles/{x}.png') img = pygame.transform.scale(img, (TILE_SIZE, TILE_SIZE)) img_list.append(img) #define colors GREEN = (144,201,120) WHITE = (255, 255 ,255) RED = (200,25,25) BLACK = (40, 40, 43) #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 print(world_data) # draws background def draw_bg(): num = 4 screen.fill(BLACK) width1 = bg1_img.get_width() width2 = bg2_img.get_width() for x in range(num): if x % 2 == 0: screen.blit(bg1_img, ((x * width1) - scroll * 0.5, 0)) else: screen.blit(bg2_img, ((x * width2) - scroll * 0.5, 0)) screen.blit(title_img, ((x * width1) - scroll * 0.6 + SCREEN_WIDTH / 2 - title_img.get_width() / 2, 0)) #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, SCREEN_HEIGHT)) #horizontal lines for c in range(ROWS + 1): pygame.draw.line(screen, WHITE, (0, c * TILE_SIZE), (SCREEN_WIDTH, c * TILE_SIZE)) # function for drawing the world tiles def draw_world(): for y, row in enumerate(world_data): for x, tile, in enumerate(row): if tile >= 0: screen.blit(img_list[0], (x * TILE_SIZE - scroll, y * TILE_SIZE)) #create buttons #make a button list button_list = [] button_col = 0 button_row = 0 for i in range(len(img_list)): tile_button = button.Button(SCREEN_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 print(title_img.get_rect().x) run = True while run: clock.tick(FPS) draw_bg() draw_grid() draw_world() # draw tile panel and tiles pygame.draw.rect(screen, BLACK, (SCREEN_WIDTH, 0, SIDE_MARGIN, SCREEN_HEIGHT + 10)) # choose a tile button_count = 0 for button_count, i in enumerate(button_list): if i.draw(screen): current_tile = button_count # highlight selected tile pygame.draw.rect(screen, RED, button_list[current_tile].rect, 3) #scroll the map if scroll_left == True and scroll > 0: scroll -= 5 * scroll_speed if scroll_right == True: scroll += 5 * scroll_speed 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 or event.key == pygame.K_a: scroll_left = True if event.key == pygame.K_RIGHT or event.key == pygame.K_d: scroll_right = True if event.key == pygame.K_LSHIFT or event.key == pygame.K_RSHIFT: scroll_speed = 5 if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT or event.key == pygame.K_a: scroll_left = False if event.key == pygame.K_RIGHT or event.key == pygame.K_d: scroll_right = False if event.key == pygame.K_LSHIFT or event.key == pygame.K_RSHIFT: scroll_speed = 1 pygame.display.update() pygame.QUIT()