import pygame import random import sys # Initialize Pygame pygame.init() # Screen dimensions WIDTH = 400 HEIGHT = 600 GROUND_HEIGHT = 100 # Colors WHITE = (255, 255, 255) BLUE = (135, 206, 250) GREEN = (0, 200, 0) RED = (255, 0, 0) # Set up screen screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Nappy Birds") # Load bird bird_radius = 20 bird_x = 60 bird_y = HEIGHT // 2 bird_velocity = 0 gravity = 0.5 jump_strength = -10 # Pipes pipe_width = 70 pipe_gap = 150 pipe_velocity = 3 pipes = [] # Clock clock = pygame.time.Clock() font = pygame.font.SysFont(None, 40) score = 0 def create_pipe(): height = random.randint(100, 400) return {'x': WIDTH, 'height': height} # Game loop running = True while running: screen.fill(BLUE) # Bird physics bird_velocity += gravity bird_y += bird_velocity # Event Handling 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_velocity = jump_strength # Draw bird pygame.draw.circle(screen, RED, (bird_x, int(bird_y)), bird_radius) # Pipes if not pipes or pipes[-1]['x'] < WIDTH - 200: pipes.append(create_pipe()) new_pipes = [] for pipe in pipes: pipe['x'] -= pipe_velocity # Top pipe pygame.draw.rect(screen, GREEN, (pipe['x'], 0, pipe_width, pipe['height'])) # Bottom pipe bottom_pipe_y = pipe['height'] + pipe_gap pygame.draw.rect(screen, GREEN, (pipe['x'], bottom_pipe_y, pipe_width, HEIGHT - bottom_pipe_y)) # Collision detection if bird_x + bird_radius > pipe['x'] and bird_x - bird_radius < pipe['x'] + pipe_width: if bird_y - bird_radius < pipe['height'] or bird_y + bird_radius > bottom_pipe_y: running = False # Add to new pipe list if still on screen if pipe['x'] + pipe_width > 0: new_pipes.append(pipe) else: score += 1 # Passed a pipe pipes = new_pipes # Ground collision if bird_y + bird_radius > HEIGHT or bird_y - bird_radius < 0: running = False # Score display score_text = font.render(f"Score: {score}", True, WHITE) screen.blit(score_text, (10, 10)) pygame.display.flip() clock.tick(60) pygame.quit() sys.exit()

Comments