Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,38 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
|
| 3 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import gym
|
| 3 |
+
import gym_super_mario_bros
|
| 4 |
+
import numpy as np
|
| 5 |
+
import cv2
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
# Initialize the environment
|
| 9 |
+
env = gym.make('SuperMarioBros-v0')
|
| 10 |
+
env.reset()
|
| 11 |
+
|
| 12 |
+
# Create a Streamlit slider for controlling Mario's actions
|
| 13 |
+
st.title("Super Mario Bros. Game!")
|
| 14 |
+
st.write("Use the arrow keys to control Mario!")
|
| 15 |
+
action = st.slider("Move Mario", 0, 3, 1, step=1)
|
| 16 |
+
|
| 17 |
+
# Function to convert the game frames to images
|
| 18 |
+
def convert_frame_to_image(frame):
|
| 19 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Convert BGR to RGB
|
| 20 |
+
return Image.fromarray(frame)
|
| 21 |
|
| 22 |
+
# Run the game loop and show the game state
|
| 23 |
+
if st.button("Start Game"):
|
| 24 |
+
st.write("Game is running. Use the slider to control Mario.")
|
| 25 |
|
| 26 |
+
while True:
|
| 27 |
+
# Step the game forward using the action chosen by the slider
|
| 28 |
+
action_taken = np.array([action])
|
| 29 |
+
state, reward, done, info = env.step(action_taken)
|
| 30 |
+
|
| 31 |
+
# Show the game frame
|
| 32 |
+
frame = convert_frame_to_image(state)
|
| 33 |
+
st.image(frame, use_column_width=True)
|
| 34 |
|
| 35 |
+
# If the game is over, reset
|
| 36 |
+
if done:
|
| 37 |
+
st.write("Game Over! Resetting...")
|
| 38 |
+
env.reset()
|