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()