import pygame import pickle from os import path from pygame.locals import * pygame.init() clock = pygame.time.Clock() fps = 60 #game window tile_size = 50 cols = 20 margin = 100 screen_width = tile_size * cols screen_height = (tile_size * cols) + margin screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption('Level Editor') #load images sun_img = pygame.image.load("mini_task/img/Mini Project 1/img/sun.png") sun_img = pygame.transform.scale(sun_img, (tile_size, tile_size)) bg_img = pygame.image.load("mini_task/img/Mini Project 1/img/sky.png") bg_img = pygame.transform.scale(bg_img, (screen_width, screen_height - margin)) dirt_img = pygame.image.load("mini_task/img/Mini Project 1/img/dirt.png") grass_img = pygame.image.load("mini_task/img/Mini Project 1/img/grass.png") blob_img = pygame.image.load("mini_task/img/Mini Project 1/img/blob.png") platfom_x_img = pygame.image.load("mini_task/img/Mini Project 1/img/platform_x.png") platfom_y_img = pygame.image.load("mini_task/img/Mini Project 1/img/platform_y.png") lava_img = pygame.image.load("mini_task/img/Mini Project 1/img/lava.png") coin_img = pygame.image.load("mini_task/img/Mini Project 1/img/coin.png") exit_img = pygame.image.load("mini_task/img/Mini Project 1/img/exit.png") save_img = pygame.image.load("mini_task/img/Mini Project 1/img/save_btn.png") load_img = pygame.image.load("mini_task/img/Mini Project 1/img/load_btn.png") #define game variables clicked = False level = 1 #define colours white = (255,255,255) green = (144,201,120) font = pygame.font.SysFont('Futura', 24) #create empty tile list world_data = [] for row in range(20): r = [0] * 20 world_data.append(r) #create boundary for tile in range(0,20): world_data[19][tile] = 2 world_data[0][tile] = 1 world_data[tile][0] = 1 world_data[tile][19] = 1 #function for outputting text into the screen def draw_text(text, font, text_col, x, y): img = font.render(text, True. text_col) screen.blit(img, (x, y)) def draw_grid(): for c in range(21): #vertical lines pygame.draw.line(screen , white, (c * tile_size, 0), (c * tile_size, screen_height - margin)) #horizontal lines pygame.draw.line(screen, white, (0, c * tile_size), (screen_width, c * tile_size)) def draw_world(): for row in range(20): for col in range(20): if world_data[row][col] > 0: if world_data[row][col] == 1: #dirt blocks img = pygame.transform.scale(dirt_img, (tile_size, tile_size)) screen.blit(img, (col * tile_size, row * tile_size)) if world_data[row][col] == 2: #grass blocks img = pygame.transform.scale(grass_img, (tile_size, tile_size)) screen.blit(img, (col * tile_size, row * tile_size)) if world_data[row][col] == 3: #enemy blocks img = pygame.transform.scale(blob_img, (tile_size, int(tile_size * 0.75))) screen.blit(img, (col * tile_size, row * tile_size + (tile_size * 0.25))) if world_data[row][col] == 4: #horizontally moving platform img = pygame.transform.scale(platform_x_img, (tile_size, tile_size // 2)) screen.blit(img, (col * tile_size, row * tile_size)) if world_data[row][col] == 5: #vertically moving platform img = pygame.transform.scale(platform_y_img, (tile_size, tile_size // 2)) screen.blit(img, (col * tile_size, row * tile_size)) if world_data[row][col] == 6: #lava img = pygame.transform.scale(lava_img, (tile_size, tile_size // 2)) screen.blit(img, (col * tile_size, row * tile_size + (tile_size // 2))) if world_data[row][col] == 7: #coin img = pygame.transform.scale(coin_img, (tile_size // 2, tile_size // 2)) screen.blit(img, (col * tile_size + (tile_size // 4), row * tile_size + (tile_size // 4))) if world_data[row][col] == 8: #exit img = pygame.transform.scale(exit_img, (tile_size, int(tile_size * 1.5))) screen.blit(img, (col * tile_size, row * tile_size - (tile_size // 2))) class Button(): def __init__(self, x, y, image): self.image = image self.rect = self.image.get_rect() self.rect.topleft = (x, y) self.clicked = False def draw(self): action = False #get mouse position pos = pygame.mouse.get_pos() #cheack mouseover and clicked conditions if self.rect.collidepoint(pos): if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False: action = True self.claicked = True if pygame.mouse.get_pressed()[0] == 0: self.claicked = False #draw button screen.blit(self . image, (self.rect.x, self.rect.y)) return action #create load and save buttons save_button = Button(screen_width // 2 - 150, screen_height - 80, save_img) load_button class World(): def __init__(self,data): self.tile_list = [] #load images dirt_img = pygame.image.load("mini_task/img/Mini Project 1/img/dirt.png") for row in data: col_count = 0 for tile in row: if tile ==1: img = pygame.transform.scale(dirt_img(tile_size,tile_size)) img_rect = img.get_rect() img_rect.x = col_count * tile_size img_rect.x = row_count * tile_size tile = (img,img_rect) self.tile_list.append(tile) col_count += 1 row_count += 1 world_data = [ [1, 1, 1, 1, 1,] [1, 0, 0, 0, 0,], [1, 0, 0, 0, 0,], [1, 0, 0, 0, 0,], [1, 1, 1, 1, 1,], ] world = World(world_data) run = True while run: screen.blit(bg_img,(0, 0)) screen.blit(sun_img,(100, 100)) draw_grid() print(world.tile_list) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False pygame.display.update() pygame.quit()