import pygame
import sys
import random

# 初始化Pygame
pygame.init()

# 屏幕设置
WIDTH, HEIGHT = 600, 450
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("天气查询小工具")

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
GRAY = (128, 128, 128)
LIGHT_GRAY = (200, 200, 200)

# 修复中文显示：使用系统中文字体
def get_chinese_font(size):
    font_names = [
        "SimHei", "Microsoft YaHei", "SimSun",
        "PingFang SC", "Heiti SC",
        "WenQuanYi Micro Hei", "Noto Sans CJK SC"
    ]
    
    for font_name in font_names:
        try:
            font = pygame.font.SysFont(font_name, size)
            test_text = font.render("测试", True, BLACK)
            if test_text.get_width() > 10:
                return font
        except:
            continue
    
    print("警告：未找到支持中文的字体，中文可能显示为方块")
    return pygame.font.Font(None, size)

# 字体设置
font_large = get_chinese_font(48)
font_medium = get_chinese_font(36)
font_small = get_chinese_font(28)

# 天气数据
weather_types = ["晴天", "多云", "阴天", "小雨", "中雨", "大雨", "雷阵雨", "小雪", "中雪", "大雪"]
cities = ["北京", "上海", "广州", "深圳", "杭州", "南京", "成都", "重庆", "武汉", "西安"]

# 按钮类
class Button:
    def __init__(self, x, y, w, h, text, color=LIGHT_GRAY, hover_color=GRAY):
        self.rect = pygame.Rect(x, y, w, h)
        self.text = text
        self.color = color
        self.hover_color = hover_color
        self.is_hovered = False

    def draw(self, screen):
        current_color = self.hover_color if self.is_hovered else self.color
        pygame.draw.rect(screen, current_color, self.rect)
        pygame.draw.rect(screen, BLACK, self.rect, 2)
        text_surface = font_small.render(self.text, True, BLACK)
        screen.blit(text_surface, (self.rect.x + self.rect.width//2 - text_surface.get_width()//2,
                                  self.rect.y + self.rect.height//2 - text_surface.get_height()//2))

    def handle_event(self, event):
        if event.type == pygame.MOUSEMOTION:
            self.is_hovered = self.rect.collidepoint(event.pos)
        if event.type == pygame.MOUSEBUTTONDOWN:
            if self.rect.collidepoint(event.pos):
                return True
        return False

# 绘制天气图标
def draw_weather_icon(weather, x, y, size=100):
    if "晴" in weather:
        pygame.draw.circle(screen, YELLOW, (x, y), size//2)
    elif "云" in weather or "阴" in weather:
        pygame.draw.circle(screen, GRAY, (x - size//4, y), size//3)
        pygame.draw.circle(screen, GRAY, (x + size//4, y), size//3)
        pygame.draw.rect(screen, GRAY, (x - size//2, y - size//6, size, size//3))
    elif "雨" in weather:
        pygame.draw.circle(screen, GRAY, (x, y - size//4), size//3)
        for i in range(5):
            pygame.draw.line(screen, BLUE, (x - size//3 + i*size//6, y + size//6),
                            (x - size//3 + i*size//6, y + size//3), 2)
    elif "雪" in weather:
        pygame.draw.circle(screen, GRAY, (x, y - size//4), size//3)
        for i in range(5):
            pygame.draw.circle(screen, WHITE, (x - size//3 + i*size//6, y + size//4), 3)

# 主程序
def main():
    city_buttons = []
    for i, city in enumerate(cities):
        x = 50 + (i % 5) * 100
        y = 100 + (i // 5) * 60
        city_buttons.append(Button(x, y, 90, 50, city))
    
    current_weather = ""
    current_temp = 0
    current_city = ""
    
    while True:
        screen.fill(WHITE)
        
        # 绘制标题
        title = font_large.render("天气查询", True, BLACK)
        screen.blit(title, (WIDTH//2 - title.get_width()//2, 30))
        
        # 绘制城市按钮
        for button in city_buttons:
            button.draw(screen)
        
        # 绘制天气信息
        if current_city:
            city_text = font_medium.render(f"{current_city} 天气", True, BLACK)
            screen.blit(city_text, (WIDTH//2 - city_text.get_width()//2, 220))
            
            draw_weather_icon(current_weather, WIDTH//2, 300)
            
            weather_text = font_medium.render(f"{current_weather} {current_temp}°C", True, BLACK)
            screen.blit(weather_text, (WIDTH//2 - weather_text.get_width()//2, 370))
        
        # 事件处理
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            
            for i, button in enumerate(city_buttons):
                if button.handle_event(event):
                    current_city = cities[i]
                    current_weather = random.choice(weather_types)
                    current_temp = random.randint(-10, 35)
        
        pygame.display.flip()

if __name__ == "__main__":
    main()