flappy bird
Drop files here
or click to upload
import streamlit as st
import pygame
import numpy as np
import time
# Initialize Streamlit
st.set_page_config(page_title="Flappy Bird")
st.title("Flappy Bird")
# Game constants
BIRD_SIZE = 30
PIPE_WIDTH = 50
GAP_SIZE = 150
GRAVITY = 0.5
JUMP_VELOCITY = -8
PIPE_SPEED = 3
# Initialize game state in session state
if 'game_state' not in st.session_state:
st.session_state.game_state = {
'bird_y': 300,
'bird_velocity': 0,
'pipes': [],
'score': 0,
'game_over': False,
'last_update': time.time()
}
# Game canvas
canvas = st.empty()
# Game controls
col1, col2, col3 = st.columns(3)
with col1:
if st.button('Jump', key='jump'):
if not st.session_state.game_state['game_over']:
st.session_state.game_state['bird_velocity'] = JUMP_VELOCITY
with col2:
if st.button('Reset Game', key='reset'):
st.session_state.game_state = {
'bird_y': 300,
'bird_velocity': 0,
'pipes': [],
'score': 0,
'game_over': False,
'last_update': time.time()
}
with col3:
st.write(f"Score: {st.session_state.game_state['score']}")
# Game loop
def update_game_state():
state = st.session_state.game_state
current_time = time.time()
dt = current_time - state['last_update']
state['last_update'] = current_time
if not state['game_over']:
# Update bird position
state['bird_velocity'] += GRAVITY
state['bird_y'] += state['bird_velocity']
# Generate pipes
if len(state['pipes']) == 0 or state['pipes'][-1][0] < 500:
gap_y = np.random.randint(100, 500)
state['pipes'].append([800, gap_y])
# Update pipes
for pipe in state['pipes']:
pipe[0] -= PIPE_SPEED
# Remove off-screen pipes
state['pipes'] = [pipe for pipe in state['pipes'] if pipe[0] > -PIPE_WIDTH]
# Check collisions
bird_rect = pygame.Rect(100, state['bird_y'], BIRD_SIZE, BIRD_SIZE)
# Check if bird hits the ground or ceiling
if state['bird_y'] < 0 or state['bird_y'] > 600:
state['game_over'] = True
# Check pipe collisions
for pipe in state['pipes']:
upper_pipe = pygame.Rect(pipe[0], 0, PIPE_WIDTH, pipe[1])
lower_pipe = pygame.Rect(pipe[0], pipe[1] + GAP_SIZE,
PIPE_WIDTH, 600 - (pipe[1] + GAP_SIZE))
if bird_rect.colliderect(upper_pipe) or bird_rect.colliderect(lower_pipe):
state['game_over'] = True
# Update score
for pipe in state['pipes']:
if 98 <= pipe[0] <= 102:
state['score'] += 1
# Game rendering
def render_game():
state = st.session_state.game_state
# Create game display using HTML canvas
html = f'''
<div style="width: 800px; height: 600px; background: skyblue; position: relative;">
<!-- Bird -->
<div style="
width: {BIRD_SIZE}px;
height: {BIRD_SIZE}px;
background: yellow;
position: absolute;
left: 100px;
top: {state['bird_y']}px;
border-radius: 50%;
"></div>
'''
# Add pipes
for pipe in state['pipes']:
# Upper pipe
html += f'''
<div style="
width: {PIPE_WIDTH}px;
height: {pipe[1]}px;
background: green;
position: absolute;
left: {pipe[0]}px;
top: 0;
"></div>
'''
# Lower pipe
html += f'''
<div style="
width: {PIPE_WIDTH}px;
height: {600 - (pipe[1] + GAP_SIZE)}px;
background: green;
position: absolute;
left: {pipe[0]}px;
top: {pipe[1] + GAP_SIZE}px;
"></div>
'''
html += '</div>'
# Display game over message
if state['game_over']:
html += '<h2 style="color: red; text-align: center;">Game Over!</h2>'
return html
# Main game loop
update_game_state()
canvas.markdown(render_game(), unsafe_allow_html=True)
# Auto-rerun to update game state
time.sleep(0.05)
st.rerun()
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?