0% found this document useful (0 votes)
2 views3 pages

Flappu Car

This document is a Python script that implements a simple Flappy Bird game using the Pygame library. It defines classes for the bird and pipes, manages game mechanics like gravity and collision detection, and includes a main loop to handle user input and game rendering. The game features a scoring system based on successfully navigating through pipes.

Uploaded by

kotharivanita19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Flappu Car

This document is a Python script that implements a simple Flappy Bird game using the Pygame library. It defines classes for the bird and pipes, manages game mechanics like gravity and collision detection, and includes a main loop to handle user input and game rendering. The game features a scoring system based on successfully navigating through pipes.

Uploaded by

kotharivanita19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

import pygame

import random

# Initialize pygame
pygame.init()

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

# Game settings
clock = pygame.time.Clock()
GRAVITY = 0.5
JUMP_STRENGTH = -10
PIPE_GAP = 150
PIPE_WIDTH = 70
PIPE_SPEED = 3

# Colors
WHITE = (255, 255, 255)
BLUE = (135, 206, 250)
GREEN = (0, 200, 0)

# Fonts
font = pygame.font.SysFont(None, 48)

# Bird class
class Bird:
def __init__(self):
self.x = 100
self.y = HEIGHT // 2
self.velocity = 0
self.radius = 20

def jump(self):
self.velocity = JUMP_STRENGTH

def move(self):
self.velocity += GRAVITY
self.y += self.velocity

def draw(self):
pygame.draw.circle(screen, WHITE, (self.x, int(self.y)), self.radius)

# Pipe class
class Pipe:
def __init__(self, x):
self.x = x
self.height = random.randint(50, HEIGHT - PIPE_GAP - 50)
self.passed = False

def move(self):
self.x -= PIPE_SPEED

def draw(self):
pygame.draw.rect(screen, GREEN, (self.x, 0, PIPE_WIDTH, self.height))
pygame.draw.rect(screen, GREEN, (self.x, self.height + PIPE_GAP, PIPE_WIDTH,
HEIGHT))

def collide(self, bird):


if bird.x + bird.radius > self.x and bird.x - bird.radius < self.x + PIPE_WIDTH:
if bird.y - bird.radius < self.height or bird.y + bird.radius > self.height + PIPE_GAP:
return True
return False

def draw_window(bird, pipes, score):


screen.fill(BLUE)
bird.draw()
for pipe in pipes:
pipe.draw()
score_text = font.render(str(score), True, WHITE)
screen.blit(score_text, (WIDTH // 2 - score_text.get_width() // 2, 20))
pygame.display.update()

def main():
bird = Bird()
pipes = [Pipe(WIDTH + 200)]
score = 0
run = True

while run:
clock.tick(60)
bird.move()

for event in pygame.event.get():


if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bird.jump()

rem = []
add_pipe = False
for pipe in pipes:
pipe.move()
if pipe.collide(bird):
run = False
if not pipe.passed and pipe.x < bird.x:
pipe.passed = True
score += 1
add_pipe = True
if pipe.x + PIPE_WIDTH < 0:
rem.append(pipe)

if add_pipe:
pipes.append(Pipe(WIDTH + 100))

for r in rem:
pipes.remove(r)

if bird.y > HEIGHT or bird.y < 0:


run = False

draw_window(bird, pipes, score)

pygame.quit()

if __name__ == "__main__":
main()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy