Spaces:
Sleeping

multiple_games / app.py
SyedaRidaAI's picture
Update app.py
2c40ea6 verified
Raw History Blame Contribute Delete
7.64 kB
import streamlit as st
import random
# Sidebar for Game Selector
st.sidebar.title("๐ŸŽฎ Mini Game Arcade")
game_choice = st.sidebar.selectbox("Choose a game:", [
"Guess the Number",
"Word Scramble",
"Emoji Quiz",
"Math Challenge",
"Riddle Me This",
"Capital Cities",
"True or False",
"Fast Typing Test",
"Guess the Sound",
"Memory Match"
])
# Sidebar instructions placeholder
st.sidebar.subheader("๐Ÿ“œ How to Play")
# Score tracker
if "score" not in st.session_state:
st.session_state.score = 0
# -------------------- GAME 1: Guess the Number --------------------
if game_choice == "Guess the Number":
st.sidebar.write("๐ŸŽฏ Guess a number between 1 and 100. Player 1 hides input, Player 2 guesses!")
if "secret_number" not in st.session_state:
st.session_state.secret_number = random.randint(1, 100)
guess = st.number_input("Player 2: Enter your guess", min_value=1, max_value=100, step=1)
if st.button("Check Guess"):
if guess == st.session_state.secret_number:
st.success("๐ŸŽ‰ Correct! You guessed it.")
st.session_state.score += 1
st.session_state.secret_number = random.randint(1, 100)
elif guess < st.session_state.secret_number:
st.warning("Too low! Try again.")
else:
st.warning("Too high! Try again.")
# -------------------- GAME 2: Word Scramble --------------------
elif game_choice == "Word Scramble":
st.sidebar.write("๐Ÿ”€ Unscramble the word!")
words = ['banana', 'elephant', 'python', 'streamlit', 'developer']
if "scramble" not in st.session_state:
word = random.choice(words)
scrambled = ''.join(random.sample(word, len(word)))
st.session_state.scramble = (scrambled, word)
scrambled, actual_word = st.session_state.scramble
st.write(f"๐Ÿ”ค Scrambled Word: `{scrambled}`")
answer = st.text_input("Your Guess:")
if st.button("Submit"):
if answer.lower() == actual_word:
st.success("โœ… Correct!")
st.session_state.score += 1
del st.session_state.scramble
else:
st.sidebar.error(f"โŒ Sorry, correct answer was `{actual_word}`.")
# -------------------- GAME 3: Emoji Quiz --------------------
elif game_choice == "Emoji Quiz":
st.sidebar.write("๐Ÿค” Guess the word from emoji hints!")
emoji_list = [
("๐Ÿ‘จโ€โš•๏ธ๐Ÿ’‰๐Ÿฅ", "doctor"),
("๐ŸŒง๏ธโ˜‚๏ธโšก", "rain"),
("๐Ÿฑ๐Ÿ“ฑ", "catfish"),
("๐ŸŒ๐Ÿš€", "astronaut")
]
if "emoji" not in st.session_state:
st.session_state.emoji = random.choice(emoji_list)
emoji, correct = st.session_state.emoji
st.write(f"๐Ÿงฉ Emoji: {emoji}")
guess = st.text_input("Your Answer:")
if st.button("Check Answer"):
if guess.lower() == correct:
st.success("๐ŸŽ‰ Correct!")
st.session_state.score += 1
del st.session_state.emoji
else:
st.sidebar.error(f"โŒ Sorry, correct answer was `{correct}`.")
# -------------------- GAME 4: Math Challenge --------------------
elif game_choice == "Math Challenge":
st.sidebar.write("๐Ÿงฎ Solve simple math!")
a, b = random.randint(1, 20), random.randint(1, 20)
st.write(f"What is {a} + {b}?")
answer = st.number_input("Your answer:", step=1)
if st.button("Submit Answer"):
if answer == a + b:
st.success("Correct!")
st.session_state.score += 1
else:
st.sidebar.error(f"โŒ Sorry, correct answer was `{a + b}`.")
# -------------------- GAME 5: Riddle Me This --------------------
elif game_choice == "Riddle Me This":
st.sidebar.write("๐Ÿง  Solve the riddle!")
riddles = [
("What has keys but can't open locks?", "keyboard"),
("What has hands but can't clap?", "clock")
]
if "riddle" not in st.session_state:
st.session_state.riddle = random.choice(riddles)
q, ans = st.session_state.riddle
st.write(f"๐Ÿ•ต๏ธ Riddle: {q}")
rans = st.text_input("Your Guess:")
if st.button("Answer"):
if rans.lower() == ans:
st.success("โœ”๏ธ That's right!")
st.session_state.score += 1
del st.session_state.riddle
else:
st.sidebar.error(f"โŒ Sorry, correct answer was `{ans}`.")
# -------------------- GAME 6: Capital Cities --------------------
elif game_choice == "Capital Cities":
st.sidebar.write("๐ŸŒ Guess the capital city!")
pairs = [("France", "Paris"), ("Japan", "Tokyo"), ("Pakistan", "Islamabad")]
if "capital" not in st.session_state:
st.session_state.capital = random.choice(pairs)
country, capital = st.session_state.capital
st.write(f"What is the capital of {country}?")
guess = st.text_input("Your Guess")
if st.button("Submit Capital"):
if guess.lower() == capital.lower():
st.success("โœ”๏ธ Correct!")
st.session_state.score += 1
del st.session_state.capital
else:
st.sidebar.error(f"โŒ The capital is `{capital}`.")
# -------------------- GAME 7: True or False --------------------
elif game_choice == "True or False":
st.sidebar.write("โœ… Decide if the statement is true!")
facts = [("The Sun is a star.", "true"), ("Humans have 3 lungs.", "false")]
if "fact" not in st.session_state:
st.session_state.fact = random.choice(facts)
statement, tf = st.session_state.fact
st.write(f"๐Ÿงพ Statement: {statement}")
ans = st.radio("Choose:", ["True", "False"])
if st.button("Submit True/False"):
if ans.lower() == tf:
st.success("๐ŸŽฏ Correct!")
st.session_state.score += 1
del st.session_state.fact
else:
st.sidebar.error(f"โŒ Correct answer is `{tf.capitalize()}`.")
# -------------------- GAME 8: Fast Typing Test --------------------
elif game_choice == "Fast Typing Test":
st.sidebar.write("โŒจ๏ธ Type the sentence exactly!")
sentence = "Streamlit makes UI easy."
st.write(f"๐Ÿ“ Type this: `{sentence}`")
user_input = st.text_input("Your typed text:")
if st.button("Check Typing"):
if user_input.strip() == sentence:
st.success("Fast and accurate!")
st.session_state.score += 1
else:
st.sidebar.error(f"โŒ Oops! Correct was: `{sentence}`")
# -------------------- GAME 9: Guess the Sound --------------------
elif game_choice == "Guess the Sound":
st.sidebar.write("๐Ÿ”Š Guess the sound from description (mock version)")
sounds = [("A loud 'moo'", "cow"), ("A buzzing 'bzzz'", "bee")]
if "sound" not in st.session_state:
st.session_state.sound = random.choice(sounds)
desc, answer = st.session_state.sound
st.write(f"๐ŸŽง Description: {desc}")
guess = st.text_input("Your Guess:")
if st.button("Submit Sound"):
if guess.lower() == answer:
st.success("โœ… Correct!")
st.session_state.score += 1
del st.session_state.sound
else:
st.sidebar.error(f"โŒ It's `{answer}`.")
# -------------------- GAME 10: Memory Match --------------------
elif game_choice == "Memory Match":
st.sidebar.write("๐Ÿง  Match emojis and remember!")
emojis = ['๐ŸŽ', '๐ŸŽ', '๐Ÿถ', '๐Ÿถ']
random.shuffle(emojis)
st.write("Game in development: Match pairs (UI needed)")
st.info("This will need more UI logic for real memory game.")
# Final Score
st.sidebar.markdown("---")
st.sidebar.success(f"๐ŸŽฏ Your Score: `{st.session_state.score}`")