0% found this document useful (0 votes)
4 views

Pygame CODE

Uploaded by

chiraghospet
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Pygame CODE

Uploaded by

chiraghospet
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

import pygame

import random

pygame.init()

SCREEN_WIDTH = 400

SCREEN_HEIGHT = 600

FPS = 40

WHITE = (255, 255, 255)

BLACK = (0, 0, 0)

GREEN = (0, 255, 0)

BIRD_WIDTH = 30

BIRD_HEIGHT = 30

BIRD_X = 50

BIRD_Y = SCREEN_HEIGHT // 2

BIRD_VELOCITY = 0

GRAVITY = 0.5

FLAP_STRENGTH = -10

PIPE_WIDTH = 70

PIPE_HEIGHT = 500

PIPE_GAP = 150

PIPE_VELOCITY = -5

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

pygame.display.set_caption("Flappy Bird")
bird_image = pygame.Surface((BIRD_WIDTH, BIRD_HEIGHT))

bird_image.fill(WHITE)

pipe_image = pygame.Surface((PIPE_WIDTH, PIPE_HEIGHT))

pipe_image.fill(GREEN)

def draw_bird(y):

screen.blit(bird_image, (BIRD_X, y))

def draw_pipe(x, y):

screen.blit(pipe_image, (x, y))

screen.blit(pipe_image, (x, y + PIPE_HEIGHT + PIPE_GAP))

def main():

clock = pygame.time.Clock()

bird_y = BIRD_Y

bird_velocity = BIRD_VELOCITY

pipe_x = SCREEN_WIDTH

pipe_y = random.randint(-PIPE_HEIGHT + 150, 0)

score = 0

running = True

while running:

for event in pygame.event.get():

if event.type == pygame.QUIT:

running = False

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:

bird_velocity = FLAP_STRENGTH

bird_velocity += GRAVITY

bird_y += bird_velocity

pipe_x += PIPE_VELOCITY

if pipe_x < -PIPE_WIDTH:

pipe_x = SCREEN_WIDTH

pipe_y = random.randint(-PIPE_HEIGHT + 150, 0)

score += 1

if (BIRD_X + BIRD_WIDTH > pipe_x and BIRD_X < pipe_x + PIPE_WIDTH) and \

(bird_y < pipe_y + PIPE_HEIGHT or bird_y + BIRD_HEIGHT > pipe_y + PIPE_HEIGHT + PIPE_GAP):

print("Game Over! Your score:", score)

running = False

if bird_y > SCREEN_HEIGHT or bird_y < 0:

print("Game Over! Your score:", score)

running = False

screen.fill(BLACK)

draw_bird(bird_y)

draw_pipe(pipe_x, pipe_y)

pygame.display.flip()

clock.tick(FPS)

pygame.quit()
if __name__ == "__main__":

main()

You might also like