# =================================================================================================== #
# (c) 2018 - BlockBreaker Pro - Developed by Vladimir Ovechkin, Samuel Rath & Vishnu Prasad Sivakumar #
# =================================================================================================== #
#     THIS COMPUTER PROGRAM IS PROTECTED BY COPYRIGHT LAW AND INTERNATIONAL TREATIES. UNAUTHORISED    #
#    REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM, OR ANY PORTION OF IT, MAY RESULT IN SEVERE CIVIL   #
#      AND CRIMINAL PENALTIES, AND WILL BE PROSECUTED TO THE MAXIMUM EXTENT POSSIBLE UNDER THE LAW    #
# =================================================================================================== #

from block import *
from graphics import *
from random import randint

windowWidth = 1080
windowHeight = 720
graphics = Graphics(width = windowWidth, height = windowHeight, title = "BlockBreaker Pro", fps = 120)

columns = 6
rows = 12
spacing = 6

blocks = []
blockX = spacing
blockY = windowHeight - int(windowHeight / 5)
blockWidth = int(windowWidth / columns) - 2 * spacing
blockHeight = int(windowHeight / 20)

rebound = graphics.loadSound("rebound.wav")

paddle = graphics.loadImage("paddle.png")
paddleX = windowWidth / 2 - 80
paddleY = 0
paddleSpeed = randint(4, 8)

ball = graphics.loadImage("ball.png")
ballX = int(windowWidth / 2)
ballY = 30
ballXDirection = "LEFT"
ballYDirection = "UP"
ballXSpeed = randint(3, 8)
ballYSpeed = randint(3, 8)
ballSize = 50

lives = 10

game_state = 0

for column in range(0, columns) :
    for row in range(0, rows) :
        block = Block()
        block.img = graphics.loadImage("block" + str(randint(1, 7)) + ".png")
        block.width = blockWidth
        block.height = blockHeight
        block.x = blockX
        block.y = blockY
        blocks.append(block)
        blockX += int(windowWidth / columns)

    blockX = spacing
    blockY -= int(windowHeight / 20) + spacing

def inputs() :
    global paddleX
    
    if graphics.isKeyDown(KEY_LEFT) and paddleX >= 0 :
        paddleX -= paddleSpeed

    if graphics.isKeyDown(KEY_RIGHT) and paddleX <= windowWidth - 160 :
        paddleX += paddleSpeed

    if graphics.isKeyDown(KEY_P):
        

def Physics() :
    global ballX, ballY, ballSpeed, ballXDirection, ballYDirection, lives
    
    if ballY < windowHeight - ballSize and ballY > 0 :
        if ballX < windowWidth - ballSize and ballX > 0 :
            if ballYDirection == "UP" :
                if ballXDirection == "LEFT" :
                    ballX -= ballXSpeed
                    ballY += ballYSpeed

                else :
                    ballX += ballXSpeed
                    ballY += ballYSpeed

            else :
                if ballXDirection == "LEFT" :
                    ballX -= ballXSpeed
                    ballY -= ballYSpeed

                else :
                    ballX += ballXSpeed
                    ballY -= ballYSpeed

            for block in blocks :
                if ((ballX >= block.x - ballSize and ballX <= block.x) and (ballY <= block.y + blockHeight and ballY >= block.y)) :
                    graphics.playSound(rebound)
                    blocks.remove(block)
                    ballXDirection = "LEFT"

                elif ((ballX <= block.x + blockWidth and ballX >= block.x) and (ballY <= block.y + blockHeight and ballY >= block.y)) :
                    graphics.playSound(rebound)
                    blocks.remove(block)
                    ballXDirection = "RIGHT"

                elif ((ballX >= block.x and ballX <= block.x + blockWidth) and (ballY <= block.y and ballY >= block.y - ballSize )) :
                    graphics.playSound(rebound)
                    blocks.remove(block)
                    ballYDirection = "DOWN"
                    
                elif ((ballX >= block.x and ballX <= block.x + blockWidth) and (ballY <= block.y + blockHeight + ballSize and ballY >= block.y + blockHeight)) :
                    graphics.playSound(rebound)
                    blocks.remove(block)
                    ballYDirection = "UP"

            if (ballX <= paddleX + 160 + int(ballSize / 2)  and ballX >= paddleX - int(ballSize / 2)) and (ballY <= paddleY + 20 and ballY >= paddleY) :
                graphics.playSound(rebound)
                ballYDirection = "UP"
                         
        else :
            if ballYDirection == "UP" :
                if ballXDirection == "LEFT" :
                    graphics.playSound(rebound)
                    ballXDirection = "RIGHT"
                    ballX += ballXSpeed
                    ballY += ballYSpeed

                else :
                    graphics.playSound(rebound)
                    ballXDirection = "LEFT"
                    ballX -= ballXSpeed
                    ballY += ballYSpeed

            else :
                if ballXDirection == "LEFT" :
                    graphics.playSound(rebound)
                    ballXDirection = "RIGHT"
                    ballX += ballXSpeed
                    ballY -= ballYSpeed

                else :
                    graphics.playSound(rebound)
                    ballXDirection = "LEFT"
                    ballX -= ballXSpeed
                    ballY += ballYSpeed 

    else :
        if ballYDirection == "UP" :
            graphics.playSound(rebound)
            ballYDirection = "DOWN"
            
            if ballXDirection == "LEFT" :
                ballX -= ballXSpeed
                ballY -= ballYSpeed

            else :
                ballX += ballXSpeed
                ballY -= ballYSpeed

        else :
            graphics.playSound(rebound)
            lives -= 1
            ballYDirection = "UP"
            
            if ballXDirection == "LEFT" :
                ballX -= ballXSpeed
                ballY += ballYSpeed

            else :
                ballX += ballXSpeed
                ballY += ballYSpeed

