import pygame
import random

from pygame import *
                      
 
                                                   
pygame.init()

width, height = 740, 500
screen = pygame.display.set_mode((width, height))
player = [pygame.transform.scale(pygame.image.load("Resources/Balljump-1(2).png"), (100,100)), pygame.transform.scale(pygame.image.load("Resources/Balljump-1.png"),(100,100))]
launch = [pygame.transform.scale(pygame.image.load("Resources/Balljump-1.png"), (100,100)), pygame.transform.scale(pygame.image.load("Resources/Balljump-1(2).png"), (100,100)),pygame.transform.scale(pygame.image.load("Resources/Balljump-2.png"), (100,100)),pygame.transform.scale(pygame.image.load("Resources/Balljump-3.png"), (100,100)), pygame.transform.scale(pygame.image.load("Resources/Balljump-4.png"),(100,100))]
shoot = [pygame.transform.scale(pygame.image.load("Resources/Balljump-5.png"), (100, 100)), pygame.transform.scale(pygame.image.load("Resources/Balljump-6.png"), (100, 100))]
ball = pygame.transform.scale(pygame.image.load("Resources/ball.png"), (100,100))
blue = (0, 0, 128)
white = (255, 255, 255)

janimation, danimation, movable, motionactivate, limit_reached, nojump = False, False, False, False, False, False
jumplock = True
ballrelease, ballregain = False, False
fr = pygame.time.Clock()
c = 0
i = 0
p = 0
x, y = 0, 300
score = 0
a, b, rpos = 0, 0, 0
xpos, ypos = 17, 313 

# Background image source: https://www.freepik.com/free-vector/floral-ornamental-abstract-background_6189902.htm#page=1&query=black%20background&position=40
background = pygame.image.load("Resources/back.jpg")


gamestart = False


def basketball():
    
    #Draw basketball
    
    global rpos, xpos, ypos, ballregain
    if gamestart == True and ballrelease == False:

        if nojump == True:

            if c % 2 == 0:
                screen.blit(ball, (xpos, ypos + 24))
            
            if c % 2 == 1:
                screen.blit(ball, (xpos + 2 , ypos ))
    

        
        if nojump == False and motionactivate == True:
            
            if p // 4 == 0:
                screen.blit(ball, (xpos, ypos)) 
            if p // 4 == 1:
                screen.blit(ball, (xpos-2, ypos-5))
            if p // 4 == 2:
                screen.blit(ball, (xpos-2, ypos-7))
            if p // 4 == 3:
                screen.blit(ball, (xpos-2, ypos-11))
            if p// 4 == 4:
                screen.blit(ball, (xpos-2, ypos-13))
        
        if janimation == True:
            rpos = y -13
            screen.blit(ball, (xpos, rpos))
    
    rposNew = 400 - rpos
    
    if gamestart == True and ballrelease == True:
        
       
        if rpos <= 325:
            
            screen.blit(ball, (xpos, rpos))
        
            if xpos <= 700:
                ballregain = False
                xpos += (rposNew / 20)
                print("rpos is: " + str(rpos) + " xpos is: " + str(xpos))
                rpos = (-1*((xpos/600)**2))+((xpos)/150)+rpos
                
            
        if xpos > 700 or rpos > 325:
            xpos = 17
            ballregain = True
        

def player_animations():

    # Animations while the user makes no input
    global c
    global player
    global i

    if nojump == True:
        
        if c % 2 == 0 and i<= 10:
            if i<10:
                screen.blit(player[c], (0, 300))
                i += 1
            if i == 10: 
                c += 1
                i += 1
        
        
        elif c % 2 == 1 and i<= 20:
            if i>10 and i<20:
                screen.blit(player[c], (0, 300))
                i += 1
            if i == 20:
                c -= 1
                i += 1
        
        elif i>20:
            i = 0
            screen.blit(player[c], (0, 300))
    
    if nojump == False:
        screen.fill(0)

