# =================================================================================================== #
# (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

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

@graphics.mainloop
def main() :
    inputs()
    physics()
    draw()

    if len(blocks) <= 0 :
        quit()

    if lives <= 0 :
        quit()

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

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.run()
