|
import streamlit as st |
|
import time |
|
|
|
|
|
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 |
|
|
|
|
|
def streamlit_interface(): |
|
st.title("Racing Game") |
|
|
|
st.write("Control the red car using arrow buttons.") |
|
|
|
|
|
move_left = st.button("Move Left") |
|
move_right = st.button("Move Right") |
|
|
|
|
|
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 |
|
|
|
|
|
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) |
|
|
|
|
|
if __name__ == "__main__": |
|
streamlit_interface() |
|
|