Lashtw commited on
Commit
19b6100
·
verified ·
1 Parent(s): c6f9b7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -13
app.py CHANGED
@@ -4,6 +4,7 @@ import gradio as gr
4
  groups = {} # 儲存每組學生和血量
5
  team_health = {} # 儲存每組的雞排王當前血量
6
  max_health = 500 # 預設雞排王的最大血量
 
7
 
8
  # 初始化組別
9
  def initialize_groups(group_info, health):
@@ -21,12 +22,19 @@ 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():
@@ -38,10 +46,6 @@ def display_teams():
38
  display += f"{team} ({', '.join(students)}): [{health_bar}] {health}/{max_health}\n"
39
  return display.strip()
40
 
41
- # 更新小隊選項
42
- def update_team_dropdown():
43
- return list(groups.keys())
44
-
45
  # Gradio 介面
46
  with gr.Blocks() as app:
47
  gr.Markdown("## 雞排王小隊對戰系統")
@@ -57,19 +61,26 @@ with gr.Blocks() as app:
57
 
58
  # 攻擊輸入區
59
  with gr.Row():
60
- team_dropdown = gr.Dropdown(label="選擇攻擊小隊", choices=[], interactive=True)
61
  damage_input = gr.Number(label="輸入攻擊值", value=50)
62
- attack_button = gr.Button("攻擊")
63
  attack_output = gr.Textbox(label="攻擊結果")
64
 
 
 
 
 
 
 
 
 
 
65
  # 更新小隊血量顯示
66
  display_button = gr.Button("顯示目前血量")
67
  teams_display = gr.Textbox(label="小隊血量狀態", lines=10)
68
 
69
  # 事件綁定
70
- attack_button.click(attack_team, inputs=[team_dropdown, damage_input], outputs=attack_output)
71
  display_button.click(display_teams, outputs=teams_display)
72
- init_button.click(update_team_dropdown, outputs=team_dropdown) # 初始化後更新選項
73
 
74
  # 啟動應用程式
75
  app.launch()
 
4
  groups = {} # 儲存每組學生和血量
5
  team_health = {} # 儲存每組的雞排王當前血量
6
  max_health = 500 # 預設雞排王的最大血量
7
+ selected_team = "" # 暫存選中的小隊
8
 
9
  # 初始化組別
10
  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 attack_selected_team(damage):
33
+ global selected_team
34
+ if selected_team in team_health:
35
+ team_health[selected_team] = max(0, team_health[selected_team] - int(damage))
36
+ return f"{selected_team} 的血量剩餘 {team_health[selected_team]}。"
37
+ return "尚未選擇有效小隊或小隊不存在。"
38
 
39
  # 顯示目前所有小隊的血量狀態
40
  def display_teams():
 
46
  display += f"{team} ({', '.join(students)}): [{health_bar}] {health}/{max_health}\n"
47
  return display.strip()
48
 
 
 
 
 
49
  # Gradio 介面
50
  with gr.Blocks() as app:
51
  gr.Markdown("## 雞排王小隊對戰系統")
 
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(attack_selected_team, inputs=[damage_input], outputs=attack_output)
83
  display_button.click(display_teams, outputs=teams_display)
 
84
 
85
  # 啟動應用程式
86
  app.launch()