maze_python/tests/test_10_saves_display.py

110 lines
3.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""测试存档界面显示10条存档的能力"""
import pygame
import sys
import os
# 添加项目根目录到路径
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from config import *
from save_ui import SaveLoadUI
from maze import Maze
def test_10_saves_display():
"""测试显示10条存档"""
print("=== 测试存档界面10条存档显示能力 ===")
# 初始化pygame
pygame.init()
screen = pygame.display.set_mode((UI_WIDTH, UI_HEIGHT))
pygame.display.set_caption("测试10条存档显示")
font = pygame.font.Font(FONT_FILE, FONT_SIZE)
# 创建对象
maze = Maze(wall_size=20, maze_size=200, file_name="test")
save_ui = SaveLoadUI(font)
# 检查max_visible_saves设置
print(f"max_visible_saves设置: {save_ui.max_visible_saves}")
# 模拟10条存档数据
mock_saves = []
for i in range(10):
mock_saves.append({
'name': f'test_save_{i+1:02d}.json',
'path': f'saves/test_save_{i+1:02d}.json',
'save_time': f'2024-01-{i+1:02d} 10:00:00',
'size': '5x5'
})
# 直接设置存档列表
save_ui.save_list = mock_saves
save_ui.show_save_list = True
print(f"模拟存档数量: {len(mock_saves)}")
# 计算显示参数
button_positions = get_button_positions()
list_area = button_positions['save_list_area']
print(f"存档列表区域: x={list_area[0]}, y={list_area[1]}, w={list_area[2]}, h={list_area[3]}")
# 计算可显示的存档数量
# 标题区域: 30像素
# 操作提示区域: 25像素
# 剩余区域高度: 330 - 30 - 25 = 275像素
# 每个存档项: 25像素
available_height = list_area[3] - 55 # 55 = 30(标题) + 25(提示)
max_displayable = available_height // 25
print(f"可用显示高度: {available_height}像素")
print(f"理论最大显示数量: {max_displayable}")
print(f"实际设置显示数量: {save_ui.max_visible_saves}")
# 测试显示
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
elif event.key == pygame.K_UP:
if save_ui.selected_save > 0:
save_ui.selected_save -= 1
print(f"选中存档: {save_ui.selected_save + 1}")
elif event.key == pygame.K_DOWN:
if save_ui.selected_save < len(save_ui.save_list) - 1:
save_ui.selected_save += 1
print(f"选中存档: {save_ui.selected_save + 1}")
# 绘制
screen.fill(COLOR_WHITE)
save_ui.draw(screen)
# 绘制测试信息
info_text = [
f"存档数量: {len(save_ui.save_list)}",
f"显示设置: {save_ui.max_visible_saves}",
f"当前选中: {save_ui.selected_save + 1}",
"按↑↓键选择存档ESC退出"
]
for i, text in enumerate(info_text):
text_surface = font.render(text, True, COLOR_BLACK)
screen.blit(text_surface, (10, 10 + i * 30))
pygame.display.flip()
clock.tick(60)
pygame.quit()
print("测试完成")
if __name__ == "__main__":
test_10_saves_display()