import pygame
import sys
import random

pygame.init()
WIDTH, HEIGHT = 960, 640
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("寻找伪装外星人｜狙击闯关")
clock = pygame.time.Clock()

# 颜色
SKY = (110, 175, 240)
GRASS = (52, 145, 45)
WHITE = (255,255,255)
GRAY = (100,100,100)
DARK_GREEN = (20,80,15)
WOOD = (120,85,50)
BLACK = (0,0,0)
RED = (220,20,20)
YELLOW = (255,230,0)

# 字体兼容
try:
    font = pygame.font.SysFont("simhei", 26)
    font_big = pygame.font.SysFont("simhei", 52)
except:
    font = pygame.font.Font(None, 26)
    font_big = pygame.font.Font(None, 52)

# 伪装类型
CLOUD = 0
ROCK = 1
BUSH = 2
BOX = 3
LAMP = 4

# 外星人（目标）
class Alien:
    def __init__(self):
        self.x = random.randint(70, WIDTH-70)
        self.y = random.randint(110, HEIGHT-130)
        self.r = 34
        self.type = random.choice([CLOUD,ROCK,BUSH,BOX,LAMP])
        self.found = False

    def draw(self):
        if self.type == CLOUD:
            pygame.draw.circle(screen,WHITE,(self.x,self.y),self.r)
            pygame.draw.circle(screen,WHITE,(self.x-26,self.y+6),self.r-11)
            pygame.draw.circle(screen,WHITE,(self.x+26,self.y+6),self.r-11)
        elif self.type == ROCK:
            pygame.draw.ellipse(screen,GRAY,[self.x-36,self.y-24,72,48])
        elif self.type == BUSH:
            pygame.draw.circle(screen,DARK_GREEN,(self.x,self.y),self.r)
        elif self.type == BOX:
            pygame.draw.rect(screen,WOOD,[self.x-32,self.y-28,64,56])
        elif self.type == LAMP:
            pygame.draw.rect(screen,GRAY,[self.x-4,self.y-self.r,8,self.r])
            pygame.draw.circle(screen,YELLOW,(self.x,self.y-self.r),12)

        # 击中之后露出外星人眼睛
        if self.found:
            pygame.draw.circle(screen,WHITE,(self.x-12,self.y-8),8)
            pygame.draw.circle(screen,WHITE,(self.x+12,self.y-8),8)
            pygame.draw.circle(screen,BLACK,(self.x-12,self.y-8),3.5)
            pygame.draw.circle(screen,BLACK,(self.x+12,self.y-8),3.5)

    def check_hit(self,mx,my):
        dist = ((mx-self.x)**2 + (my-self.y)**2)**0.5
        return dist < self.r

# 假装饰物（干扰，不是外星人）
class FakeDecor:
    def __init__(self):
        self.x = random.randint(30,WIDTH-30)
        self.y = random.randint(80,HEIGHT-100)
        self.r = random.randint(24,36)
        self.type = random.choice([CLOUD,ROCK,BUSH,BOX,LAMP])

    def draw(self):
        if self.type == CLOUD:
            pygame.draw.circle(screen,WHITE,(self.x,self.y),self.r)
            pygame.draw.circle(screen,WHITE,(self.x-22,self.y+5),self.r-9)
            pygame.draw.circle(screen,WHITE,(self.x+22,self.y+5),self.r-9)
        elif self.type == ROCK:
            pygame.draw.ellipse(screen,GRAY,[self.x-32,self.y-20,64,42])
        elif self.type == BUSH:
            pygame.draw.circle(screen,DARK_GREEN,(self.x,self.y),self.r)
        elif self.type == BOX:
            pygame.draw.rect(screen,WOOD,[self.x-28,self.y-24,56,48])
        elif self.type == LAMP:
            pygame.draw.rect(screen,GRAY,[self.x-3,self.y-self.r,6,self.r])
            pygame.draw.circle(screen,YELLOW,(self.x,self.y-self.r),10)

# 游戏变量
TARGET_COUNT = 10  #每关10只外星人
level = 1
score = 0
aliens = []
fakes = []

def create_level():
    global aliens,fakes
    aliens.clear()
    fakes.clear()
    #生成目标外星人
    for _ in range(TARGET_COUNT):
        aliens.append(Alien())
    #大量虚假干扰物体
    for _ in range(22):
        fakes.append(FakeDecor())

create_level()

running = True
while running:
    clock.tick(60)
    screen.fill(SKY)
    #地面草地
    pygame.draw.rect(screen,GRASS,[0,HEIGHT-90,WIDTH,90])
    mx,my = pygame.mouse.get_pos()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        #点击射击
        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            hit_success = False
            for alien in aliens:
                if not alien.found and alien.check_hit(mx,my):
                    alien.found = True
                    score += 20
                    hit_success = True
                    break
        #空格通关下一关
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                all_find = all(a.found for a in aliens)
                if all_find:
                    level += 1
                    create_level()

    #先绘制假装饰
    for obj in fakes:
        obj.draw()
    #绘制外星人伪装物体
    for alien in aliens:
        alien.draw()

    #准星
    cross = 24
    pygame.draw.line(screen,RED,(mx-cross,my),(mx+cross,my),2)
    pygame.draw.line(screen,RED,(mx,my-cross),(mx,my+cross),2)
    pygame.draw.circle(screen,RED,(mx,my),cross,2)

    found = sum(1 for a in aliens if a.found)
    all_clear = all(a.found for a in aliens)

    #UI
    info = font.render(f"第{level}关 | 已找到：{found}/{TARGET_COUNT} | 得分：{score}",True,BLACK)
    screen.blit(info,(15,12))

    if all_clear:
        text1 = font_big.render("✅ 本关全部清除！",True,RED)
        text2 = font.render("按下空格键进入下一关",True,BLACK)
        screen.blit(text1,(WIDTH//2 - 260, HEIGHT//2 - 60))
        screen.blit(text2,(WIDTH//2 - 220, HEIGHT//2 + 10))

    pygame.display.flip()

pygame.quit()
sys.exit()