import streamlit as st # Constants MAX_Y = 280 MIN_Y = -280 INITIAL_SPEED = 0.09 SPEED_DECREMENT = 0.009 MAX_X = 400 MIN_X = -400 PADDLE_DISTANCE = 50 SCORE_LIMIT = 5 # Initialize session state if 'ball_position' not in st.session_state: st.session_state.ball_position = {'x': 0, 'y': 0} if 'score_p1' not in st.session_state: st.session_state.score_p1 = 0 if 'score_p2' not in st.session_state: st.session_state.score_p2 = 0 if 'game_speed' not in st.session_state: st.session_state.game_speed = INITIAL_SPEED if 'game_on' not in st.session_state: st.session_state.game_on = True # Function to move the ball and update the game state def move_ball(): if st.session_state.game_on: # Ball movement logic st.session_state.ball_position['y'] += 10 # Example movement # Check for collisions and scoring if st.session_state.ball_position['y'] > MAX_Y or st.session_state.ball_position['y'] < MIN_Y: # Ball bounces on y-axis # Your bounce logic here # Update scores and reset ball if needed if st.session_state.ball_position['x'] > MAX_X: st.session_state.score_p2 += 1 st.session_state.ball_position = {'x': 0, 'y': 0} # Reset ball position if st.session_state.ball_position['x'] < MIN_X: st.session_state.score_p1 += 1 st.session_state.ball_position = {'x': 0, 'y': 0} # Reset ball position # End game if a player reaches the score limit if st.session_state.score_p1 == SCORE_LIMIT or st.session_state.score_p2 == SCORE_LIMIT: st.session_state.game_on = False # Display end game message st.write("Game Over!") # Display the ball's position and the score st.write(f"Ball position: {st.session_state.ball_position}") st.write(f"Score - Player 1: {st.session_state.score_p1}, Player 2: {st.session_state.score_p2}") # Button to move the ball if st.button("Move Ball"): move_ball()