Flappu Car
Flappu Car
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 main():
bird = Bird()
pipes = [Pipe(WIDTH + 200)]
score = 0
run = True
while run:
clock.tick(60)
bird.move()
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)
pygame.quit()
if __name__ == "__main__":
main()