import pygame
import sys

# 初始化
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("简化版马里奥")
clock = pygame.time.Clock()

# 颜色
SKY_BLUE = (107, 140, 255)
GROUND_BROWN = (139, 69, 19)
MARIO_RED = (255, 0, 0)
MARIO_BLUE = (0, 0, 255)
COIN_YELLOW = (255, 215, 0)
ENEMY_BROWN = (150, 75, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

# 物理参数
GRAVITY = 0.8
JUMP_STRENGTH = -15
MOVE_SPEED = 5
FPS = 60

class Player:
    def __init__(self, x, y):
        self.rect = pygame.Rect(x, y, 30, 40)
        self.vel_y = 0
        self.on_ground = False
        self.facing_right = True
        self.score = 0
        self.alive = True

    def update(self, platforms, enemies, coins):
        if not self.alive:
            return
        keys = pygame.key.get_pressed()
        dx = 0
        # 水平移动
        if keys[pygame.K_LEFT] or keys[pygame.K_a]:
            dx = -MOVE_SPEED
            self.facing_right = False
        if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
            dx = MOVE_SPEED
            self.facing_right = True

        # 重力
        self.vel_y += GRAVITY
        if self.vel_y > 20:
            self.vel_y = 20

        # 跳跃
        if (keys[pygame.K_SPACE] or keys[pygame.K_UP] or keys[pygame.K_w]) and self.on_ground:
            self.vel_y = JUMP_STRENGTH
            self.on_ground = False

        # 水平移动与碰撞
        self.rect.x += dx
        for p in platforms:
            if self.rect.colliderect(p):
                if dx > 0:
                    self.rect.right = p.left
                elif dx < 0:
                    self.rect.left = p.right

        # 垂直移动与碰撞
        self.rect.y += self.vel_y
        self.on_ground = False
        for p in platforms:
            if self.rect.colliderect(p):
                if self.vel_y > 0:  # 下落
                    self.rect.bottom = p.top
                    self.vel_y = 0
                    self.on_ground = True
                elif self.vel_y < 0:  # 上升
                    self.rect.top = p.bottom
                    self.vel_y = 0

        # 边界
        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.right > WIDTH:
            self.rect.right = WIDTH
        if self.rect.top > HEIGHT:
            self.alive = False

        # 收集金币
        for coin in coins[:]:
            if self.rect.colliderect(coin):
                coins.remove(coin)
                self.score += 10

        # 敌人碰撞
        for enemy in enemies:
            if self.rect.colliderect(enemy.rect):
                # 如果从上方踩到敌人
                if self.vel_y > 0 and self.rect.bottom < enemy.rect.centery + 10:
                    enemies.remove(enemy)
                    self.vel_y = JUMP_STRENGTH // 1.5
                    self.score += 20
                else:
                    self.alive = False

    def draw(self, surface):
        if not self.alive:
            return
        # 身体
        pygame.draw.rect(surface, MARIO_RED, self.rect)
        # 帽子
        hat_rect = pygame.Rect(self.rect.x, self.rect.y, self.rect.width, 10)
        pygame.draw.rect(surface, MARIO_RED, hat_rect)
        # 脸部
        face_rect = pygame.Rect(self.rect.x + 5, self.rect.y + 10, 20, 15)
        pygame.draw.rect(surface, (255, 220, 177), face_rect)
        # 眼睛
        eye_x = self.rect.x + 20 if self.facing_right else self.rect.x + 8
        pygame.draw.circle(surface, BLACK, (eye_x, self.rect.y + 15), 3)
        # 衣服
        shirt_rect = pygame.Rect(self.rect.x + 3, self.rect.y + 25, 24, 10)
        pygame.draw.rect(surface, MARIO_BLUE, shirt_rect)

class Enemy:
    def __init__(self, x, y, platform_width):
        self.rect = pygame.Rect(x, y, 30, 30)
        self.direction = 1
        self.speed = 2
        self.platform_width = platform_width
        self.start_x = x

    def update(self, platforms):
        self.rect.x += self.direction * self.speed
        # 在平台上巡逻
        if self.rect.x <= self.start_x - 50 or self.rect.x >= self.start_x + 50:
            self.direction *= -1
        # 检查是否超出平台边缘
        on_platform = False
        for p in platforms:
            if self.rect.colliderect(p):
                on_platform = True
                # 如果快要掉下去，掉头
                if self.rect.bottom == p.top:
                    if self.direction == 1 and self.rect.right >= p.right - 5:
                        self.direction = -1
                    elif self.direction == -1 and self.rect.left <= p.left + 5:
                        self.direction = 1
        if not on_platform:
            self.direction *= -1

    def draw(self, surface):
        pygame.draw.rect(surface, ENEMY_BROWN, self.rect)
        # 眼睛
        eye_y = self.rect.y + 10
        pygame.draw.circle(surface, WHITE, (self.rect.x + 8, eye_y), 5)
        pygame.draw.circle(surface, WHITE, (self.rect.x + 22, eye_y), 5)
        pygame.draw.circle(surface, BLACK, (self.rect.x + 8 + self.direction*2, eye_y), 2)
        pygame.draw.circle(surface, BLACK, (self.rect.x + 22 + self.direction*2, eye_y), 2)

def create_level():
    platforms = [
        pygame.Rect(0, HEIGHT - 50, WIDTH, 50),  # 地面
        pygame.Rect(150, 450, 150, 20),
        pygame.Rect(400, 350, 150, 20),
        pygame.Rect(600, 250, 150, 20),
        pygame.Rect(300, 200, 100, 20),
    ]
    enemies = [
        Enemy(200, 420, 150),
        Enemy(450, 320, 150),
        Enemy(650, 220, 150),
    ]
    coins = [
        pygame.Rect(200, 400, 15, 15),
        pygame.Rect(230, 400, 15, 15),
        pygame.Rect(450, 300, 15, 15),
        pygame.Rect(480, 300, 15, 15),
        pygame.Rect(650, 200, 15, 15),
        pygame.Rect(680, 200, 15, 15),
        pygame.Rect(320, 160, 15, 15),
        pygame.Rect(350, 160, 15, 15),
    ]
    return platforms, enemies, coins

def draw_coin(surface, coin):
    pygame.draw.ellipse(surface, COIN_YELLOW, coin)
    pygame.draw.ellipse(surface, (200, 160, 0), coin, 2)

def draw_background(surface):
    surface.fill(SKY_BLUE)
    # 画一些云
    for cx, cy in [(100, 100), (400, 80), (700, 120)]:
        pygame.draw.ellipse(surface, WHITE, (cx, cy, 80, 40))
        pygame.draw.ellipse(surface, WHITE, (cx+20, cy-10, 60, 40))
        pygame.draw.ellipse(surface, WHITE, (cx-10, cy+5, 60, 35))
    # 太阳
    pygame.draw.circle(surface, (255, 255, 100), (750, 50), 30)

def draw_ui(surface, player, font):
    score_text = font.render(f"Score: {player.score}", True, BLACK)
    surface.blit(score_text, (10, 10))
    if not player.alive:
        game_over_text = font.render("GAME OVER", True, (255, 0, 0))
        surface.blit(game_over_text, (WIDTH//2 - 100, HEIGHT//2 - 30))
        restart_text = font.render("Press R to Restart", True, BLACK)
        surface.blit(restart_text, (WIDTH//2 - 110, HEIGHT//2 + 20))

def main():
    font = pygame.font.SysFont("arial", 32)
    platforms, enemies, coins = create_level()
    player = Player(50, HEIGHT - 100)

    running = True
    while running:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_r and not player.alive:
                    platforms, enemies, coins = create_level()
                    player = Player(50, HEIGHT - 100)

        # 更新
        if player.alive:
            player.update(platforms, enemies, coins)
            for enemy in enemies[:]:
                enemy.update(platforms)

        # 绘制
        draw_background(screen)
        for p in platforms:
            pygame.draw.rect(screen, GROUND_BROWN, p)
            pygame.draw.rect(screen, (100, 50, 0), p, 3)
        for coin in coins:
            draw_coin(screen, coin)
        for enemy in enemies:
            enemy.draw(screen)
        player.draw(screen)
        draw_ui(screen, player, font)

        pygame.display.flip()

    pygame.quit()
    sys.exit()

if __name__ == "__main__":
    main()