import pygame
import random
import sys

pygame.init()

WIDTH, HEIGHT = 900, 560
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("水循环动画")
clock = pygame.time.Clock()
FPS = 60

# 颜色
SKY = (175, 220, 255)
OCEAN = (35, 100, 180)
OCEAN_LIGHT = (70, 140, 220)
MOUNTAIN_DARK = (110, 95, 80)
MOUNTAIN = (140, 120, 100)
MOUNTAIN_LIGHT = (165, 145, 125)
GRASS = (75, 145, 75)
SOIL = (120, 90, 65)
ROCK = (145, 140, 135)
RIVER = (60, 140, 210)
RIVER_HIGHLIGHT = (110, 180, 230)
CLOUD = (255, 255, 255)
CLOUD_SHADOW = (230, 240, 250)
VAPOR = (205, 230, 255)
DROP = (50, 130, 210)
TRUNK = (95, 65, 45)
LEAVES = (65, 130, 65)
LEAVES_DARK = (50, 105, 50)
SUN = (255, 235, 90)
SUN_GLOW = (255, 250, 180)
WHITE = (255, 255, 255)
BLACK = (20, 20, 20)

sun_x, sun_y = 110, 90

# 5朵云
clouds = [
    {"x": 200, "y": 90, "speed": 0.30},
    {"x": 340, "y": 65, "speed": 0.22},
    {"x": 480, "y": 100, "speed": 0.28},
    {"x": 620, "y": 75, "speed": 0.24},
    {"x": 760, "y": 110, "speed": 0.26},
]

mountain_topline = [
    (340, HEIGHT - 110),
    (380, 370),
    (420, 390),
    (460, 340),
    (510, 360),
    (560, 260),
    (610, 300),
    (660, 280),
    (720, 330),
    (770, 310),
    (820, 340),
    (WIDTH, 320),
    (WIDTH, HEIGHT - 110),
    (340, HEIGHT - 110),
]

mountain_shadowline = [
    (560, 260),
    (610, 300),
    (660, 280),
    (720, 330),
    (770, 310),
    (820, 340),
    (WIDTH, 320),
    (WIDTH, HEIGHT - 110),
    (560, HEIGHT - 110),
]

grass_topline = [
    (340, HEIGHT - 110),
    (380, 370),
    (420, 390),
    (460, 340),
    (510, 360),
    (560, 260),
    (610, 300),
    (660, 280),
    (720, 330),
    (770, 310),
    (820, 340),
    (WIDTH, 320),
    (WIDTH, HEIGHT - 110),
]

class VaporParticle:
    def __init__(self):
        self.reset()

    def reset(self):
        self.x = random.randint(60, 340)
        self.y = HEIGHT - 110
        self.vy = -random.uniform(0.4, 1.0)
        self.vx = random.uniform(-0.15, 0.15)
        self.life = random.randint(180, 360)
        self.size = random.randint(2, 4)
        self.state = "rise"

    def update(self):
        if self.state == "rise":
            self.x += self.vx
            self.y += self.vy
            self.life -= 1
            if self.y < 140 or self.life <= 0:
                self.state = "fade"
        elif self.state == "fade":
            self.y += self.vy * 0.3
            self.life -= 1

    def draw(self):
        if self.life > 0:
            alpha = max(30, min(220, int(self.life)))
            s = pygame.Surface((self.size * 2, self.size * 2), pygame.SRCALPHA)
            pygame.draw.circle(s, (*VAPOR, alpha), (self.size, self.size), self.size)
            screen.blit(s, (int(self.x), int(self.y)))

class RainDrop:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.vy = random.uniform(4, 7)
        self.vx = random.uniform(-0.4, 0.4)
        self.size = random.randint(2, 4)

    def update(self):
        self.x += self.vx
        self.y += self.vy

    def draw(self):
        pygame.draw.circle(screen, DROP, (int(self.x), int(self.y)), self.size)

class RunoffDrop:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.vx = random.uniform(-1.6, -0.8)
        self.size = random.randint(2, 4)

    def update(self):
        self.x += self.vx
        self.y += 0.05

    def draw(self):
        pygame.draw.circle(screen, DROP, (int(self.x), int(self.y)), self.size)

class InfiltrationDrop:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.vy = random.uniform(0.6, 1.2)
        self.vx = random.uniform(-0.25, -0.05)
        self.size = random.randint(2, 4)
        self.state = "infiltrate"

    def update(self):
        if self.state == "infiltrate":
            self.y += self.vy
            self.x += self.vx
            if self.y > HEIGHT - 170:
                self.state = "groundwater"
                self.vy = 0
        elif self.state == "groundwater":
            self.x += self.vx * 0.4
            self.y += 0.02

    def draw(self):
        c = (40, 100, 170) if self.state == "groundwater" else DROP
        pygame.draw.circle(screen, c, (int(self.x), int(self.y)), self.size)

class TranspirationParticle:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.vy = -random.uniform(0.5, 1.1)
        self.vx = random.uniform(-0.2, 0.2)
        self.life = random.randint(120, 240)
        self.size = random.randint(2, 4)

    def update(self):
        self.x += self.vx
        self.y += self.vy
        self.life -= 1

    def draw(self):
        if self.life > 0:
            alpha = max(30, min(200, int(self.life)))
            s = pygame.Surface((self.size * 2, self.size * 2), pygame.SRCALPHA)
            pygame.draw.circle(s, (*VAPOR, alpha), (self.size, self.size), self.size)
            screen.blit(s, (int(self.x), int(self.y)))

