diff --git a/FlapyBird b/FlapyBird new file mode 100644 index 00000000..55296de9 --- /dev/null +++ b/FlapyBird @@ -0,0 +1,93 @@ +import pygame +import random + +# Initialisation de Pygame +pygame.init() + +# Dimensions de la fenêtre +WIDTH, HEIGHT = 400, 600 +screen = pygame.display.set_mode((WIDTH, HEIGHT)) +pygame.display.set_caption("Flappy Bird") + +# Couleurs +WHITE = (255, 255, 255) +BLACK = (0, 0, 0) +SKY_BLUE = (135, 206, 235) +GREEN = (0, 255, 0) + +# Paramètres du jeu +gravity = 0.5 +bird_jump = -10 +pipe_speed = 3 +gap = 150 # Espace entre les tuyaux + +# Police pour le score +font = pygame.font.SysFont(None, 48) + +# Bird (oiseau) +bird = pygame.Rect(50, HEIGHT // 2, 30, 30) +bird_vel = 0 + +# Pipes (tuyaux) +pipe_width = 70 +pipe_list = [] + +def create_pipe(): + height = random.randint(100, HEIGHT - 300) + top_pipe = pygame.Rect(WIDTH, 0, pipe_width, height) + bottom_pipe = pygame.Rect(WIDTH, height + gap, pipe_width, HEIGHT - height - gap) + return top_pipe, bottom_pipe + +pipe_list.extend(create_pipe()) + +score = 0 +clock = pygame.time.Clock() + +running = True +while running: + clock.tick(60) # 60 FPS + screen.fill(SKY_BLUE) + + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_SPACE: + bird_vel = bird_jump + + # Mouvement de l'oiseau + bird_vel += gravity + bird.y += int(bird_vel) + + # Dessiner l'oiseau + pygame.draw.rect(screen, BLACK, bird) + + # Mouvement des tuyaux + for pipe in pipe_list: + pipe.x -= pipe_speed + + # Enlever les tuyaux hors écran + if pipe_list[0].right < 0: + pipe_list.pop(0) + pipe_list.pop(0) + pipe_list.extend(create_pipe()) + score += 1 + + # Dessiner les tuyaux + for pipe in pipe_list: + pygame.draw.rect(screen, GREEN, pipe) + + # Collision + for pipe in pipe_list: + if bird.colliderect(pipe): + running = False + if bird.top <= 0 or bird.bottom >= HEIGHT: + running = False + + # Afficher le score + score_text = font.render(str(score), True, BLACK) + screen.blit(score_text, (WIDTH // 2 - score_text.get_width() // 2, 10)) + + pygame.display.flip() + +pygame.quit()