Spaces:
Sleeping
Sleeping
App.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import random
|
| 3 |
+
|
| 4 |
+
# Game setup
|
| 5 |
+
st.title("Fighting Game")
|
| 6 |
+
|
| 7 |
+
# Character stats
|
| 8 |
+
player = {"name": "Player", "health": 100, "attack_range": (10, 20)}
|
| 9 |
+
enemy = {"name": "Enemy", "health": 100, "attack_range": (8, 15)}
|
| 10 |
+
|
| 11 |
+
# Game state
|
| 12 |
+
if "game_over" not in st.session_state:
|
| 13 |
+
st.session_state.game_over = False
|
| 14 |
+
if "player_health" not in st.session_state:
|
| 15 |
+
st.session_state.player_health = player["health"]
|
| 16 |
+
if "enemy_health" not in st.session_state:
|
| 17 |
+
st.session_state.enemy_health = enemy["health"]
|
| 18 |
+
|
| 19 |
+
# Functions
|
| 20 |
+
def attack(attacker, defender, defender_health):
|
| 21 |
+
damage = random.randint(*attacker["attack_range"])
|
| 22 |
+
defender_health -= damage
|
| 23 |
+
return damage, defender_health
|
| 24 |
+
|
| 25 |
+
# Game loop
|
| 26 |
+
if not st.session_state.game_over:
|
| 27 |
+
st.subheader(f"Player Health: {st.session_state.player_health} | Enemy Health: {st.session_state.enemy_health}")
|
| 28 |
+
|
| 29 |
+
# Player's attack
|
| 30 |
+
if st.button("Attack Enemy"):
|
| 31 |
+
damage, st.session_state.enemy_health = attack(player, enemy, st.session_state.enemy_health)
|
| 32 |
+
st.write(f"You attacked the Enemy for {damage} damage!")
|
| 33 |
+
|
| 34 |
+
# Enemy retaliates if alive
|
| 35 |
+
if st.session_state.enemy_health > 0:
|
| 36 |
+
damage, st.session_state.player_health = attack(enemy, player, st.session_state.player_health)
|
| 37 |
+
st.write(f"The Enemy attacked you for {damage} damage!")
|
| 38 |
+
|
| 39 |
+
# Check if game is over
|
| 40 |
+
if st.session_state.enemy_health <= 0:
|
| 41 |
+
st.success("You defeated the Enemy!")
|
| 42 |
+
st.session_state.game_over = True
|
| 43 |
+
elif st.session_state.player_health <= 0:
|
| 44 |
+
st.error("You were defeated by the Enemy!")
|
| 45 |
+
st.session_state.game_over = True
|
| 46 |
+
|
| 47 |
+
else:
|
| 48 |
+
if st.button("Restart Game"):
|
| 49 |
+
st.session_state.game_over = False
|
| 50 |
+
st.session_state.player_health = player["health"]
|
| 51 |
+
st.session_state.enemy_health = enemy["health"]
|
| 52 |
+
st.experimental_rerun()
|