<commit_before><!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>Breakout</title>
            <style type="text/css">
                canvas {
                border:1px dashed #000000;
                }
            </style>
    </head>

    <body onload="init()">
        <canvas id="canvas" width="800" height="600">
            Navegador não compatível com HTML5 Canvas
        </canvas>
        <p id="info"></p>

        <script>

var canvas, context, btnStart, relogio,intervalo,info_display;
var width, height;
var snd_bola, snd_gameover, snd_inicial, snd_musica;
var gameFieldWidth, gameFieldHeight;
var heroFieldWidth, heroFieldHeight;
var frameWidth, frameHeight;
var posXGame, posYGame;
var playerPosX, playerPosY;
var playerWidth, playerHeight;
var ballWidth, ballHeight;
var ballPosX, ballPosY, ballRaio, previousBallPosX;
var manPosX, manPosY;
var ballUp, ballAngle, setBallOnce;
var imgBarra, imgBall, imgFrame, imgBackground, imgLife;
var imgMan, imgManLeft, imgManRight;
var imgGameOver, imgInicial, imgButtonRestart, imgWinGame;
var leftPressed, rightPressed, previousPressed;
var speedPlayer, speedBall;
var wallMatrix, addNumRows;
var wallNumRows, wallNumColumns, wallNumRowsDraw, wallNumColumnsDraw;
var wallWidth, wallHeight, wallMargin;
var startGame, gameOver, winGame;
var score;
var showInicial;
var totalBlocks;
var totalLife;
var isGameOver;

function init() {
    canvas = document.getElementById("canvas");
    context = canvas.getContext("2d");
    btnStart = document.getElementById("btStart");
    info_display = document.getElementById("info");

    snd_bola = document.getElementById("bola");
    snd_gameover = document.getElementById("gameOver");
    snd_inicial = document.getElementById("inicial");
    snd_musica = document.getElementById("musica")

    width = canvas.width;
    height = canvas.height;

    gameFieldWidth = 560;
    gameFieldHeight = 500;
    heroFieldWidth = 120;
    heroFieldHeight = 520;
    frameWidth = 40;
    frameHeight = 40;

    posXGame = 40;
    posYGame = 40;

    startGame = false;
    showInicial = true;
    gameOver = false;
    winGame = false;
    totalLife = 3;

    imgGameOver = new Image();
    imgGameOver.src = "../assets/game_over.png";

    imgFrame = new Image();
    imgFrame.src = "../assets/frame.png";

    imgBackground = new Image();
    imgBackground.src = "../assets/background.png";

    imgManLeft = new Image();
    imgManLeft.src = "../assets/man_left.png";

    imgManLeft = new Image();
    imgManLeft.src = "../assets/man_left.png";

    imgManRight = new Image();
    imgManRight.src = "../assets/man_right.png";

    imgMan = new Image();
    imgMan.src =  "../assets/man_right.png";

    imgLife = new Image();
    imgLife.src =  "../assets/life.png";

    imgInicial = new Image();
    imgInicial.src =  "../assets/inicial.png";

    imgButtonRestart = new Image();
    imgButtonRestart.src =  "../assets/button_restart.png";

    imgWinGame = new Image();
    imgWinGame.src =  "../assets/win.png";

    manPosY = height - posYGame - 107;
    manPosX = ((width - posXGame) + ((width - 2 * posXGame) - 120)) / 2;

    playerWidth = 90;
    playerHeight = 30;
    imgBarra = new Image();
    imgBarra.src = "../assets/barra.png";

    playerPosX = posXGame + ((gameFieldWidth - playerWidth) / 2);
    playerPosY = posYGame + gameFieldHeight - playerHeight;

    ballWidth = 20;
    ballHeight = 20;
    speedBall = 10;
    ballRaio = ballWidth / 2;
    ballPosX = posXGame + (gameFieldWidth / 2);
    ballPosY = posYGame + (gameFieldHeight - playerHeight - ballRaio);
    previousBallPosX = ballPosX;
    ballAngle = Math.floor(Math.random() * 21) - 10;
    ballUp = false;
    setBallOnce = false;
    imgBall = new Image();
    imgBall.src = "../assets/bola.png";

    leftPressed = false;
    rightPressed = false;
    speedPlayer = 10;

    snd_bola.volume = 0.7;

    wallMatrix = new Array();
    wallWidth = 56;

    wallHeight = 30;
    wallNumRows = 13;

    wallNumColumns = 10;
    wallNumRowsDraw = 5;
    wallNumColumnsDraw = 10;
    wallMargin = 0;
    totalBlocks = wallNumRowsDraw * wallNumColumnsDraw;
    addNumRows = false;
    createWall();

    score = 0;
    isGameOver = true;

    intervalo = 20;

    document.addEventListener('keyup', keyUp, false);
    document.addEventListener('keydown', keyDown, false);
    setInterval(gameLoop, 30);
    setInterval(addWallNumRows, 10000);
}

