| import streamlit as st |
|
|
| |
| 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 |
|
|
| |
| 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 |
|
|
| |
| def move_ball(): |
| if st.session_state.game_on: |
| |
| st.session_state.ball_position['y'] += 10 |
|
|
| |
| if st.session_state.ball_position['y'] > MAX_Y or st.session_state.ball_position['y'] < MIN_Y: |
| |
| |
|
|
| |
| if st.session_state.ball_position['x'] > MAX_X: |
| st.session_state.score_p2 += 1 |
| st.session_state.ball_position = {'x': 0, 'y': 0} |
|
|
| if st.session_state.ball_position['x'] < MIN_X: |
| st.session_state.score_p1 += 1 |
| st.session_state.ball_position = {'x': 0, 'y': 0} |
|
|
| |
| if st.session_state.score_p1 == SCORE_LIMIT or st.session_state.score_p2 == SCORE_LIMIT: |
| st.session_state.game_on = False |
| |
| st.write("Game Over!") |
|
|
| |
| 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}") |
|
|
| |
| if st.button("Move Ball"): |
| move_ball() |