import pygame
import time

# 初始化pygame
pygame.init()
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("农家乐 - 种菜小游戏")
clock = pygame.time.Clock()
FPS = 60

# 颜色常量
WHITE = (255, 255, 255)
BROWN = (101, 67, 33)
GREEN = (34, 139, 34)
LIGHT_GREEN = (144, 238, 144)
BLUE = (30,144,255)
YELLOW = (255,215,0)
RED = (220, 50, 50)
GRAY = (120,120,120)

# 修复字体报错，使用安全参数
try:
    font = pygame.font.SysFont("simhei", 28)
    big_font = pygame.font.SysFont("simhei",40)
except:
    font = pygame.font.Font(None, 28)
    big_font = pygame.font.Font(None,40)

# 游戏参数
GRID_SIZE = 4
CELL_W = 150
CELL_H = 120
offset_x = 40
offset_y = 80

# 种子配置 名字，成熟秒数，售价，播种成本
seed_info = {
    0:{"name":"白菜","grow_time":8,"sell":15,"cost":5,"color":LIGHT_GREEN},
    1:{"name":"番茄","grow_time":15,"sell":35,"cost":12,"color":RED},
    2:{"name":"小麦","grow_time":6,"sell":10,"cost":3,"color":YELLOW}
}
seed_name_list = ["白菜","番茄","小麦"]

# 地块状态:0=空地 1=已播种 2=缺水 3=成熟可收割
class LandCell:
    def __init__(self):
        self.state = 0
        self.seed_type = -1
        self.plant_start = 0
        self.watered = True

land = [[LandCell() for _ in range(GRID_SIZE)] for _ in range(GRID_SIZE)]
money = 50
current_seed = 0

def draw_ui():
    global money,current_seed
    # 顶部状态栏
    text1 = big_font.render(f"金币: {money}", True, YELLOW)
    screen.blit(text1,(20,10))
    text2 = font.render(f"当前种子:{seed_info[current_seed]['name']} 播种花费{seed_info[current_seed]['cost']}", True, WHITE)
    screen.blit(text2,(220,15))
    hint = font.render("按键1/2/3切换种子 | 鼠标左键:播种/浇水/收割", True, WHITE)
    screen.blit(hint,(20,520))

def draw_lands():
    now = time.time()
    for y in range(GRID_SIZE):
        for x in range(GRID_SIZE):
            cx = offset_x + x*CELL_W
            cy = offset_y + y*CELL_H
            cell = land[y][x]
            # 空地泥土
            pygame.draw.rect(screen,BROWN,(cx,cy,CELL_W-5,CELL_H-5))
            pygame.draw.rect(screen,GRAY,(cx,cy,CELL_W-5,CELL_H-5),2)

            if cell.state == 0:
                # 空地
                t = font.render("空地", True, WHITE)
                screen.blit(t,(cx+40,cy+45))

            elif cell.state ==1:
                # 已经播种，判断缺水
                plant = seed_info[cell.seed_type]
                pass_time = now - cell.plant_start
                if pass_time>3 and not cell.watered:
                    cell.state = 2
                else:
                    # 判断成熟
                    if pass_time >= plant["grow_time"]:
                        cell.state = 3

                pygame.draw.circle(screen,plant["color"],(cx+70,cy+60),25)
                t = font.render("幼苗", True, WHITE)
                screen.blit(t,(cx+45,cy+90))

            elif cell.state ==2:
                plant = seed_info[cell.seed_type]
                pygame.draw.circle(screen,plant["color"],(cx+70,cy+60),25)
                tw = font.render("缺水!", True, BLUE)
                screen.blit(tw,(cx+45,cy+90))

            elif cell.state ==3:
                plant = seed_info[cell.seed_type]
                pygame.draw.circle(screen,plant["color"],(cx+70,cy+60),45)
                tr = font.render("可收割", True, YELLOW)
                screen.blit(tr,(cx+35,cy+90))


def mouse_click(pos):
    global money,current_seed
    mx,my = pos
    for y in range(GRID_SIZE):
        for x in range(GRID_SIZE):
            cx = offset_x + x*CELL_W
            cy = offset_y + y*CELL_H
            if cx <= mx <= cx+CELL_W and cy <= my <= cy+CELL_H:
                cell = land[y][x]
                if cell.state ==0:
                    # 播种
                    cost = seed_info[current_seed]["cost"]
                    if money >= cost:
                        money -= cost
                        cell.state = 1
                        cell.seed_type = current_seed
                        cell.plant_start = time.time()
                        cell.watered = True
                elif cell.state == 2:
                    #浇水
                    cell.watered = True
                    cell.state = 1
                elif cell.state ==3:
                    #收割卖钱
                    earn = seed_info[cell.seed_type]["sell"]
                    money += earn
                    #重置地块
                    cell.__init__()

#主循环
running = True
while running:
    screen.fill((20,70,20))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                mouse_click(pygame.mouse.get_pos())
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_1:
                current_seed =0
            elif event.key == pygame.K_2:
                current_seed =1
            elif event.key == pygame.K_3:
                current_seed =2

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

pygame.quit()
