Thursday 21 January 2021

Game No. 3: Flappy Birds Game in Python

 Game No. 3: Flappy Birds Game in Python


import pygame

import random,sys

pygame.init()

 

clock = pygame.time.Clock()

 

screen = pygame.display.set_mode((800,600))

pygame.display.set_caption("FLAPPY BIRDS")

 

#BIRD

x = 200

y = 300

jump = 0

speed  = 0.5

def draw_circle(x,y):

    pygame.draw.circle(screen, (255,0,0), (x,y), 30)

 

 

#PIPES

pipe1 = [800, 0, 50, random.randint(20,250)]

pipe2 = [400, 0, 50, random.randint(50,250)]

 

Pipes = []

Pipes.append(pipe1)

Pipes.append(pipe2)

 

def draw_pipes(PIPE):

    #left, top, width, heihgt

 

    #TOP

    pygame.draw.rect(screen, (0,255,0), (PIPE[0], PIPE[1], PIPE[2], PIPE[3]))

 

    #BOTTOM

    pygame.draw.rect(screen, (0,255,0), (PIPE[0], 150+PIPE[3], PIPE[2], PIPE[3]+400))

 

score = 0

 

running = True

 

while running:

    screen.fill((120,120,255))

 

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            running = False

            pygame.quit()

            sys.exit()

 

        if event.type == pygame.KEYDOWN:

            if event.key == pygame.K_SPACE: jump = 1

 

        if event.type == pygame.KEYUP:

            if event.key == pygame.K_SPACE: jump = 0 

 

    #BIRD MOVEMENT

    draw_circle(x,y)

    if jump == 1:

        y -= 3

    else:

        y += speed

 

    #PIPE MOVEMENT

    for i in Pipes:

        draw_pipes(i)

        i[0] -= 3

        if i[0] < 0:

            i[0] = 800

            i[3] = random.randint(50,250)

 

    #GAME OVER AND SCORE

    for i in Pipes:

        if i[0] == 200:

            if y <= i[3] or y >= 150+i[3]:

                print("GAME OVER")

                running = False

                pygame.quit()

                sys.exit()

            elif i[3] < y < 150+i[3]:

                score += 1

                print(score)

 

    clock.tick(30)

    pygame.display.update()

 

No comments:

Post a Comment