|
import streamlit as st |
|
import random |
|
from PIL import Image |
|
import os |
|
|
|
|
|
def load_images(): |
|
image_folder = "images" |
|
images = { |
|
"Mark": "mark.jpeg", "Renjun": "renjun.jpeg", "Jeno": "jeno.jpeg", |
|
"Haechan": "haechan.jpeg", "Jaemin": "jaemin.jpeg", "Chenle": "chenle.jpeg", "Jisung": "jisung.jpeg" |
|
} |
|
loaded_images = {} |
|
for label, filename in images.items(): |
|
img_path = os.path.join(image_folder, filename) |
|
if os.path.exists(img_path): |
|
loaded_images[label] = Image.open(img_path) |
|
return loaded_images |
|
|
|
|
|
images = load_images() |
|
image_labels = list(images.keys()) |
|
|
|
|
|
if "score" not in st.session_state: |
|
st.session_state.score = 0 |
|
st.session_state.image_pool = image_labels.copy() |
|
random.shuffle(st.session_state.image_pool) |
|
st.session_state.selected_image = None |
|
st.session_state.wrong_message = "" |
|
st.session_state.correct_message = "" |
|
|
|
st.title("7DREAM Image Matching Game") |
|
|
|
|
|
if st.session_state.score == len(image_labels): |
|
st.success("π Congratulations! You are a Dreamzen. π") |
|
|
|
col1, col2 = st.columns(2) |
|
with col1: |
|
if st.button("π Play Again"): |
|
st.session_state.score = 0 |
|
st.session_state.image_pool = image_labels.copy() |
|
random.shuffle(st.session_state.image_pool) |
|
st.session_state.selected_image = None |
|
st.session_state.wrong_message = "" |
|
st.session_state.correct_message = "" |
|
st.rerun() |
|
|
|
with col2: |
|
if st.button("β Exit Game"): |
|
st.warning("You can now close this tab to exit the game.") |
|
st.stop() |
|
|
|
|
|
else: |
|
st.write("Match the image to the correct member!") |
|
|
|
|
|
if not st.session_state.selected_image: |
|
if not st.session_state.image_pool: |
|
st.session_state.image_pool = image_labels.copy() |
|
random.shuffle(st.session_state.image_pool) |
|
|
|
st.session_state.selected_image = st.session_state.image_pool.pop() |
|
|
|
|
|
st.image(images[st.session_state.selected_image], caption="Who is this?", use_container_width=True) |
|
|
|
|
|
selected_label = st.radio("Choose the correct member:", image_labels, key="selected_label") |
|
|
|
if st.button("Submit"): |
|
if selected_label == st.session_state.selected_image: |
|
st.session_state.correct_message = "β
Correct! π" |
|
st.session_state.wrong_message = "" |
|
st.session_state.score += 1 |
|
st.session_state.selected_image = None |
|
st.rerun() |
|
else: |
|
st.session_state.wrong_message = "β Wrong! Try again." |
|
st.session_state.correct_message = "" |
|
|
|
|
|
if st.session_state.correct_message: |
|
st.success(st.session_state.correct_message) |
|
|
|
if st.session_state.wrong_message: |
|
st.error(st.session_state.wrong_message) |
|
|
|
st.write(f"**Score:** {st.session_state.score} / {len(image_labels)}") |
|
|
|
|