import pygame
from pygame.locals import *
import random

pygame.init()

clock = pygame.time.Clock()
fps = 60

screen_width = 864
screen_height = 936

screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Flappy Bird')

#Define THE game Variables maybe idk?
ground_scroll = 0
scroll_speed = 4
flying = False
game_over = False
pipe_gap = 150
pipe_frequency = 1500 #Millilililililiseconnds
last_pipe = pygame.time.get_ticks() - pipe_frequency



#load images please
bg = pygame.image.load('Flappy Bird/bg.png')
ground_img = pygame.image.load('Flappy Bird/ground (1).png')
dead_img = pygame.image.load('Flappy Bird/deadbird.png')


#creates bird animation
class Bird(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.images = []
        self.index = 0
        self.counter = 0
        for num in range(1, 4):
            img = pygame.image.load(f'Flappy Bird/bird{num}.png')
            self.images.append(img)
        self.image = self.images[self.index]
        self.rect = self.image.get_rect()
        self.rect.center = [x, y]
        self.vel = 0
        self.clicked = False

    def update(self):
        #Gravity (Makes things fall ok?)
        if flying == True:
            self.vel += 0.5
            if self.vel > 8:
                self.vel = 8
            print(self.vel)
            if self.rect.bottom < 768:
                self.rect.y += int(self.vel)

        if game_over == False:
            #jumping? how does a bird jump in mid air?
            if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
                self.clicked = True
                self.vel = -10
            if pygame.mouse.get_pressed()[0] == 0:
                self.clicked = False

            #handle animation
            self.counter += 1
            flap_cooldown = 7

            if self.counter > flap_cooldown:
                self.counter = 0
                self.index += 1
                if self.index >= len(self.images):
                    self.index = 0
            self.image = self.images[self.index]

            #ROTATE THE BIRDIE!!!!!!
            self.image = pygame.transform.rotate(self.images[self.index], self.vel * -2)
        #else: #DIE!
            #self.image = dead_img
            #self.image = pygame.transform.rotate(self.image, 10)
            #dead_img = pygame.transform.flip(dead_img)
            #self.rect.y += int(self.vel)

class Pipe(pygame.sprite.Sprite):
    def __init__(self, x, y, position):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('Flappy Bird/pipe.png')
        self.rect = self.image.get_rect()
        #p1 = max verstappen -1 = bottom------BRO what is this binary code!?
        if position == 1:
            self.image = pygame.transform.flip(self.image, False, True)
            self. rect.bottomleft = [x,y - int(pipe_gap / 2)]
        if position == -1:
            self.rect.topleft = [x,y + int(pipe_gap / 2)]

    def update(self):
        self.rect.x -= scroll_speed
        if self.rect.right < 0:
            self.kill()

bird_group = pygame.sprite.Group()
pipe_group = pygame.sprite.Group()

flappy = Bird(100, int(screen_height / 2))

bird_group.add(flappy)



run = True
while run:

    clock.tick(fps)

    #draw background or else
    screen.blit(bg, (0, 0))

    bird_group.draw(screen)
    bird_group.update()
    pipe_group.draw(screen)


    #draw on the ground or draw the ground?
    screen.blit(ground_img, (ground_scroll, 768))


    #Look for pipes Birdie
    if pygame.sprite.groupcollide(bird_group, pipe_group, False, False, collided = None) or flappy.rect.top < 0:
        game_over = True


    #check for road kill
    if flappy.rect.bottom >= 768:
        game_over = True
        flying = False

    if game_over == False and flying == True:

        #Generate pips? But whats the fruit?
        time_now = pygame.time.get_ticks()
        if time_now - last_pipe > pipe_frequency:
            pipe_height = random.randint(-100, 100)
            btm_pipe = Pipe(screen_width, int(screen_height / 2) + pipe_height, -1)
            top_pipe = Pipe(screen_width, int(screen_height / 2) + pipe_height, 1)
            pipe_group.add(btm_pipe)
            pipe_group.add(top_pipe)
            last_pipe = time_now

        #Draw and Scroll me!
        ground_scroll -= scroll_speed
        if abs(ground_scroll) > 35:
            ground_scroll = 0

        pipe_group.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN and flying == flying == False and game_over == False:
            flying = True

    pygame.display.update()

pygame.quit()