Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| # 全局變數設定 | |
| groups = {} # 儲存每組學生和血量 | |
| team_health = {} # 儲存每組的雞排王當前血量 | |
| max_health = 500 # 預設雞排王的最大血量 | |
| # 初始化組別 | |
| def initialize_groups(group_info, health): | |
| global groups, team_health, max_health | |
| max_health = int(health) | |
| groups.clear() | |
| team_health.clear() | |
| group_data = group_info.split("\n") | |
| for idx, group in enumerate(group_data, start=1): | |
| students = group.split(",") # 每組學生用逗號分隔 | |
| group_name = f"小隊{idx}" | |
| groups[group_name] = students | |
| team_health[group_name] = max_health | |
| return f"已建立 {len(groups)} 組,每組血量為 {max_health}。" | |
| # 輸入攻擊值並扣血 | |
| def attack_team(team_name, damage): | |
| if team_name in team_health: | |
| team_health[team_name] = max(0, team_health[team_name] - int(damage)) | |
| return f"{team_name} 的血量剩餘 {team_health[team_name]}。" | |
| return "小隊名稱不存在。" | |
| # 顯示目前所有小隊的血量狀態 | |
| def display_teams(): | |
| display = "" | |
| for team, students in groups.items(): | |
| health = team_health.get(team, 0) | |
| bar_length = int((health / max_health) * 20) | |
| health_bar = "█" * bar_length + " " * (20 - bar_length) | |
| display += f"{team} ({', '.join(students)}): [{health_bar}] {health}/{max_health}\n" | |
| return display.strip() | |
| # Gradio 介面 | |
| with gr.Blocks() as app: | |
| gr.Markdown("## 雞排王小隊對戰系統") | |
| # 初始化組別與最大血量 | |
| with gr.Row(): | |
| group_input = gr.Textbox(label="輸入組別資訊(每組學生用逗號分隔,換行分組)", placeholder="範例:小明,小華\n小美,小強") | |
| health_input = gr.Number(label="設定雞排王最大血量", value=500) | |
| init_button = gr.Button("初始化組別") | |
| output_init = gr.Textbox(label="系統訊息") | |
| init_button.click(initialize_groups, inputs=[group_input, health_input], outputs=output_init) | |
| # 攻擊輸入區 | |
| with gr.Row(): | |
| team_input = gr.Textbox(label="輸入攻擊小隊名稱", placeholder="例如:小隊1") | |
| damage_input = gr.Number(label="輸入攻擊值", value=50) | |
| attack_button = gr.Button("攻擊") | |
| attack_output = gr.Textbox(label="攻擊結果") | |
| # 更新小隊血量顯示 | |
| display_button = gr.Button("顯示目前血量") | |
| teams_display = gr.Textbox(label="小隊血量狀態", lines=10) | |
| # 事件綁定 | |
| attack_button.click(attack_team, inputs=[team_input, damage_input], outputs=attack_output) | |
| display_button.click(display_teams, outputs=teams_display) | |
| # 啟動應用程式 | |
| app.launch() | |