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