# playerA= input("Enter first Player Name ")

import pygame
pygame.init()

#importing the paddle class and the ball class
from paddle import Paddle
from ball import Ball

#define some colors
BLACK=(0,0,0)
WHITE=(255,255,255)

#opening a new window
size=(700,500 )
screen= pygame.display.set_mode(size)
pygame.display.set_caption("Pong")

paddleA= Paddle(WHITE, 10, 100)
paddleA.rect.x= 20
paddleA.rect.y= 200
 
paddleB= Paddle(WHITE, 10, 100)
paddleB.rect.x = 670
paddleB.rect.y= 200

ball= Ball(WHITE, 10 , 10)
ball.rect.x= 345
ball.rect.y=195

#This will be a list that will contain all the sprites we intend to
all_sprites_list = pygame.sprite.Group()

#Add the paddle to the group of sprites
all_sprites_list.add(paddleA)
all_sprites_list.add(paddleB)
all_sprites_list.add(ball)



#always true condition unless stated otherwise to keep the game running
carryOn= True

#The clock will be used to control the frame rate of the game
clock= pygame.time.Clock()

#Initialise player scores
scoreA= 0
scoreB= 0

#--------------Main program Loop-------------

while carryOn:
    #Main event
    for event in pygame.event.get(): #User did something 
        if event.type== pygame.QUIT: #If user clicked close
            carryOn= False           #flag that we are done
        elif event.type== pygame.KEYDOWN:
            if event.key==pygame.K_x:      #pressing the x key will quit the game
                text= font.render("Thanks For the Game :) !", .5, WHITE)
                screen.blit(text, (50,230))
                pygame.display.flip()
                clock.tick(1)
                carryOn=False
      
    
    #moving the paddles when the user uses the arrow keys (Player A) or "W/S" (player B)
    keys= pygame.key.get_pressed()
    if keys[pygame.K_w]:
        paddleA.moveUp(5)
    if keys[pygame.K_s]:
        paddleA.moveDown(5)
    if keys[pygame.K_UP]:
        paddleB.moveUp(5)
    if keys[pygame.K_DOWN]:
        paddleB.moveDown(5)
    
    
    #game logic
    all_sprites_list.update()
    
    #check if the ball is bouncing against any of the 4 walls:
    if ball.rect.x>=690:
        scoreA+=1
        ball.velocity[0]= -ball.velocity[0]
    if ball.rect.x<=0:
        scoreB+=1
        ball.velocity[0]= -ball.velocity[0]
    if ball.rect.y>490:
        ball.velocity[1]= -ball.velocity[1]
    if ball.rect.y<0:
        ball.velocity[1]= -ball.velocity[1]
    
    
    #Detect collisions between the ball and the paddles
    if pygame.sprite.collide_mask(ball, paddleA) or pygame.sprite.collide_mask(ball, paddleB):
        ball.bounce()
    
    #--------DRAWING CODE--------
    
    
    #first, clear the screen to black
    screen.fill(BLACK)
    
    #Draw the net between
    pygame.draw.line(screen, WHITE, [349,0],[349,500],5)
    
    #Now let's draw all the sprites in one go.
    all_sprites_list.draw(screen)
    
    #Display scores:
    font= pygame.font.Font(None, 74)
    text= font.render(str("Player A " + str(scoreA)), .5  , WHITE)
    screen.blit(text, (10,10))
    text= font.render(str("Player B "+str(scoreB)), 1, WHITE)
    screen.blit(text, (420,10))
    
    #TO bring the change in the screen update the screen
    pygame.display.flip()
    
    # limit frames to any limit you wish
    clock.tick(60)
    
#once we have exited the main program loop we can stop the game engine

pygame.quit()
    

