games / app.py
duarizwan's picture
Create app.py
37a83e7 verified
Raw
History Blame Contribute Delete
5.05 kB
import streamlit as st
import random
import time
# ---------- Sidebar Game Selection ----------
st.sidebar.title("๐ŸŽฎ Mini Game Arcade")
game = st.sidebar.selectbox("Choose a Game", [
"Rock Paper Scissors", "Guess the Number", "Word Scramble", "Emoji Quiz",
"Dice Roller", "Coin Toss", "Math Quiz", "Hangman", "Typing Speed Test",
"Color Guess", "Score Tracker"
])
# ---------- Game 1: Rock Paper Scissors ----------
if game == "Rock Paper Scissors":
st.title("๐Ÿชจ๐Ÿ“„โœ‚๏ธ Rock Paper Scissors")
user_choice = st.selectbox("Choose your move", ["Rock", "Paper", "Scissors"])
if st.button("Play"):
comp_choice = random.choice(["Rock", "Paper", "Scissors"])
st.write(f"Computer chose: {comp_choice}")
if user_choice == comp_choice:
st.success("It's a Tie!")
elif (user_choice == "Rock" and comp_choice == "Scissors") or \
(user_choice == "Paper" and comp_choice == "Rock") or \
(user_choice == "Scissors" and comp_choice == "Paper"):
st.success("You Win!")
else:
st.error("You Lose!")
# ---------- Game 2: Guess the Number ----------
elif game == "Guess the Number":
st.title("๐Ÿ”ข Guess the Number (1-100)")
secret = random.randint(1, 100)
guess = st.number_input("Enter your guess", 1, 100, step=1)
if st.button("Check"):
if guess == secret:
st.success("๐ŸŽ‰ Correct!")
elif guess < secret:
st.info("Too low!")
else:
st.info("Too high!")
# ---------- Game 3: Word Scramble ----------
elif game == "Word Scramble":
st.title("๐Ÿ”€ Word Scramble")
words = ["streamlit", "arcade", "python", "gamer", "developer"]
word = random.choice(words)
scrambled = ''.join(random.sample(word, len(word)))
st.write(f"Scrambled Word: **{scrambled}**")
answer = st.text_input("Your Guess")
if st.button("Submit"):
if answer.lower() == word:
st.success("Correct!")
else:
st.error(f"Wrong! The word was {word}")
# ---------- Game 4: Emoji Quiz ----------
elif game == "Emoji Quiz":
st.title("๐Ÿ˜Š Emoji Quiz")
quiz = {"๐ŸŽฌ๐ŸŸ": "Finding Nemo", "๐Ÿฆ๐Ÿ‘‘": "The Lion King", "๐Ÿ‘ป๐ŸŽ’": "Ghostbusters"}
emoji, answer = random.choice(list(quiz.items()))
st.write(f"Guess the Movie: {emoji}")
user = st.text_input("Your Guess")
if st.button("Check Answer"):
if user.lower() == answer.lower():
st.success("Correct!")
else:
st.error(f"Nope! It was {answer}")
# ---------- Game 5: Dice Roller ----------
elif game == "Dice Roller":
st.title("๐ŸŽฒ Dice Roller")
if st.button("Roll Dice"):
dice = random.randint(1, 6)
st.write(f"You rolled: **{dice}**")
# ---------- Game 6: Coin Toss ----------
elif game == "Coin Toss":
st.title("๐Ÿช™ Coin Toss")
if st.button("Toss Coin"):
result = random.choice(["Heads", "Tails"])
st.write(f"It's: **{result}**")
# ---------- Game 7: Math Quiz ----------
elif game == "Math Quiz":
st.title("โž• Math Quiz")
a, b = random.randint(1, 10), random.randint(1, 10)
user_ans = st.number_input(f"What is {a} + {b}?", 0, 100)
if st.button("Submit Answer"):
if user_ans == a + b:
st.success("Correct!")
else:
st.error(f"Wrong! Answer is {a + b}")
# ---------- Game 8: Hangman (simple) ----------
elif game == "Hangman":
st.title("๐Ÿ‘จโ€๐ŸŽค Hangman Lite")
word = random.choice(["apple", "banana", "grape", "mango"])
hint = word[0] + "_"*(len(word)-2) + word[-1]
st.write(f"Hint: {hint}")
guess = st.text_input("Guess the word")
if st.button("Guess"):
if guess.lower() == word:
st.success("Correct!")
else:
st.error(f"Wrong! The word was {word}")
# ---------- Game 9: Typing Speed Test ----------
elif game == "Typing Speed Test":
st.title("โŒจ๏ธ Typing Speed Test")
sentence = "Streamlit makes Python apps easy!"
st.write("Type this: ", sentence)
user_input = st.text_input("Start typing here")
if st.button("Test Speed"):
if user_input == sentence:
st.success("Perfect Match!")
else:
st.warning("Not matching. Try again!")
# ---------- Game 10: Color Guess ----------
elif game == "Color Guess":
st.title("๐ŸŽจ Color Guess")
colors = ["Red", "Blue", "Green", "Yellow"]
selected = random.choice(colors)
guess = st.selectbox("Which color am I thinking?", colors)
if st.button("Guess"):
if guess == selected:
st.success("You're right!")
else:
st.error(f"Nope! It was {selected}")
# ---------- Bonus: Score Tracker ----------
elif game == "Score Tracker":
st.title("๐Ÿ† Score Tracker")
score = st.number_input("Your score:", 0, 100)
if score >= 80:
st.success("Great job! ๐Ÿฅ‡")
elif score >= 50:
st.info("Keep going! ๐Ÿฅˆ")
else:
st.warning("Needs improvement ๐Ÿฅ‰")