app that lets you fly
To upload files, please first save the app
import streamlit as st
import pygame
import numpy as np
from PIL import Image, ImageDraw
import io
# Initialize the game state
if 'bird_y' not in st.session_state:
st.session_state.bird_y = 300
st.session_state.velocity = 0
st.session_state.score = 0
st.session_state.game_over = False
# Constants
GRAVITY = 0.5
JUMP_STRENGTH = -8
GROUND_HEIGHT = 550
BIRD_SIZE = 30
st.title("🐦 Flying Bird Game")
# Create a sidebar with instructions and score
with st.sidebar:
st.write("## How to Play")
st.write("Press SPACE to make the bird fly!")
st.write("Avoid hitting the ground")
st.write(f"Score: {st.session_state.score}")
if st.button("Reset Game"):
st.session_state.bird_y = 300
st.session_state.velocity = 0
st.session_state.score = 0
st.session_state.game_over = False
st.rerun()
# Create a canvas for the game
def create_game_frame():
# Create a new image with a white background
img = Image.new('RGB', (800, 600), 'skyblue')
draw = ImageDraw.Draw(img)
# Draw ground
draw.rectangle([(0, GROUND_HEIGHT), (800, 600)], fill='green')
# Draw bird
bird_color = 'yellow'
if st.session_state.game_over:
bird_color = 'red'
draw.ellipse(
[(400-BIRD_SIZE/2, st.session_state.bird_y-BIRD_SIZE/2),
(400+BIRD_SIZE/2, st.session_state.bird_y+BIRD_SIZE/2)],
fill=bird_color
)
return img
# Game logic
if not st.session_state.game_over:
# Apply gravity
st.session_state.velocity += GRAVITY
st.session_state.bird_y += st.session_state.velocity
# Check for ground collision
if st.session_state.bird_y >= GROUND_HEIGHT - BIRD_SIZE/2:
st.session_state.bird_y = GROUND_HEIGHT - BIRD_SIZE/2
st.session_state.game_over = True
# Increment score if game is still running
st.session_state.score += 1
# Handle keyboard input
if st.button("JUMP!", key="jump"):
if not st.session_state.game_over:
st.session_state.velocity = JUMP_STRENGTH
st.rerun()
# Display the game
game_frame = create_game_frame()
st.image(game_frame)
if st.session_state.game_over:
st.write("### Game Over!")
st.write(f"Final Score: {st.session_state.score}")
if st.button("Play Again"):
st.session_state.bird_y = 300
st.session_state.velocity = 0
st.session_state.score = 0
st.session_state.game_over = False
st.rerun()
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?