function resetGame() {

    gameOver = false;
    isGameOver = true;
    startGame = false;
    showInicial = false;
    winGame = false;
    totalLife = 3;

    manPosY = height - posYGame - 107;
    manPosX = ((width - posXGame) + ((width - 2 * posXGame) - 120)) / 2;

    playerPosX = posXGame + ((gameFieldWidth - playerWidth) / 2);
    playerPosY = posYGame + gameFieldHeight - playerHeight;

    ballPosX = posXGame + (gameFieldWidth / 2);
    ballPosY = posYGame + (gameFieldHeight - playerHeight - ballRaio);
    ballAngle = Math.floor(Math.random() * 21) - 10;
    ballUp = false;

    leftPressed = false;
    rightPressed = false;

    wallNumRowsDraw = 5;
    wallNumColumnsDraw = 10;
    addNumRows = false;

    score = 0;
    createWall();

    drawGame();
}

function drawFrame() {
    context.drawImage(imgFrame, 0, 0, canvas.width, canvas.height);
}

function drawInicial() {
    context.drawImage(imgInicial, 0, 0, canvas.width, canvas.height);
}

function drawWinGame() {
    context.drawImage(imgWinGame, 0, 0, canvas.width, canvas.height);
}

function Pausar() {
    startGame = !startGame;
    if (startGame) {
        relogio = setInterval("Desenhar()" , intervalo);
        info_display.innerHTML = "Executando...";
        btnStart.innerHTML = "Pausar";
    } else {
        clearInterval(relogio);
        info_display.innerHTML = "Pausa";
        btnStart.innerHTML = "Iniciar";
    }
}

function drawGameField() {
    context.fillStyle = "white";
    context.fillRect(0, 0, gameFieldWidth, gameFieldHeight);
}

function drawBackground() {
    context.drawImage(imgBackground, 0, 0, width, height);
}

function drawBar(img, posX, posY) {
    context.drawImage(img, posX, posY, playerWidth, playerHeight);
}

function drawBall(img, posX, posY) {
    context.drawImage(img, posX, posY, ballWidth, ballHeight);
}

function drawMan() {
    context.drawImage(imgMan, manPosX, manPosY);
}

function drawLife() {
    var marginLife = 120 / imgLife.width;
    var widthLife = imgLife.width + marginLife;
    var widthLifes = totalLife * widthLife;
    var posXLife = (gameFieldWidth + (2 * posXGame) + width - posXGame - widthLifes) / 2;
    var posYLife = 3 * frameHeight + 50;

    for(var i = 0; i<totalLife; i++){
        context.drawImage(imgLife, posXLife + (widthLife * i), posYLife);
    }

    if(totalLife == 0) {
        gameOver = true;
    }
}

function gameOverFunction() {
    startGame = false;
    drawGameOver();
}

function drawGameOver() {
    context.drawImage(imgGameOver, 0, 0, width, height);
    context.drawImage(imgButtonRestart, (width - imgButtonRestart.width) / 2,
        (height - imgButtonRestart.height) / 2);
}

function createWall() {
     var img2 = new Image();
     img2.src = "../assets/bloco.png";

    for (var i = 0; i < wallNumRows; i++) {
        wallMatrix[i] = new Array();
        for (var j = 0; j < wallNumColumns; j++) {
            wallMatrix[i][j] = img2;
        }
    }
}

function drawWall() {
    var contRows = 0;
    var contBlocks = wallNumRows * wallNumColumns;
    var isLineExist = false;

    for (var i = wallNumRowsDraw - 1; i >= 0 ; i--) {
        isLineExist  = false;
        for (var j = wallNumColumnsDraw - 1; j >= 0; j--) {
            var img = wallMatrix[i][j];
            if (img != null) {
                isLineExist = true;
                context.drawImage(img, posXGame + ((wallWidth + wallMargin) * j),
                    3 * posYGame + ((wallHeight + wallMargin) * contRows),
                    wallWidth, wallHeight);
            } else {
                if (wallNumRowsDraw == wallNumRows) {
                    isLineExist = true;
            	}
                totalBlocks--;
                contBlocks--;
            }
        }

        if (isLineExist) {
            contRows++;
        }
    }

    if (addNumRows && wallNumRowsDraw < wallNumRows) {
        wallNumRowsDraw++;
        addNumRows = false;
        totalBlocks += wallNumColumns;
    }

    if (startGame && playerPosY <= (posYGame + 40 + ((wallHeight + wallMargin) * contRows))) {
        gameOver = true;
    }

    if (contBlocks==0) {
        winGame = true;
    }
}

