#imports the pygame library, and sys library.
import pygame, sys
#import all your key names
from pygame.locals import *



class Paddle:    
    def __init__(self, x, y):    
        self.x = x
        self.y = y
        self.height = 40
        self.width = 10
        
    def draw(self, canvas):
        pygame.draw.rect(canvas, pygame.Color(0,255,0), (self.x,self.y,self.width,self.height))
    def contains(self, ptX, ptY):
        return self.x < ptX < self.x + self.width & self.y < ptY < self.y + self.height
    def overlaps(self, otherRectangle):
        return otherRectangle.colliderect(Rect(self.x,self.y,self.height, self.width))

class Ball:
    def __init__(self, x, y):    
        #position of ball
        self.x = x
        self.y = y
        
        #speed of ball
        self.dx = 5
        self.dy = 5
        
        self.height = 10
        self.width = 10
         
    def draw(self, canvas):
       pygame.draw.rect(canvas, pygame.Color(0,255,0), (self.x,self.y,self.width,self.height))
       
    def reset(self):
        self.x = 320
        self.y = 240

        self.dx = -self.dx
        self.dy = 5
       
def main():
    #initialize the library and start a clock, just in case we want it.
    pygame.init()

    #initialize the clock that will determine how often we render a frame
    clock = pygame.time.Clock()

    #create our canvas that will be draw on.
    resolution = (640, 480)
    canvas = pygame.display.set_mode(resolution)
    pygame.display.set_caption('Pong')

    #Pick our background color with R,G,B values between 0 and 255
    backgroundColor = pygame.Color(0,0,0)
    greenColor = pygame.Color(0, 255, 0)

    #initialize the scores of each player
    playerOneScore = 0
    playerTwoScore = 0

    #create/initialize a paddle for each side
    paddle1 = Paddle(5, 100)
    paddle2 = Paddle(625, 100)

    #create a ball
    ball = Ball(300, 300)
    
    #keep track of all the keys which are currently pressed
    keysPressed = []

    #for ever and ever, keep rendering a new frame of our game.
    #this is where code that needs to be run every single frame belongs
    while True:
        #get all of our input events that have happened since the last frame
        for event in pygame.event.get():

            #deal with key presses
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                    return
                else:
                    keysPressed.append(event.key) 
                    
            #deal with key releases
            elif event.type == KEYUP:
                keysPressed.remove(event.key)
                    


        #do the physics of pong
        if K_UP in keysPressed:
            paddle2.y = paddle2.y - 8
        if K_DOWN in keysPressed:
            paddle2.y = paddle2.y + 8
        if K_a in keysPressed:
            paddle1.y = paddle1.y - 8
        if K_z in keysPressed:
            paddle1.y = paddle1.y + 8
 
        #Paddle collision
           
        #Wall collision
        if ball.x + ball.dx > 650:
            ball.reset()
            playerOneScore = playerOneScore + 1
        if ball.x + ball.dx < 0:
            ball.reset()
            playerTwoScore = playerTwoScore + 1
        if ball.y + ball.dy > 480 or ball.y + ball.dy < 0:
            ball.dy = -ball.dy
            
        ball.x = ball.x + ball.dx
        ball.y = ball.y + ball.dy

        
        
        
        #Done dealing with events, lets draw updated things on our canvas
        #fill our canvas with a background color, effectively erasing the last frame
        canvas.fill(backgroundColor)

        #draw objects on our
        paddle1.draw(canvas)
        paddle2.draw(canvas)
        ball.draw(canvas)
        
        #done drawing all the stuff on our canvas, now lets show it to the user
        pygame.display.update()

        #wait the amount of time which
        clock.tick(30)
        
    
main()

    
    
