game2 / app.py
drwaseem's picture
Create app.py
95d1b0c verified
import streamlit as st
import time
# Game settings
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
CAR_WIDTH = 50
CAR_HEIGHT = 100
CAR_COLOR = "red"
car_x = SCREEN_WIDTH // 2 - CAR_WIDTH // 2
car_y = SCREEN_HEIGHT - CAR_HEIGHT - 10
# Streamlit Interface
def streamlit_interface():
st.title("Racing Game")
st.write("Control the red car using arrow buttons.")
# Create sliders for controlling the car
move_left = st.button("Move Left")
move_right = st.button("Move Right")
# Simple car movement logic
global car_x
if move_left and car_x > 0:
car_x -= 10
if move_right and car_x < SCREEN_WIDTH - CAR_WIDTH:
car_x += 10
# Drawing the car as a rectangle
st.markdown(
f"""
<style>
.game-canvas {{
width: {SCREEN_WIDTH}px;
height: {SCREEN_HEIGHT}px;
background-color: white;
position: relative;
}}
.car {{
width: {CAR_WIDTH}px;
height: {CAR_HEIGHT}px;
background-color: {CAR_COLOR};
position: absolute;
top: {car_y}px;
left: {car_x}px;
}}
</style>
<div class="game-canvas">
<div class="car"></div>
</div>
""",
unsafe_allow_html=True
)
st.write("Use the buttons to move the car.")
time.sleep(0.1) # Add a small delay for better interaction
# Run Streamlit interface
if __name__ == "__main__":
streamlit_interface()