Spaces:
Sleeping
Sleeping
File size: 6,682 Bytes
f356ae9 2c7691f f356ae9 2c7691f f356ae9 2c7691f f356ae9 2c7691f f356ae9 2c7691f f356ae9 2c7691f f356ae9 2c7691f 7311d3f 2c7691f f356ae9 2c7691f 7311d3f 2c7691f f356ae9 2c7691f 7311d3f f356ae9 7311d3f 2c7691f 70a917d 2c7691f 70a917d 2c7691f 70a917d f356ae9 2c7691f 70a917d f356ae9 70a917d f356ae9 2c7691f f356ae9 2c7691f f356ae9 2c7691f f356ae9 2c7691f f356ae9 2c7691f f356ae9 2c7691f f356ae9 2c7691f |
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 |
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!")
|