function drawGameScore() {
    context.font = "28pt Helvetica";
    context.fillStyle = "white";
    context.fillText("Score", canvas.width - heroFieldWidth - frameWidth + 15, 2 * frameHeight);
    if (score >= 100000) {
        context.font = "22pt Helvetica";
    }
    context.fillText(score, canvas.width - heroFieldWidth - frameWidth + 15, 3 * frameHeight + 8);
}

function drawGame() {
    context.clearRect(0, 0, canvas.width, canvas.height);
    drawGameField();
    drawBackground();
    drawMan();
    drawBar(imgBarra, playerPosX, playerPosY);
    drawBall(imgBall, ballPosX - ballRaio, ballPosY - ballRaio);
    drawWall();
    drawFrame();
    drawGameScore();
    drawLife();
}

function keyDown(e) {
    if (e.keyCode == 37) {
        if (previousPressed == 39) {
            imgMan.src = imgManLeft.src;
        }
        leftPressed = true;
        previousPressed = 37;
    } else if (e.keyCode == 39) {
        if (previousPressed == 37) {
            imgMan.src = imgManRight.src;
        }
        rightPressed = true;
        previousPressed = 39;
    }
}

function keyUp(e) {
    if (e.keyCode == 37) {
        leftPressed = false;
    } else if (e.keyCode == 39) {
        rightPressed = false;
    } else if (e.keyCode == 82) {
        if (gameOver) {
            resetGame();
            snd_musica.loop = true;
        	snd_musica.play();
        }
    } else if(e.keyCode == 73) {
        showInicial = false;
        snd_inicial.pause();
        snd_musica.loop = true;
    	snd_musica.play();
    }
}

function getBarPosition() {
    if (leftPressed != rightPressed) {
        if (leftPressed) {
            if (playerPosX > posXGame) {
                playerPosX -= speedPlayer;
            }
            if (playerPosX < posXGame) {
                playerPosX = posXGame;
            }
        } else {
            if (playerPosX < (gameFieldWidth - playerWidth) + posXGame) {
                playerPosX += speedPlayer;
            }

            if (playerPosX > (gameFieldWidth - playerWidth) + posXGame) {
                playerPosX = gameFieldWidth - playerWidth + posXGame;
            }
        }
    }
}

function getBallPosition() {
    if ((ballPosY - ballRaio) <= posYGame) {
        ballUp = false;
    } else if (ballPosY + ballRaio >= playerPosY) {
        if ((ballPosX-ballRaio >= playerPosX-(ballWidth*0.8)) &&
                (ballPosX + ballRaio <= playerPosX + playerWidth + (ballWidth*0.8))) {
            ballUp = true;
            calculateAngle();
        }

        if (ballPosY + ballRaio > playerPosY) {
            startGame = false;
            totalLife--;
            resetBall();
        }
    }
    if (startGame){
        if ((ballPosX - ballRaio <= posXGame) ||
                (ballPosX + ballRaio > gameFieldWidth + posXGame)) {

            if ((ballPosX - ballRaio <= posXGame) && (ballPosX + ballAngle <= posXGame)) {
                ballPosX = posXGame + ballRaio;
            } else if ((ballPosX + ballRaio > gameFieldWidth + posXGame) &&
                    (ballPosX - ballAngle >= gameFieldWidth + posXGame)) {
                ballPosX = gameFieldWidth - ballRaio;
            }
            ballAngle = -ballAngle;
        }
        previousBallPosX = ballPosX;
        ballPosX += ballAngle;

        if (ballUp) {
            ballPosY -= speedBall;
        } else {
            ballPosY += speedBall;
        }
    }
}

function calculateAngle() {
    var divBar = playerWidth / 5;
    var divBar1 = playerPosX + divBar;
    var divBar2 = divBar1 + divBar;
    var divBar3 = divBar2 + divBar;
    var divBar4 = divBar3 + divBar;

    if ((ballPosX >= playerPosX) && (ballPosX < divBar1)) {
        ballAngle = Math.floor((Math.random() * 4)) + 6;
    } else if ((ballPosX >= divBar1) && (ballPosX < divBar2)) {
        ballAngle = Math.floor((Math.random() * 3)) + 3;
    } else if ((ballPosX >= divBar2) && (ballPosX < divBar3)) {
        ballAngle = Math.floor((Math.random() * 4));
    } else if ((ballPosX >= divBar3) && (ballPosX < divBar4)) {
        ballAngle = Math.floor((Math.random() * 3)) + 3;
    } else if ((ballPosX >= divBar4) && (ballPosX <= playerPosX + playerWidth)) {
        ballAngle = Math.floor((Math.random() * 4)) + 6;
    }

    if (ballPosX - previousBallPosX < 0) {
        ballAngle = -ballAngle;
    }
}

