abcd / game
chmawia's picture
Create game
40e22e0 verified
import streamlit as st
import random
# Game Settings
MAP_SIZE = 20 # 20 km x 20 km map
# Initialize session state
if "player_position" not in st.session_state:
st.session_state.player_position = [10, 10] # Start in the center
if "player_health" not in st.session_state:
st.session_state.player_health = 100
if "enemy_health" not in st.session_state:
st.session_state.enemy_health = random.randint(50, 100)
if "inventory" not in st.session_state:
st.session_state.inventory = {"vehicles": "Car", "weapons": ["UMP"]}
# Helper functions
def move_player(direction):
if direction == "North" and st.session_state.player_position[1] < MAP_SIZE:
st.session_state.player_position[1] += 1
elif direction == "South" and st.session_state.player_position[1] > 0:
st.session_state.player_position[1] -= 1
elif direction == "East" and st.session_state.player_position[0] < MAP_SIZE:
st.session_state.player_position[0] += 1
elif direction == "West" and st.session_state.player_position[0] > 0:
st.session_state.player_position[0] -= 1
def fight_enemy():
weapon = st.session_state.inventory["weapons"][0] # Use the first weapon
damage = random.randint(10, 30)
st.session_state.enemy_health -= damage
st.write(f"You used {weapon} and dealt {damage} damage!")
# Enemy counterattack
if st.session_state.enemy_health > 0:
enemy_damage = random.randint(5, 20)
st.session_state.player_health -= enemy_damage
st.write(f"The enemy attacked and dealt {enemy_damage} damage!")
else:
st.write("Enemy defeated!")
def explore():
event = random.choice(["Nothing found", "Found ammo", "Enemy encountered"])
st.write(f"Exploring... {event}")
if event == "Found ammo":
st.session_state.inventory["weapons"].append(random.choice(["AK-47", "Pistol"]))
st.write("You found a new weapon!")
elif event == "Enemy encountered":
st.session_state.enemy_health = random.randint(50, 100)
st.write("An enemy appeared!")
# Game Interface
st.title("Adventure Game")
# Display Player Stats
st.subheader("Player Stats")
st.write(f"Health: {st.session_state.player_health}")
st.write(f"Position: {st.session_state.player_position}")
st.write(f"Inventory: {st.session_state.inventory}")
# Display Enemy Stats (if any)
if st.session_state.enemy_health > 0:
st.subheader("Enemy Stats")
st.write(f"Enemy Health: {st.session_state.enemy_health}")
# Game Actions
st.subheader("Actions")
# Movement
st.write("Move:")
col1, col2, col3 = st.columns(3)
with col1:
if st.button("North"):
move_player("North")
with col2:
if st.button("South"):
move_player("South")
with col3:
if st.button("East"):
move_player("East")
if st.button("West"):
move_player("West")
# Fight
if st.session_state.enemy_health > 0:
if st.button("Fight"):
fight_enemy()
# Explore
if st.button("Explore"):
explore()
# End Game if Health <= 0
if st.session_state.player_health <= 0:
st.write("Game Over! You have been defeated.")
st.stop()
st.write("Keep playing and exploring the map!")