app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import random
|
3 |
+
from PIL import Image
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Load images from the 'images' folder
|
7 |
+
def load_images():
|
8 |
+
image_folder = "images"
|
9 |
+
images = {
|
10 |
+
"Mark": "mark.jpeg", "Renjun": "renjun.jpeg", "Jeno": "jeno.jpeg",
|
11 |
+
"Haechan": "haechan.jpeg", "Jaemin": "jaemin.jpeg", "Chenle": "chenle.jpeg", "Jisung": "jisung.jpeg"
|
12 |
+
}
|
13 |
+
loaded_images = {}
|
14 |
+
for label, filename in images.items():
|
15 |
+
img_path = os.path.join(image_folder, filename)
|
16 |
+
if os.path.exists(img_path):
|
17 |
+
loaded_images[label] = Image.open(img_path)
|
18 |
+
return loaded_images
|
19 |
+
|
20 |
+
# Load images and store labels
|
21 |
+
images = load_images()
|
22 |
+
image_labels = list(images.keys())
|
23 |
+
|
24 |
+
# Initialize game state
|
25 |
+
if "score" not in st.session_state:
|
26 |
+
st.session_state.score = 0 # Tracks correct answers
|
27 |
+
st.session_state.image_pool = image_labels.copy() # Pool of images to show
|
28 |
+
random.shuffle(st.session_state.image_pool) # Shuffle images
|
29 |
+
st.session_state.selected_image = None # Current image to guess
|
30 |
+
st.session_state.wrong_message = "" # Stores wrong answer message
|
31 |
+
st.session_state.correct_message = "" # Stores correct answer message
|
32 |
+
|
33 |
+
st.title("7DREAM Image Matching Game")
|
34 |
+
|
35 |
+
# Check if all images have been correctly matched
|
36 |
+
if st.session_state.score == len(image_labels):
|
37 |
+
st.success("π Congratulations! You are a Dreamzen. π")
|
38 |
+
|
39 |
+
col1, col2 = st.columns(2)
|
40 |
+
with col1:
|
41 |
+
if st.button("π Play Again"):
|
42 |
+
st.session_state.score = 0
|
43 |
+
st.session_state.image_pool = image_labels.copy()
|
44 |
+
random.shuffle(st.session_state.image_pool)
|
45 |
+
st.session_state.selected_image = None
|
46 |
+
st.session_state.wrong_message = ""
|
47 |
+
st.session_state.correct_message = ""
|
48 |
+
st.rerun()
|
49 |
+
|
50 |
+
with col2:
|
51 |
+
if st.button("β Exit Game"):
|
52 |
+
st.warning("You can now close this tab to exit the game.")
|
53 |
+
st.stop()
|
54 |
+
|
55 |
+
# Continue game if not finished
|
56 |
+
else:
|
57 |
+
st.write("Match the image to the correct label!")
|
58 |
+
|
59 |
+
# Pick a new image from the pool
|
60 |
+
if not st.session_state.selected_image:
|
61 |
+
if not st.session_state.image_pool: # Reshuffle if all images were shown
|
62 |
+
st.session_state.image_pool = image_labels.copy()
|
63 |
+
random.shuffle(st.session_state.image_pool)
|
64 |
+
|
65 |
+
st.session_state.selected_image = st.session_state.image_pool.pop() # Select next image
|
66 |
+
|
67 |
+
# Display the image
|
68 |
+
st.image(images[st.session_state.selected_image], caption="Who is this?", use_container_width=True)
|
69 |
+
|
70 |
+
# Provide answer choices
|
71 |
+
selected_label = st.radio("Choose the correct label:", image_labels, key="selected_label")
|
72 |
+
|
73 |
+
if st.button("Submit"):
|
74 |
+
if selected_label == st.session_state.selected_image:
|
75 |
+
st.session_state.correct_message = "β
Correct! π"
|
76 |
+
st.session_state.wrong_message = "" # Clear wrong message
|
77 |
+
st.session_state.score += 1 # Increase score
|
78 |
+
st.session_state.selected_image = None # Move to next image
|
79 |
+
st.rerun()
|
80 |
+
else:
|
81 |
+
st.session_state.wrong_message = "β Wrong! Try again."
|
82 |
+
st.session_state.correct_message = "" # Clear correct message
|
83 |
+
|
84 |
+
# Show messages persistently
|
85 |
+
if st.session_state.correct_message:
|
86 |
+
st.success(st.session_state.correct_message)
|
87 |
+
|
88 |
+
if st.session_state.wrong_message:
|
89 |
+
st.error(st.session_state.wrong_message)
|
90 |
+
|
91 |
+
st.write(f"**Score:** {st.session_state.score} / {len(image_labels)}")
|
92 |
+
|
93 |
+
streamlit
|
94 |
+
pillow
|