Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,174 +1,169 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import random
|
|
|
|
| 3 |
|
| 4 |
-
# Page
|
| 5 |
-
st.set_page_config(page_title="๐ฎ Mini Game Arcade", layout="centered")
|
| 6 |
|
| 7 |
-
|
| 8 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
# Game list
|
| 11 |
games = [
|
| 12 |
-
"Rock-Paper-Scissors",
|
| 13 |
-
"Guess the Number",
|
| 14 |
-
"Word Scramble",
|
| 15 |
-
"Emoji Quiz",
|
| 16 |
-
"Coin Toss",
|
| 17 |
-
"Dice Roller"
|
| 18 |
-
"Simple Math Quiz",
|
| 19 |
-
"Even or Odd",
|
| 20 |
-
"Higher or Lower",
|
| 21 |
-
"Lucky Draw"
|
| 22 |
]
|
| 23 |
|
| 24 |
-
# Score Tracker (Simple)
|
| 25 |
-
if "score" not in st.session_state:
|
| 26 |
-
st.session_state.score = 0
|
| 27 |
-
|
| 28 |
-
# Game selection
|
| 29 |
selected_game = st.selectbox("Choose a game:", games)
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
st.divider()
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
elif
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
st.info("Try higher.")
|
| 61 |
else:
|
| 62 |
st.info("Try lower.")
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
st.write(f"Scrambled word: **{st.session_state.scrambled}**")
|
| 72 |
-
|
| 73 |
-
if st.button("
|
| 74 |
-
if
|
| 75 |
st.success("Correct!")
|
| 76 |
-
st.session_state.
|
| 77 |
-
del st.session_state.
|
| 78 |
else:
|
| 79 |
st.error("Try again.")
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
| 84 |
emoji, answer = random.choice(list(emojis.items()))
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
if st.button("Submit
|
| 88 |
-
if
|
| 89 |
st.success("Correct!")
|
| 90 |
-
st.session_state.
|
| 91 |
else:
|
| 92 |
st.error(f"Wrong! It was {answer}.")
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
| 98 |
result = random.choice(["Heads", "Tails"])
|
| 99 |
st.write(f"The coin shows: **{result}**")
|
| 100 |
if guess == result:
|
| 101 |
-
st.success("You guessed it right!")
|
| 102 |
-
st.session_state.score += 1
|
| 103 |
-
else:
|
| 104 |
-
st.error("Wrong guess!")
|
| 105 |
-
|
| 106 |
-
elif selected_game == "Dice Roller":
|
| 107 |
-
st.header("๐ฒ Dice Roller")
|
| 108 |
-
if st.button("Roll Dice"):
|
| 109 |
-
roll = random.randint(1, 6)
|
| 110 |
-
st.write(f"You rolled: **{roll}**")
|
| 111 |
-
|
| 112 |
-
elif selected_game == "Simple Math Quiz":
|
| 113 |
-
st.header("โ Simple Math Quiz")
|
| 114 |
-
|
| 115 |
-
# Store random numbers only once
|
| 116 |
-
if "a" not in st.session_state or "b" not in st.session_state:
|
| 117 |
-
st.session_state.a = random.randint(1, 10)
|
| 118 |
-
st.session_state.b = random.randint(1, 10)
|
| 119 |
-
|
| 120 |
-
a = st.session_state.a
|
| 121 |
-
b = st.session_state.b
|
| 122 |
-
|
| 123 |
-
answer = st.number_input(f"What is {a} + {b}?", step=1)
|
| 124 |
-
|
| 125 |
-
if st.button("Check Answer"):
|
| 126 |
-
if answer == a + b:
|
| 127 |
st.success("Correct!")
|
| 128 |
-
st.session_state.
|
| 129 |
-
# Reset for next question
|
| 130 |
-
del st.session_state.a
|
| 131 |
-
del st.session_state.b
|
| 132 |
else:
|
| 133 |
-
st.error("Wrong
|
| 134 |
-
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
if st.button("Check"):
|
| 141 |
-
correct = "Even" if num % 2 == 0 else "Odd"
|
| 142 |
-
if guess == correct:
|
| 143 |
-
st.success("Correct!")
|
| 144 |
-
st.session_state.score += 1
|
| 145 |
-
else:
|
| 146 |
-
st.error(f"Wrong! It was {correct}.")
|
| 147 |
-
|
| 148 |
-
elif selected_game == "Higher or Lower":
|
| 149 |
-
st.header("๐ Higher or Lower")
|
| 150 |
-
if "hl_num" not in st.session_state:
|
| 151 |
-
st.session_state.hl_num = random.randint(1, 100)
|
| 152 |
-
|
| 153 |
-
guess = st.number_input("Guess a number between 1 and 100:", 1, 100, step=1)
|
| 154 |
-
if st.button("Submit Guess"):
|
| 155 |
-
if guess == st.session_state.hl_num:
|
| 156 |
-
st.success("You got it!")
|
| 157 |
-
st.session_state.score += 1
|
| 158 |
-
st.session_state.hl_num = random.randint(1, 100)
|
| 159 |
-
elif guess < st.session_state.hl_num:
|
| 160 |
-
st.info("Try higher.")
|
| 161 |
-
else:
|
| 162 |
-
st.info("Try lower.")
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
prize = random.choice(["Better luck next time", "Small Gift", "Free Course", "Chocolate", "Amazon Gift Card"])
|
| 168 |
-
st.write(f"๐ You got: **{prize}**")
|
| 169 |
-
if prize != "Better luck next time":
|
| 170 |
-
st.session_state.score += 1
|
| 171 |
|
| 172 |
-
#
|
| 173 |
st.divider()
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import random
|
| 3 |
+
import time
|
| 4 |
|
| 5 |
+
# ----------------- Page Setup -----------------
|
| 6 |
+
st.set_page_config(page_title="๐ฎ Mini Game Arcade Pro", layout="centered", page_icon="๐ฎ")
|
| 7 |
|
| 8 |
+
# ----------------- Styling -----------------
|
| 9 |
+
st.markdown("""
|
| 10 |
+
<style>
|
| 11 |
+
.main {background-color: #f9f9f9;}
|
| 12 |
+
.stButton button {background-color: #4CAF50; color: white;}
|
| 13 |
+
</style>
|
| 14 |
+
""", unsafe_allow_html=True)
|
| 15 |
+
|
| 16 |
+
st.title("๐ฎ Mini Game Arcade Pro")
|
| 17 |
+
st.write("Select a game and enjoy!")
|
| 18 |
+
|
| 19 |
+
# ----------------- Initialize State -----------------
|
| 20 |
+
if "total_score" not in st.session_state:
|
| 21 |
+
st.session_state.total_score = 0
|
| 22 |
+
if "leaderboard" not in st.session_state:
|
| 23 |
+
st.session_state.leaderboard = []
|
| 24 |
|
|
|
|
| 25 |
games = [
|
| 26 |
+
"โ Rock-Paper-Scissors",
|
| 27 |
+
"๐ข Guess the Number",
|
| 28 |
+
"๐ค Word Scramble",
|
| 29 |
+
"๐ Emoji Quiz",
|
| 30 |
+
"๐ช Coin Toss",
|
| 31 |
+
"๐ฒ Dice Roller"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
]
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
selected_game = st.selectbox("Choose a game:", games)
|
| 35 |
|
| 36 |
+
st.progress(st.session_state.total_score % 10 / 10)
|
| 37 |
+
st.metric(label="Total Score", value=st.session_state.total_score)
|
| 38 |
+
|
| 39 |
+
# ----------------- Game Logic -----------------
|
| 40 |
st.divider()
|
| 41 |
|
| 42 |
+
# --- Rock-Paper-Scissors ---
|
| 43 |
+
if selected_game == "โ Rock-Paper-Scissors":
|
| 44 |
+
with st.expander("โน๏ธ How to Play"):
|
| 45 |
+
st.write("Choose Rock, Paper or Scissors. Try to beat the computer!")
|
| 46 |
+
|
| 47 |
+
col1, col2 = st.columns(2)
|
| 48 |
+
with col1:
|
| 49 |
+
choice = st.radio("Your move:", ["Rock", "Paper", "Scissors"], key="rps_choice")
|
| 50 |
+
with col2:
|
| 51 |
+
if st.button("Play", key="rps_play"):
|
| 52 |
+
computer = random.choice(["Rock", "Paper", "Scissors"])
|
| 53 |
+
st.write(f"Computer chose: **{computer}**")
|
| 54 |
+
if choice == computer:
|
| 55 |
+
st.info("It's a tie!")
|
| 56 |
+
elif (choice == "Rock" and computer == "Scissors") or (choice == "Paper" and computer == "Rock") or (choice == "Scissors" and computer == "Paper"):
|
| 57 |
+
st.success("You win!")
|
| 58 |
+
st.session_state.total_score += 1
|
| 59 |
+
else:
|
| 60 |
+
st.error("Computer wins!")
|
| 61 |
+
|
| 62 |
+
# --- Guess the Number ---
|
| 63 |
+
elif selected_game == "๐ข Guess the Number":
|
| 64 |
+
with st.expander("โน๏ธ How to Play"):
|
| 65 |
+
st.write("Guess the number between 1 and 20.")
|
| 66 |
+
|
| 67 |
+
if "guess_target" not in st.session_state:
|
| 68 |
+
st.session_state.guess_target = random.randint(1, 20)
|
| 69 |
+
|
| 70 |
+
guess = st.number_input("Your guess:", 1, 20, step=1, key="guess_input")
|
| 71 |
+
if st.button("Check", key="guess_check"):
|
| 72 |
+
if guess == st.session_state.guess_target:
|
| 73 |
+
st.success("Correct! ๐")
|
| 74 |
+
st.session_state.total_score += 1
|
| 75 |
+
st.session_state.guess_target = random.randint(1, 20)
|
| 76 |
+
elif guess < st.session_state.guess_target:
|
| 77 |
st.info("Try higher.")
|
| 78 |
else:
|
| 79 |
st.info("Try lower.")
|
| 80 |
|
| 81 |
+
# --- Word Scramble ---
|
| 82 |
+
elif selected_game == "๐ค Word Scramble":
|
| 83 |
+
with st.expander("โน๏ธ How to Play"):
|
| 84 |
+
st.write("Unscramble the word!")
|
| 85 |
+
|
| 86 |
+
words = ["streamlit", "python", "arcade"]
|
| 87 |
+
if "scramble_word" not in st.session_state:
|
| 88 |
+
st.session_state.scramble_word = random.choice(words)
|
| 89 |
+
st.session_state.scrambled = "".join(random.sample(st.session_state.scramble_word, len(st.session_state.scramble_word)))
|
| 90 |
|
| 91 |
st.write(f"Scrambled word: **{st.session_state.scrambled}**")
|
| 92 |
+
guess = st.text_input("Your guess:", key="scramble_guess")
|
| 93 |
+
if st.button("Submit", key="scramble_submit"):
|
| 94 |
+
if guess.lower() == st.session_state.scramble_word:
|
| 95 |
st.success("Correct!")
|
| 96 |
+
st.session_state.total_score += 1
|
| 97 |
+
del st.session_state.scramble_word
|
| 98 |
else:
|
| 99 |
st.error("Try again.")
|
| 100 |
|
| 101 |
+
# --- Emoji Quiz ---
|
| 102 |
+
elif selected_game == "๐ Emoji Quiz":
|
| 103 |
+
with st.expander("โน๏ธ How to Play"):
|
| 104 |
+
st.write("Guess what the emoji represents.")
|
| 105 |
+
|
| 106 |
+
emojis = {"๐": "Pizza", "๐": "Snake", "๐": "Car"}
|
| 107 |
emoji, answer = random.choice(list(emojis.items()))
|
| 108 |
+
options = list(emojis.values())
|
| 109 |
+
guess = st.radio(f"What does this emoji mean? {emoji}", options, key="emoji_guess")
|
| 110 |
+
if st.button("Submit", key="emoji_submit"):
|
| 111 |
+
if guess == answer:
|
| 112 |
st.success("Correct!")
|
| 113 |
+
st.session_state.total_score += 1
|
| 114 |
else:
|
| 115 |
st.error(f"Wrong! It was {answer}.")
|
| 116 |
|
| 117 |
+
# --- Coin Toss ---
|
| 118 |
+
elif selected_game == "๐ช Coin Toss":
|
| 119 |
+
with st.expander("โน๏ธ How to Play"):
|
| 120 |
+
st.write("Guess Heads or Tails.")
|
| 121 |
+
|
| 122 |
+
guess = st.radio("Heads or Tails?", ["Heads", "Tails"], key="coin_guess")
|
| 123 |
+
if st.button("Toss Coin", key="coin_toss"):
|
| 124 |
result = random.choice(["Heads", "Tails"])
|
| 125 |
st.write(f"The coin shows: **{result}**")
|
| 126 |
if guess == result:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
st.success("Correct!")
|
| 128 |
+
st.session_state.total_score += 1
|
|
|
|
|
|
|
|
|
|
| 129 |
else:
|
| 130 |
+
st.error("Wrong guess.")
|
|
|
|
| 131 |
|
| 132 |
+
# --- Dice Roller ---
|
| 133 |
+
elif selected_game == "๐ฒ Dice Roller":
|
| 134 |
+
with st.expander("โน๏ธ How to Play"):
|
| 135 |
+
st.write("Roll the dice and see the result.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
+
if st.button("Roll Dice", key="dice_roll"):
|
| 138 |
+
roll = random.randint(1, 6)
|
| 139 |
+
st.write(f"You rolled: **{roll}**")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
# ----------------- Reset & Leaderboard -----------------
|
| 142 |
st.divider()
|
| 143 |
+
col1, col2 = st.columns(2)
|
| 144 |
+
with col1:
|
| 145 |
+
if st.button("Reset Score"):
|
| 146 |
+
st.session_state.total_score = 0
|
| 147 |
+
st.session_state.guess_target = random.randint(1, 20)
|
| 148 |
+
st.session_state.scramble_word = None
|
| 149 |
+
|
| 150 |
+
with col2:
|
| 151 |
+
if st.button("Add to Leaderboard"):
|
| 152 |
+
st.session_state.leaderboard.append(st.session_state.total_score)
|
| 153 |
+
st.success("Added to leaderboard!")
|
| 154 |
+
|
| 155 |
+
if st.session_state.leaderboard:
|
| 156 |
+
st.write("### ๐ Leaderboard:")
|
| 157 |
+
sorted_lb = sorted(st.session_state.leaderboard, reverse=True)
|
| 158 |
+
for idx, score in enumerate(sorted_lb[:5], 1):
|
| 159 |
+
st.write(f"{idx}. {score} points")
|
| 160 |
+
|
| 161 |
+
# ----------------- Timer Example -----------------
|
| 162 |
+
# This is a simple demonstration, full countdown logic is possible with time.sleep() and st.empty()
|
| 163 |
+
with st.expander("โฑ๏ธ Timer Example"):
|
| 164 |
+
if st.button("Start 5-second Timer"):
|
| 165 |
+
timer_placeholder = st.empty()
|
| 166 |
+
for i in range(5, 0, -1):
|
| 167 |
+
timer_placeholder.write(f"Time remaining: {i} seconds")
|
| 168 |
+
time.sleep(1)
|
| 169 |
+
timer_placeholder.write("Time's up!")
|