Upload 2 files
Browse files- app.py +81 -0
- requirements.txt +1 -0
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
|
| 4 |
+
# 🎮 Game logic function
|
| 5 |
+
def play_rps(user_choice, player_score, computer_score, theme):
|
| 6 |
+
choices = ["rock", "paper", "scissors"]
|
| 7 |
+
computer_choice = random.choice(choices)
|
| 8 |
+
|
| 9 |
+
# Determine result
|
| 10 |
+
if user_choice == computer_choice:
|
| 11 |
+
result = "It's a tie!"
|
| 12 |
+
sound = "https://www.soundjay.com/button/beep-05.mp3"
|
| 13 |
+
elif (user_choice == "rock" and computer_choice == "scissors") or \
|
| 14 |
+
(user_choice == "paper" and computer_choice == "rock") or \
|
| 15 |
+
(user_choice == "scissors" and computer_choice == "paper"):
|
| 16 |
+
result = "You Win!"
|
| 17 |
+
player_score += 1
|
| 18 |
+
sound = "https://www.soundjay.com/button/beep-07.mp3"
|
| 19 |
+
else:
|
| 20 |
+
result = "You Lose!"
|
| 21 |
+
computer_score += 1
|
| 22 |
+
sound = "https://www.soundjay.com/button/beep-10.mp3"
|
| 23 |
+
|
| 24 |
+
# Return updated outputs
|
| 25 |
+
return (
|
| 26 |
+
f"🧑 You chose: {user_choice}\n💻 Computer chose: {computer_choice}\n🎯 Result: {result}",
|
| 27 |
+
player_score,
|
| 28 |
+
computer_score,
|
| 29 |
+
gr.Audio.update(value=sound),
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# 🌙 Toggle theme
|
| 33 |
+
def toggle_theme(theme):
|
| 34 |
+
return "light" if theme == "dark" else "dark"
|
| 35 |
+
|
| 36 |
+
# 🎨 CSS for background image and styling
|
| 37 |
+
custom_css = """
|
| 38 |
+
body {
|
| 39 |
+
background: url('https://img.freepik.com/free-vector/children-playing-rock-paper-scissors_1308-33220.jpg') no-repeat center center fixed;
|
| 40 |
+
background-size: cover;
|
| 41 |
+
}
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
# 🖥️ Gradio Blocks UI
|
| 45 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 46 |
+
gr.Markdown("<h1 style='text-align:center;'>✊ ✋ ✌️ Rock Paper Scissors Game</h1>")
|
| 47 |
+
|
| 48 |
+
theme_state = gr.State("dark")
|
| 49 |
+
player_score = gr.State(0)
|
| 50 |
+
computer_score = gr.State(0)
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
with gr.Column():
|
| 54 |
+
choice = gr.Radio(["rock", "paper", "scissors"], label="Choose Rock, Paper, or Scissors")
|
| 55 |
+
submit_btn = gr.Button("Submit")
|
| 56 |
+
toggle_btn = gr.Button("Toggle Theme")
|
| 57 |
+
|
| 58 |
+
gr.Markdown("#### Scores")
|
| 59 |
+
score_display = gr.Markdown("Player: 0 | Computer: 0")
|
| 60 |
+
with gr.Column():
|
| 61 |
+
output = gr.Markdown()
|
| 62 |
+
sound_output = gr.Audio(interactive=False)
|
| 63 |
+
|
| 64 |
+
# 📝 Button functions
|
| 65 |
+
submit_btn.click(
|
| 66 |
+
play_rps,
|
| 67 |
+
inputs=[choice, player_score, computer_score, theme_state],
|
| 68 |
+
outputs=[output, player_score, computer_score, sound_output]
|
| 69 |
+
).then(
|
| 70 |
+
lambda p, c: f"Player: {p} | Computer: {c}",
|
| 71 |
+
inputs=[player_score, computer_score],
|
| 72 |
+
outputs=[score_display]
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
toggle_btn.click(
|
| 76 |
+
toggle_theme,
|
| 77 |
+
inputs=[theme_state],
|
| 78 |
+
outputs=[theme_state]
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
gradio
|