class LoadGameWindow: """ Loads an in game window for handling saved games """ def __init__(self, x_pos=CONFIG.WINDOW_WIDTH // 2, y_pos=CONFIG.WINDOW_HEIGHT // 2, width=0.5 * CONFIG.WINDOW_WIDTH, height=0.8 * CONFIG.WINDOW_HEIGHT): self.x_pos = x_pos self.y_pos = y_pos self.width = width self.height = height self.list = [fn.replace('.save', '') for fn in os.listdir(SAVE_FOLDER)] self.file_list = List(self.x_pos, self.y_pos, self.list) self.background = pygame.Surface(( self.width, self.height, )) self.background.fill(BG_COLOR_2) common_y = self.y_pos + self.height // 2 - 3 * CONFIG.CHARACTER_SIZE self.back_button = Button(self.x_pos + self.width // 4 - 15, common_y, 'BACK') self.load_button = Button( self.x_pos + self.width // 4 - self.back_button.width - 15, common_y, 'LOAD') def display(self, screen): """ :returns bool: true if the window is still opened, false if it can be closed """ screen.blit(self.background, ( self.x_pos - self.width // 2, self.y_pos - self.height // 2, )) self.load_button.display(screen) self.back_button.display(screen) self.file_list.display(screen) if self.load_button.state == ButtonState.RELEASED: return False elif self.back_button.state == ButtonState.RELEASED: return False return True
class MainMenuScreen(Screen): def __init__(self): self.logo = self.logo = pygame.image.load(IMAGE_FOLDER + 'logo.png') self.logo_size = self.logo.get_rect().size self.show_game_loader = False self.game_loader = LoadGameWindow() button_margin = 50 self.new_game_button = Button( 96, CONFIG.WINDOW_HEIGHT - 96 - 3 * button_margin, 'NEW GAME') self.load_game_button = Button( 96, CONFIG.WINDOW_HEIGHT - 96 - 2 * button_margin, 'LOAD GAME') self.settings_button = Button( 96, CONFIG.WINDOW_HEIGHT - 96 - button_margin, 'SETTINGS') self.exit_button = Button(96, CONFIG.WINDOW_HEIGHT - 96, 'EXIT') def display(self, screen): screen.blit(self.logo, ( CONFIG.WINDOW_WIDTH - self.logo_size[0], 0, )) self.new_game_button.display(screen) self.load_game_button.display(screen) self.settings_button.display(screen) self.exit_button.display(screen) if self.show_game_loader: self.show_game_loader = self.game_loader.display(screen) else: if self.game_loader.file_list.selected_index != -1: CONFIG.SAVE_GAME = self.game_loader.list[ self.game_loader.file_list.selected_index] + '.save' self.game_loader.file_list.selected_index = -1 # Unselect the current save file return Screens.GAME # Check buttons if self.exit_button.state == ButtonState.RELEASED: return Screens.EXIT if self.new_game_button.state == ButtonState.RELEASED: return Screens.GAME if self.load_game_button.state == ButtonState.RELEASED: self.show_game_loader = True return Screens.MAIN_MENU if self.settings_button.state == ButtonState.RELEASED: return Screens.SETTINGS return Screens.MAIN_MENU
class SettingsScreen(Screen): def __init__(self, last_screen_enum=None): self.last_screen_enum = last_screen_enum label_margin = 75 self.labels = [( RM.readable_font.render('CONTROLS', 1, (255, 255, 255)), (75, 50), ), ( RM.readable_font.render('UP', 1, (240, 240, 240)), (75, 50 + 2 * label_margin), ), ( RM.readable_font.render('DOWN', 1, (240, 240, 240)), (75, 50 + 3 * label_margin), ), ( RM.readable_font.render('RIGHT', 1, (240, 240, 240)), (75, 50 + 4 * label_margin), ), ( RM.readable_font.render('LEFT', 1, (240, 240, 240)), (75, 50 + 5 * label_margin), )] self.buttons = [(Button(2 * 75, 40 + 2 * label_margin, label=pygame.key.name(CONFIG.KEY_UP), font_family=RM.readable_font), 'Up'), (Button(2 * 75, 40 + 3 * label_margin, label=pygame.key.name(CONFIG.KEY_DOWN), font_family=RM.readable_font), 'Down'), (Button(2 * 75, 40 + 4 * label_margin, label=pygame.key.name(CONFIG.KEY_RIGHT), font_family=RM.readable_font), 'Right'), (Button(2 * 75, 40 + 5 * label_margin, label=pygame.key.name(CONFIG.KEY_LEFT), font_family=RM.readable_font), 'Left')] self.back_button = Button(50, CONFIG.WINDOW_HEIGHT - 4 * CONFIG.CHARACTER_SIZE, label='BACK') def check_for_choosing_key(self, button, label): if button.state == ButtonState.RELEASED: in_key_choosing = True while in_key_choosing: for event in pygame.event.get(): if event.type != pygame.KEYDOWN: continue if event.key == pygame.locals.K_ESCAPE: in_key_choosing = False break button.set_label(pygame.key.name(KEYBOARD[event.key]), RM.readable_font) in_key_choosing = False # Change key in configuration, too if label == 'Up': CONFIG.KEY_UP = KEYBOARD[event.key] elif label == 'Down': CONFIG.KEY_DOWN = KEYBOARD[event.key] elif label == 'Right': CONFIG.KEY_RIGHT = KEYBOARD[event.key] elif label == 'Left': CONFIG.KEY_LEFT = KEYBOARD[event.key] CONFIG.save() break def display(self, screen): for label in self.labels: screen.blit(label[0], label[1]) for button, button_label in self.buttons: self.check_for_choosing_key(button, button_label) button.display(screen) self.back_button.display(screen) if self.back_button.state == ButtonState.RELEASED: return Screens.MAIN_MENU if self.last_screen_enum is None else self.last_screen_enum return Screens.SETTINGS
class GameScreen(Screen): def __init__(self): self.subscreen = GameSubScreen.START_MENU self.map = Map(MAP_FOLDER + CONFIG.CURRENT_LEVEL + '.tcm') # Stores the current map self.player = Player(len(self.map.layers[4].tiles[0]), 32) self.bot = Bot(len(self.map.layers[4].tiles[0]) + 32, 32) self.in_game_menu_bg = None # In game menu elements button_margin = 50 self.in_game_resume_button = Button(96, CONFIG.WINDOW_HEIGHT - 96 - 3 * button_margin, label='RESUME') self.in_game_save_button = Button(96, CONFIG.WINDOW_HEIGHT - 96 - 2 * button_margin, label='SAVE GAME') self.in_game_exit_button = Button(96, CONFIG.WINDOW_HEIGHT - 96 - button_margin, label='EXIT') if CONFIG.SAVE_GAME != '': load_save_game(self) CONFIG.SAVE_GAME = '' def display_start_menu(self, screen): self.subscreen = GameSubScreen.GAME return Screens.GAME def display_in_game_menu(self, screen): screen.blit(self.in_game_menu_bg, (0, 0)) self.in_game_resume_button.display(screen) self.in_game_save_button.display(screen) self.in_game_exit_button.display(screen) if self.in_game_exit_button.state == ButtonState.RELEASED: return Screens.MAIN_MENU elif self.in_game_resume_button.state == ButtonState.RELEASED: self.subscreen = GameSubScreen.GAME return Screens.GAME elif self.in_game_save_button.state == ButtonState.RELEASED: save_game(self) return Screens.GAME return Screens.GAME def display_game(self, screen): self.map.draw(screen, self.player, 0, 5) self.bot.display(screen, self.map, self.player) self.player.display(screen, self.map) self.map.draw(screen, self.player, 5, 8) if pygame.key.get_pressed()[pygame.locals.K_ESCAPE]: # Save game screenshot as a background pygame.image.save(screen, IMAGE_FOLDER + 'screenshot.png') self.in_game_menu_bg = pygame.image.load(IMAGE_FOLDER + 'screenshot.png') self.in_game_menu_bg.set_alpha(150) self.subscreen = GameSubScreen.IN_GAME_MENU return Screens.GAME return Screens.GAME def display(self, screen): if self.subscreen == GameSubScreen.GAME: return self.display_game(screen) elif self.subscreen == GameSubScreen.START_MENU: return self.display_start_menu(screen) else: return self.display_in_game_menu(screen) return Screens.GAME