import pygame
import random
import math

pygame.init()
WIDTH, HEIGHT = 800, 600
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("像素荒岛")
clock = pygame.time.Clock()
TILE = 32

# 颜色
WATER = (15,75,155)
SAND = (230,218,170)
GRASS = (52,122,36)
TRUNK = (82,55,28)
LEAF = (28,62,18)
SKIN = (255, 204, 153)
BODY_COLOR = (224,60,45)

# 生成噪声式岛屿
map_w = WIDTH // TILE
map_h = HEIGHT // TILE
terrain = [[0 for _ in range(map_w)] for _ in range(map_h)]
trees = []

cx, cy = map_w / 2, map_h / 2
for y_idx in range(map_h):
    for x_idx in range(map_w):
        dist = math.hypot(x_idx - cx, y_idx - cy)
        noise = random.uniform(-1.2,1.2)
        real_dist = dist + noise

        if real_dist > 12:
            terrain[y_idx][x_idx] = 0
        elif real_dist > 10:
            terrain[y_idx][x_idx] = 1
        else:
            terrain[y_idx][x_idx] = 2
            if random.random() < 0.15:
                trees.append((x_idx * TILE, y_idx * TILE))

class Player:
    def __init__(self):
        self.rect = pygame.Rect(cx*TILE, cy*TILE,24,24)
        self.speed = 3

    def update(self):
        dx = 0
        dy = 0
        keys = pygame.key.get_pressed()
        if keys[pygame.K_w]: dy -= self.speed
        if keys[pygame.K_s]: dy += self.speed
        if keys[pygame.K_a]: dx -= self.speed
        if keys[pygame.K_d]: dx += self.speed

        # X轴碰撞
        self.rect.x += dx
        if self.check_water():
            self.rect.x -= dx
        # Y轴碰撞
        self.rect.y += dy
        if self.check_water():
            self.rect.y -= dy

    def check_water(self):
        tile1_x = self.rect.left // TILE
        tile2_x = self.rect.right // TILE
        tile1_y = self.rect.top // TILE
        tile2_y = self.rect.bottom // TILE
        for ty in range(tile1_y, tile2_y + 1):
            for tx in range(tile1_x, tile2_x +1):
                if 0 <= tx < map_w and 0 <= ty < map_h:
                    if terrain[ty][tx] == 0:
                        return True
        return False

    def draw(self):
        px = self.rect.x
        py = self.rect.y
        # 像素小人绘制(16×24像素)
        # 脑袋
        pygame.draw.rect(SCREEN, SKIN, (px + 4, py, 12, 10))
        # 眼睛
        pygame.draw.rect(SCREEN, (0,0,0), (px+6, py+3,2,2))
        pygame.draw.rect(SCREEN, (0,0,0), (px+12, py+3,2,2))
        # 身体
        pygame.draw.rect(SCREEN, BODY_COLOR, (px + 5, py + 10, 10, 8))
        # 腿
        pygame.draw.rect(SCREEN, (40,40,40), (px+5, py+18,4,6))
        pygame.draw.rect(SCREEN, (40,40,40), (px+11, py+18,4,6))

player = Player()
time_count = 0

running = True
while running:
    clock.tick(60)
    time_count += 1
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    player.update()

    # 绘制地形 + 海浪动态变色
    for y_idx in range(map_h):
        for x_idx in range(map_w):
            rx = x_idx * TILE
            ry = y_idx * TILE
            rect = pygame.Rect(rx, ry, TILE -1, TILE -1)
            t = terrain[y_idx][x_idx]
            if t == 0:
                wave_offset = int(10 * math.sin(time_count * 0.03 + x_idx * 0.3))
                color = (max(0, WATER[0]+wave_offset), WATER[1], min(200, WATER[2]+wave_offset))
                pygame.draw.rect(SCREEN, color, rect)
            elif t == 1:
                pygame.draw.rect(SCREEN, SAND, rect)
            elif t == 2:
                pygame.draw.rect(SCREEN, GRASS, rect)

    # 绘制像素树
    for (tx, ty) in trees:
        pygame.draw.rect(SCREEN, TRUNK, (tx+12, ty+14, 8,16))
        pygame.draw.circle(SCREEN, LEAF, (tx+16, ty+9), 11)

    player.draw()

    # 昼夜渐变
    dark = pygame.Surface((WIDTH, HEIGHT))
    dark.fill((0,0,20))
    alpha = int(70 * abs(math.sin(time_count * 0.007)))
    dark.set_alpha(alpha)
    SCREEN.blit(dark, (0,0))
    pygame.display.update()

pygame.quit()