Skip to content

WIDTH, HEIGHT = 400, 600 #127224

@tripurari-kuma

Description

@tripurari-kuma

Feature or enhancement

Proposal:

# Add a code block here, if required

import pygame
import random
import sys

Initialize Pygame

pygame.init()

Game window dimensions

WIDTH, HEIGHT = 400, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Flappy Bird")

Game colors

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)

FPS (frames per second) controller

clock = pygame.time.Clock()

Game variables

GRAVITY = 0.25
JUMP_STRENGTH = -6
PIPE_WIDTH = 70
PIPE_GAP = 150
bird_y = HEIGHT // 2
bird_velocity = 0
bird_radius = 20

Font for score

font = pygame.font.SysFont("Arial", 36)

Game elements

def draw_bird(y_position):
pygame.draw.circle(screen, RED, (100, int(y_position)), bird_radius)

def draw_pipe(x_position, height):
pygame.draw.rect(screen, GREEN, (x_position, 0, PIPE_WIDTH, height))
pygame.draw.rect(screen, GREEN, (x_position, height + PIPE_GAP, PIPE_WIDTH, HEIGHT - height - PIPE_GAP))

def game_over():
text = font.render("Game Over", True, BLACK)
screen.blit(text, (WIDTH // 3, HEIGHT // 3))
pygame.display.update()
pygame.time.wait(2000)
sys.exit()

Main game loop

def main():
global bird_y, bird_velocity
pipes = []
score = 0
pipe_x = WIDTH

while True:
    screen.fill(WHITE)

    # Event handling
    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:
                bird_velocity = JUMP_STRENGTH

    # Bird physics
    bird_velocity += GRAVITY
    bird_y += bird_velocity

    # Pipe logic
    pipe_x -= 4
    if pipe_x < -PIPE_WIDTH:
        pipe_x = WIDTH
        pipe_height = random.randint(100, HEIGHT - PIPE_GAP - 100)
        pipes.append((pipe_x, pipe_height))

    # Check collision with pipes or ground
    for pipe in pipes:
        if pipe[0] < 100 + bird_radius < pipe[0] + PIPE_WIDTH:
            if not (pipe[1] < bird_y - bird_radius or bird_y + bird_radius < pipe[1] + PIPE_GAP):
                game_over()
        if pipe[0] + PIPE_WIDTH < 0:
            pipes.remove(pipe)
            score += 1

    # Game over if the bird hits the floor or goes out of bounds
    if bird_y + bird_radius >= HEIGHT or bird_y - bird_radius <= 0:
        game_over()

    # Draw everything
    for pipe in pipes:
        draw_pipe(pipe[0], pipe[1])

    draw_bird(bird_y)

    # Display score
    score_text = font.render(f"Score: {score}", True, BLACK)
    screen.blit(score_text, (10, 10))

    # Update the screen
    pygame.display.update()

    # Set the frame rate
    clock.tick(60)

if name == "main":
main()

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

import pygame
import random
import sys

Initialize Pygame

pygame.init()

Game window dimensions

WIDTH, HEIGHT = 400, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Flappy Bird")

Game colors

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)

FPS (frames per second) controller

clock = pygame.time.Clock()

Game variables

GRAVITY = 0.25
JUMP_STRENGTH = -6
PIPE_WIDTH = 70
PIPE_GAP = 150
bird_y = HEIGHT // 2
bird_velocity = 0
bird_radius = 20

Font for score

font = pygame.font.SysFont("Arial", 36)

Game elements

def draw_bird(y_position):
pygame.draw.circle(screen, RED, (100, int(y_position)), bird_radius)

def draw_pipe(x_position, height):
pygame.draw.rect(screen, GREEN, (x_position, 0, PIPE_WIDTH, height))
pygame.draw.rect(screen, GREEN, (x_position, height + PIPE_GAP, PIPE_WIDTH, HEIGHT - height - PIPE_GAP))

def game_over():
text = font.render("Game Over", True, BLACK)
screen.blit(text, (WIDTH // 3, HEIGHT // 3))
pygame.display.update()
pygame.time.wait(2000)
sys.exit()

Main game loop

def main():
global bird_y, bird_velocity
pipes = []
score = 0
pipe_x = WIDTH

while True:
    screen.fill(WHITE)

    # Event handling
    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:
                bird_velocity = JUMP_STRENGTH

    # Bird physics
    bird_velocity += GRAVITY
    bird_y += bird_velocity

    # Pipe logic
    pipe_x -= 4
    if pipe_x < -PIPE_WIDTH:
        pipe_x = WIDTH
        pipe_height = random.randint(100, HEIGHT - PIPE_GAP - 100)
        pipes.append((pipe_x, pipe_height))

    # Check collision with pipes or ground
    for pipe in pipes:
        if pipe[0] < 100 + bird_radius < pipe[0] + PIPE_WIDTH:
            if not (pipe[1] < bird_y - bird_radius or bird_y + bird_radius < pipe[1] + PIPE_GAP):
                game_over()
        if pipe[0] + PIPE_WIDTH < 0:
            pipes.remove(pipe)
            score += 1

    # Game over if the bird hits the floor or goes out of bounds
    if bird_y + bird_radius >= HEIGHT or bird_y - bird_radius <= 0:
        game_over()

    # Draw everything
    for pipe in pipes:
        draw_pipe(pipe[0], pipe[1])

    draw_bird(bird_y)

    # Display score
    score_text = font.render(f"Score: {score}", True, BLACK)
    screen.blit(score_text, (10, 10))

    # Update the screen
    pygame.display.update()

    # Set the frame rate
    clock.tick(60)

if name == "main":
main()

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions