hareshchander's picture
Update app.py
02a63aa verified
Raw History Blame Contribute Delete
2.8 kB
import streamlit as st
import random
# --- Custom CSS for Colorful Background ---
st.markdown(
"""
<style>
body {
background: linear-gradient(135deg, #FFDEE9 0%, #B5FFFC 100%);
color: #000000;
}
.stApp {
background: linear-gradient(135deg, #FFDEE9 0%, #B5FFFC 100%);
}
.stButton>button {
background-color: #FF7F50;
color: white;
font-size: 16px;
border-radius: 10px;
}
.stButton>button:hover {
background-color: #FF4500;
color: white;
}
</style>
""",
unsafe_allow_html=True
)
# --- Title and Instructions ---
st.title("๐Ÿชจโœ‚๏ธ๐Ÿ“„ Rock-Paper-Scissors Game with Points")
st.write("Choose Rock, Paper, or Scissors and play against the computer! Each win earns you a point.")
# --- Options for the Game ---
choices = ["๐Ÿชจ Rock", "๐Ÿ“„ Paper", "โœ‚๏ธ Scissors"]
# --- Initialize or Retrieve Scores ---
if 'user_score' not in st.session_state:
st.session_state.user_score = 0
if 'computer_score' not in st.session_state:
st.session_state.computer_score = 0
# --- Display Current Scores ---
st.markdown(f"### **Your Score:** ๐ŸŽฏ {st.session_state.user_score}")
st.markdown(f"### **Computer Score:** ๐Ÿค– {st.session_state.computer_score}")
# --- User's Choice ---
user_choice = st.selectbox("Make your choice:", choices)
# --- Button to Submit the Choice ---
if st.button("๐ŸŽฎ Play"):
# Extract the actual choice (remove the emoji)
user_choice_text = user_choice.split()[1]
# Computer's random choice
computer_choice = random.choice(choices)
computer_choice_text = computer_choice.split()[1]
# Display both choices
st.write(f"**You chose:** {user_choice}")
st.write(f"**Computer chose:** {computer_choice}")
# Determine the winner and update scores
if user_choice_text == computer_choice_text:
st.info("๐Ÿค It's a tie!")
elif (
(user_choice_text == "Rock" and computer_choice_text == "Scissors") or
(user_choice_text == "Paper" and computer_choice_text == "Rock") or
(user_choice_text == "Scissors" and computer_choice_text == "Paper")
):
st.success("๐ŸŽ‰ You win!")
st.session_state.user_score += 1
else:
st.error("๐Ÿ˜ข You lose!")
st.session_state.computer_score += 1
# --- Reset Button to Restart the Game ---
if st.button("๐Ÿ”„ Reset Scores"):
st.session_state.user_score = 0
st.session_state.computer_score = 0
st.success("โœ… Scores have been reset!")
# --- Add Image for Fun ---
st.image("https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Rock-paper-scissors.svg/1920px-Rock-paper-scissors.svg.png", caption="Rock-Paper-Scissors", width=400)