Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,48 +5,31 @@ import streamlit as st
|
|
5 |
import svgwrite
|
6 |
from PIL import Image
|
7 |
|
8 |
-
DEFAULT_CARD_WIDTH = 75
|
9 |
-
DEFAULT_CARD_HEIGHT = 100
|
10 |
-
|
11 |
PARTS = {"background": ["white", "black", "red", "blue", "green", "yellow"],
|
12 |
"suit": ["clubs", "diamonds", "hearts", "spades"],
|
13 |
"value": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]}
|
14 |
|
15 |
LEVELS = ["common", "uncommon", "rare", "epic", "legendary", "mythic", "godlike"]
|
16 |
|
17 |
-
|
18 |
def draw_card(background, suit, value, level, card_width, card_height):
|
19 |
dwg = svgwrite.Drawing(size=(f"{card_width}px", f"{card_height}px"))
|
20 |
dwg.add(dwg.rect((0, 0), (card_width, card_height), rx=10, ry=10, fill=background, stroke="black", stroke_width=2))
|
21 |
-
|
22 |
-
|
23 |
-
dwg.add(
|
24 |
-
value = svgwrite.text.Text(value, insert=(5, card_height - 10), fill="black", font_size="16px", font_weight="bold")
|
25 |
-
dwg.add(value)
|
26 |
-
level = svgwrite.text.Text(level.upper(), insert=(card_width - 50, card_height - 10), fill="black", font_size="12px", font_weight="bold")
|
27 |
-
dwg.add(level)
|
28 |
-
|
29 |
return dwg.tostring()
|
30 |
|
31 |
-
|
32 |
def display_parts_selection():
|
33 |
-
|
34 |
-
for part, options in PARTS.items():
|
35 |
-
selected_parts[part] = st.sidebar.selectbox(f"Select {part}", options)
|
36 |
-
return selected_parts
|
37 |
-
|
38 |
|
39 |
def display_level_selection():
|
40 |
-
|
41 |
-
return selected_level
|
42 |
-
|
43 |
|
44 |
def display_size_selection(default_width, default_height):
|
45 |
card_width = st.sidebar.slider("Card Width", min_value=50, max_value=150, value=default_width, step=5)
|
46 |
card_height = st.sidebar.slider("Card Height", min_value=75, max_value=200, value=default_height, step=5)
|
47 |
return card_width, card_height
|
48 |
|
49 |
-
|
50 |
def display_image_upload(card_width, card_height):
|
51 |
uploaded_file = st.sidebar.file_uploader("Upload Card Background Image", type=["png", "jpg", "jpeg"])
|
52 |
if uploaded_file is not None:
|
@@ -60,7 +43,6 @@ def display_image_upload(card_width, card_height):
|
|
60 |
else:
|
61 |
return None
|
62 |
|
63 |
-
|
64 |
def display_card(selected_parts, selected_level, card_width, card_height, background):
|
65 |
if background and os.path.exists(background):
|
66 |
background_name = os.path.basename(background)
|
@@ -70,32 +52,34 @@ def display_card(selected_parts, selected_level, card_width, card_height, backgr
|
|
70 |
st.sidebar.markdown(f"**Background Image:** {selected_parts['background'].capitalize()}")
|
71 |
background_color = selected_parts["background"]
|
72 |
|
73 |
-
card_svg = draw_card(background_color,
|
74 |
-
|
75 |
-
selected_parts["value"],
|
76 |
-
selected_level,
|
77 |
-
card_width,
|
78 |
-
card_height)
|
79 |
-
|
80 |
-
# Add background
|
81 |
-
if background and os.path.exists(background):
|
82 |
img = svgwrite.image.Image(href=background, size=(f"{card_width}px", f"{card_height}px"))
|
83 |
dwg = svgwrite.fromstring(card_svg)
|
84 |
dwg.add(img)
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
|
89 |
# Create a download link for the card SVG
|
90 |
-
svg_content = '<?xml version="1.0" encoding="utf-8" ?>' + card_svg
|
91 |
st.download_button("Download Card as SVG", svg_content, "card.svg", "text/plain")
|
92 |
|
93 |
# Create button to level up card
|
94 |
if selected_level != "godlike":
|
95 |
if st.button("Level Up"):
|
96 |
current_level_index = LEVELS.index(selected_level)
|
97 |
-
|
98 |
-
selected_level = new_level
|
99 |
st.sidebar.warning(f"Card has been leveled up to {selected_level}")
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
import svgwrite
|
6 |
from PIL import Image
|
7 |
|
|
|
|
|
|
|
8 |
PARTS = {"background": ["white", "black", "red", "blue", "green", "yellow"],
|
9 |
"suit": ["clubs", "diamonds", "hearts", "spades"],
|
10 |
"value": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]}
|
11 |
|
12 |
LEVELS = ["common", "uncommon", "rare", "epic", "legendary", "mythic", "godlike"]
|
13 |
|
|
|
14 |
def draw_card(background, suit, value, level, card_width, card_height):
|
15 |
dwg = svgwrite.Drawing(size=(f"{card_width}px", f"{card_height}px"))
|
16 |
dwg.add(dwg.rect((0, 0), (card_width, card_height), rx=10, ry=10, fill=background, stroke="black", stroke_width=2))
|
17 |
+
dwg.add(svgwrite.text.Text(suit.upper(), insert=(5, 15), fill="black", font_size="16px", font_weight="bold"))
|
18 |
+
dwg.add(svgwrite.text.Text(value, insert=(5, card_height - 10), fill="black", font_size="16px", font_weight="bold"))
|
19 |
+
dwg.add(svgwrite.text.Text(level.upper(), insert=(card_width - 50, card_height - 10), fill="black", font_size="12px", font_weight="bold"))
|
|
|
|
|
|
|
|
|
|
|
20 |
return dwg.tostring()
|
21 |
|
|
|
22 |
def display_parts_selection():
|
23 |
+
return {part: st.sidebar.selectbox(f"Select {part}", options) for part, options in PARTS.items()}
|
|
|
|
|
|
|
|
|
24 |
|
25 |
def display_level_selection():
|
26 |
+
return st.sidebar.selectbox("Select Level", LEVELS)
|
|
|
|
|
27 |
|
28 |
def display_size_selection(default_width, default_height):
|
29 |
card_width = st.sidebar.slider("Card Width", min_value=50, max_value=150, value=default_width, step=5)
|
30 |
card_height = st.sidebar.slider("Card Height", min_value=75, max_value=200, value=default_height, step=5)
|
31 |
return card_width, card_height
|
32 |
|
|
|
33 |
def display_image_upload(card_width, card_height):
|
34 |
uploaded_file = st.sidebar.file_uploader("Upload Card Background Image", type=["png", "jpg", "jpeg"])
|
35 |
if uploaded_file is not None:
|
|
|
43 |
else:
|
44 |
return None
|
45 |
|
|
|
46 |
def display_card(selected_parts, selected_level, card_width, card_height, background):
|
47 |
if background and os.path.exists(background):
|
48 |
background_name = os.path.basename(background)
|
|
|
52 |
st.sidebar.markdown(f"**Background Image:** {selected_parts['background'].capitalize()}")
|
53 |
background_color = selected_parts["background"]
|
54 |
|
55 |
+
card_svg = draw_card(background_color, selected_parts["suit"], selected_parts["value"], selected_level, card_width, card_height)
|
56 |
+
if background:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
img = svgwrite.image.Image(href=background, size=(f"{card_width}px", f"{card_height}px"))
|
58 |
dwg = svgwrite.fromstring(card_svg)
|
59 |
dwg.add(img)
|
60 |
+
st.write(f'<svg viewBox="0 0 {card_width} {card_height}">{dwg.tostring()}</svg>', unsafe_allow_html=True)
|
61 |
+
else:
|
62 |
+
st.write(f'<svg viewBox="0 0 {card_width} {card_height}">{card_svg.decode("utf-8")}</svg>', unsafe_allow_html=True)
|
63 |
|
64 |
# Create a download link for the card SVG
|
65 |
+
svg_content = '<?xml version="1.0" encoding="utf-8" ?>' + card_svg.decode("utf-8")
|
66 |
st.download_button("Download Card as SVG", svg_content, "card.svg", "text/plain")
|
67 |
|
68 |
# Create button to level up card
|
69 |
if selected_level != "godlike":
|
70 |
if st.button("Level Up"):
|
71 |
current_level_index = LEVELS.index(selected_level)
|
72 |
+
selected_level = LEVELS[current_level_index + 1]
|
|
|
73 |
st.sidebar.warning(f"Card has been leveled up to {selected_level}")
|
74 |
+
|
75 |
+
def main():
|
76 |
+
st.set_page_config(page_title="Card Evolution Game", page_icon=":hearts:")
|
77 |
+
selected_parts = display_parts_selection()
|
78 |
+
selected_level = display_level_selection()
|
79 |
+
card_width, card_height = display_size_selection(75, 100)
|
80 |
+
background = display_image_upload(card_width, card_height)
|
81 |
+
display_card(selected_parts, selected_level, card_width, card_height, background)
|
82 |
+
|
83 |
+
if __name__ == '__main__':
|
84 |
+
main()
|
85 |
+
|