def screen_text():

    global score
    global nojump
    global movable

    if nojump == True:

        font = pygame.font.Font("Resources/android.ttf", 16)
        text2 = font.render("Hold space to throw the ball", True, white)
        textRect2 = text2.get_rect()
        textRect2.center = (width // 2, height // 2 + 200)
        screen.blit(text2, textRect2)
        movable = True

    font = pygame.font.Font("Resources/android.ttf", 16)
    text2 = font.render("Score: "+ str(score), True, white)
    textRect2 = text2.get_rect()
    textRect2.center = (width // 2 - 300, height // 2 - 200)
    screen.blit(text2, textRect2)    

def player_jump():

    # Initial animations before the player jumps
    global p, nojump, movable, x, y, janimation, danimation, a, b, motionactivate, limit_reached
    global jumplock, ballrelease, ballregain
    

    if movable == True and keypress[K_SPACE]:
        #print(pygame.time.get_ticks())
        
        motionactivate = True
        #print(nojump)
        #if p >= 19:
        #    p = 0    
    if motionactivate == True:
        #screen.fill(0)
        nojump = False     
        if p < 21:
            screen.blit(launch[p // 4], (0, 300))
            p += 1

        if p == 20: 
            a = pygame.time.get_ticks()
            janimation = True
            p += 1
            
            
    
    #elif keypress[K_SPACE]:
        # what to do when jump is completed
    

    if janimation == True and limit_reached == False:
        
        if keypress[K_SPACE] and pygame.KEYDOWN and jumplock == True:
            b = pygame.time.get_ticks()
            if y > 239:
                y = ((b - a) / -25) + 310
            if y >= 305:
                screen.fill(0)
                screen.blit(shoot[0], (x, y))
            if y < 305 and y > 240:
                screen.blit(shoot[1], (x,y))
            if y <= 239:
                screen.blit(shoot[0], (x, y))
                danimation = True
                limit_reached = True
                #print(danimation)

    if event.type == pygame.KEYUP:
        if event.key == K_SPACE:
            danimation = True
            motionactivate = False
            ballrelease = True
    if danimation == True:
        jumplock = False    
    if danimation == True or limit_reached == True:
        #print("poopc "+ str(y))
        if y < 310:
            screen.blit(shoot[0], (x, y))
            y += 2
            #
            # print("zag")
                #print("poop: " + str(pygame.KEYUP) + " key down is: " + str(pygame.KEYDOWN))
        if y >= 310:
            nojump = True
            danimation = False
            janimation = False
            movable = False
            limit_reached = False
            p = 0
            jumplock = True
            if ballregain == True:
              
                ballrelease = False

            #print("y value is: "+ str(y)+ " a is: "+ str(a) + " b is: "+ str(b)) 
        

        
while 1:
    
    keypress = pygame.key.get_pressed()
    fr.tick(30)
    screen.fill(0)

    if keypress[K_RETURN]:
        gamestart = True

    if gamestart == False:
        #screen.fill(0)
        screen.blit(background, (0,0))

        # Draw opening texts
        font = pygame.font.Font("Resources/android.ttf", 64)
        text = font.render("Portal Hoop", True, white)
        textRect = text.get_rect()
        textRect.center = (width // 2, height // 2 - 100)
        screen.blit(text, textRect)

        font = pygame.font.Font("Resources/android.ttf", 18)
        text2 = font.render("Press Return to start", True, white)
        textRect2 = text2.get_rect()
        textRect2.center = (width // 2, height // 2 + 100)
        screen.blit(text2, textRect2)

        nojump = True

    
    # Check if any

    if gamestart == True:
        #screen.fill(0)
        
        player_animations()
        player_jump()
        basketball()
        screen_text()
       
        
        

    pygame.display.flip()
    pygame.display.set_caption("Portal Hoop")
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit() 
            exit(0)




[((8, 0, 8, 13), 'pygame.init', 'pygame.init', ({}, {}), '()', False, 'import pygame\n'), ((11, 9, 11, 49), 'pygame.display.set_mode', 'pygame.display.set_mode', ({(11, 33, 11, 48): '(width, height)'}, {}), '((width, height))', False, 'import pygame\n'), ((22, 5, 22, 24), 'pygame.time.Clock', 'pygame.time.Clock', ({}, {}), '()', False, 'import pygame\n'), ((32, 13, 32, 52), 'pygame.image.load', 'pygame.image.load', ({(32, 31, 32, 51): '"""Resources/back.jpg"""'}, {}), "('Resources/back.jpg')", False, 'import pygame\n'), ((15, 30, 15, 69), 'pygame.image.load', 'pygame.image.load', ({(15, 48, 15, 68): '"""Resources/ball.png"""'}, {}), "('Resources/ball.png')", False, 'import pygame\n'), ((141, 11, 141, 56), 'pygame.font.Font', 'pygame.font.Font', ({(141, 28, 141, 51): '"""Resources/android.ttf"""', (141, 53, 141, 55): '16'}, {}), "('Resources/android.ttf', 16)", False, 'import pygame\n'), ((229, 15, 229, 39), 'pygame.key.get_pressed', 'pygame.key.get_pressed', ({}, {}), '()', False, 'import pygame\n'), ((269, 4, 269, 25), 'pygame.display.flip', 'pygame.display.flip', ({}, {}), '()', False, 'import pygame\n'), ((270, 4, 270, 45), 'pygame.display.set_caption', 'pygame.display.set_caption', ({(270, 31, 270, 44): '"""Portal Hoop"""'}, {}), "('Portal Hoop')", False, 'import pygame\n'), ((271, 17, 271, 35), 'pygame.event.get', 'pygame.event.get', ({}, {}), '()', False, 'import pygame\n'), ((12, 33, 12, 81), 'pygame.image.load', 'pygame.image.load', ({(12, 51, 12, 80): '"""Resources/Balljump-1(2).png"""'}, {}), "('Resources/Balljump-1(2).png')", False, 'import pygame\n'), ((12, 118, 12, 163), 'pygame.image.load', 'pygame.image.load', ({(12, 136, 12, 162): '"""Resources/Balljump-1.png"""'}, {}), "('Resources/Balljump-1.png')", False, 'import pygame\n'), ((13, 33, 13, 78), 'pygame.image.load', 'pygame.image.load', ({(13, 51, 13, 77): '"""Resources/Balljump-1.png"""'}, {}), "('Resources/Balljump-1.png')", False, 'import pygame\n'), ((13, 115, 13, 163), 'pygame.image.load', 'pygame.image.load', ({(13, 133, 13, 162): '"""Resources/Balljump-1(2).png"""'}, {}), "('Resources/Balljump-1(2).png')", False, 'import pygame\n'), ((13, 199, 13, 244), 'pygame.image.load', 'pygame.image.load', ({(13, 217, 13, 243): '"""Resources/Balljump-2.png"""'}, {}), "('Resources/Balljump-2.png')", False, 'import pygame\n'), ((13, 280, 13, 325), 'pygame.image.load', 'pygame.image.load', ({(13, 298, 13, 324): '"""Resources/Balljump-3.png"""'}, {}), "('Resources/Balljump-3.png')", False, 'import pygame\n'), ((13, 362, 13, 407), 'pygame.image.load', 'pygame.image.load', ({(13, 380, 13, 406): '"""Resources/Balljump-4.png"""'}, {}), "('Resources/Balljump-4.png')", False, 'import pygame\n'), ((14, 32, 14, 77), 'pygame.image.load', 'pygame.image.load', ({(14, 50, 14, 76): '"""Resources/Balljump-5.png"""'}, {}), "('Resources/Balljump-5.png')", False, 'import pygame\n'), ((14, 115, 14, 160), 'pygame.image.load', 'pygame.image.load', ({(14, 133, 14, 159): '"""Resources/Balljump-6.png"""'}, {}), "('Resources/Balljump-6.png')", False, 'import pygame\n'), ((134, 15, 134, 60), 'pygame.font.Font', 'pygame.font.Font', ({(134, 32, 134, 55): '"""Resources/android.ttf"""', (134, 57, 134, 59): '16'}, {}), "('Resources/android.ttf', 16)", False, 'import pygame\n'), ((241, 15, 241, 60), 'pygame.font.Font', 'pygame.font.Font', ({(241, 32, 241, 55): '"""Resources/android.ttf"""', (241, 57, 241, 59): '64'}, {}), "('Resources/android.ttf', 64)", False, 'import pygame\n'), ((247, 15, 247, 60), 'pygame.font.Font', 'pygame.font.Font', ({(247, 32, 247, 55): '"""Resources/android.ttf"""', (247, 57, 247, 59): '18'}, {}), "('Resources/android.ttf', 18)", False, 'import pygame\n'), ((169, 16, 169, 39), 'pygame.time.get_ticks', 'pygame.time.get_ticks', ({}, {}), '()', False, 'import pygame\n'), ((182, 16, 182, 39), 'pygame.time.get_ticks', 'pygame.time.get_ticks', ({}, {}), '()', False, 'import pygame\n'), ((273, 12, 273, 25), 'pygame.quit', 'pygame.quit', ({}, {}), '()', False, 'import pygame\n')]