Spaces:
Sleeping
Sleeping
File size: 7,946 Bytes
9eba199 0aeb19f 9eba199 71097f2 9eba199 0aeb19f a7187b1 0aeb19f 71097f2 9eba199 71097f2 1c5c4e0 0aeb19f 1c5c4e0 0aeb19f 1c5c4e0 71097f2 9eba199 a7187b1 9eba199 0aeb19f 9eba199 0aeb19f 9eba199 0aeb19f 9eba199 0aeb19f 9eba199 0aeb19f 9eba199 0aeb19f 9eba199 a7187b1 9eba199 0aeb19f 9eba199 1c5c4e0 9eba199 a7187b1 0aeb19f 9eba199 a7187b1 9eba199 a7187b1 0aeb19f 9eba199 a7187b1 9eba199 a7187b1 0aeb19f 9eba199 0aeb19f a7187b1 9eba199 0aeb19f a7187b1 9eba199 0aeb19f 9eba199 0aeb19f 9eba199 0aeb19f 9eba199 0aeb19f 9eba199 0aeb19f 9eba199 0aeb19f 9eba199 0aeb19f 9eba199 a7187b1 9eba199 a7187b1 9eba199 71097f2 9eba199 1c5c4e0 0aeb19f 9eba199 1c5c4e0 71097f2 0aeb19f 9eba199 0aeb19f 1c5c4e0 0aeb19f 9eba199 a7187b1 | 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | import streamlit as st
import random
st.set_page_config(page_title="Mini Game Arcade", layout="wide")
# Initialize session state
if "score" not in st.session_state:
st.session_state.score = {}
if "guessed_letters" not in st.session_state:
st.session_state.guessed_letters = set()
if "scramble_word" not in st.session_state:
st.session_state.scramble_word = None
if "secret_number" not in st.session_state:
st.session_state.secret_number = random.randint(1, 10)
if "math_a" not in st.session_state:
st.session_state.math_a = random.randint(1, 10)
if "math_b" not in st.session_state:
st.session_state.math_b = random.randint(1, 10)
if "emoji_question" not in st.session_state:
emojis = {"๐": "apple", "๐": "car", "๐ถ": "dog", "๐ธ": "guitar"}
st.session_state.emoji_question = random.choice(list(emojis.items()))
if "trivia_question" not in st.session_state:
st.session_state.trivia_question = {
"question": "What is the capital of France?",
"options": ["Paris", "London", "Rome", "Berlin"],
"answer": "Paris"
}
if "hangman_word" not in st.session_state:
st.session_state.hangman_word = "apple"
# Game list
GAMES = [
"Rock Paper Scissors",
"Guess the Number",
"Word Scramble",
"Emoji Quiz",
"Math Quiz",
"Hangman",
"Coin Toss",
"Dice Roll",
"Typing Speed Test",
"Trivia"
]
# Sidebar layout
st.sidebar.title("๐ฎ Mini Game Arcade")
selected_game = st.sidebar.selectbox("Select a Game", GAMES)
st.sidebar.markdown("---")
if st.sidebar.button("๐ Reset Score"):
st.session_state.score = {}
st.title(f"๐น๏ธ {selected_game}")
# Score tracking
def update_score(game, won):
if game not in st.session_state.score:
st.session_state.score[game] = {"wins": 0, "losses": 0}
if won:
st.session_state.score[game]["wins"] += 1
else:
st.session_state.score[game]["losses"] += 1
# Game Functions
def rock_paper_scissors():
choices = ["Rock", "Paper", "Scissors"]
user = st.radio("โ Choose your move", choices, horizontal=True)
if st.button("Play"):
comp = random.choice(choices)
st.write(f"๐ค Computer chose: **{comp}**")
if user == comp:
st.info("It's a draw!")
elif (user == "Rock" and comp == "Scissors") or (user == "Paper" and comp == "Rock") or (user == "Scissors" and comp == "Paper"):
st.success("You win!")
update_score("Rock Paper Scissors", True)
else:
st.error("You lose!")
update_score("Rock Paper Scissors", False)
def guess_the_number():
guess = st.number_input("๐ข Guess a number between 1 and 10", min_value=1, max_value=10, step=1)
if st.button("Check Guess"):
if guess == st.session_state.secret_number:
st.success("Correct!")
update_score("Guess the Number", True)
st.session_state.secret_number = random.randint(1, 10)
else:
st.error("Wrong! Try again.")
update_score("Guess the Number", False)
def word_scramble():
words = ["streamlit", "python", "arcade", "game", "guess"]
if not st.session_state.scramble_word:
word = random.choice(words)
scrambled = "".join(random.sample(word, len(word)))
st.session_state.scramble_word = (word, scrambled)
st.write(f"๐ Unscramble this word: `{st.session_state.scramble_word[1]}`")
user_input = st.text_input("Your guess:")
if st.button("Submit"):
if user_input.lower() == st.session_state.scrble_word[0]:
st.success("Correct!")
update_score("Word Scramble", True)
st.session_state.scramble_word = None
else:
st.error("Incorrect!")
update_score("Word Scramble", False)
def emoji_quiz():
emoji, answer = st.session_state.emoji_question
st.write(f"โ What does this emoji mean? {emoji}")
guess = st.text_input("Your answer:")
if st.button("Submit Answer"):
if guess.lower() == answer:
st.success("Correct!")
update_score("Emoji Quiz", True)
emojis = {"๐": "apple", "๐": "car", "๐ถ": "dog", "๐ธ": "guitar"}
st.session_state.emoji_question = random.choice(list(emojis.items()))
else:
st.error("Nope!")
update_score("Emoji Quiz", False)
def math_quiz():
a, b = st.session_state.math_a, st.session_state.math_b
st.write(f"โ What is {a} + {b}?")
ans = st.number_input("Answer:", step=1)
if st.button("Submit Answer"):
if ans == a + b:
st.success("Correct!")
update_score("Math Quiz", True)
else:
st.error("Incorrect!")
update_score("Math Quiz", False)
st.session_state.math_a = random.randint(1, 10)
st.session_state.math_b = random.randint(1, 10)
def hangman():
word = st.session_state.hangman_word
guess = st.text_input("๐ค Guess a letter:")
if st.button("Guess"):
st.session_state.guessed_letters.add(guess.lower())
display = [c if c in st.session_state.guessed_letters else "_" for c in word]
st.write("Word: " + " ".join(display))
if "_" not in display:
st.success("You guessed the word!")
update_score("Hangman", True)
st.session_state.guessed_letters.clear()
st.session_state.hangman_word = "apple"
elif len(st.session_state.guessed_letters) > 6:
st.error("Too many wrong guesses!")
update_score("Hangman", False)
st.session_state.guessed_letters.clear()
st.session_state.hangman_word = "apple"
def coin_toss():
choice = st.radio("๐ช Heads or Tails?", ["Heads", "Tails"], horizontal=True)
if st.button("Flip Coin"):
result = random.choice(["Heads", "Tails"])
st.write(f"It was: **{result}**")
if choice == result:
st.success("You win!")
update_score("Coin Toss", True)
else:
st.error("You lose!")
update_score("Coin Toss", False)
def dice_roll():
if st.button("๐ฒ Roll the Dice"):
result = random.randint(1, 6)
st.write(f"You rolled a **{result}**")
if result == 6:
st.success("๐ Jackpot!")
update_score("Dice Roll", True)
else:
update_score("Dice Roll", False)
def typing_test():
prompt = "streamlit is fun"
st.write(f"โจ๏ธ Type this exactly: `{prompt}`")
typed = st.text_input("Start typing:")
if st.button("Submit Typing"):
if typed.strip().lower() == prompt:
st.success("Perfect!")
update_score("Typing Speed Test", True)
else:
st.error("Mismatch!")
update_score("Typing Speed Test", False)
def trivia():
q = st.session_state.trivia_question
user_ans = st.radio("๐ " + q["question"], q["options"])
if st.button("Submit Answer"):
if user_ans == q["answer"]:
st.success("Correct!")
update_score("Trivia", True)
else:
st.error("Wrong!")
update_score("Trivia", False)
# Dispatcher
GAME_FUNCTIONS = {
"Rock Paper Scissors": rock_paper_scissors,
"Guess the Number": guess_the_number,
"Word Scramble": word_scramble,
"Emoji Quiz": emoji_quiz,
"Math Quiz": math_quiz,
"Hangman": hangman,
"Coin Toss": coin_toss,
"Dice Roll": dice_roll,
"Typing Speed Test": typing_test,
"Trivia": trivia,
}
# Run selected game
GAME_FUNCTIONS[selected_game]()
# Score summary
st.markdown("---")
with st.expander("๐ Score Summary"):
for game, result in st.session_state.score.items():
st.write(f"**{game}** โ โ
Wins: {result['wins']} | โ Losses: {result['losses']}")
# Footer
st.markdown("---")
st.markdown("<center><sub>๐ฏ Made by <strong>Muhammad Bilal Hussain</strong></sub></center>", unsafe_allow_html=True)
|