Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,45 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import random
|
|
|
|
| 3 |
import time
|
| 4 |
from datetime import datetime
|
| 5 |
-
from streamlit_lottie import st_lottie
|
| 6 |
-
import requests
|
| 7 |
|
| 8 |
st.set_page_config(page_title="๐ฎ Ultimate Mini Game Arcade", layout="centered")
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
<style>
|
| 13 |
-
.main {
|
| 14 |
-
background-color: #f4f4f4;
|
| 15 |
-
padding: 1rem;
|
| 16 |
-
border-radius: 20px;
|
| 17 |
-
}
|
| 18 |
-
.game-box {
|
| 19 |
-
background-color: white;
|
| 20 |
-
padding: 1rem;
|
| 21 |
-
border-radius: 15px;
|
| 22 |
-
box-shadow: 0 0 10px rgba(0,0,0,0.1);
|
| 23 |
-
}
|
| 24 |
-
</style>
|
| 25 |
-
""", unsafe_allow_html=True)
|
| 26 |
-
|
| 27 |
-
# ------------------ Animation Loader ------------------
|
| 28 |
-
def load_lottie(url):
|
| 29 |
r = requests.get(url)
|
| 30 |
-
if r.status_code
|
| 31 |
-
return
|
| 32 |
-
return
|
| 33 |
|
| 34 |
-
|
| 35 |
-
lottie_gamepad
|
| 36 |
-
|
| 37 |
-
lottie_lose = load_lottie("https://lottie.host/94f0aa61-daa8-4ff3-8716-bdbb9cf1010c/hEzI1FfMFA.json")
|
| 38 |
-
lottie_typing = load_lottie("https://lottie.host/0e342720-2d97-4ab3-9b91-4ccdc2df5db5/gZ5I7lLWB0.json")
|
| 39 |
|
| 40 |
-
st_lottie(lottie_gamepad, height=200)
|
| 41 |
st.title("๐ฎ Ultimate Mini Game Arcade")
|
| 42 |
-
st.caption("
|
| 43 |
st.markdown("---")
|
| 44 |
|
| 45 |
-
# ------------------ Game Selector ------------------
|
| 46 |
games = [
|
| 47 |
"Rock Paper Scissors",
|
| 48 |
"Guess the Number",
|
| 49 |
"Word Scramble",
|
| 50 |
"Emoji Quiz",
|
| 51 |
-
"Hangman
|
| 52 |
"Math Quiz",
|
| 53 |
-
"Quick Typing
|
| 54 |
-
"
|
| 55 |
"Tic Tac Toe"
|
| 56 |
]
|
| 57 |
-
|
| 58 |
-
selected_game = st.selectbox("๐น๏ธ Choose a game to play:", games)
|
| 59 |
st.markdown("---")
|
| 60 |
|
| 61 |
-
#
|
| 62 |
def rock_paper_scissors():
|
| 63 |
st.subheader("๐ชจ Rock Paper Scissors")
|
| 64 |
-
player = st.radio("
|
| 65 |
if st.button("Play!"):
|
| 66 |
computer = random.choice(["Rock", "Paper", "Scissors"])
|
| 67 |
st.write(f"Computer chose: **{computer}**")
|
|
@@ -71,10 +49,8 @@ def rock_paper_scissors():
|
|
| 71 |
(player == "Paper" and computer == "Rock") or \
|
| 72 |
(player == "Scissors" and computer == "Paper"):
|
| 73 |
st.success("You win!")
|
| 74 |
-
st_lottie(lottie_win, height=150)
|
| 75 |
else:
|
| 76 |
st.error("You lose!")
|
| 77 |
-
st_lottie(lottie_lose, height=150)
|
| 78 |
|
| 79 |
def guess_the_number():
|
| 80 |
st.subheader("๐ข Guess the Number")
|
|
@@ -89,41 +65,41 @@ def guess_the_number():
|
|
| 89 |
st.session_state.attempts_left -= 1
|
| 90 |
if guess == st.session_state.target_number:
|
| 91 |
st.success("๐ Correct!")
|
| 92 |
-
st_lottie(lottie_win, height=150)
|
| 93 |
del st.session_state.target_number
|
| 94 |
elif st.session_state.attempts_left == 0:
|
| 95 |
st.error(f"๐ข Out of attempts! The number was {st.session_state.target_number}")
|
| 96 |
-
st_lottie(lottie_lose, height=150)
|
| 97 |
del st.session_state.target_number
|
| 98 |
elif guess < st.session_state.target_number:
|
| 99 |
st.warning("Too low!")
|
| 100 |
else:
|
| 101 |
st.warning("Too high!")
|
| 102 |
with col2:
|
| 103 |
-
st.info(f"Attempts left: {st.session_state.attempts_left}")
|
| 104 |
|
| 105 |
def word_scramble():
|
| 106 |
st.subheader("๐ก Word Scramble")
|
| 107 |
words = ["streamlit", "developer", "python", "banana"]
|
| 108 |
if "scramble_word" not in st.session_state:
|
| 109 |
st.session_state.scramble_word = random.choice(words)
|
| 110 |
-
st.session_state.
|
| 111 |
-
|
| 112 |
scrambled = ''.join(random.sample(st.session_state.scramble_word, len(st.session_state.scramble_word)))
|
| 113 |
st.write(f"Unscramble this: **{scrambled}**")
|
|
|
|
| 114 |
col1, col2 = st.columns(2)
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
| 127 |
|
| 128 |
def emoji_quiz():
|
| 129 |
st.subheader("๐ Emoji Quiz")
|
|
@@ -140,7 +116,6 @@ def emoji_quiz():
|
|
| 140 |
if st.button("Check Answer"):
|
| 141 |
if guess.strip().lower() == st.session_state.emoji_answer.lower():
|
| 142 |
st.success("Correct!")
|
| 143 |
-
st_lottie(lottie_win, height=150)
|
| 144 |
del st.session_state.emoji_question
|
| 145 |
del st.session_state.emoji_answer
|
| 146 |
else:
|
|
@@ -152,19 +127,21 @@ def hangman():
|
|
| 152 |
if "hangman_word" not in st.session_state:
|
| 153 |
st.session_state.hangman_word = random.choice(words)
|
| 154 |
st.session_state.hangman_tries = 5
|
|
|
|
| 155 |
col1, col2 = st.columns(2)
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
| 168 |
|
| 169 |
def math_quiz():
|
| 170 |
st.subheader("โ Math Quiz")
|
|
@@ -176,88 +153,54 @@ def math_quiz():
|
|
| 176 |
if st.button("Submit"):
|
| 177 |
if ans == st.session_state.a + st.session_state.b:
|
| 178 |
st.success("Correct!")
|
| 179 |
-
st_lottie(lottie_win, height=150)
|
| 180 |
-
del st.session_state.a
|
| 181 |
-
del st.session_state.b
|
| 182 |
else:
|
| 183 |
st.error(f"Wrong! Answer is {st.session_state.a + st.session_state.b}")
|
| 184 |
-
|
|
|
|
| 185 |
|
| 186 |
def quick_typing():
|
| 187 |
-
st.subheader("โจ๏ธ Quick Typing
|
| 188 |
words = ["keyboard", "streamlit", "python", "arcade"]
|
| 189 |
if "typing_word" not in st.session_state:
|
| 190 |
st.session_state.typing_word = random.choice(words)
|
| 191 |
st.session_state.start_time = time.time()
|
| 192 |
-
|
| 193 |
-
st_lottie(lottie_typing, height=150)
|
| 194 |
st.info(f"Type this: **{st.session_state.typing_word}**")
|
| 195 |
user = st.text_input("Start typing:")
|
| 196 |
-
duration = len(st.session_state.typing_word) * 0.7
|
| 197 |
time_passed = time.time() - st.session_state.start_time
|
| 198 |
-
|
| 199 |
st.write(f"โณ Time used: {int(time_passed)}s / {int(duration)}s")
|
| 200 |
-
|
| 201 |
if st.button("Check"):
|
| 202 |
if time_passed > duration:
|
| 203 |
-
st.error("Time exceeded!")
|
| 204 |
-
st_lottie(lottie_lose, height=150)
|
| 205 |
elif user == st.session_state.typing_word:
|
| 206 |
st.success("Perfect!")
|
| 207 |
-
st_lottie(lottie_win, height=150)
|
| 208 |
else:
|
| 209 |
st.error("Incorrect!")
|
| 210 |
del st.session_state.typing_word
|
| 211 |
|
| 212 |
-
def
|
| 213 |
-
st.subheader("
|
| 214 |
-
|
| 215 |
-
if "
|
| 216 |
-
st.session_state.
|
| 217 |
-
|
| 218 |
-
if
|
| 219 |
-
st.
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
|
|
|
|
|
|
| 227 |
|
| 228 |
def tic_tac_toe():
|
| 229 |
st.subheader("โ Tic Tac Toe")
|
| 230 |
-
|
| 231 |
-
st.session_state.board = [""] * 9
|
| 232 |
-
st.session_state.turn = "X"
|
| 233 |
-
st.session_state.winner = None
|
| 234 |
-
|
| 235 |
-
def check_winner(b):
|
| 236 |
-
wins = [(0,1,2), (3,4,5), (6,7,8), (0,3,6), (1,4,7), (2,5,8), (0,4,8), (2,4,6)]
|
| 237 |
-
for i,j,k in wins:
|
| 238 |
-
if b[i] == b[j] == b[k] and b[i] != "":
|
| 239 |
-
return b[i]
|
| 240 |
-
return None
|
| 241 |
-
|
| 242 |
-
for i in range(3):
|
| 243 |
-
cols = st.columns(3)
|
| 244 |
-
for j in range(3):
|
| 245 |
-
idx = i * 3 + j
|
| 246 |
-
if cols[j].button(st.session_state.board[idx] or " ", key=idx) and not st.session_state.board[idx] and not st.session_state.winner:
|
| 247 |
-
st.session_state.board[idx] = st.session_state.turn
|
| 248 |
-
st.session_state.winner = check_winner(st.session_state.board)
|
| 249 |
-
if not st.session_state.winner:
|
| 250 |
-
st.session_state.turn = "O" if st.session_state.turn == "X" else "X"
|
| 251 |
-
|
| 252 |
-
if st.session_state.winner:
|
| 253 |
-
st.success(f"Player {st.session_state.winner} wins!")
|
| 254 |
-
st_lottie(lottie_win, height=150)
|
| 255 |
-
st.session_state.board = [""] * 9
|
| 256 |
-
st.session_state.turn = "X"
|
| 257 |
-
st.session_state.winner = None
|
| 258 |
-
|
| 259 |
-
# ------------------ Game Dispatcher ------------------
|
| 260 |
|
|
|
|
| 261 |
if selected_game == "Rock Paper Scissors":
|
| 262 |
rock_paper_scissors()
|
| 263 |
elif selected_game == "Guess the Number":
|
|
@@ -266,17 +209,16 @@ elif selected_game == "Word Scramble":
|
|
| 266 |
word_scramble()
|
| 267 |
elif selected_game == "Emoji Quiz":
|
| 268 |
emoji_quiz()
|
| 269 |
-
elif selected_game == "Hangman
|
| 270 |
hangman()
|
| 271 |
elif selected_game == "Math Quiz":
|
| 272 |
math_quiz()
|
| 273 |
-
elif selected_game == "Quick Typing
|
| 274 |
quick_typing()
|
| 275 |
-
elif selected_game == "
|
| 276 |
-
|
| 277 |
elif selected_game == "Tic Tac Toe":
|
| 278 |
tic_tac_toe()
|
| 279 |
|
| 280 |
-
# ------------------ Footer ------------------
|
| 281 |
st.markdown("---")
|
| 282 |
-
st.markdown("<center>๐
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from streamlit_lottie import st_lottie
|
| 3 |
import random
|
| 4 |
+
import requests
|
| 5 |
import time
|
| 6 |
from datetime import datetime
|
|
|
|
|
|
|
| 7 |
|
| 8 |
st.set_page_config(page_title="๐ฎ Ultimate Mini Game Arcade", layout="centered")
|
| 9 |
|
| 10 |
+
# Load Lottie animation
|
| 11 |
+
def load_lottieurl(url):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
r = requests.get(url)
|
| 13 |
+
if r.status_code == 200:
|
| 14 |
+
return r.json()
|
| 15 |
+
return None
|
| 16 |
|
| 17 |
+
lottie_gamepad = load_lottieurl("https://lottie.host/02f43b1d-15c7-4055-a02d-bbfb25d9b5f6/KUzEMZuOE4.json")
|
| 18 |
+
if lottie_gamepad:
|
| 19 |
+
st_lottie(lottie_gamepad, height=200)
|
|
|
|
|
|
|
| 20 |
|
|
|
|
| 21 |
st.title("๐ฎ Ultimate Mini Game Arcade")
|
| 22 |
+
st.caption("Fun games in a beautiful interface โจ")
|
| 23 |
st.markdown("---")
|
| 24 |
|
|
|
|
| 25 |
games = [
|
| 26 |
"Rock Paper Scissors",
|
| 27 |
"Guess the Number",
|
| 28 |
"Word Scramble",
|
| 29 |
"Emoji Quiz",
|
| 30 |
+
"Hangman",
|
| 31 |
"Math Quiz",
|
| 32 |
+
"Quick Typing",
|
| 33 |
+
"Emoji Memory Match",
|
| 34 |
"Tic Tac Toe"
|
| 35 |
]
|
| 36 |
+
selected_game = st.selectbox("๐น๏ธ Choose a game:", games)
|
|
|
|
| 37 |
st.markdown("---")
|
| 38 |
|
| 39 |
+
# Games
|
| 40 |
def rock_paper_scissors():
|
| 41 |
st.subheader("๐ชจ Rock Paper Scissors")
|
| 42 |
+
player = st.radio("Your move:", ["Rock", "Paper", "Scissors"], horizontal=True)
|
| 43 |
if st.button("Play!"):
|
| 44 |
computer = random.choice(["Rock", "Paper", "Scissors"])
|
| 45 |
st.write(f"Computer chose: **{computer}**")
|
|
|
|
| 49 |
(player == "Paper" and computer == "Rock") or \
|
| 50 |
(player == "Scissors" and computer == "Paper"):
|
| 51 |
st.success("You win!")
|
|
|
|
| 52 |
else:
|
| 53 |
st.error("You lose!")
|
|
|
|
| 54 |
|
| 55 |
def guess_the_number():
|
| 56 |
st.subheader("๐ข Guess the Number")
|
|
|
|
| 65 |
st.session_state.attempts_left -= 1
|
| 66 |
if guess == st.session_state.target_number:
|
| 67 |
st.success("๐ Correct!")
|
|
|
|
| 68 |
del st.session_state.target_number
|
| 69 |
elif st.session_state.attempts_left == 0:
|
| 70 |
st.error(f"๐ข Out of attempts! The number was {st.session_state.target_number}")
|
|
|
|
| 71 |
del st.session_state.target_number
|
| 72 |
elif guess < st.session_state.target_number:
|
| 73 |
st.warning("Too low!")
|
| 74 |
else:
|
| 75 |
st.warning("Too high!")
|
| 76 |
with col2:
|
| 77 |
+
st.info(f"๐ฏ Attempts left: {st.session_state.attempts_left}")
|
| 78 |
|
| 79 |
def word_scramble():
|
| 80 |
st.subheader("๐ก Word Scramble")
|
| 81 |
words = ["streamlit", "developer", "python", "banana"]
|
| 82 |
if "scramble_word" not in st.session_state:
|
| 83 |
st.session_state.scramble_word = random.choice(words)
|
| 84 |
+
st.session_state.scramble_attempts = 3
|
|
|
|
| 85 |
scrambled = ''.join(random.sample(st.session_state.scramble_word, len(st.session_state.scramble_word)))
|
| 86 |
st.write(f"Unscramble this: **{scrambled}**")
|
| 87 |
+
answer = st.text_input("Your guess:")
|
| 88 |
col1, col2 = st.columns(2)
|
| 89 |
+
with col1:
|
| 90 |
+
if st.button("Check"):
|
| 91 |
+
if answer.lower() == st.session_state.scramble_word:
|
| 92 |
+
st.success("Correct!")
|
| 93 |
+
del st.session_state.scramble_word
|
| 94 |
+
else:
|
| 95 |
+
st.session_state.scramble_attempts -= 1
|
| 96 |
+
if st.session_state.scramble_attempts <= 0:
|
| 97 |
+
st.error(f"Out of tries! Word was **{st.session_state.scramble_word}**")
|
| 98 |
+
del st.session_state.scramble_word
|
| 99 |
+
else:
|
| 100 |
+
st.warning("Try again!")
|
| 101 |
+
with col2:
|
| 102 |
+
st.info(f"๐ Tries left: {st.session_state.scramble_attempts}")
|
| 103 |
|
| 104 |
def emoji_quiz():
|
| 105 |
st.subheader("๐ Emoji Quiz")
|
|
|
|
| 116 |
if st.button("Check Answer"):
|
| 117 |
if guess.strip().lower() == st.session_state.emoji_answer.lower():
|
| 118 |
st.success("Correct!")
|
|
|
|
| 119 |
del st.session_state.emoji_question
|
| 120 |
del st.session_state.emoji_answer
|
| 121 |
else:
|
|
|
|
| 127 |
if "hangman_word" not in st.session_state:
|
| 128 |
st.session_state.hangman_word = random.choice(words)
|
| 129 |
st.session_state.hangman_tries = 5
|
| 130 |
+
guess = st.text_input("Guess the word:")
|
| 131 |
col1, col2 = st.columns(2)
|
| 132 |
+
with col1:
|
| 133 |
+
if st.button("Submit"):
|
| 134 |
+
st.session_state.hangman_tries -= 1
|
| 135 |
+
if guess.lower() == st.session_state.hangman_word:
|
| 136 |
+
st.success("Correct!")
|
| 137 |
+
del st.session_state.hangman_word
|
| 138 |
+
elif st.session_state.hangman_tries <= 0:
|
| 139 |
+
st.error(f"Game over! Word was {st.session_state.hangman_word}")
|
| 140 |
+
del st.session_state.hangman_word
|
| 141 |
+
else:
|
| 142 |
+
st.warning(f"Incorrect!")
|
| 143 |
+
with col2:
|
| 144 |
+
st.info(f"Tries left: {st.session_state.hangman_tries}")
|
| 145 |
|
| 146 |
def math_quiz():
|
| 147 |
st.subheader("โ Math Quiz")
|
|
|
|
| 153 |
if st.button("Submit"):
|
| 154 |
if ans == st.session_state.a + st.session_state.b:
|
| 155 |
st.success("Correct!")
|
|
|
|
|
|
|
|
|
|
| 156 |
else:
|
| 157 |
st.error(f"Wrong! Answer is {st.session_state.a + st.session_state.b}")
|
| 158 |
+
del st.session_state.a
|
| 159 |
+
del st.session_state.b
|
| 160 |
|
| 161 |
def quick_typing():
|
| 162 |
+
st.subheader("โจ๏ธ Quick Typing")
|
| 163 |
words = ["keyboard", "streamlit", "python", "arcade"]
|
| 164 |
if "typing_word" not in st.session_state:
|
| 165 |
st.session_state.typing_word = random.choice(words)
|
| 166 |
st.session_state.start_time = time.time()
|
|
|
|
|
|
|
| 167 |
st.info(f"Type this: **{st.session_state.typing_word}**")
|
| 168 |
user = st.text_input("Start typing:")
|
|
|
|
| 169 |
time_passed = time.time() - st.session_state.start_time
|
| 170 |
+
duration = len(st.session_state.typing_word) * 0.7
|
| 171 |
st.write(f"โณ Time used: {int(time_passed)}s / {int(duration)}s")
|
|
|
|
| 172 |
if st.button("Check"):
|
| 173 |
if time_passed > duration:
|
| 174 |
+
st.error("โฐ Time exceeded!")
|
|
|
|
| 175 |
elif user == st.session_state.typing_word:
|
| 176 |
st.success("Perfect!")
|
|
|
|
| 177 |
else:
|
| 178 |
st.error("Incorrect!")
|
| 179 |
del st.session_state.typing_word
|
| 180 |
|
| 181 |
+
def emoji_memory():
|
| 182 |
+
st.subheader("๐ง Emoji Memory Match")
|
| 183 |
+
emojis = random.sample(["๐", "๐", "๐ถ", "๐", "๐", "๐"], 4)
|
| 184 |
+
if "emoji_mem" not in st.session_state:
|
| 185 |
+
st.session_state.emoji_mem = emojis
|
| 186 |
+
st.session_state.mem_start = datetime.now()
|
| 187 |
+
if (datetime.now() - st.session_state.mem_start).seconds < 5:
|
| 188 |
+
st.info("Memorize these:")
|
| 189 |
+
st.code(" ".join(st.session_state.emoji_mem))
|
| 190 |
+
else:
|
| 191 |
+
guess = st.text_input("Enter one emoji you remember:")
|
| 192 |
+
if st.button("Check"):
|
| 193 |
+
if guess.strip() in st.session_state.emoji_mem:
|
| 194 |
+
st.success("Correct!")
|
| 195 |
+
else:
|
| 196 |
+
st.error(f"Oops! Emojis were: {' '.join(st.session_state.emoji_mem)}")
|
| 197 |
+
del st.session_state.emoji_mem
|
| 198 |
|
| 199 |
def tic_tac_toe():
|
| 200 |
st.subheader("โ Tic Tac Toe")
|
| 201 |
+
st.write("โ
Coming soon! Placeholder for a playable board.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
+
# Dispatcher
|
| 204 |
if selected_game == "Rock Paper Scissors":
|
| 205 |
rock_paper_scissors()
|
| 206 |
elif selected_game == "Guess the Number":
|
|
|
|
| 209 |
word_scramble()
|
| 210 |
elif selected_game == "Emoji Quiz":
|
| 211 |
emoji_quiz()
|
| 212 |
+
elif selected_game == "Hangman":
|
| 213 |
hangman()
|
| 214 |
elif selected_game == "Math Quiz":
|
| 215 |
math_quiz()
|
| 216 |
+
elif selected_game == "Quick Typing":
|
| 217 |
quick_typing()
|
| 218 |
+
elif selected_game == "Emoji Memory Match":
|
| 219 |
+
emoji_memory()
|
| 220 |
elif selected_game == "Tic Tac Toe":
|
| 221 |
tic_tac_toe()
|
| 222 |
|
|
|
|
| 223 |
st.markdown("---")
|
| 224 |
+
st.markdown("<center>๐ Built with โค๏ธ for fun and feedback on LinkedIn!</center>", unsafe_allow_html=True)
|