function checkCollisionWall(){
    var contRows = 0;
    for (var i = wallNumRowsDraw - 1; i >= 0 ; i--) {
        for (var j = wallNumColumnsDraw - 1; j >= 0; j--) {
            if (wallMatrix[i][j]) {
                var posXwall = posXGame + ((wallWidth + wallMargin) * j) ;
                var posYwall = ((wallHeight + wallMargin) * contRows) + 3*posYGame;
                if (ballCollidedBlock(posXwall, posYwall)) {
                    changeBallDirection(posXwall, posYwall);
                    wallMatrix[i][j] = null;
                    score = (score + 1) % 1000000;
                    totalBlocks--;
                    snd_bola.play();
                    if (totalBlocks == 0) {
                        addWallNumRows();
                    }
                }
            }
        }
        contRows++;
        setBallOnce = false;
    }
}

function ballCollidedBlock(posXwall, posYwall) {
    var ballCollidesHorizontally = (ballPosX - ballRaio >= posXwall - ballWidth) &&
        (ballPosX + ballRaio <= posXwall + wallWidth + ballWidth);
    var ballCollidesVertically = (ballPosY - ballRaio >= posYwall - ballHeight) &&
        (ballPosY + ballRaio <= posYwall + wallHeight + ballHeight);

    return (ballCollidesHorizontally && ballCollidesVertically);
}

function isBallCollidingHorizontally(posXwall, posYwall) {
    var halfRaio = ballRaio / 2;

    return (ballPosX + halfRaio > posXwall) && (ballPosX - halfRaio < posXwall + wallWidth);
}

function isBallCollidingVertically(posXwall, posYwall) {
    var halfRaio = ballRaio / 2;

    return (ballPosY + halfRaio > posYwall) && (ballPosY - halfRaio < posYwall + wallHeight);
}

function changeBallDirection(posXwall, posYwall) {
    var ballCollidedHorizontally = isBallCollidingHorizontally(posXwall, posYwall);
    var ballCollidedVertically = isBallCollidingVertically(posXwall, posYwall);

    if (ballCollidedHorizontally) {
        if (setBallOnce == false) {
            ballUp = !ballUp;
            setBallOnce = true;
        }
    }
    if (ballCollidedVertically) {
        ballAngle = -ballAngle;
    }
}

function gameLoop() {
    if (gameOver) {
        gameOverFunction();
        if (isGameOver == true) {
            isGameOver = false;
            snd_musica.pause();
            snd_gameover.play();
        }
    } else if (winGame) {
        drawWinGame();
        snd_musica.pause();
        snd_vitoria.play();
    } else if (!startGame && showInicial) {
        drawInicial();
        snd_inicial.play();
    } else if (startGame) {
        checkCollisionWall();
        getBarPosition();
        getBallPosition();
        drawGame();
    } else {
        drawInicial();
        checkStartGame();
        drawGame();
    }
}

function checkStartGame() {
    if (leftPressed) {
        previousPressed = 37;
        startGame = true;
    } else if (rightPressed) {
        previousPressed = 39;
        startGame = true;
    }
}

function resetBall() {
    playerPosX = posXGame + ((gameFieldWidth - playerWidth) / 2);
    playerPosY = posYGame + gameFieldHeight - playerHeight;

    ballPosX = posXGame + (gameFieldWidth / 2);
    ballPosY = posYGame + gameFieldHeight - playerHeight-ballRaio;
    ballAngle = Math.floor(Math.random() * 21) - 10;
}

function addWallNumRows() {
    if(startGame) {
        addNumRows = true;
    }
}
        </script>
        <audio id='bola'>
        <source src="sounds/Bola.mp3" type="audio/mp3">
        Seu browser não suporta o formato do audio.
    </audio>
    <audio id='gameOver'>
        <source src="sounds/Gameover.mp3" type="audio/mp3">
        Seu browser não suporta o formato do audio.
    </audio>
    <audio id='inicial'>
        <source src="sounds/Inicial.mp3" type="audio/mp3">
        Seu browser não suporta o formato do audio.
    </audio>
    <audio id='musica'>
        <source src="sounds/Musica2.mp3" type="audio/mp3">
        Seu browser não suporta o formato do audio.
    </audio>
        <audio id='vitoria'>
        <source src="sounds/Victory.mp3" type="audio/mp3">
        Seu browser não suporta o formato do audio.
    </audio>
    </body>
</html>

<commit_msg>Adicionando som de vitoria<commit_after><!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>Breakout</title>
            <style type="text/css">
                canvas {
                border:1px dashed #000000;
                }
            </style>
    </head>

    <body onload="init()">
        <canvas id="canvas" width="800" height="600">
            Navegador não compatível com HTML5 Canvas
        </canvas>
        <p id="info"></p>

        <script>

