File size: 3,513 Bytes
12d7e8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1013f0
12d7e8f
 
 
 
 
 
 
 
 
 
 
 
 
67ac071
12d7e8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import streamlit as st
import random
from PIL import Image
import os

# Load images from the 'images' folder
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

# Load images and store labels
images = load_images()
image_labels = list(images.keys())

# Initialize game state
if "score" not in st.session_state:
    st.session_state.score = 0  # Tracks correct answers
    st.session_state.image_pool = image_labels.copy()  # Pool of images to show
    random.shuffle(st.session_state.image_pool)  # Shuffle images
    st.session_state.selected_image = None  # Current image to guess
    st.session_state.wrong_message = ""  # Stores wrong answer message
    st.session_state.correct_message = ""  # Stores correct answer message

st.title("7DREAM Image Matching Game")

# Check if all images have been correctly matched
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()

# Continue game if not finished
else:
    st.write("Match the image to the correct member!")

    # Pick a new image from the pool
    if not st.session_state.selected_image:
        if not st.session_state.image_pool:  # Reshuffle if all images were shown
            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()  # Select next image

    # Display the image
    st.image(images[st.session_state.selected_image], caption="Who is this?", use_container_width=True)

    # Provide answer choices
    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 = ""  # Clear wrong message
            st.session_state.score += 1  # Increase score
            st.session_state.selected_image = None  # Move to next image
            st.rerun()
        else:
            st.session_state.wrong_message = "❌ Wrong! Try again."
            st.session_state.correct_message = ""  # Clear correct message

    # Show messages persistently
    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)}")