File size: 2,204 Bytes
822d57b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | import gradio as gr
import random
player = {"name": "์ฉ์ฌ", "hp": 100, "max_hp": 100, "atk": 20, "def": 5}
monster = {}
def start_battle():
global monster
monster = {
"name": random.choice(["๊ณ ๋ธ๋ฆฐ", "๋๋", "์ฌ๋ผ์", "๋์ "]),
"hp": random.randint(30, 70),
"atk": random.randint(10, 20),
"def": random.randint(2, 6)
}
return f"{monster['name']}์ด(๊ฐ) ๋ํ๋ฌ๋ค! ์ฒด๋ ฅ: {monster['hp']}"
def attack():
if not monster:
return "๋จผ์ ๋ชฌ์คํฐ๋ฅผ ์ํํ์ธ์!"
dmg = max(1, player["atk"] - monster["def"])
monster["hp"] -= dmg
log = f"{player['name']}์ด(๊ฐ) {monster['name']}์๊ฒ {dmg}์ ํผํด๋ฅผ ์
ํ๋ค.\n"
if monster["hp"] <= 0:
log += f"{monster['name']}์(๋ฅผ) ์ฒ์นํ๋ค!"
monster.clear()
return log
enemy_dmg = max(1, monster["atk"] - player["def"])
player["hp"] -= enemy_dmg
log += f"{monster['name']}์ด(๊ฐ) {player['name']}์๊ฒ {enemy_dmg}์ ํผํด๋ฅผ ์
ํ๋ค.\n"
if player["hp"] <= 0:
return log + "๋น์ ์ ์ฐ๋ฌ์ก์ต๋๋ค..."
return log
def heal():
if player["hp"] >= player["max_hp"]:
return "์ด๋ฏธ ์ฒด๋ ฅ์ด ๊ฐ๋ ์ฐผ์ต๋๋ค."
player["hp"] = min(player["max_hp"], player["hp"] + 30)
return f"ํฌ์
์ ์ฌ์ฉํ์ฌ ์ฒด๋ ฅ์ ํ๋ณตํ์ต๋๋ค. ํ์ฌ ์ฒด๋ ฅ: {player['hp']}"
def get_status():
return f"{player['name']} ์ฒด๋ ฅ: {player['hp']}/{player['max_hp']}"
with gr.Blocks() as demo:
gr.Markdown("# โ๏ธ ๋๋ง์ RPG ๊ฒ์")
status = gr.Textbox(label="ํ๋ ์ด์ด ์ํ", value=get_status())
log = gr.Textbox(label="์ ํฌ ๋ก๊ทธ")
with gr.Row():
btn_battle = gr.Button("๋ชฌ์คํฐ ์ํ")
btn_attack = gr.Button("๊ณต๊ฒฉ")
btn_heal = gr.Button("ํฌ์
์ฌ์ฉ")
btn_battle.click(fn=start_battle, outputs=log, show_progress=False)
btn_attack.click(fn=attack, outputs=log, show_progress=False)
btn_heal.click(fn=heal, outputs=log, show_progress=False)
btn_battle.click(fn=get_status, outputs=status)
btn_attack.click(fn=get_status, outputs=status)
btn_heal.click(fn=get_status, outputs=status)
demo.launch() |