import pygame
import random
import sys

# 初始化pygame
pygame.init()
WIDTH, HEIGHT = 900, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("沙威玛传奇")
clock = pygame.time.Clock()

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BROWN = (120, 70, 30)
ORANGE = (255, 140, 0)
RED = (220, 30, 30)
GREEN = (40, 180, 60)
GOLD = (255, 215, 0)

# 字体
font = pygame.font.SysFont("simhei", 28)
small_font = pygame.font.SysFont("simhei", 20)

# 游戏变量
money = 0
waiting_customers = []
cooking = False
cook_timer = 0
COOK_TIME = 120  # 制作所需帧数
customer_spawn_timer = 0

# 顾客类
class Customer:
    def __init__(self):
        self.x = WIDTH
        self.y = 220
        self.want_food = random.choice(["基础沙威玛", "豪华沙威玛"])
        self.price = 12 if self.want_food == "基础沙威玛" else 22
        self.patience = 350  # 耐心值
        self.speed = 1.2

    def update(self):
        # 顾客走到柜台
        if self.x > 620:
            self.x -= self.speed
        else:
            self.patience -= 1

    def draw(self):
        # 顾客小人
        pygame.draw.circle(screen, (60, 80, 160), (self.x, self.y), 32)
        text = small_font.render(self.want_food, True, WHITE)
        screen.blit(text, (self.x - 35, self.y - 50))
        # 耐心条
        bar_w = 60
        pygame.draw.rect(screen, RED, [self.x-30, self.y+40, bar_w, 8])
        pygame.draw.rect(screen, GREEN, [self.x-30, self.y+40, bar_w*(self.patience/350), 8])

# 游戏主循环
running = True
while running:
    dt = clock.tick(60)
    screen.fill((35,35,45))

    # 事件监听
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            # 按空格开始制作沙威玛
            if event.key == pygame.K_SPACE and not cooking and len(waiting_customers) > 0:
                cooking = True
                cook_timer = COOK_TIME

    # 生成顾客
    customer_spawn_timer += 1
    if customer_spawn_timer > 240 and len(waiting_customers) < 4:
        waiting_customers.append(Customer())
        customer_spawn_timer = 0

    # 更新顾客
    remove_list = []
    for cus in waiting_customers:
        cus.update()
        # 耐心耗尽直接离开
        if cus.patience <= 0:
            remove_list.append(cus)
    for r in remove_list:
        waiting_customers.remove(r)

    # 烹饪逻辑
    if cooking:
        cook_timer -= 1
        # 烹饪完成，交付第一位顾客
        if cook_timer <= 0:
            cooking = False
            if waiting_customers:
                guest = waiting_customers.pop(0)
                money += guest.price

    # ===== 绘制场景 =====
    # 柜台
    pygame.draw.rect(screen, BROWN, (580, 300, 300, 40))
    pygame.draw.rect(screen, (90,50,20), (580, 340, 300, 120))

    # 烤炉区域
    pygame.draw.rect(screen, ORANGE, (80, 320, 180, 140))
    oven_text = font.render("烤炉", True, BLACK)
    screen.blit(oven_text, (130, 370))

    # 绘制所有顾客
    for customer in waiting_customers:
        customer.draw()

    # 烹饪进度显示
    if cooking:
        progress = cook_timer / COOK_TIME
        pygame.draw.rect(screen, BLACK, (100, 480, 140, 22))
        pygame.draw.rect(screen, GOLD, (100, 480, 140*(1-progress), 22))
        cook_text = small_font.render("正在制作沙威玛...", True, WHITE)
        screen.blit(cook_text, (95, 510))

    # UI信息
    money_text = font.render(f"💰 金币：{money}", True, GOLD)
    screen.blit(money_text, (20, 20))
    hint_text = small_font.render("【空格】制作沙威玛", True, WHITE)
    screen.blit(hint_text, (20, 60))

    pygame.display.flip()

pygame.quit()
sys.exit()
