Write a nice working python code for a flappy bird game with nice realistic graphics and…
- User
Write a nice working python code for a flappy bird game with nice realistic graphics and realistic physics and animations
- Assistant
Write a nice working python code for a flappy bird game with nice realistic graphics and realistic physics and animations
Creating a Flappy Bird clone in Python with realistic graphics and physics can be achieved using the pygame library. While "realistic" can be subjective, we'll aim for smooth animations, responsive controls, and visually appealing graphics using images. To enhance realism, we'll incorporate gravity, collision detection, and smooth animations.
Prerequisites:
Python Installed: Ensure you have Python 3.x installed on your system.
Pygame Installed: Install pygame using pip if you haven't already:
pip install pygame
Assets: For realistic graphics, you’ll need image assets for the bird, pipes, background, and base. You can download free assets from sites like Kenney.nl or OpenGameArt.org. For simplicity, I'll assume you have the following images in a folder named assets:
bird.pngpipe.pngbackground.pngbase.pngProject Structure:
flappy_bird_clone/
├── assets/
│ ├── bird.png
│ ├── pipe.png
│ ├── background.png
│ └── base.png
└── main.py
Complete Code (main.py):
import pygame
import sys
import random
# Initialize Pygame
pygame.init()
# Game Constants
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 600
FPS = 60
GRAVITY = 0.5
BIRD_FLAP = -10
PIPE_SPEED = 3
PIPE_GAP = 150
BASE_HEIGHT = 100
# Setup the screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Flappy Bird Clone")
# Load images
# Make sure to have these images in an 'assets' folder
# You can replace these with your own images
try:
bg_image = pygame.image.load('assets/background.png').convert()
base_image = pygame.image.load('assets/base.png').convert()
bird_images = [
pygame.image.load('assets/bird.png').convert_alpha(),
pygame.transform.rotate(pygame.image.load('assets/bird.png').convert_alpha(), -25),
pygame.transform.rotate(pygame.image.load('assets/bird.png').convert_alpha(), 25)
]
pipe_image = pygame.image.load('assets/pipe.png').convert_alpha()
except pygame.error as e:
print(f"Error loading images: {e}")
sys.exit()
# Resize images if necessary
bg_image = pygame.transform.scale(bg_image, (SCREEN_WIDTH, SCREEN_HEIGHT))
base_image = pygame.transform.scale(base_image, (SCREEN_WIDTH, BASE_HEIGHT))
# Fonts
font = pygame.font.SysFont("Arial", 32)
# Bird Class
class Bird:
def __init__(self, x, y):
self.x = x
self.y = y
self.gravity = GRAVITY
self.velocity = 0
self.image_count = 0
self.images = bird_images
self.image = self.images[0]
self.rect = self.image.get_rect(center=(self.x, self.y))
self.rotation = 0
def flap(self):
self.velocity = BIRD_FLAP
def update(self):
self.velocity += self.gravity
self.y += self.velocity
self.rect.centery = self.y
# Rotate the bird
if self.velocity < 0:
self.rotation = max(-25, self.rotation - 5)
else:
self.rotation = min(90, self.rotation + 5)
self.image = pygame.transform.rotate(self.images[self.image_count % len(self.images)], self.rotation)
self.image_count += 1
if self.image_count >= 30:
self.image_count = 0
def draw(self, surface):
rotated_image = pygame.transform.rotate(self.image, self.rotation)
new_rect = rotated_image.get_rect(center=self.rect.center)
surface.blit(rotated_image, new_rect.topleft)
# Pipe Class
class Pipe:
def __init__(self, x):
self.x = x
self.height = 0
self.gap = PIPE_GAP
self.top = 0
self.bottom = 0
self.PIPE_TOP = pygame.transform.flip(pipe_image, False, True)
self.PIPE_BOTTOM = pipe_image
self.passed = False
self.set_height()
def set_height(self):
self.height = random.randint(50, SCREEN_HEIGHT - BASE_HEIGHT - self.gap - 50)
self.top = self.height - self.PIPE_TOP.get_height()
self.bottom = self.height + self.gap
def move(self):
self.x -= PIPE_SPEED
def draw(self, surface):
surface.blit(self.PIPE_TOP, (self.x, self.top))
surface.blit(self.PIPE_BOTTOM, (self.x, self.bottom))
def collide(self, bird):
bird_mask = pygame.mask.from_surface(bird.image)
top_mask = pygame.mask.from_surface(self.PIPE_TOP)
bottom_mask = pygame.mask.from_surface(self.PIPE_BOTTOM)
top_offset = (self.x - bird.rect.left, self.top - round(bird.rect.top))
bottom_offset = (self.x - bird.rect.left, self.bottom - round(bird.rect.top))
b_point = bird_mask.overlap(bottom_mask, bottom_offset)
t_point = bird_mask.overlap(top_mask, top_offset)
if t_point or b_point:
return True
return False
# Base Class
class Base:
def __init__(self, y):
self.y = y
self.x1 = 0
self.x2 = SCREEN_WIDTH
self.speed = PIPE_SPEED
def move(self):
self.x1 -= self.speed
self.x2 -= self.speed
if self.x1 + SCREEN_WIDTH < 0:
self.x1 = self.x2 + SCREEN_WIDTH
if self.x2 + SCREEN_WIDTH < 0:
self.x2 = self.x1 + SCREEN_WIDTH
def draw(self, surface):
surface.blit(base_image, (self.x1, self.y))
surface.blit(base_image, (self.x2, self.y))
# Main Game Function
def main():
clock = pygame.time.Clock()
bird = Bird(100, SCREEN_HEIGHT//2)
base = Base(SCREEN_HEIGHT - BASE_HEIGHT)
pipes = [Pipe(SCREEN_WIDTH + 100)]
score = 0
run = True
game_over = False
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and not game_over:
bird.flap()
if event.key == pygame.K_SPACE and game_over:
main() # Restart the game
if not game_over:
bird.update()
base.move()
# Move pipes
add_pipe = False
rem = []
for pipe in pipes:
pipe.move()
# Check for collision
if pipe.collide(bird):
game_over = True
if pipe.x + pipe.PIPE_TOP.get_width() < bird.x and not pipe.passed:
pipe.passed = True
add_pipe = True
if pipe.x + pipe.PIPE_TOP.get_width() < 0:
rem.append(pipe)
if add_pipe:
score += 1
pipes.append(Pipe(SCREEN_WIDTH + 100))
for r in rem:
pipes.remove(r)
# Check for collision with ground or ceiling
if bird.y + bird.image.get_height()//2 >= SCREEN_HEIGHT - BASE_HEIGHT or bird.y - bird.image.get_height()//2 <= 0:
game_over = True
# Draw everything
screen.blit(bg_image, (0, 0))
for pipe in pipes:
pipe.draw(screen)
base.draw(screen)
bird.draw(screen)
# Display score
score_text = font.render(str(score), True, (255, 255, 255))
screen.blit(score_text, (SCREEN_WIDTH//2 - score_text.get_width()//2, 50))
if game_over:
game_over_text = font.render("Game Over! Press Space to Restart", True, (255, 0, 0))
screen.blit(game_over_text, (SCREEN_WIDTH//2 - game_over_text.get_width()//2, SCREEN_HEIGHT//2))
pygame.display.update()
if __name__ == "__main__":
main()
Explanation:
Imports and Initialization:
pygame, sys, and random.Loading Assets:
assets folder relative to your script.Classes:
Main Game Loop (main function):
Running the Game:
Ensure you have all the required assets in the assets folder.
Run the script using Python:
python main.py
Controls:
Notes for Enhancements:
Final Remarks:
This implementation provides a basic yet functional Flappy Bird clone with structured code and the ability to incorporate more advanced features and assets for enhanced realism. Customize the assets and tweak the physics constants to better match the desired realism and gameplay feel.
Not yet included in a published release.