import tkinter as tk
from tkinter import ttk, messagebox
import random
import os
from PIL import Image, ImageTk

class SurvivalGame:
    def __init__(self, root):
        self.root = root
        self.root.title("荒岛求生")
        self.root.geometry("600x720")
        self.root.resizable(False, False)

        # 基础状态
        self.day = 1
        self.health = 100
        self.hunger = 100
        self.thirst = 100
        self.actions_left = 5

        # 资源
        self.wood = 0
        self.food = 0
        self.water = 0
        self.herbs = 0
        self.trap_count = 0
        self.crop_growth = 0
        self.tool_level = 0
        self.weapon = False
        self.clothing = False
        self.shelter = False
        self.fire = False

        # 加载所有图标
        self.icons = self.load_icons()

        self.create_widgets()
        self.update_display()
        self.log("你醒来发现自己身处一座荒岛，想办法活下去吧！")

    def load_icons(self):
        """从 icons 文件夹加载 PNG 图标，返回 {key: PhotoImage 或 None}"""
        icons = {}
        icon_map = {
            'eat': 'eat', 'drink': 'drink', 'rest': 'rest',
            'use_herb': 'use_herb', 'weather': 'weather', 'sos': 'sos',
            'gather_wood': 'gather_wood', 'fish': 'fish', 'collect_water': 'collect_water',
            'gather_herbs': 'gather_herbs', 'hunt': 'hunt',
            'build_shelter': 'build_shelter', 'make_fire': 'make_fire',
            'set_trap': 'set_trap', 'plant_crop': 'plant_crop',
            'craft_tool': 'craft_tool', 'craft_weapon': 'craft_weapon',
            'weave_clothing': 'weave_clothing',
            'explore': 'explore', 'explore_cave': 'explore_cave', 'swim': 'swim'
        }
        for key, filename in icon_map.items():
            path = os.path.join('icons', f'{filename}.png')
            if os.path.exists(path):
                try:
                    img = Image.open(path).resize((28, 28), Image.LANCZOS)
                    icons[key] = ImageTk.PhotoImage(img)
                except Exception:
                    icons[key] = None
            else:
                icons[key] = None
        return icons

    def create_icon_button(self, parent, text, icon_key, command, width=16, **kwargs):
        """创建带图标的按钮，如果没有图标则使用纯文字"""
        icon = self.icons.get(icon_key)
        if icon:
            btn = tk.Button(parent, text=text, image=icon, compound='left',
                            width=width, command=command, **kwargs)
            btn.image = icon  # 保持引用
        else:
            btn = tk.Button(parent, text=text, width=width, command=command, **kwargs)
        return btn

    def create_widgets(self):
        main_frame = tk.Frame(self.root)
        main_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)

        # ========== 左侧可滚动状态面板 ==========
        status_container = tk.LabelFrame(main_frame, text="生存状态", width=220)
        status_container.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=(0,5))
        status_container.pack_propagate(False)

        canvas = tk.Canvas(status_container, highlightthickness=0)
        scrollbar = tk.Scrollbar(status_container, orient="vertical", command=canvas.yview)
        self.status_frame = tk.Frame(canvas)

        self.status_frame.bind(
            "<Configure>",
            lambda e: canvas.configure(scrollregion=canvas.bbox("all"))
        )

        canvas.create_window((0, 0), window=self.status_frame, anchor="nw")
        canvas.configure(yscrollcommand=scrollbar.set)
        canvas.pack(side="left", fill="both", expand=True)
        scrollbar.pack(side="right", fill="y")

        # 鼠标滚轮
        def on_mousewheel(event):
            canvas.yview_scroll(int(-1*(event.delta/120)), "units")
        canvas.bind_all("<MouseWheel>", on_mousewheel)
        canvas.bind_all("<Button-4>", lambda e: canvas.yview_scroll(-1, "units"))
        canvas.bind_all("<Button-5>", lambda e: canvas.yview_scroll(1, "units"))

        # 状态标签
        self.lbl_day = tk.Label(self.status_frame, text="", font=("微软雅黑", 11))
        self.lbl_day.pack(anchor="w", padx=10, pady=2)
        self.lbl_health = tk.Label(self.status_frame, text="", font=("微软雅黑", 11), fg="red")
        self.lbl_health.pack(anchor="w", padx=10, pady=2)
        self.lbl_hunger = tk.Label(self.status_frame, text="", font=("微软雅黑", 11))
        self.lbl_hunger.pack(anchor="w", padx=10, pady=2)
        self.lbl_thirst = tk.Label(self.status_frame, text="", font=("微软雅黑", 11))
        self.lbl_thirst.pack(anchor="w", padx=10, pady=2)

        tk.Label(self.status_frame, text="───资源───").pack(anchor="w", padx=10, pady=(5,0))
        self.lbl_wood = tk.Label(self.status_frame, text="", font=("微软雅黑", 11))
        self.lbl_wood.pack(anchor="w", padx=10, pady=2)
        self.lbl_food = tk.Label(self.status_frame, text="", font=("微软雅黑", 11))
        self.lbl_food.pack(anchor="w", padx=10, pady=2)
        self.lbl_water = tk.Label(self.status_frame, text="", font=("微软雅黑", 11))
        self.lbl_water.pack(anchor="w", padx=10, pady=2)
        self.lbl_herbs = tk.Label(self.status_frame, text="", font=("微软雅黑", 11))
        self.lbl_herbs.pack(anchor="w", padx=10, pady=2)

        tk.Label(self.status_frame, text="───设施───").pack(anchor="w", padx=10, pady=(5,0))
        self.lbl_trap = tk.Label(self.status_frame, text="", font=("微软雅黑", 11))
        self.lbl_trap.pack(anchor="w", padx=10, pady=2)
        self.lbl_crop = tk.Label(self.status_frame, text="", font=("微软雅黑", 11))
        self.lbl_crop.pack(anchor="w", padx=10, pady=2)
        self.lbl_tool = tk.Label(self.status_frame, text="", font=("微软雅黑", 11))
        self.lbl_tool.pack(anchor="w", padx=10, pady=2)
        self.lbl_weapon = tk.Label(self.status_frame, text="", font=("微软雅黑", 11))
        self.lbl_weapon.pack(anchor="w", padx=10, pady=2)
        self.lbl_clothing = tk.Label(self.status_frame, text="", font=("微软雅黑", 11))
        self.lbl_clothing.pack(anchor="w", padx=10, pady=2)

        self.lbl_actions = tk.Label(self.status_frame, text="", font=("微软雅黑", 11, "bold"), fg="blue")
        self.lbl_actions.pack(anchor="w", padx=10, pady=10)

        # ========== 右侧行动面板（Notebook） ==========
        action_container = tk.Frame(main_frame)
        action_container.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)
        self.notebook = ttk.Notebook(action_container)
        self.notebook.pack(fill=tk.BOTH, expand=True)

        self.tab_survive = ttk.Frame(self.notebook)
        self.tab_gather = ttk.Frame(self.notebook)
        self.tab_build = ttk.Frame(self.notebook)
        self.tab_adventure = ttk.Frame(self.notebook)

        self.notebook.add(self.tab_survive, text="生存")
        self.notebook.add(self.tab_gather, text="采集")
        self.notebook.add(self.tab_build, text="建造")
        self.notebook.add(self.tab_adventure, text="探索")

        btn_w = 16

        # 生存页
        self.create_icon_button(self.tab_survive, "  吃食物", 'eat', self.eat, btn_w).grid(row=0, column=0, padx=10, pady=6)
        self.create_icon_button(self.tab_survive, "  喝水", 'drink', self.drink, btn_w).grid(row=0, column=1, padx=10, pady=6)
        self.create_icon_button(self.tab_survive, "  休息", 'rest', self.rest, btn_w).grid(row=1, column=0, padx=10, pady=6)
        self.create_icon_button(self.tab_survive, "  使用草药", 'use_herb', self.use_herb, btn_w).grid(row=1, column=1, padx=10, pady=6)
        self.create_icon_button(self.tab_survive, "  观察天气", 'weather', self.observe_weather, btn_w).grid(row=2, column=0, padx=10, pady=6)
        self.create_icon_button(self.tab_survive, "  求救信号", 'sos', self.sos, btn_w).grid(row=2, column=1, padx=10, pady=6)

        # 采集页
        self.create_icon_button(self.tab_gather, "  收集木材", 'gather_wood', self.gather_wood, btn_w).grid(row=0, column=0, padx=10, pady=6)
        self.create_icon_button(self.tab_gather, "  捕鱼", 'fish', self.fish, btn_w).grid(row=0, column=1, padx=10, pady=6)
        self.create_icon_button(self.tab_gather, "  收集淡水", 'collect_water', self.collect_water, btn_w).grid(row=1, column=0, padx=10, pady=6)
        self.create_icon_button(self.tab_gather, "  采集草药", 'gather_herbs', self.gather_herbs, btn_w).grid(row=1, column=1, padx=10, pady=6)
        self.create_icon_button(self.tab_gather, "  狩猎", 'hunt', self.hunt, btn_w).grid(row=2, column=0, padx=10, pady=6)

        # 建造页
        self.create_icon_button(self.tab_build, "  庇护所(5木)", 'build_shelter', self.build_shelter, btn_w).grid(row=0, column=0, padx=10, pady=6)
        self.create_icon_button(self.tab_build, "  生火(2木)", 'make_fire', self.make_fire, btn_w).grid(row=0, column=1, padx=10, pady=6)
        self.create_icon_button(self.tab_build, "  陷阱(5木)", 'set_trap', self.set_trap, btn_w).grid(row=1, column=0, padx=10, pady=6)
        self.create_icon_button(self.tab_build, "  种植(2食)", 'plant_crop', self.plant_crop, btn_w).grid(row=1, column=1, padx=10, pady=6)
        self.create_icon_button(self.tab_build, "  工具(3木)", 'craft_tool', self.craft_tool, btn_w).grid(row=2, column=0, padx=10, pady=6)
        self.create_icon_button(self.tab_build, "  武器(3木)", 'craft_weapon', self.craft_weapon, btn_w).grid(row=2, column=1, padx=10, pady=6)
        self.create_icon_button(self.tab_build, "  衣物(5木)", 'weave_clothing', self.weave_clothing, btn_w).grid(row=3, column=0, padx=10, pady=6)

        # 探索页
        self.create_icon_button(self.tab_adventure, "  探索岛屿", 'explore', self.explore, btn_w).grid(row=0, column=0, padx=10, pady=6)
        self.create_icon_button(self.tab_adventure, "  探索洞穴", 'explore_cave', self.explore_cave, btn_w).grid(row=0, column=1, padx=10, pady=6)
        self.create_icon_button(self.tab_adventure, "  游泳", 'swim', self.swim, btn_w).grid(row=1, column=0, padx=10, pady=6)
        tk.Button(self.tab_adventure, text="重新开始", width=btn_w, command=self.new_game, bg="#ffccaa").grid(row=1, column=1, padx=10, pady=6)

        # 底部日志
        log_frame = tk.LabelFrame(self.root, text="事件记录", height=140)
        log_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=(0,10))
        self.log_text = tk.Text(log_frame, height=6, wrap=tk.WORD, state=tk.DISABLED)
        self.log_text.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)

    # ================== 游戏逻辑（保持不变） ==================
    def update_display(self):
        self.lbl_day.config(text=f"天数：{self.day}")
        self.lbl_health.config(text=f"生命：{self.health}")
        self.lbl_hunger.config(text=f"饥饿：{self.hunger}")
        self.lbl_thirst.config(text=f"口渴：{self.thirst}")
        self.lbl_wood.config(text=f"木材：{self.wood}")
        self.lbl_food.config(text=f"食物：{self.food}")
        self.lbl_water.config(text=f"淡水：{self.water}")
        self.lbl_herbs.config(text=f"草药：{self.herbs}")
        self.lbl_trap.config(text=f"陷阱：{self.trap_count}个")
        self.lbl_crop.config(text=f"作物生长：{self.crop_growth}/10")
        self.lbl_tool.config(text=f"工具等级：{self.tool_level}")
        self.lbl_weapon.config(text=f"武器：{'有' if self.weapon else '无'}")
        self.lbl_clothing.config(text=f"衣物：{'有' if self.clothing else '无'}")
        self.lbl_actions.config(text=f"剩余行动：{self.actions_left}")

        if self.hunger <= 30: self.lbl_hunger.config(fg="orange")
        else: self.lbl_hunger.config(fg="black")
        if self.thirst <= 30: self.lbl_thirst.config(fg="orange")
        else: self.lbl_thirst.config(fg="black")
        if self.health <= 30: self.lbl_health.config(fg="red")
        else: self.lbl_health.config(fg="black")

    def log(self, msg):
        self.log_text.config(state=tk.NORMAL)
        self.log_text.insert(tk.END, f"● {msg}\n")
        self.log_text.see(tk.END)
        self.log_text.config(state=tk.DISABLED)

    def clamp_stats(self):
        self.health = max(0, min(100, self.health))
        self.hunger = max(0, min(100, self.hunger))
        self.thirst = max(0, min(100, self.thirst))

    def consume_vitals(self, h, t):
        self.hunger -= h
        self.thirst -= t
        self.clamp_stats()
        self.update_display()

    def consume_action(self):
        self.actions_left -= 1
        self.update_display()
        if self.actions_left <= 0:
            self.night_phase()

    # 所有行动方法（与之前相同）
    def eat(self):
        if self.actions_left <= 0: return
        if self.food <= 0: self.log("没有食物可以吃。"); return
        self.food -= 1
        self.hunger += 25
        self.consume_vitals(0, 0)
        self.log("你吃了一些食物，饥饿值+25。")
        self.consume_action()

    def drink(self):
        if self.actions_left <= 0: return
        if self.water <= 0: self.log("没有淡水可以喝。"); return
        self.water -= 1
        self.thirst += 25
        self.consume_vitals(0, 0)
        self.log("你喝了一些淡水，口渴值+25。")
        self.consume_action()

    def rest(self):
        if self.actions_left <= 0: return
        self.consume_vitals(3, 3)
        heal = random.randint(10, 20)
        self.health += heal
        self.log(f"你躺下休息，生命+{heal}。")
        self.consume_action()

    def use_herb(self):
        if self.actions_left <= 0: return
        if self.herbs <= 0: self.log("没有草药可用。"); return
        self.herbs -= 1
        heal = random.randint(15, 30)
        self.health += heal
        self.consume_vitals(0, 0)
        self.log(f"你敷上草药，生命+{heal}。")
        self.consume_action()

    def observe_weather(self):
        if self.actions_left <= 0: return
        r = random.random()
        if r < 0.3: self.log("你观察天象，今晚可能有野兽出没。")
        elif r < 0.5: self.log("云层很厚，暴风雨可能要来了。")
        else: self.log("今晚看起来会是个平静的夜晚。")
        self.consume_vitals(1, 1)
        self.consume_action()

    def sos(self):
        if self.actions_left <= 0: return
        if self.wood < 10: self.log("需要10木材才能搭建求救信号堆。"); return
        self.wood -= 10
        self.consume_vitals(5, 5)
        if random.random() < 0.1:
            self.log("🎉 救援队发现了你！你成功获救！")
            self.game_over(win=True)
        else:
            self.log("你燃起浓烟，但似乎没有人看见……")
            self.consume_action()

    def gather_wood(self):
        if self.actions_left <= 0: return
        amount = 3 + self.tool_level
        self.wood += amount
        self.consume_vitals(4, 3)
        self.log(f"获得{amount}木材。")
        self.consume_action()

    def fish(self):
        if self.actions_left <= 0: return
        self.consume_vitals(5, 4)
        success = random.random() < (0.6 + 0.1*self.tool_level)
        if success:
            amount = random.randint(3, 6)
            self.food += amount
            self.log(f"捕到了鱼，食物+{amount}。")
        else:
            self.log("今天一无所获。")
        self.consume_action()

    def collect_water(self):
        if self.actions_left <= 0: return
        amount = random.randint(3, 6) + self.tool_level
        self.water += amount
        self.consume_vitals(4, 3)
        self.log(f"收集到{amount}淡水。")
        self.consume_action()

    def gather_herbs(self):
        if self.actions_left <= 0: return
        amount = random.randint(1, 3)
        self.herbs += amount
        self.consume_vitals(3, 3)
        self.log(f"采集到{amount}草药。")
        self.consume_action()

    def hunt(self):
        if self.actions_left <= 0: return
        self.consume_vitals(6, 5)
        chance = 0.4 + 0.15*self.tool_level + (0.2 if self.weapon else 0)
        if random.random() < chance:
            food = random.randint(5, 10)
            self.food += food
            self.log(f"狩猎成功！获得{food}食物。")
        else:
            damage = random.randint(5, 15)
            self.health -= damage
            self.log(f"狩猎失败，你受了点伤，生命-{damage}。")
        self.consume_action()

    def build_shelter(self):
        if self.actions_left <= 0: return
        if self.shelter: self.log("你已经拥有庇护所。"); return
        if self.wood < 5: self.log("木材不足5。"); return
        self.wood -= 5
        self.consume_vitals(5, 5)
        self.shelter = True
        self.log("庇护所搭建完成。")
        self.consume_action()

    def make_fire(self):
        if self.actions_left <= 0: return
        if self.fire: self.log("今晚的篝火已经准备好了。"); return
        if self.wood < 2: self.log("木材不足2。"); return
        self.wood -= 2
        self.consume_vitals(2, 2)
        self.fire = True
        self.log("篝火燃起，今晚将更安全。")
        self.consume_action()

    def set_trap(self):
        if self.actions_left <= 0: return
        if self.wood < 5: self.log("需要5木材制作陷阱。"); return
        self.wood -= 5
        self.trap_count += 1
        self.consume_vitals(4, 4)
        self.log(f"设置了一个陷阱，目前共有{self.trap_count}个。")
        self.consume_action()

    def plant_crop(self):
        if self.actions_left <= 0: return
        if self.food < 2: self.log("需要2食物作为种子。"); return
        self.food -= 2
        self.crop_growth += 2
        self.consume_vitals(5, 5)
        self.log(f"你播种了一些作物，生长进度+2 (当前{self.crop_growth}/10)。")
        self.consume_action()

    def craft_tool(self):
        if self.actions_left <= 0: return
        if self.tool_level >= 2: self.log("工具已经最高级。"); return
        if self.wood < 3: self.log("需要3木材制作工具。"); return
        self.wood -= 3
        self.tool_level += 1
        self.consume_vitals(4, 3)
        self.log(f"工具升级至{self.tool_level}级，采集效率提升。")
        self.consume_action()

    def craft_weapon(self):
        if self.actions_left <= 0: return
        if self.weapon: self.log("你已经拥有武器。"); return
        if self.wood < 3: self.log("需要3木材制造武器。"); return
        self.wood -= 3
        self.weapon = True
        self.consume_vitals(4, 3)
        self.log("你制作了一把简易武器。")
        self.consume_action()

    def weave_clothing(self):
        if self.actions_left <= 0: return
        if self.clothing: self.log("你已经拥有衣物。"); return
        if self.wood < 5: self.log("需要5木材编织衣物。"); return
        self.wood -= 5
        self.clothing = True
        self.consume_vitals(5, 4)
        self.log("你用树皮和藤蔓编织了简陋衣物。")
        self.consume_action()

    def explore(self):
        if self.actions_left <= 0: return
        self.consume_vitals(5, 5)
        r = random.random()
        if r < 0.25:
            f = random.randint(2,5); self.food += f; self.log(f"发现野果，食物+{f}")
        elif r < 0.45:
            w = random.randint(2,5); self.water += w; self.log(f"找到水源，淡水+{w}")
        elif r < 0.65:
            wd = random.randint(2,4); self.wood += wd; self.log(f"捡到木材，木材+{wd}")
        elif r < 0.80:
            self.health -= 10; self.log("你不小心摔伤，生命-10")
        else:
            self.food += 2; self.water += 2; self.log("运气不错，同时找到食物和水")
        self.consume_action()

    def explore_cave(self):
        if self.actions_left <= 0: return
        self.consume_vitals(6, 6)
        r = random.random()
        if r < 0.3:
            h = random.randint(2,4); self.herbs += h; self.log(f"洞穴里发现珍贵草药，草药+{h}")
        elif r < 0.55:
            if self.tool_level < 2:
                self.tool_level += 1; self.log("你在洞穴中找到高级工具，工具等级提升！")
            else:
                self.wood += 5; self.log("找到一些储存的木材，木材+5")
        elif r < 0.8:
            self.health -= 15; self.log("洞穴塌方，你受了重伤！")
        else:
            self.food += 5; self.water += 5; self.log("洞穴深处有丰富资源！")
        self.consume_action()

    def swim(self):
        if self.actions_left <= 0: return
        self.consume_vitals(8, 6)
        if random.random() < 0.5:
            r = random.random()
            if r < 0.4:
                self.food += random.randint(2,5); self.log("你捞到一些贝类。")
            elif r < 0.7:
                self.wood += random.randint(2,4); self.log("你捡到漂浮的木材。")
            else:
                self.water += random.randint(2,4); self.log("你发现了漂浮的密封水壶。")
        else:
            self.health -= 10; self.log("海浪太大，你有些疲惫，生命-10。")
        self.consume_action()

    def night_phase(self):
        self.log("🌙 夜幕降临……")
        self.hunger -= 10
        self.thirst -= 10

        if self.hunger <= 0:
            self.hunger = 0
            self.health -= 15
            self.log("你极度饥饿，生命流失！")
        if self.thirst <= 0:
            self.thirst = 0
            self.health -= 15
            self.log("你极度口渴，生命流失！")

        if self.crop_growth > 0 and self.crop_growth < 10:
            self.crop_growth += 1
        if self.crop_growth >= 10:
            self.food += random.randint(8, 15)
            self.log("🌾 作物成熟了！你收获了大量食物。")
            self.crop_growth = 0

        if self.trap_count > 0:
            trapped_food = 0
            for _ in range(self.trap_count):
                if random.random() < 0.4:
                    trapped_food += random.randint(1, 3)
            if trapped_food > 0:
                self.food += trapped_food
                self.log(f"🪤 陷阱捕获了{trapped_food}食物。")

        event = random.random()
        if event < 0.3:
            if self.fire: self.log("🔥 篝火吓退了野兽。")
            else:
                base_dmg = 15
                if self.weapon: base_dmg = 5
                if self.shelter: base_dmg -= 3
                self.health -= base_dmg
                self.log(f"🐗 野兽袭击！(减伤后损失{base_dmg}生命)")
        elif event < 0.55:
            if not self.shelter:
                loss = min(self.wood, random.randint(1,3))
                self.wood -= loss
                self.log(f"🌧️ 暴风雨毁坏了{loss}木材。")
            else: self.log("🏠 庇护所抵御了暴风雨。")
        elif event < 0.65:
            if self.clothing:
                self.log("🧥 衣物让你在寒冷中保持温暖。")
            else:
                self.health -= 5
                self.log("❄️ 夜晚寒冷，生命-5。")

        if self.health <= 0:
            self.health = 0
            self.update_display()
            self.game_over()
            return

        self.clamp_stats()
        self.day += 1
        self.actions_left = 5
        self.fire = False
        self.update_display()
        self.log(f"☀️ 第{self.day}天清晨到来。")

    def game_over(self, win=False):
        if not win:
            self.log("💀 你未能活下来……")
            messagebox.showinfo("游戏结束", f"你存活了 {self.day} 天。")
        else:
            messagebox.showinfo("获救！", f"你在第{self.day}天获救，干得漂亮！")
        for tab in [self.tab_survive, self.tab_gather, self.tab_build, self.tab_adventure]:
            for widget in tab.winfo_children():
                if isinstance(widget, tk.Button) and widget.cget("text") != "重新开始":
                    widget.config(state=tk.DISABLED)

    def new_game(self):
        self.day = 1
        self.health = 100
        self.hunger = 100
        self.thirst = 100
        self.actions_left = 5
        self.wood = self.food = self.water = self.herbs = 0
        self.trap_count = 0
        self.crop_growth = 0
        self.tool_level = 0
        self.weapon = self.clothing = self.shelter = self.fire = False

        self.log_text.config(state=tk.NORMAL)
        self.log_text.delete(1.0, tk.END)
        self.log_text.config(state=tk.DISABLED)

        for tab in [self.tab_survive, self.tab_gather, self.tab_build, self.tab_adventure]:
            for widget in tab.winfo_children():
                if isinstance(widget, tk.Button):
                    widget.config(state=tk.NORMAL)

        self.update_display()
        self.log("重新开始荒岛求生……祝你好运！")

if __name__ == "__main__":
    root = tk.Tk()
    game = SurvivalGame(root)
    root.mainloop()