def draw() :
    graphics.clear()
    
    for block in blocks :
        graphics.drawImage(block.img, x = block.x, y = block.y, width = block.width, height = block.height)

    graphics.drawText("Lives: " + str(lives), x = int(windowWidth / 50), y = windowHeight - int(windowWidth / 50) - 15, fontName = "Courier New", fontSize = 15)
    graphics.drawImage(paddle, x = paddleX, y = paddleY, width = 160, height = 20)
    graphics.drawImage(ball, x = ballX, y = ballY, width = ballSize, height = ballSize)

@graphics.mainloop
def main() :
    if game_state == 0:
        if jmss.isKeyDown(KEY_SPACE):
            game_state = 1
        #Draws Title Background
        jmss.drawImage(bg_image, x = 0, y = 0, width = 1000, height = 600)
        jmss.drawImage("TitleFont.png", x = 250, y = 425, width = 506, height = 144)
        jmss.drawText("Press SPACE to start game!", fontName = "Times", x = 260, y = 60, fontSize = 30)
        jmss.drawText("Player 1 Controls are 'W' for up and 'S' for down", fontName = "Times", x = 20, y = 325, fontSize = 20)
        jmss.drawText("Player 2 Controls are 'I' for up and 'K' for down", fontName = "Times", x = 20, y = 275, fontSize = 20)
        jmss.drawText("Press 'P' to pause for 10 seconds", fontName = "Times", x = 20, y = 225, fontSize = 20)

    elif game_state == 1:
        inputs()
        Physics()
        draw()

        if len(blocks) <= 0 or lives <= 0:
            game_state = 2

    elif game_state == 2:
        if jmss.isKeyDown(KEY_R):
            game_state = 1
            columns = 6
            rows = 12
            spacing = 6
            blocks = []
            blockX = spacing
            blockY = windowHeight - int(windowHeight / 5)
            blockWidth = int(windowWidth / columns) - 2 * spacing
            blockHeight = int(windowHeight / 20)
            paddleX = windowWidth / 2 - 80
            paddleY = 0
            paddleSpeed = randint(4, 8) 
            ballX = int(windowWidth / 2)
            ballY = 30
            ballXDirection = "LEFT"
            ballYDirection = "UP"
            ballXSpeed = randint(3, 8)
            ballYSpeed = randint(3, 8)
            ballSize = 50
            lives = 10

        if jmss.isKeyDown(KEY_Q):
            quit()

        jmss.clear()
        jmss.drawText("Press Key - R to restart the game", x = 280, y = 55, fontSize = 20)
        jmss.drawText("Press Key - Q to restart the game", x = 280, y = 35, fontSize = 20)
        
graphics.run()
