Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- # Initialize Pygame
- pygame.init()
- # Game constants
- SCREEN_WIDTH = 288
- SCREEN_HEIGHT = 512
- FPS = 30
- BACKGROUND_COLOR = (135, 206, 250) # Light blue
- GROUND_COLOR = (0, 128, 0) # Green
- BIRD_COLOR = (255, 255, 0) # Yellow
- GROUND_HEIGHT = 50
- class Bird:
- def __init__(self):
- self.x = int(SCREEN_WIDTH * 0.2) # Start position
- self.y = SCREEN_HEIGHT // 2
- self.velocity = 0
- self.gravity = 0.3
- self.radius = 15
- def jump(self):
- self.velocity = -7 # Upward velocity
- def update(self):
- self.velocity += self.gravity
- self.y += self.velocity
- def draw(self, screen):
- pygame.draw.circle(screen, BIRD_COLOR, (self.x, int(self.y)), self.radius)
- def get_rect(self):
- return pygame.Rect(
- self.x - self.radius,
- self.y - self.radius,
- self.radius * 2,
- self.radius * 2
- )
- class Pipe:
- def __init__(self, x):
- self.x = x
- self.top_height = random.randint(50, 350)
- self.gap = 150 # Gap between pipes
- self.bottom_y = self.top_height + self.gap
- self.passed = False
- def move(self, speed):
- self.x -= speed
- def draw(self, screen):
- # Draw top pipe
- pygame.draw.rect(screen, GROUND_COLOR, (self.x, 0, 50, self.top_height))
- # Draw bottom pipe
- pygame.draw.rect(screen, GROUND_COLOR,
- (self.x, self.bottom_y, 50, SCREEN_HEIGHT - self.bottom_y))
- def get_top_rect(self):
- return pygame.Rect(self.x, 0, 50, self.top_height)
- def get_bottom_rect(self):
- return pygame.Rect(self.x, self.bottom_y, 50, SCREEN_HEIGHT - self.bottom_y)
- def main():
- screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
- pygame.display.set_caption("Flappy Bird Clone")
- clock = pygame.time.Clock()
- bird = Bird()
- pipes = []
- pipe_speed = 3
- pipe_add_interval = 70 # Add new pipe every x frames
- pipe_count = 0
- score = 0
- font = pygame.font.Font(None, 36)
- ground_y = SCREEN_HEIGHT - GROUND_HEIGHT
- running = True
- game_over = False
- while running:
- screen.fill(BACKGROUND_COLOR)
- pygame.draw.rect(screen, GROUND_COLOR,
- (0, ground_y, SCREEN_WIDTH, GROUND_HEIGHT))
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- if not game_over and event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
- bird.jump()
- if not game_over:
- bird.update()
- pipe_count += 1
- if pipe_count >= pipe_add_interval:
- pipes.append(Pipe(SCREEN_WIDTH))
- pipe_count = 0
- # Move and remove pipes
- pipes_to_remove = []
- for pipe in pipes:
- pipe.move(pipe_speed)
- if pipe.x + 50 < 0:
- pipes_to_remove.append(pipe)
- for pipe in pipes_to_remove:
- pipes.remove(pipe)
- # Check collisions
- for pipe in pipes:
- if bird.get_rect().colliderect(pipe.get_top_rect()) or \
- bird.get_rect().colliderect(pipe.get_bottom_rect()):
- game_over = True
- if not pipe.passed and bird.x > pipe.x + 50:
- pipe.passed = True
- score += 1
- if (bird.y + bird.radius > ground_y) or (bird.y - bird.radius < 0):
- game_over = True
- # Draw game elements
- for pipe in pipes:
- pipe.draw(screen)
- bird.draw(screen)
- # Draw score
- text = font.render(f"Score: {score}", True, (0,0,0))
- screen.blit(text, (10, 10))
- else:
- # Game over screen
- text = font.render("Game Over! Press R to Restart", True, (0,0,0))
- screen.blit(text, (40, SCREEN_HEIGHT//2))
- keys = pygame.key.get_pressed()
- if keys[pygame.K_r]:
- bird = Bird()
- pipes = []
- score = 0
- game_over = False
- pygame.display.flip()
- clock.tick(FPS)
- pygame.quit()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement