hareshchander commited on
Commit
02a63aa
ยท
verified ยท
1 Parent(s): 66454b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -23
app.py CHANGED
@@ -1,56 +1,85 @@
1
  import streamlit as st
2
  import random
3
 
4
- # Title of the app
5
- st.title("Rock-Paper-Scissors Game with Points")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 game
11
- choices = ["Rock", "Paper", "Scissors"]
12
 
13
- # Initialize or retrieve scores using Streamlit session state
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 current scores
20
- st.write(f"**Your Score:** {st.session_state.user_score}")
21
- st.write(f"**Computer Score:** {st.session_state.computer_score}")
22
 
23
- # User's choice
24
  user_choice = st.selectbox("Make your choice:", choices)
25
 
26
- # Button to submit the choice
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 user_choice == computer_choice:
37
- st.info("It's a tie!")
38
  elif (
39
- (user_choice == "Rock" and computer_choice == "Scissors") or
40
- (user_choice == "Paper" and computer_choice == "Rock") or
41
- (user_choice == "Scissors" and computer_choice == "Paper")
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 button to restart the game
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 an image for fun (optional)
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)