import pygame import json import random import os from config import * class MechanismUI: def __init__(self, font): self.font = font self.is_showing = False self.mechanism_data = None self.window_rect = pygame.Rect(0, 0, 600, 400) self.close_button_rect = pygame.Rect(0, 0, 80, 40) self.setup_window() def setup_window(self): """设置窗口位置""" self.window_rect.center = (UI_WIDTH // 2, UI_HEIGHT // 2) self.close_button_rect.x = self.window_rect.right - 90 self.close_button_rect.y = self.window_rect.y + 10 def load_random_password_file(self): """从pwd_output目录随机加载一个密码文件""" pwd_dir = "pwd_output" if not os.path.exists(pwd_dir): print(f"目录 {pwd_dir} 不存在") return None # 获取所有json文件 json_files = [f for f in os.listdir(pwd_dir) if f.endswith('.json')] if not json_files: print(f"目录 {pwd_dir} 中没有找到json文件") return None # 随机选择一个文件 selected_file = random.choice(json_files) file_path = os.path.join(pwd_dir, selected_file) try: with open(file_path, 'r', encoding='utf-8') as f: data = json.load(f) print(f"成功加载密码文件: {selected_file}") return data except Exception as e: print(f"加载密码文件失败: {str(e)}") return None def show_mechanism(self): """显示机关界面""" self.mechanism_data = self.load_random_password_file() if self.mechanism_data: self.is_showing = True print("机关界面已显示") else: print("无法显示机关界面:密码数据加载失败") def hide_mechanism(self): """隐藏机关界面""" self.is_showing = False self.mechanism_data = None print("机关界面已隐藏") def handle_event(self, event): """处理事件""" if not self.is_showing: return False if event.type == pygame.MOUSEBUTTONDOWN: if self.close_button_rect.collidepoint(event.pos): self.hide_mechanism() return True elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: self.hide_mechanism() return True return True # 阻止其他事件处理 def draw(self, screen): """绘制机关界面""" if not self.is_showing or not self.mechanism_data: return # 绘制半透明背景 overlay = pygame.Surface((UI_WIDTH, UI_HEIGHT)) overlay.set_alpha(128) overlay.fill((0, 0, 0)) screen.blit(overlay, (0, 0)) # 绘制主窗口 pygame.draw.rect(screen, COLOR_WHITE, self.window_rect) pygame.draw.rect(screen, COLOR_BLACK, self.window_rect, 3) # 绘制标题 title_text = self.font.render("机关密码破解", True, COLOR_BLACK) title_rect = title_text.get_rect(centerx=self.window_rect.centerx, y=self.window_rect.y + 20) screen.blit(title_text, title_rect) # 绘制密码信息 password = self.mechanism_data.get("password", "未知") password_text = self.font.render(f"最终密码: {password}", True, (255, 0, 0)) password_rect = password_text.get_rect(centerx=self.window_rect.centerx, y=self.window_rect.y + 60) screen.blit(password_text, password_rect) # 绘制三种策略的信息 results = self.mechanism_data.get("results", {}) y_offset = 100 method_names = { "method1": "策略一: 暴力破解", "method2": "策略二: 启发式搜索", "method3": "策略三: 智能分析" } for method_key, method_info in results.items(): if method_key in method_names: method_name = method_names[method_key] tries = method_info.get("tries", 0) password_array = method_info.get("password", []) # 绘制策略名称 method_text = self.font.render(method_name, True, (0, 0, 255)) screen.blit(method_text, (self.window_rect.x + 30, self.window_rect.y + y_offset)) # 绘制尝试次数 tries_text = self.font.render(f"尝试次数: {tries}", True, COLOR_BLACK) screen.blit(tries_text, (self.window_rect.x + 50, self.window_rect.y + y_offset + 25)) # 绘制密码数组 password_str = "".join(map(str, password_array)) password_array_text = self.font.render(f"密码: {password_str}", True, COLOR_GREEN) screen.blit(password_array_text, (self.window_rect.x + 50, self.window_rect.y + y_offset + 50)) y_offset += 80 # 绘制关闭按钮 pygame.draw.rect(screen, COLOR_GRAY, self.close_button_rect) pygame.draw.rect(screen, COLOR_BLACK, self.close_button_rect, 2) close_text = self.font.render("关闭", True, COLOR_BLACK) close_rect = close_text.get_rect(center=self.close_button_rect.center) screen.blit(close_text, close_rect) # 绘制操作提示 hint_text = self.font.render("按ESC键或点击关闭按钮退出", True, COLOR_GRAY) hint_rect = hint_text.get_rect(centerx=self.window_rect.centerx, y=self.window_rect.bottom - 30) screen.blit(hint_text, hint_rect)