Create a Streamlit app which contain a game, the game should be a flappy bird clone
Drop files here
or click to upload
import streamlit as st
import pygame
import random
import sys
# Initialize Pygame
pygame.init()
# Game settings
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 600
FLOOR_HEIGHT = 100
FPS = 60
# Colors
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# Load assets
BIRD_IMG = pygame.image.load("bird.png")
PIPE_IMG = pygame.image.load("pipe.png")
# Game class
class Game:
def __init__(self):
self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
self.clock = pygame.time.Clock()
self.bird_y = SCREEN_HEIGHT // 2
self.bird_y_velocity = 0
self.pipes = []
self.score = 0
self.running = True
def draw_bird(self):
self.screen.blit(BIRD_IMG, (50, self.bird_y))
def create_pipe(self):
height = random.randint(150, 450)
top_pipe = PIPE_IMG.get_rect(midbottom=(SCREEN_WIDTH, height - 150))
bottom_pipe = PIPE_IMG.get_rect(midtop=(SCREEN_WIDTH, height))
return top_pipe, bottom_pipe
def move_pipes(self):
for pipe in self.pipes:
pipe.centerx -= 5
def remove_offscreen_pipes(self):
self.pipes = [pipe for pipe in self.pipes if pipe.centerx > 0]
def check_collision(self):
for pipe in self.pipes:
if pipe.colliderect((50, self.bird_y, BIRD_IMG.get_width(), BIRD_IMG.get_height())):
self.running = False
if self.bird_y > SCREEN_HEIGHT - FLOOR_HEIGHT or self.bird_y < 0:
self.running = False
def run(self):
while self.running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.bird_y_velocity = -10
self.bird_y_velocity += 1
self.bird_y += self.bird_y_velocity
if len(self.pipes) == 0 or self.pipes[-1].centerx < SCREEN_WIDTH - 200:
self.pipes.extend(self.create_pipe())
self.screen.fill(WHITE)
self.move_pipes()
self.remove_offscreen_pipes()
self.draw_bird()
for pipe in self.pipes:
self.screen.blit(PIPE_IMG, pipe)
self.check_collision()
pygame.display.flip()
self.clock.tick(FPS)
# Main function
if __name__ == '__main__':
game = Game()
game.run()
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?