Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,85 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import random
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# Instructions
|
| 8 |
st.write("Choose Rock, Paper, or Scissors and play against the computer! Each win earns you a point.")
|
| 9 |
|
| 10 |
-
# Options for the
|
| 11 |
-
choices = ["Rock", "Paper", "Scissors"]
|
| 12 |
|
| 13 |
-
# Initialize or
|
| 14 |
if 'user_score' not in st.session_state:
|
| 15 |
st.session_state.user_score = 0
|
| 16 |
if 'computer_score' not in st.session_state:
|
| 17 |
st.session_state.computer_score = 0
|
| 18 |
|
| 19 |
-
# Display
|
| 20 |
-
st.
|
| 21 |
-
st.
|
| 22 |
|
| 23 |
-
# User's
|
| 24 |
user_choice = st.selectbox("Make your choice:", choices)
|
| 25 |
|
| 26 |
-
# Button to
|
| 27 |
-
if st.button("Play"):
|
|
|
|
|
|
|
|
|
|
| 28 |
# Computer's random choice
|
| 29 |
computer_choice = random.choice(choices)
|
|
|
|
| 30 |
|
| 31 |
# Display both choices
|
| 32 |
st.write(f"**You chose:** {user_choice}")
|
| 33 |
st.write(f"**Computer chose:** {computer_choice}")
|
| 34 |
|
| 35 |
# Determine the winner and update scores
|
| 36 |
-
if
|
| 37 |
-
st.info("It's a tie!")
|
| 38 |
elif (
|
| 39 |
-
(
|
| 40 |
-
(
|
| 41 |
-
(
|
| 42 |
):
|
| 43 |
-
st.success("You win!
|
| 44 |
st.session_state.user_score += 1
|
| 45 |
else:
|
| 46 |
-
st.error("You lose!
|
| 47 |
st.session_state.computer_score += 1
|
| 48 |
|
| 49 |
-
# Reset
|
| 50 |
-
if st.button("Reset Scores"):
|
| 51 |
st.session_state.user_score = 0
|
| 52 |
st.session_state.computer_score = 0
|
| 53 |
-
st.success("Scores have been reset!")
|
| 54 |
|
| 55 |
-
# Add
|
| 56 |
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)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import random
|
| 3 |
|
| 4 |
+
# --- Custom CSS for Colorful Background ---
|
| 5 |
+
st.markdown(
|
| 6 |
+
"""
|
| 7 |
+
<style>
|
| 8 |
+
body {
|
| 9 |
+
background: linear-gradient(135deg, #FFDEE9 0%, #B5FFFC 100%);
|
| 10 |
+
color: #000000;
|
| 11 |
+
}
|
| 12 |
+
.stApp {
|
| 13 |
+
background: linear-gradient(135deg, #FFDEE9 0%, #B5FFFC 100%);
|
| 14 |
+
}
|
| 15 |
+
.stButton>button {
|
| 16 |
+
background-color: #FF7F50;
|
| 17 |
+
color: white;
|
| 18 |
+
font-size: 16px;
|
| 19 |
+
border-radius: 10px;
|
| 20 |
+
}
|
| 21 |
+
.stButton>button:hover {
|
| 22 |
+
background-color: #FF4500;
|
| 23 |
+
color: white;
|
| 24 |
+
}
|
| 25 |
+
</style>
|
| 26 |
+
""",
|
| 27 |
+
unsafe_allow_html=True
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# --- Title and Instructions ---
|
| 31 |
+
st.title("๐ชจโ๏ธ๐ Rock-Paper-Scissors Game with Points")
|
| 32 |
|
|
|
|
| 33 |
st.write("Choose Rock, Paper, or Scissors and play against the computer! Each win earns you a point.")
|
| 34 |
|
| 35 |
+
# --- Options for the Game ---
|
| 36 |
+
choices = ["๐ชจ Rock", "๐ Paper", "โ๏ธ Scissors"]
|
| 37 |
|
| 38 |
+
# --- Initialize or Retrieve Scores ---
|
| 39 |
if 'user_score' not in st.session_state:
|
| 40 |
st.session_state.user_score = 0
|
| 41 |
if 'computer_score' not in st.session_state:
|
| 42 |
st.session_state.computer_score = 0
|
| 43 |
|
| 44 |
+
# --- Display Current Scores ---
|
| 45 |
+
st.markdown(f"### **Your Score:** ๐ฏ {st.session_state.user_score}")
|
| 46 |
+
st.markdown(f"### **Computer Score:** ๐ค {st.session_state.computer_score}")
|
| 47 |
|
| 48 |
+
# --- User's Choice ---
|
| 49 |
user_choice = st.selectbox("Make your choice:", choices)
|
| 50 |
|
| 51 |
+
# --- Button to Submit the Choice ---
|
| 52 |
+
if st.button("๐ฎ Play"):
|
| 53 |
+
# Extract the actual choice (remove the emoji)
|
| 54 |
+
user_choice_text = user_choice.split()[1]
|
| 55 |
+
|
| 56 |
# Computer's random choice
|
| 57 |
computer_choice = random.choice(choices)
|
| 58 |
+
computer_choice_text = computer_choice.split()[1]
|
| 59 |
|
| 60 |
# Display both choices
|
| 61 |
st.write(f"**You chose:** {user_choice}")
|
| 62 |
st.write(f"**Computer chose:** {computer_choice}")
|
| 63 |
|
| 64 |
# Determine the winner and update scores
|
| 65 |
+
if user_choice_text == computer_choice_text:
|
| 66 |
+
st.info("๐ค It's a tie!")
|
| 67 |
elif (
|
| 68 |
+
(user_choice_text == "Rock" and computer_choice_text == "Scissors") or
|
| 69 |
+
(user_choice_text == "Paper" and computer_choice_text == "Rock") or
|
| 70 |
+
(user_choice_text == "Scissors" and computer_choice_text == "Paper")
|
| 71 |
):
|
| 72 |
+
st.success("๐ You win!")
|
| 73 |
st.session_state.user_score += 1
|
| 74 |
else:
|
| 75 |
+
st.error("๐ข You lose!")
|
| 76 |
st.session_state.computer_score += 1
|
| 77 |
|
| 78 |
+
# --- Reset Button to Restart the Game ---
|
| 79 |
+
if st.button("๐ Reset Scores"):
|
| 80 |
st.session_state.user_score = 0
|
| 81 |
st.session_state.computer_score = 0
|
| 82 |
+
st.success("โ
Scores have been reset!")
|
| 83 |
|
| 84 |
+
# --- Add Image for Fun ---
|
| 85 |
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)
|