var canvas, context, btnStart, relogio,intervalo,info_display;
var width, height;
var snd_bola, snd_gameover, snd_inicial, snd_musica, snd_vitoria;
var gameFieldWidth, gameFieldHeight;
var heroFieldWidth, heroFieldHeight;
var frameWidth, frameHeight;
var posXGame, posYGame;
var playerPosX, playerPosY;
var playerWidth, playerHeight;
var ballWidth, ballHeight;
var ballPosX, ballPosY, ballRaio, previousBallPosX;
var manPosX, manPosY;
var ballUp, ballAngle, setBallOnce;
var imgBarra, imgBall, imgFrame, imgBackground, imgLife;
var imgMan, imgManLeft, imgManRight;
var imgGameOver, imgInicial, imgButtonRestart, imgWinGame;
var leftPressed, rightPressed, previousPressed;
var speedPlayer, speedBall;
var wallMatrix, addNumRows;
var wallNumRows, wallNumColumns, wallNumRowsDraw, wallNumColumnsDraw;
var wallWidth, wallHeight, wallMargin;
var startGame, gameOver, winGame;
var score;
var showInicial;
var totalBlocks;
var totalLife;
var isGameOver;

function init() {
    canvas = document.getElementById("canvas");
    context = canvas.getContext("2d");
    btnStart = document.getElementById("btStart");
    info_display = document.getElementById("info");

    snd_bola = document.getElementById("bola");
    snd_gameover = document.getElementById("gameOver");
    snd_inicial = document.getElementById("inicial");
    snd_musica = document.getElementById("musica");
    snd_vitoria = document.getElementById("vitoria");

    width = canvas.width;
    height = canvas.height;

    gameFieldWidth = 560;
    gameFieldHeight = 500;
    heroFieldWidth = 120;
    heroFieldHeight = 520;
    frameWidth = 40;
    frameHeight = 40;

    posXGame = 40;
    posYGame = 40;

    startGame = false;
    showInicial = true;
    gameOver = false;
    winGame = false;
    totalLife = 3;

    imgGameOver = new Image();
    imgGameOver.src = "../assets/game_over.png";

    imgFrame = new Image();
    imgFrame.src = "../assets/frame.png";

    imgBackground = new Image();
    imgBackground.src = "../assets/background.png";

    imgManLeft = new Image();
    imgManLeft.src = "../assets/man_left.png";

    imgManLeft = new Image();
    imgManLeft.src = "../assets/man_left.png";

    imgManRight = new Image();
    imgManRight.src = "../assets/man_right.png";

    imgMan = new Image();
    imgMan.src =  "../assets/man_right.png";

    imgLife = new Image();
    imgLife.src =  "../assets/life.png";

    imgInicial = new Image();
    imgInicial.src =  "../assets/inicial.png";

    imgButtonRestart = new Image();
    imgButtonRestart.src =  "../assets/button_restart.png";

    imgWinGame = new Image();
    imgWinGame.src =  "../assets/win.png";

    manPosY = height - posYGame - 107;
    manPosX = ((width - posXGame) + ((width - 2 * posXGame) - 120)) / 2;

    playerWidth = 90;
    playerHeight = 30;
    imgBarra = new Image();
    imgBarra.src = "../assets/barra.png";

    playerPosX = posXGame + ((gameFieldWidth - playerWidth) / 2);
    playerPosY = posYGame + gameFieldHeight - playerHeight;

    ballWidth = 20;
    ballHeight = 20;
    speedBall = 10;
    ballRaio = ballWidth / 2;
    ballPosX = posXGame + (gameFieldWidth / 2);
    ballPosY = posYGame + (gameFieldHeight - playerHeight - ballRaio);
    previousBallPosX = ballPosX;
    ballAngle = Math.floor(Math.random() * 21) - 10;
    ballUp = false;
    setBallOnce = false;
    imgBall = new Image();
    imgBall.src = "../assets/bola.png";

    leftPressed = false;
    rightPressed = false;
    speedPlayer = 10;

    snd_bola.volume = 0.7;

    wallMatrix = new Array();
    wallWidth = 56;

    wallHeight = 30;
    wallNumRows = 13;

    wallNumColumns = 10;
    wallNumRowsDraw = 5;
    wallNumColumnsDraw = 10;
    wallMargin = 0;
    totalBlocks = wallNumRowsDraw * wallNumColumnsDraw;
    addNumRows = false;
    createWall();

    score = 0;
    isGameOver = true;

    intervalo = 20;

    document.addEventListener('keyup', keyUp, false);
    document.addEventListener('keydown', keyDown, false);
    setInterval(gameLoop, 30);
    setInterval(addWallNumRows, 10000);
}

