import pygame, os import pickle from os import path # Set working directory os.chdir('w:/11/hayden.derohan/Programming/91883 - Assignment/Platformer_Assignment') # Print current working directory print("Current working directory:", os.getcwd()) 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, bg_Sprites = pygame.image.load('Newsprites/Cave.png') bg_Sprites = pygame.transform.scale(bg_Sprites, (screen_width, screen_height - margin)) dirt_Sprites = pygame.image.load('Newsprites/Ground.png') grass_Sprites = pygame.image.load('Newsprites/Ground2.png') torch_sprites = pygame.image.load('Newsprites/Torch.png') Key_sprites = pygame.image.load("Newsprites/key.png") blob_Sprites = pygame.image.load('Sprites/blob.png') platform_x_Sprites = pygame.image.load('Sprites/platform_x.png') platform_y_Sprites = pygame.image.load('Sprites/platform_y.png') lava_Sprites = pygame.image.load('Sprites/lava.png') coin_Sprites = pygame.image.load('Newsprites/Gem.png') exit_Sprites = pygame.image.load('Sprites/exit.png') exitkey_Sprites = pygame.image.load('Newsprites/exitkey.png') save_Sprites = pygame.image.load('Sprites/save_btn.png') load_Sprites = pygame.image.load('Sprites/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 onto the screen def draw_text(text, font, text_col, x, y): Sprites = font.render(text, True, text_col) screen.blit(Sprites, (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: Sprites = pygame.transform.scale(dirt_Sprites, (tile_size, tile_size)) screen.blit(Sprites, (col * tile_size, row * tile_size)) elif world_data[row][col] == 2: Sprites = pygame.transform.scale(grass_Sprites, (tile_size, tile_size)) screen.blit(Sprites, (col * tile_size, row * tile_size)) elif world_data[row][col] == 3: Sprites = pygame.transform.scale(blob_Sprites, (tile_size, int(tile_size * 0.75))) screen.blit(Sprites, (col * tile_size, row * tile_size + (tile_size * 0.25))) elif world_data[row][col] == 4: Sprites = pygame.transform.scale(platform_x_Sprites, (tile_size, tile_size // 2)) screen.blit(Sprites, (col * tile_size, row * tile_size)) elif world_data[row][col] == 5: Sprites = pygame.transform.scale(platform_y_Sprites, (tile_size, tile_size // 2)) screen.blit(Sprites, (col * tile_size, row * tile_size)) elif world_data[row][col] == 6: Sprites = pygame.transform.scale(lava_Sprites, (tile_size, tile_size // 2)) screen.blit(Sprites, (col * tile_size, row * tile_size + (tile_size // 2))) elif world_data[row][col] == 7: Sprites = pygame.transform.scale(coin_Sprites, (tile_size // 2, tile_size // 2)) screen.blit(Sprites, (col * tile_size + (tile_size // 4), row * tile_size + (tile_size // 4))) elif world_data[row][col] == 8: Sprites = pygame.transform.scale(exit_Sprites, (tile_size, int(tile_size * 1.5))) screen.blit(Sprites, (col * tile_size, row * tile_size - (tile_size // 2))) elif world_data[row][col] == 9: Sprites = pygame.transform.scale(torch_sprites, (tile_size, tile_size)) screen.blit(Sprites, (col * tile_size, row * tile_size)) elif world_data[row][col] == 10: Sprites = pygame.transform.scale(Key_sprites, (tile_size // 1.5, tile_size // 1.1)) screen.blit(Sprites, (col * tile_size + 7.5, row * tile_size)) elif world_data[row][col] == 11: Sprites = pygame.transform.scale(exitkey_Sprites, (tile_size, int(tile_size * 1.5))) screen.blit(Sprites, (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() #check mouseover and clicked conditions if self.rect.collidepoint(pos): if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False: action = True self.clicked = True if pygame.mouse.get_pressed()[0] == 0: self.clicked = 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_Sprites) load_button = Button(screen_width // 2 + 50, screen_height - 80, load_Sprites) #main game loop run = True while run: clock.tick(fps) #draw background screen.fill(green) screen.blit(bg_Sprites, (0, 0)) #load and save level if save_button.draw(): #save level data pickle_out = open(f'level{level}_data', 'wb') pickle.dump(world_data, pickle_out) pickle_out.close() print("Level{}_Data saved".format(level)) if load_button.draw(): #load in level data if path.exists(f'level{level}_data'): pickle_in = open(f'level{level}_data', 'rb') world_data = pickle.load(pickle_in) print("Level{}_Data".format(level)) #show the grid and draw the level tiles draw_grid() draw_world() #text showing current level draw_text(f'Level: {level}', font, white, tile_size, screen_height - 60) draw_text('Press UP or DOWN to change level', font, white, tile_size, screen_height - 40) #event handler for event in pygame.event.get(): #quit game if event.type == pygame.QUIT: run = False #mouseclicks to change tiles if event.type == pygame.MOUSEBUTTONDOWN and clicked == False: clicked = True pos = pygame.mouse.get_pos() x = pos[0] // tile_size y = pos[1] // tile_size #check that the coordinates are within the tile area if x < 20 and y < 20: #update tile value if pygame.mouse.get_pressed()[0] == 1: world_data[y][x] += 1 if world_data[y][x] > 11: world_data[y][x] = 0 elif pygame.mouse.get_pressed()[2] == 1: world_data[y][x] -= 1 if world_data[y][x] < 0: world_data[y][x] = 11 if event.type == pygame.MOUSEBUTTONUP: clicked = False #up and down key presses to change level number if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: level += 1 elif event.key == pygame.K_DOWN and level > 1: level -= 1 #Make level editor easier by using no. 0-9 to make tiles elif event.key == pygame.K_SPACE: pos = pygame.mouse.get_pos() x = pos[0] // tile_size y = pos[1] // tile_size #check that the coordinates are within the tile area if x < 20 and y < 20: world_data[y][x] = 0 elif event.key == pygame.K_1: pos = pygame.mouse.get_pos() x = pos[0] // tile_size y = pos[1] // tile_size #check that the coordinates are within the tile area if x < 20 and y < 20: world_data[y][x] = 1 elif event.key == pygame.K_2: pos = pygame.mouse.get_pos() x = pos[0] // tile_size y = pos[1] // tile_size #check that the coordinates are within the tile area if x < 20 and y < 20: world_data[y][x] = 2 elif event.key == pygame.K_3: pos = pygame.mouse.get_pos() x = pos[0] // tile_size y = pos[1] // tile_size #check that the coordinates are within the tile area if x < 20 and y < 20: world_data[y][x] = 3 elif event.key == pygame.K_4: pos = pygame.mouse.get_pos() x = pos[0] // tile_size y = pos[1] // tile_size #check that the coordinates are within the tile area if x < 20 and y < 20: world_data[y][x] = 4 elif event.key == pygame.K_5: pos = pygame.mouse.get_pos() x = pos[0] // tile_size y = pos[1] // tile_size #check that the coordinates are within the tile area if x < 20 and y < 20: world_data[y][x] = 5 elif event.key == pygame.K_6: pos = pygame.mouse.get_pos() x = pos[0] // tile_size y = pos[1] // tile_size #check that the coordinates are within the tile area if x < 20 and y < 20: world_data[y][x] = 6 elif event.key == pygame.K_7: pos = pygame.mouse.get_pos() x = pos[0] // tile_size y = pos[1] // tile_size #check that the coordinates are within the tile area if x < 20 and y < 20: world_data[y][x] = 7 elif event.key == pygame.K_8: pos = pygame.mouse.get_pos() x = pos[0] // tile_size y = pos[1] // tile_size #check that the coordinates are within the tile area if x < 20 and y < 20: world_data[y][x] = 8 elif event.key == pygame.K_9: pos = pygame.mouse.get_pos() x = pos[0] // tile_size y = pos[1] // tile_size #check that the coordinates are within the tile area if x < 20 and y < 20: world_data[y][x] = 9 elif event.key == pygame.K_0: pos = pygame.mouse.get_pos() x = pos[0] // tile_size y = pos[1] // tile_size #check that the coordinates are within the tile area if x < 20 and y < 20: world_data[y][x] = 0 #update game display window pygame.display.update() pygame.quit()