def draw_ocean():
    pygame.draw.rect(screen, OCEAN, (0, HEIGHT - 110, 360, 110))
    pygame.draw.rect(screen, OCEAN_LIGHT, (0, HEIGHT - 110, 360, 12))
    for i in range(10):
        x = 20 + i * 34
        y = HEIGHT - 95 + (i % 2) * 5
        pygame.draw.circle(screen, OCEAN_LIGHT, (x, y), 4)

def draw_mountain():
    pygame.draw.polygon(screen, MOUNTAIN, mountain_topline)
    pygame.draw.polygon(screen, MOUNTAIN_LIGHT, mountain_shadowline)
    pygame.draw.lines(screen, MOUNTAIN_DARK, True, mountain_topline, 3)

def draw_grass():
    pygame.draw.lines(screen, GRASS, False, grass_topline, 5)

def draw_ground_layers():
    pygame.draw.rect(screen, SOIL, (340, HEIGHT - 70, WIDTH - 340, 40))
    pygame.draw.rect(screen, ROCK, (340, HEIGHT - 30, WIDTH - 340, 30))

def draw_river():
    points = [
        (360, HEIGHT - 110),
        (410, 380),
        (460, 350),
        (520, 340),
        (580, 310),
        (640, 300),
        (700, 320),
        (760, 310),
        (820, 330),
        (WIDTH, 310),
    ]
    for i in range(len(points) - 1):
        pygame.draw.line(screen, RIVER, points[i], points[i + 1], 6)
        pygame.draw.line(screen, RIVER_HIGHLIGHT, points[i], points[i + 1], 2)

def draw_tree(x, y):
    pygame.draw.rect(screen, TRUNK, (x - 6, y - 40, 12, 40))
    pygame.draw.circle(screen, LEAVES, (x, y - 50), 22)
    pygame.draw.circle(screen, LEAVES_DARK, (x - 14, y - 42), 14)
    pygame.draw.circle(screen, LEAVES_DARK, (x + 14, y - 42), 14)

def draw_cloud(cx, cy):
    pygame.draw.circle(screen, CLOUD, (cx, cy), 30)
    pygame.draw.circle(screen, CLOUD, (cx + 28, cy - 8), 26)
    pygame.draw.circle(screen, CLOUD, (cx + 55, cy), 30)
    pygame.draw.circle(screen, CLOUD, (cx + 28, cy + 12), 24)
    pygame.draw.circle(screen, CLOUD_SHADOW, (cx + 28, cy + 12), 20)

def main():
    vapors = []
    raindrops = []
    runoff = []
    infiltration = []
    transpiration = []

    pause = False
    frame = 0

    while True:
        screen.fill(SKY)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    pause = not pause
            # 鼠标左键点击，在点击位置生成雨滴
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    mx, my = event.pos
                    # 一次点击生成8滴雨
                    for _ in range(8):
                        rx = mx + random.randint(-25, 25)
                        ry = my
                        raindrops.append(RainDrop(rx, ry))

        if not pause:
            frame += 1

            # 更新云朵
            for c in clouds:
                c["x"] += c["speed"]
                if c["x"] > WIDTH + 80:
                    c["x"] = -80

            if frame % 6 == 0:
                vapors.append(VaporParticle())

            # 云朵自动下雨
            for c in clouds:
                if c["x"] > 330 and c["x"] < WIDTH and random.random() < 0.08:
                    raindrops.append(RainDrop(c["x"] + random.randint(-40, 40), c["y"] + 30))

            for v in vapors[:]:
                v.update()
                if v.life <= 0:
                    vapors.remove(v)

            for d in raindrops[:]:
                d.update()
                if d.y > HEIGHT - 110:
                    if random.random() < 0.45:
                        runoff.append(RunoffDrop(d.x, HEIGHT - 110))
                    else:
                        infiltration.append(InfiltrationDrop(d.x, HEIGHT - 110))
                    raindrops.remove(d)

            for r in runoff[:]:
                r.update()
                if r.x < 60:
                    runoff.remove(r)

            for i in infiltration[:]:
                i.update()
                if i.y > HEIGHT - 20:
                    infiltration.remove(i)

            if frame % 11 == 0:
                transpiration.append(TranspirationParticle(470, HEIGHT - 110))
                transpiration.append(TranspirationParticle(730, HEIGHT - 110))

            for t in transpiration[:]:
                t.update()
                if t.life <= 0:
                    transpiration.remove(t)

        # 画太阳
        pygame.draw.circle(screen, SUN_GLOW, (sun_x, sun_y), 48, 2)
        pygame.draw.circle(screen, SUN, (sun_x, sun_y), 38)

        # 画5朵云
        for c in clouds:
            draw_cloud(c["x"], c["y"])

        draw_ocean()
        draw_mountain()
        draw_grass()
        draw_ground_layers()
        draw_river()

        draw_tree(470, HEIGHT - 110)
        draw_tree(730, HEIGHT - 110)

        for v in vapors:
            v.draw()
        for d in raindrops:
            d.draw()
        for r in runoff:
            r.draw()
        for i in infiltration:
            i.draw()
        for t in transpiration:
            t.draw()

        pygame.display.flip()
        clock.tick(FPS)

if __name__ == "__main__":
    main()