function resetGame() {

    gameOver = false;
    isGameOver = true;
    startGame = false;
    showInicial = false;
    winGame = false;
    totalLife = 3;

    manPosY = height - posYGame - 107;
    manPosX = ((width - posXGame) + ((width - 2 * posXGame) - 120)) / 2;

    playerPosX = posXGame + ((gameFieldWidth - playerWidth) / 2);
    playerPosY = posYGame + gameFieldHeight - playerHeight;

    ballPosX = posXGame + (gameFieldWidth / 2);
    ballPosY = posYGame + (gameFieldHeight - playerHeight - ballRaio);
    ballAngle = Math.floor(Math.random() * 21) - 10;
    ballUp = false;

    leftPressed = false;
    rightPressed = false;

    wallNumRowsDraw = 5;
    wallNumColumnsDraw = 10;
    addNumRows = false;

    score = 0;
    createWall();

    drawGame();
}

function drawFrame() {
    context.drawImage(imgFrame, 0, 0, canvas.width, canvas.height);
}

function drawInicial() {
    context.drawImage(imgInicial, 0, 0, canvas.width, canvas.height);
}

function drawWinGame() {
    context.drawImage(imgWinGame, 0, 0, canvas.width, canvas.height);
}

function Pausar() {
    startGame = !startGame;
    if (startGame) {
        relogio = setInterval("Desenhar()" , intervalo);
        info_display.innerHTML = "Executando...";
        btnStart.innerHTML = "Pausar";
    } else {
        clearInterval(relogio);
        info_display.innerHTML = "Pausa";
        btnStart.innerHTML = "Iniciar";
    }
}

function drawGameField() {
    context.fillStyle = "white";
    context.fillRect(0, 0, gameFieldWidth, gameFieldHeight);
}

function drawBackground() {
    context.drawImage(imgBackground, 0, 0, width, height);
}

function drawBar(img, posX, posY) {
    context.drawImage(img, posX, posY, playerWidth, playerHeight);
}

function drawBall(img, posX, posY) {
    context.drawImage(img, posX, posY, ballWidth, ballHeight);
}

function drawMan() {
    context.drawImage(imgMan, manPosX, manPosY);
}

function drawLife() {
    var marginLife = 120 / imgLife.width;
    var widthLife = imgLife.width + marginLife;
    var widthLifes = totalLife * widthLife;
    var posXLife = (gameFieldWidth + (2 * posXGame) + width - posXGame - widthLifes) / 2;
    var posYLife = 3 * frameHeight + 50;

    for(var i = 0; i<totalLife; i++){
        context.drawImage(imgLife, posXLife + (widthLife * i), posYLife);
    }

    if(totalLife == 0) {
        gameOver = true;
    }
}

function gameOverFunction() {
    startGame = false;
    drawGameOver();
}

function drawGameOver() {
    context.drawImage(imgGameOver, 0, 0, width, height);
    context.drawImage(imgButtonRestart, (width - imgButtonRestart.width) / 2,
        (height - imgButtonRestart.height) / 2);
}

function createWall() {
     var img2 = new Image();
     img2.src = "../assets/bloco.png";

    for (var i = 0; i < wallNumRows; i++) {
        wallMatrix[i] = new Array();
        for (var j = 0; j < wallNumColumns; j++) {
            wallMatrix[i][j] = img2;
        }
    }
}

function drawWall() {
    var contRows = 0;
    var contBlocks = wallNumRows * wallNumColumns;
    var isLineExist = false;

    for (var i = wallNumRowsDraw - 1; i >= 0 ; i--) {
        isLineExist  = false;
        for (var j = wallNumColumnsDraw - 1; j >= 0; j--) {
            var img = wallMatrix[i][j];
            if (img != null) {
                isLineExist = true;
                context.drawImage(img, posXGame + ((wallWidth + wallMargin) * j),
                    3 * posYGame + ((wallHeight + wallMargin) * contRows),
                    wallWidth, wallHeight);
            } else {
                if (wallNumRowsDraw == wallNumRows) {
                    isLineExist = true;
            	}
                totalBlocks--;
                contBlocks--;
            }
        }

        if (isLineExist) {
            contRows++;
        }
    }

    if (addNumRows && wallNumRowsDraw < wallNumRows) {
        wallNumRowsDraw++;
        addNumRows = false;
        totalBlocks += wallNumColumns;
    }

    if (startGame && playerPosY <= (posYGame + 40 + ((wallHeight + wallMargin) * contRows))) {
        gameOver = true;
    }

    if (contBlocks==0) {
        winGame = true;
    }
}

