Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import random | |
| import time | |
| # ----------------- Page Setup ----------------- | |
| st.set_page_config(page_title="๐ฎ Mini Game Arcade Pro", layout="centered", page_icon="๐ฎ") | |
| # ----------------- Styling ----------------- | |
| st.markdown(""" | |
| <style> | |
| .main {background-color: #f9f9f9;} | |
| .stButton button {background-color: #4CAF50; color: white;} | |
| </style> | |
| """, unsafe_allow_html=True) | |
| st.title("๐ฎ Mini Game Arcade Pro") | |
| st.write("Select a game and enjoy!") | |
| # ----------------- Initialize State ----------------- | |
| if "total_score" not in st.session_state: | |
| st.session_state.total_score = 0 | |
| if "leaderboard" not in st.session_state: | |
| st.session_state.leaderboard = [] | |
| games = [ | |
| "โ Rock-Paper-Scissors", | |
| "๐ข Guess the Number", | |
| "๐ค Word Scramble", | |
| "๐ Emoji Quiz", | |
| "๐ช Coin Toss", | |
| "๐ฒ Dice Roller" | |
| ] | |
| selected_game = st.selectbox("Choose a game:", games) | |
| st.progress(st.session_state.total_score % 10 / 10) | |
| st.metric(label="Total Score", value=st.session_state.total_score) | |
| # ----------------- Game Logic ----------------- | |
| st.divider() | |
| # --- Rock-Paper-Scissors --- | |
| if selected_game == "โ Rock-Paper-Scissors": | |
| with st.expander("โน๏ธ How to Play"): | |
| st.write("Choose Rock, Paper or Scissors. Try to beat the computer!") | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| choice = st.radio("Your move:", ["Rock", "Paper", "Scissors"], key="rps_choice") | |
| with col2: | |
| if st.button("Play", key="rps_play"): | |
| computer = random.choice(["Rock", "Paper", "Scissors"]) | |
| st.write(f"Computer chose: **{computer}**") | |
| if choice == computer: | |
| st.info("It's a tie!") | |
| elif (choice == "Rock" and computer == "Scissors") or (choice == "Paper" and computer == "Rock") or (choice == "Scissors" and computer == "Paper"): | |
| st.success("You win!") | |
| st.session_state.total_score += 1 | |
| else: | |
| st.error("Computer wins!") | |
| # --- Guess the Number --- | |
| elif selected_game == "๐ข Guess the Number": | |
| with st.expander("โน๏ธ How to Play"): | |
| st.write("Guess the number between 1 and 20.") | |
| if "guess_target" not in st.session_state: | |
| st.session_state.guess_target = random.randint(1, 20) | |
| guess = st.number_input("Your guess:", 1, 20, step=1, key="guess_input") | |
| if st.button("Check", key="guess_check"): | |
| if guess == st.session_state.guess_target: | |
| st.success("Correct! ๐") | |
| st.session_state.total_score += 1 | |
| st.session_state.guess_target = random.randint(1, 20) | |
| elif guess < st.session_state.guess_target: | |
| st.info("Try higher.") | |
| else: | |
| st.info("Try lower.") | |
| # --- Word Scramble --- | |
| elif selected_game == "๐ค Word Scramble": | |
| with st.expander("โน๏ธ How to Play"): | |
| st.write("Unscramble the word!") | |
| words = ["streamlit", "python", "arcade"] | |
| # Correct initialization | |
| if "scramble_word" not in st.session_state or "scrambled" not in st.session_state: | |
| st.session_state.scramble_word = random.choice(words) | |
| st.session_state.scrambled = "".join(random.sample(st.session_state.scramble_word, len(st.session_state.scramble_word))) | |
| st.write(f"Scrambled word: **{st.session_state.scrambled}**") | |
| guess = st.text_input("Your guess:", key="scramble_guess") | |
| if st.button("Submit", key="scramble_submit"): | |
| if guess.lower() == st.session_state.scramble_word: | |
| st.success("Correct!") | |
| st.session_state.total_score += 1 | |
| del st.session_state.scramble_word | |
| del st.session_state.scrambled | |
| else: | |
| st.error("Try again.") | |
| # --- Emoji Quiz --- | |
| elif selected_game == "๐ Emoji Quiz": | |
| with st.expander("โน๏ธ How to Play"): | |
| st.write("Guess what the emoji represents.") | |
| emojis = {"๐": "Pizza", "๐": "Snake", "๐": "Car"} | |
| # Correct initialization only once | |
| if "emoji" not in st.session_state or "emoji_answer" not in st.session_state: | |
| st.session_state.emoji, st.session_state.emoji_answer = random.choice(list(emojis.items())) | |
| options = list(emojis.values()) | |
| guess = st.radio(f"What does this emoji mean? {st.session_state.emoji}", options, key="emoji_guess") | |
| if st.button("Submit", key="emoji_submit"): | |
| if guess == st.session_state.emoji_answer: | |
| st.success("Correct!") | |
| st.session_state.total_score += 1 | |
| # Reset for next round | |
| del st.session_state.emoji | |
| del st.session_state.emoji_answer | |
| else: | |
| st.error(f"Wrong! It was {st.session_state.emoji_answer}.") | |
| # --- Coin Toss --- | |
| elif selected_game == "๐ช Coin Toss": | |
| with st.expander("โน๏ธ How to Play"): | |
| st.write("Guess Heads or Tails.") | |
| guess = st.radio("Heads or Tails?", ["Heads", "Tails"], key="coin_guess") | |
| if st.button("Toss Coin", key="coin_toss"): | |
| result = random.choice(["Heads", "Tails"]) | |
| st.write(f"The coin shows: **{result}**") | |
| if guess == result: | |
| st.success("Correct!") | |
| st.session_state.total_score += 1 | |
| else: | |
| st.error("Wrong guess.") | |
| # --- Dice Roller --- | |
| elif selected_game == "๐ฒ Dice Roller": | |
| with st.expander("โน๏ธ How to Play"): | |
| st.write("Roll the dice and see the result.") | |
| if st.button("Roll Dice", key="dice_roll"): | |
| roll = random.randint(1, 6) | |
| st.write(f"You rolled: **{roll}**") | |
| # ----------------- Reset & Leaderboard ----------------- | |
| st.divider() | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| if st.button("Reset Score"): | |
| st.session_state.total_score = 0 | |
| st.session_state.guess_target = random.randint(1, 20) | |
| st.session_state.scramble_word = None | |
| with col2: | |
| if st.button("Add to Leaderboard"): | |
| st.session_state.leaderboard.append(st.session_state.total_score) | |
| st.success("Added to leaderboard!") | |
| if st.session_state.leaderboard: | |
| st.write("### ๐ Leaderboard:") | |
| sorted_lb = sorted(st.session_state.leaderboard, reverse=True) | |
| for idx, score in enumerate(sorted_lb[:5], 1): | |
| st.write(f"{idx}. {score} points") | |
| # ----------------- Timer Example ----------------- | |
| # This is a simple demonstration, full countdown logic is possible with time.sleep() and st.empty() | |
| with st.expander("โฑ๏ธ Timer Example"): | |
| if st.button("Start 5-second Timer"): | |
| timer_placeholder = st.empty() | |
| for i in range(5, 0, -1): | |
| timer_placeholder.write(f"Time remaining: {i} seconds") | |
| time.sleep(1) | |
| timer_placeholder.write("Time's up!") | |