Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import random | |
| st.set_page_config(page_title="Mini Game Arcade ๐ฎ", layout="centered") | |
| # ---------- CSS for Block Buttons ---------- | |
| st.markdown(""" | |
| <style> | |
| .block-button { | |
| display: inline-block; | |
| padding: 1rem 2rem; | |
| margin: 0.5rem; | |
| font-size: 1.2rem; | |
| border-radius: 15px; | |
| background-color: #f9f9f9; | |
| color: black; | |
| border: 2px solid #555; | |
| cursor: pointer; | |
| transition: 0.3s; | |
| } | |
| .block-button:hover { | |
| background-color: #ddd; | |
| border-color: #000; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| # ---------- Title and Intro ---------- | |
| st.title("๐ฎ Mini Game Arcade") | |
| st.markdown("Choose a game below and start playing! Score will be tracked across session runtime.") | |
| # ---------- Score Tracker ---------- | |
| if "score" not in st.session_state: | |
| st.session_state.score = 0 | |
| st.sidebar.header("๐ฏ Your Score") | |
| st.sidebar.success(f"Total Score: {st.session_state.score}") | |
| # ---------- Game Options ---------- | |
| games = { | |
| "Rock Paper Scissors โโโ๏ธ": "rps", | |
| "Guess the Number ๐ข": "guess", | |
| "Word Scramble ๐ค": "scramble", | |
| "Emoji Quiz ๐": "emoji", | |
| "Math Challenge โโโ๏ธโ": "math", | |
| "Hangman ๐ ": "hangman", | |
| "Flag Guess ๐": "flag", | |
| "Capital Quiz ๐๏ธ": "capital", | |
| "Riddle Me ๐ง ": "riddle", | |
| "Color Match ๐จ": "color", | |
| "Quick Reaction โฑ๏ธ": "reaction", | |
| "Memory Match ๐งฉ": "memory", | |
| "Odd One Out ๐ค": "odd", | |
| "Animal Sound Guess ๐พ": "sound", | |
| "Typing Speed Test โจ๏ธ": "typing" | |
| } | |
| # ---------- Block Button Layout ---------- | |
| st.markdown("### ๐น๏ธ Pick a Game:") | |
| game_keys = list(games.keys()) | |
| selected_game = None | |
| for i in range(0, len(game_keys), 3): | |
| row = st.columns(3) | |
| for j in range(3): | |
| if i + j < len(game_keys): | |
| game_name = game_keys[i + j] | |
| key = games[game_name] | |
| if row[j].button(game_name, key=f"btn_{key}"): | |
| selected_game = key | |
| # ---------- Game Implementations ---------- | |
| def rock_paper_scissors(): | |
| st.header("Rock Paper Scissors") | |
| options = ["Rock", "Paper", "Scissors"] | |
| user_choice = st.radio("Choose your move:", options, horizontal=True) | |
| if st.button("Play"): | |
| comp_choice = random.choice(options) | |
| st.write(f"๐ค Computer chose: {comp_choice}") | |
| if user_choice == comp_choice: | |
| st.info("It's a draw!") | |
| 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!") | |
| st.session_state.score += 1 | |
| else: | |
| st.error("You lose!") | |
| def guess_the_number(): | |
| st.header("Guess the Number") | |
| if "number" not in st.session_state: | |
| st.session_state.number = random.randint(1, 20) | |
| guess = st.number_input("Guess a number between 1 and 20", min_value=1, max_value=20, step=1) | |
| if st.button("Submit Guess"): | |
| if guess == st.session_state.number: | |
| st.success("Correct! ๐") | |
| st.session_state.score += 1 | |
| st.session_state.number = random.randint(1, 20) | |
| elif guess < st.session_state.number: | |
| st.info("Too low!") | |
| else: | |
| st.info("Too high!") | |
| def word_scramble(): | |
| st.header("Word Scramble") | |
| words = ["streamlit", "arcade", "python", "huggingface", "interface"] | |
| if "scramble_word" not in st.session_state: | |
| word = random.choice(words) | |
| st.session_state.scramble_word = word | |
| st.session_state.scrambled = ''.join(random.sample(word, len(word))) | |
| st.write(f"Unscramble this word: **{st.session_state.scrambled}**") | |
| user_input = st.text_input("Your answer:") | |
| if st.button("Check Word"): | |
| if user_input.lower() == st.session_state.scramble_word: | |
| st.success("Correct!") | |
| st.session_state.score += 1 | |
| del st.session_state.scramble_word | |
| del st.session_state.scrambled | |
| else: | |
| st.error("Wrong guess!") | |
| def emoji_quiz(): | |
| st.header("Emoji Quiz") | |
| emojis = { | |
| "๐๐ข": "ninjaturtle", | |
| "โ๏ธโ": "frozen", | |
| "๐๐": "moonlanding", | |
| "๐ฆ๐ฆธโโ๏ธ": "batman", | |
| "๐๐ป": "python" | |
| } | |
| question, answer = random.choice(list(emojis.items())) | |
| st.write(f"Guess the word/phrase: {question}") | |
| user_ans = st.text_input("Your answer:") | |
| if st.button("Submit"): | |
| if user_ans.lower().replace(" ", "") == answer: | |
| st.success("Correct!") | |
| st.session_state.score += 1 | |
| else: | |
| st.error("Nope, try again!") | |
| def math_challenge(): | |
| st.header("Math Challenge") | |
| a = random.randint(1, 20) | |
| b = random.randint(1, 20) | |
| op = random.choice(["+", "-", "*"]) | |
| correct = eval(f"{a}{op}{b}") | |
| st.write(f"Solve: **{a} {op} {b} = ?**") | |
| ans = st.number_input("Your answer:", step=1) | |
| if st.button("Check Answer"): | |
| if ans == correct: | |
| st.success("Correct!") | |
| st.session_state.score += 1 | |
| else: | |
| st.error(f"Wrong! Correct answer was {correct}") | |
| def hangman(): | |
| st.header("Hangman ๐ ") | |
| st.info("Coming soon! Classic hangman challenge.") | |
| def flag_guess(): | |
| st.header("Flag Guess ๐") | |
| st.info("Guess the country based on the flag! (images can be added)") | |
| def capital_quiz(): | |
| st.header("Capital Quiz ๐๏ธ") | |
| st.info("What is the capital of France? (example: Paris)") | |
| def riddle_me(): | |
| st.header("Riddle Me ๐ง ") | |
| st.info("I speak without a mouth and hear without ears. What am I?") | |
| def color_match(): | |
| st.header("Color Match ๐จ") | |
| st.info("Match the color name to the correct swatch!") | |
| def quick_reaction(): | |
| st.header("Quick Reaction โฑ๏ธ") | |
| st.info("Click the button as fast as you can when it turns green!") | |
| def memory_match(): | |
| st.header("Memory Match ๐งฉ") | |
| st.info("Remember the sequence of emojis!") | |
| def odd_one_out(): | |
| st.header("Odd One Out ๐ค") | |
| st.info("Which item doesn't belong: Apple, Banana, Carrot?") | |
| def animal_sound_guess(): | |
| st.header("Animal Sound Guess ๐พ") | |
| st.info("Guess the animal from the sound (playable audio coming soon).") | |
| def typing_speed_test(): | |
| st.header("Typing Speed Test โจ๏ธ") | |
| st.info("Type the following sentence as fast as possible!") | |
| # ---------- Run Selected Game ---------- | |
| if selected_game == "rps": | |
| rock_paper_scissors() | |
| elif selected_game == "guess": | |
| guess_the_number() | |
| elif selected_game == "scramble": | |
| word_scramble() | |
| elif selected_game == "emoji": | |
| emoji_quiz() | |
| elif selected_game == "math": | |
| math_challenge() | |
| elif selected_game == "hangman": | |
| hangman() | |
| elif selected_game == "flag": | |
| flag_guess() | |
| elif selected_game == "capital": | |
| capital_quiz() | |
| elif selected_game == "riddle": | |
| riddle_me() | |
| elif selected_game == "color": | |
| color_match() | |
| elif selected_game == "reaction": | |
| quick_reaction() | |
| elif selected_game == "memory": | |
| memory_match() | |
| elif selected_game == "odd": | |
| odd_one_out() | |
| elif selected_game == "sound": | |
| animal_sound_guess() | |
| elif selected_game == "typing": | |
| typing_speed_test() | |
| # ---------- Footer ---------- | |
| st.markdown("---") | |
| st.markdown("Made with โค๏ธ using Streamlit. Add more games to keep it fun!") | |