Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ import gradio as gr
|
|
| 4 |
groups = {} # 儲存每組學生和血量
|
| 5 |
team_health = {} # 儲存每組的雞排王當前血量
|
| 6 |
max_health = 500 # 預設雞排王的最大血量
|
| 7 |
-
selected_team = "" # 暫存選中的小隊
|
| 8 |
|
| 9 |
# 初始化組別
|
| 10 |
def initialize_groups(group_info, health):
|
|
@@ -22,19 +21,12 @@ def initialize_groups(group_info, health):
|
|
| 22 |
|
| 23 |
return f"已建立 {len(groups)} 組,每組血量為 {max_health}。"
|
| 24 |
|
| 25 |
-
# 選擇攻擊小隊
|
| 26 |
-
def select_team(team_name):
|
| 27 |
-
global selected_team
|
| 28 |
-
selected_team = team_name
|
| 29 |
-
return f"已選擇攻擊小隊:{team_name}"
|
| 30 |
-
|
| 31 |
# 輸入攻擊值並扣血
|
| 32 |
-
def
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
return "尚未選擇有效小隊或小隊不存在。"
|
| 38 |
|
| 39 |
# 顯示目前所有小隊的血量狀態
|
| 40 |
def display_teams():
|
|
@@ -61,26 +53,19 @@ with gr.Blocks() as app:
|
|
| 61 |
|
| 62 |
# 攻擊輸入區
|
| 63 |
with gr.Row():
|
|
|
|
| 64 |
damage_input = gr.Number(label="輸入攻擊值", value=50)
|
| 65 |
-
attack_button = gr.Button("
|
| 66 |
attack_output = gr.Textbox(label="攻擊結果")
|
| 67 |
|
| 68 |
-
# 小隊按鈕生成
|
| 69 |
-
team_buttons = gr.Row()
|
| 70 |
-
def generate_team_buttons():
|
| 71 |
-
return [
|
| 72 |
-
gr.Button(team_name).click(select_team, inputs=[], outputs=attack_output, _js=f"() => '{team_name}'")
|
| 73 |
-
for team_name in groups.keys()
|
| 74 |
-
]
|
| 75 |
-
init_button.click(generate_team_buttons, outputs=team_buttons)
|
| 76 |
-
|
| 77 |
# 更新小隊血量顯示
|
| 78 |
display_button = gr.Button("顯示目前血量")
|
| 79 |
teams_display = gr.Textbox(label="小隊血量狀態", lines=10)
|
| 80 |
|
| 81 |
# 事件綁定
|
| 82 |
-
attack_button.click(
|
| 83 |
display_button.click(display_teams, outputs=teams_display)
|
| 84 |
|
| 85 |
# 啟動應用程式
|
| 86 |
app.launch()
|
|
|
|
|
|
| 4 |
groups = {} # 儲存每組學生和血量
|
| 5 |
team_health = {} # 儲存每組的雞排王當前血量
|
| 6 |
max_health = 500 # 預設雞排王的最大血量
|
|
|
|
| 7 |
|
| 8 |
# 初始化組別
|
| 9 |
def initialize_groups(group_info, health):
|
|
|
|
| 21 |
|
| 22 |
return f"已建立 {len(groups)} 組,每組血量為 {max_health}。"
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
# 輸入攻擊值並扣血
|
| 25 |
+
def attack_team(team_name, damage):
|
| 26 |
+
if team_name in team_health:
|
| 27 |
+
team_health[team_name] = max(0, team_health[team_name] - int(damage))
|
| 28 |
+
return f"{team_name} 的血量剩餘 {team_health[team_name]}。"
|
| 29 |
+
return "小隊名稱不存在。"
|
|
|
|
| 30 |
|
| 31 |
# 顯示目前所有小隊的血量狀態
|
| 32 |
def display_teams():
|
|
|
|
| 53 |
|
| 54 |
# 攻擊輸入區
|
| 55 |
with gr.Row():
|
| 56 |
+
team_input = gr.Textbox(label="輸入攻擊小隊名稱", placeholder="例如:小隊1")
|
| 57 |
damage_input = gr.Number(label="輸入攻擊值", value=50)
|
| 58 |
+
attack_button = gr.Button("攻擊")
|
| 59 |
attack_output = gr.Textbox(label="攻擊結果")
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
# 更新小隊血量顯示
|
| 62 |
display_button = gr.Button("顯示目前血量")
|
| 63 |
teams_display = gr.Textbox(label="小隊血量狀態", lines=10)
|
| 64 |
|
| 65 |
# 事件綁定
|
| 66 |
+
attack_button.click(attack_team, inputs=[team_input, damage_input], outputs=attack_output)
|
| 67 |
display_button.click(display_teams, outputs=teams_display)
|
| 68 |
|
| 69 |
# 啟動應用程式
|
| 70 |
app.launch()
|
| 71 |
+
|