function drawGameScore() {
    context.font = "28pt Helvetica";
    context.fillStyle = "white";
    context.fillText("Score", canvas.width - heroFieldWidth - frameWidth + 15, 2 * frameHeight);
    if (score >= 100000) {
        context.font = "22pt Helvetica";
    }
    context.fillText(score, canvas.width - heroFieldWidth - frameWidth + 15, 3 * frameHeight + 8);
}

function drawGame() {
    context.clearRect(0, 0, canvas.width, canvas.height);
    drawGameField();
    drawBackground();
    drawMan();
    drawBar(imgBarra, playerPosX, playerPosY);
    drawBall(imgBall, ballPosX - ballRaio, ballPosY - ballRaio);
    drawWall();
    drawFrame();
    drawGameScore();
    drawLife();
}

function keyDown(e) {
    if (e.keyCode == 37) {
        if (previousPressed == 39) {
            imgMan.src = imgManLeft.src;
        }
        leftPressed = true;
        previousPressed = 37;
    } else if (e.keyCode == 39) {
        if (previousPressed == 37) {
            imgMan.src = imgManRight.src;
        }
        rightPressed = true;
        previousPressed = 39;
    }
}

function keyUp(e) {
    if (e.keyCode == 37) {
        leftPressed = false;
    } else if (e.keyCode == 39) {
        rightPressed = false;
    } else if (e.keyCode == 82) {
        if (gameOver) {
            resetGame();
            snd_musica.loop = true;
        	snd_musica.play();
        }
    } else if(e.keyCode == 73) {
        showInicial = false;
        snd_inicial.pause();
        snd_musica.loop = true;
    	snd_musica.play();
    }
}

function getBarPosition() {
    if (leftPressed != rightPressed) {
        if (leftPressed) {
            if (playerPosX > posXGame) {
                playerPosX -= speedPlayer;
            }
            if (playerPosX < posXGame) {
                playerPosX = posXGame;
            }
        } else {
            if (playerPosX < (gameFieldWidth - playerWidth) + posXGame) {
                playerPosX += speedPlayer;
            }

            if (playerPosX > (gameFieldWidth - playerWidth) + posXGame) {
                playerPosX = gameFieldWidth - playerWidth + posXGame;
            }
        }
    }
}

function getBallPosition() {
    if ((ballPosY - ballRaio) <= posYGame) {
        ballUp = false;
    } else if (ballPosY + ballRaio >= playerPosY) {
        if ((ballPosX-ballRaio >= playerPosX-(ballWidth*0.8)) &&
                (ballPosX + ballRaio <= playerPosX + playerWidth + (ballWidth*0.8))) {
            ballUp = true;
            calculateAngle();
        }

        if (ballPosY + ballRaio > playerPosY) {
            startGame = false;
            totalLife--;
            resetBall();
        }
    }
    if (startGame){
        if ((ballPosX - ballRaio <= posXGame) ||
                (ballPosX + ballRaio > gameFieldWidth + posXGame)) {

            if ((ballPosX - ballRaio <= posXGame) && (ballPosX + ballAngle <= posXGame)) {
                ballPosX = posXGame + ballRaio;
            } else if ((ballPosX + ballRaio > gameFieldWidth + posXGame) &&
                    (ballPosX - ballAngle >= gameFieldWidth + posXGame)) {
                ballPosX = gameFieldWidth - ballRaio;
            }
            ballAngle = -ballAngle;
        }
        previousBallPosX = ballPosX;
        ballPosX += ballAngle;

        if (ballUp) {
            ballPosY -= speedBall;
        } else {
            ballPosY += speedBall;
        }
    }
}

function calculateAngle() {
    var divBar = playerWidth / 5;
    var divBar1 = playerPosX + divBar;
    var divBar2 = divBar1 + divBar;
    var divBar3 = divBar2 + divBar;
    var divBar4 = divBar3 + divBar;

    if ((ballPosX >= playerPosX) && (ballPosX < divBar1)) {
        ballAngle = Math.floor((Math.random() * 4)) + 6;
    } else if ((ballPosX >= divBar1) && (ballPosX < divBar2)) {
        ballAngle = Math.floor((Math.random() * 3)) + 3;
    } else if ((ballPosX >= divBar2) && (ballPosX < divBar3)) {
        ballAngle = Math.floor((Math.random() * 4));
    } else if ((ballPosX >= divBar3) && (ballPosX < divBar4)) {
        ballAngle = Math.floor((Math.random() * 3)) + 3;
    } else if ((ballPosX >= divBar4) && (ballPosX <= playerPosX + playerWidth)) {
        ballAngle = Math.floor((Math.random() * 4)) + 6;
    }

    if (ballPosX - previousBallPosX < 0) {
        ballAngle = -ballAngle;
    }
}

