Untitled

 avatar
unknown
plain_text
25 days ago
3.3 kB
3
Indexable
Here's a simplified version of Flappy Bird using Python and the Pygame library:

```
import pygame
import sys

Game Variables
gravity = 0.25
bird_movement = 0
game_active = True
score = 0
high_score = 0

Pygame Initialization
pygame.init()
screen = pygame.display.set_mode((288, 512))
clock = pygame.time.Clock()

Background
bg_surface = pygame.Surface((288, 512))
bg_surface.fill((135, 206, 235))

Floor
floor_surface = pygame.Surface((288, 112))
floor_surface.fill((222, 196, 169))
floor_x_pos = 0

Bird
bird_surface = pygame.Surface((20, 20))
bird_surface.fill((255, 255, 255))
bird_rect = bird_surface.get_rect(center=(50, 256))

Pipes
pipe_surface = pygame.Surface((50, 400))
pipe_surface.fill((0, 255, 0))
pipe_rect = pipe_surface.get_rect(center=(300, 400))
pipe_list = []

Score
font = pygame.font.Font('freesansbold.ttf', 32)

def create_pipe():
    new_pipe_rect = pipe_surface.get_rect(center=(300, 400))
    return new_pipe_rect

def move_pipes(pipes):
    for pipe in pipes:
        pipe.centerx -= 5
    return pipes

def draw_pipes(pipes):
    for pipe in pipes:
        screen.blit(pipe_surface, pipe)

def check_collision(pipes):
    for pipe in pipes:
        if bird_rect.colliderect(pipe):
            return False
    if bird_rect.top <= -100 or bird_rect.bottom >= 400:
        return False
    return True

def score_display(game_state):
    if game_state == 'main_game':
        score_surface = font.render(str(int(score)), True, (255, 255, 255))
        screen.blit(score_surface, (144, 50))
    if game_state == 'game_over':
        score_surface = font.render(f'Score: {int(score)}', True, (255, 255, 255))
        screen.blit(score_surface, (75, 150))
        high_score_surface = font.render(f'High Score: {int(high_score)}', True, (255, 255, 255))
        screen.blit(high_score_surface, (50, 180))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and game_active:
                bird_movement = 0
                bird_movement -= 6
            if event.key == pygame.K_SPACE and game_active == False:
                game_active = True
                pipe_list.clear()
                bird_rect.center = (50, 256)
                bird_movement = 0
                score = 0

    screen.blit(bg_surface, (0, 0))

    if game_active:
        # Bird
        bird_movement += gravity
        bird_rect.centery += bird_movement
        screen.blit(bird_surface, bird_rect)

        # Pipes
        if len(pipe_list) == 0 or pipe_list[-1].right < 200:
            pipe_list.append(create_pipe())
        pipe_list = move_pipes(pipe_list)
        draw_pipes(pipe_list)

        # Collision
        game_active = check_collision(pipe_list)

        # Score
        score += 0.01
        score_display('main_game')

    else:
        score_display('game_over')
        if score > high_score:
            high_score = score

    # Floor
    floor_x_pos -= 1
    screen.blit(floor_surface, (floor_x_pos, 400))
    screen.blit(floor_surface, (floor_x_pos + 288, 400))
    if floor_x_pos <= -288:
        floor_x_pos = 0

    pygame.display.update()
    clock.tick(120)
```

Leave a Comment