Untitled

Anonymous
plain_text
02/12/2026 7:16 PM
6.1 KB
11
Indexable
import pygame
import random

pygame.init()


FPS = 180
WINDOW_WIDTH = 870
WINDOW_HEIGHT = 920

GREEN = (0, 200, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)

window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
background = pygame.image.load("background-day.png").convert()
background = pygame.transform.scale(background, (WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Flappy (Helicopter-style)")

clock = pygame.time.Clock()



class Area:
    def __init__(self, x=0, y=0, width=10, height=10, color=None):
        self.rect = pygame.Rect(x, y, width, height)
        self.fill_color = color

    def fill(self):
        if self.fill_color is not None:
            pygame.draw.rect(window, self.fill_color, self.rect)


class Picture(Area):
    def __init__(self, x, y, width, height, color, filename):
        super().__init__(x, y, width, height, color)
        self.image = None
        try:
            self.image = pygame.image.load(filename)
            #self.image = pygame.transform.scale(img, (width, height))
        except Exception as ex:
            print("IMAGE LOAD FAILED:", filename, "->", ex)
            self.fill_color = RED

    def draw(self):
        if self.image is not None:
            window.blit(self.image, (self.rect.x, self.rect.y))
        else:
            self.fill()

 



bird = Picture(150, WINDOW_HEIGHT // 2, 34, 24, RED, "bluebird-downflap.png")







BIRD_SPEED = 6
move_up = False
move_down = False


PIPE_WIDTH = 80
PIPE_SPEED = 6
PIPE_GAP = 220          
PIPE_SPACING = 300
TOP_MARGIN = 60
BOTTOM_MARGIN = 60

pipes = []  


def spawn_pipe_pair(x):
    TOP_MARGIN = 80
    BOTTOM_MARGIN = 120
    PIPE_GAP = 220

    
    min_gap_y = TOP_MARGIN
    max_gap_y = WINDOW_HEIGHT - BOTTOM_MARGIN - PIPE_GAP

    
    if max_gap_y <= min_gap_y:
        gap_y = min_gap_y
    else:
        gap_y = random.randint(min_gap_y, max_gap_y)

    top_height = gap_y
    bottom_y = gap_y + PIPE_GAP
    bottom_height = WINDOW_HEIGHT - bottom_y  

    top_pipe = Area(x, 0, PIPE_WIDTH, top_height, GREEN)
    bottom_pipe = Area(x, bottom_y, PIPE_WIDTH, bottom_height, GREEN)
    return top_pipe, bottom_pipe




start_x = WINDOW_WIDTH + 200
for i in range(5):
    pipes.append(spawn_pipe_pair(start_x + i * PIPE_SPACING))


game_over = False
running = True

while running:
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            running = False

        if e.type == pygame.KEYDOWN:
            if e.key == pygame.K_w:
                move_up = True
            if e.key == pygame.K_s:
                move_down = True

            if e.key == pygame.K_r and game_over:
             
                bird.rect.x, bird.rect.y = 150, WINDOW_HEIGHT // 2
                pipes = []
                for i in range(5):
                    pipes.append(spawn_pipe_pair(start_x + i * PIPE_SPACING))
                game_over = False

        if e.type == pygame.KEYUP:
            if e.key == pygame.K_w:
                move_up = False
            if e.key == pygame.K_s:
                move_down = False

    if not game_over:
        
        if move_up:
            bird.rect.y -= BIRD_SPEED
        if move_down:
            bird.rect.y += BIRD_SPEED

        
        if bird.rect.y < 0:
            bird.rect.y = 0
        if bird.rect.bottom > WINDOW_HEIGHT:
            bird.rect.bottom = WINDOW_HEIGHT

        
        for top_pipe, bottom_pipe in pipes:
            top_pipe.rect.x -= PIPE_SPEED
            bottom_pipe.rect.x -= PIPE_SPEED

        
        first_top, first_bottom = pipes[0]
        if first_top.rect.right < 0:
            pipes.pop(0)
            last_top, last_bottom = pipes[-1]
            new_x = last_top.rect.x + PIPE_SPACING
            pipes.append(spawn_pipe_pair(new_x))

        
        for top_pipe, bottom_pipe in pipes:
            if bird.rect.colliderect(top_pipe.rect) or bird.rect.colliderect(bottom_pipe.rect):
                game_over = True
                break

    
    if background:
        window.blit(background, (0, 0))
    else:
        window.fill(WHITE)

    for top_pipe, bottom_pipe in pipes:
        top_pipe.fill()
        bottom_pipe.fill()

    bird.draw()

    if game_over:
        font = pygame.font.SysFont("Arial", 48)
        txt = font.render("GAME OVER - Press R", True, (255, 0, 0))
        window.blit(txt, (WINDOW_WIDTH // 2 - txt.get_width() // 2, 200))

    pygame.display.update()
    clock.tick(FPS)

pygame.quit()

background_img

spawn_pipe_pair
Editor is loading...
Leave a Comment