Untitled

 avatar
unknown
plain_text
2 months ago
3.2 kB
5
Indexable
pip install pygameimport pygame
import random

# Initialize pygame
pygame.init()

# Game Constants
WIDTH, HEIGHT = 400, 600
GROUND_HEIGHT = 100
BIRD_RADIUS = 20
PIPE_WIDTH = 70
PIPE_GAP = 150
PIPE_SPEED = 3
GRAVITY = 0.5
FLAP_STRENGTH = -8
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
BLUE = (0, 100, 255)

# Create game window
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Flappy Bird")

# Load bird image
bird_img = pygame.image.load("bird.png")  # Use a small bird image
bird_img = pygame.transform.scale(bird_img, (40, 40))

# Bird class
class Bird:
    def __init__(self):
        self.x = 50
        self.y = HEIGHT // 2
        self.velocity = 0

    def flap(self):
        self.velocity = FLAP_STRENGTH

    def update(self):
        self.velocity += GRAVITY
        self.y += self.velocity
        self.y = max(self.y, 0)  # Prevent going above the screen

    def draw(self):
        screen.blit(bird_img, (self.x, self.y))

# Pipe class
class Pipe:
    def __init__(self, x):
        self.x = x
        self.height = random.randint(100, 400)
    
    def update(self):
        self.x -= PIPE_SPEED
    
    def draw(self):
        pygame.draw.rect(screen, GREEN, (self.x, 0, PIPE_WIDTH, self.height))
        pygame.draw.rect(screen, GREEN, (self.x, self.height + PIPE_GAP, PIPE_WIDTH, HEIGHT - self.height - PIPE_GAP))

    def off_screen(self):
        return self.x < -PIPE_WIDTH

    def collides_with(self, bird):
        bird_rect = pygame.Rect(bird.x, bird.y, 40, 40)
        top_pipe = pygame.Rect(self.x, 0, PIPE_WIDTH, self.height)
        bottom_pipe = pygame.Rect(self.x, self.height + PIPE_GAP, PIPE_WIDTH, HEIGHT - self.height - PIPE_GAP)
        return bird_rect.colliderect(top_pipe) or bird_rect.colliderect(bottom_pipe)

# Main game loop
def main():
    clock = pygame.time.Clock()
    bird = Bird()
    pipes = [Pipe(WIDTH + i * 200) for i in range(3)]
    running = True
    score = 0

    while running:
        clock.tick(30)
        screen.fill(BLUE)
        
        # Handle events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                bird.flap()
        
        # Update bird
        bird.update()
        
        # Update pipes
        for pipe in pipes:
            pipe.update()
            if pipe.off_screen():
                pipes.remove(pipe)
                pipes.append(Pipe(WIDTH))
                score += 1

            if pipe.collides_with(bird):
                running = False  # End game if bird collides

        # Check if bird hits ground
        if bird.y >= HEIGHT - GROUND_HEIGHT:
            running = False
        
        # Draw everything
        bird.draw()
        for pipe in pipes:
            pipe.draw()

        pygame.draw.rect(screen, (200, 100, 50), (0, HEIGHT - GROUND_HEIGHT, WIDTH, GROUND_HEIGHT))  # Ground
        score_text = pygame.font.SysFont("Arial", 30).render(f"Score: {score}", True, WHITE)
        screen.blit(score_text, (10, 10))

        pygame.display.update()

    pygame.quit()

if __name__ == "__main__":
    main()
Editor is loading...
Leave a Comment