Provide me the python code for the Flappy bird game, should be fine tuned working code
- User
Provide me the python code for the Flappy bird game, should be fine tuned working code
- Assistant
Provide me the python code for the Flappy bird game, should be fine tuned working code
Certainly! Below is a fully functional implementation of the Flappy Bird game using Python and the pygame library. This code is self-contained and can be run directly after installing the required dependencies.
Ensure you have Python 3 installed. You'll also need to install the pygame library. You can install it using pip:
pip install pygame
import pygame
import sys
import random
# Initialize pygame
pygame.init()
# Screen dimensions
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 600
GROUND_HEIGHT = 100
# Colors
WHITE = (255, 255, 255)
# Game settings
GRAVITY = 0.5
FLAP_STRENGTH = -10
PIPE_SPEED = -3
PIPE_GAP = 150 # Gap between the upper and lower pipes
# Set up the display
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Flappy Bird")
# Load images
bird_image = pygame.Surface((34, 24))
bird_image.fill((255, 255, 0)) # Yellow bird
pipe_image = pygame.Surface((52, 320))
pipe_image.fill((0, 255, 0)) # Green pipes
ground_image = pygame.Surface((SCREEN_WIDTH, GROUND_HEIGHT))
ground_image.fill((222, 161, 81)) # Brown ground
# Font for score
font = pygame.font.SysFont(None, 48)
class Bird(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = bird_image
self.rect = self.image.get_rect()
self.rect.center = (50, SCREEN_HEIGHT // 2)
self.velocity = 0
def update(self):
self.velocity += GRAVITY
self.rect.y += int(self.velocity)
# Prevent the bird from going off-screen
if self.rect.top <= 0:
self.rect.top = 0
self.velocity = 0
if self.rect.bottom >= SCREEN_HEIGHT - GROUND_HEIGHT:
self.rect.bottom = SCREEN_HEIGHT - GROUND_HEIGHT
self.velocity = 0
def flap(self):
self.velocity = FLAP_STRENGTH
class Pipe(pygame.sprite.Sprite):
def __init__(self, x, y, is_top):
super().__init__()
self.image = pipe_image
self.rect = self.image.get_rect()
if is_top:
self.image = pygame.transform.flip(self.image, False, True)
self.rect.bottomleft = (x, y - PIPE_GAP // 2)
else:
self.rect.topleft = (x, y + PIPE_GAP // 2)
def update(self):
self.rect.x += PIPE_SPEED
if self.rect.right < 0:
self.kill()
def create_pipe_pair(x):
center_y = random.randint(100, SCREEN_HEIGHT - GROUND_HEIGHT - 100)
top_pipe = Pipe(x, center_y, is_top=True)
bottom_pipe = Pipe(x, center_y, is_top=False)
return top_pipe, bottom_pipe
def main():
clock = pygame.time.Clock()
bird = Bird()
bird_group = pygame.sprite.GroupSingle(bird)
pipe_group = pygame.sprite.Group()
score = 0
running = True
pipe_timer = 0
while running:
clock.tick(60) # Limit FPS to 60
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bird.flap()
# Update bird and pipes
bird_group.update()
pipe_group.update()
# Add new pipes
pipe_timer += 1
if pipe_timer >= 100:
pipe_timer = 0
pipes = create_pipe_pair(SCREEN_WIDTH)
pipe_group.add(pipes[0])
pipe_group.add(pipes[1])
# Check collisions
if pygame.sprite.spritecollideany(bird, pipe_group):
print(f"Game Over! Your score: {score}")
running = False
# Increment score
for pipe in pipe_group:
if pipe.rect.right < bird.rect.left and not hasattr(pipe, 'scored'):
setattr(pipe, 'scored', True)
score += 0.5 # Each pipe pair counts as 1 point (0.5 for each pipe)
# Draw everything
screen.fill(WHITE)
bird_group.draw(screen)
pipe_group.draw(screen)
screen.blit(ground_image, (0, SCREEN_HEIGHT - GROUND_HEIGHT))
# Render score
score_surface = font.render(f"Score: {int(score)}", True, (0, 0, 0))
screen.blit(score_surface, (10, 10))
pygame.display.flip()
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()
Install Python 3: Ensure that Python 3.x is installed on your system. You can download it from the official website.
Install Pygame Library: Open your command prompt or terminal and run:
pip install pygame
This command installs the pygame library, which is used for creating games in Python.
Save the Code:
.py extension, for example, flappy_bird.py.Run the Game:
python flappy_bird.py
python with python3 if necessary, depending on your system configuration.How to Play:
Feel free to customize the game by:
fill methods to change the colors of the bird, pipes, and ground.Surface objects with pygame.image.load('path/to/image.png') to use custom images.GRAVITY, FLAP_STRENGTH, PIPE_SPEED, and PIPE_GAP to adjust the game's difficulty level.pygame, ensure it's installed correctly and that you're using the correct Python interpreter.Enjoy playing your very own version of Flappy Bird! If you have any questions or need further assistance, feel free to ask.
Not yet included in a published release.