function checkCollisionWall(){
    var contRows = 0;
    for (var i = wallNumRowsDraw - 1; i >= 0 ; i--) {
        for (var j = wallNumColumnsDraw - 1; j >= 0; j--) {
            if (wallMatrix[i][j]) {
                var posXwall = posXGame + ((wallWidth + wallMargin) * j) ;
                var posYwall = ((wallHeight + wallMargin) * contRows) + 3*posYGame;
                if (ballCollidedBlock(posXwall, posYwall)) {
                    changeBallDirection(posXwall, posYwall);
                    wallMatrix[i][j] = null;
                    score = (score + 1) % 1000000;
                    totalBlocks--;
                    snd_bola.play();
                    if (totalBlocks == 0) {
                        addWallNumRows();
                    }
                }
            }
        }
        contRows++;
        setBallOnce = false;
    }
}

function ballCollidedBlock(posXwall, posYwall) {
    var ballCollidesHorizontally = (ballPosX - ballRaio >= posXwall - ballWidth) &&
        (ballPosX + ballRaio <= posXwall + wallWidth + ballWidth);
    var ballCollidesVertically = (ballPosY - ballRaio >= posYwall - ballHeight) &&
        (ballPosY + ballRaio <= posYwall + wallHeight + ballHeight);

    return (ballCollidesHorizontally && ballCollidesVertically);
}

function isBallCollidingHorizontally(posXwall, posYwall) {
    var halfRaio = ballRaio / 2;

    return (ballPosX + halfRaio > posXwall) && (ballPosX - halfRaio < posXwall + wallWidth);
}

function isBallCollidingVertically(posXwall, posYwall) {
    var halfRaio = ballRaio / 2;

    return (ballPosY + halfRaio > posYwall) && (ballPosY - halfRaio < posYwall + wallHeight);
}

function changeBallDirection(posXwall, posYwall) {
    var ballCollidedHorizontally = isBallCollidingHorizontally(posXwall, posYwall);
    var ballCollidedVertically = isBallCollidingVertically(posXwall, posYwall);

    if (ballCollidedHorizontally) {
        if (setBallOnce == false) {
            ballUp = !ballUp;
            setBallOnce = true;
        }
    }
    if (ballCollidedVertically) {
        ballAngle = -ballAngle;
    }
}

function gameLoop() {
    if (gameOver) {
        gameOverFunction();
        if (isGameOver == true) {
            isGameOver = false;
            snd_musica.pause();
            snd_gameover.play();
        }
    } else if (winGame) {
        drawWinGame();
        snd_musica.pause();
        snd_vitoria.play();
    } else if (!startGame && showInicial) {
        drawInicial();
        snd_inicial.play();
    } else if (startGame) {
        checkCollisionWall();
        getBarPosition();
        getBallPosition();
        drawGame();
    } else {
        drawInicial();
        checkStartGame();
        drawGame();
    }
}

function checkStartGame() {
    if (leftPressed) {
        previousPressed = 37;
        startGame = true;
    } else if (rightPressed) {
        previousPressed = 39;
        startGame = true;
    }
}

function resetBall() {
    playerPosX = posXGame + ((gameFieldWidth - playerWidth) / 2);
    playerPosY = posYGame + gameFieldHeight - playerHeight;

    ballPosX = posXGame + (gameFieldWidth / 2);
    ballPosY = posYGame + gameFieldHeight - playerHeight-ballRaio;
    ballAngle = Math.floor(Math.random() * 21) - 10;
}

function addWallNumRows() {
    if(startGame) {
        addNumRows = true;
    }
}
        </script>
        <audio id='bola'>
        <source src="sounds/Bola.mp3" type="audio/mp3">
        Seu browser não suporta o formato do audio.
    </audio>
    <audio id='gameOver'>
        <source src="sounds/Gameover.mp3" type="audio/mp3">
        Seu browser não suporta o formato do audio.
    </audio>
    <audio id='inicial'>
        <source src="sounds/Inicial.mp3" type="audio/mp3">
        Seu browser não suporta o formato do audio.
    </audio>
    <audio id='musica'>
        <source src="sounds/Musica2.mp3" type="audio/mp3">
        Seu browser não suporta o formato do audio.
    </audio>
        <audio id='vitoria'>
        <source src="sounds/Victory.mp3" type="audio/mp3">
        Seu browser não suporta o formato do audio.
    </audio>
    </body>
</html>


{'fraction_non_alphanumeric': 0.12495245896843275, 'fraction_numerical': 0.014745033790702437, 'mean_word_length': 2.3163869215096535, 'pattern_counts': {'":': 0, '<': 113, '<?xml version=': 0, '>': 113, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7574287', 'n_tokens_mistral': 11544, 'n_tokens_neox': 10864, 'n_words': 2480}