2
respostas

Solução com coordenadas variáveis

Segue aqui a minha solução do problema, no qual eu incrementei o programa para caso as variáveis initialXPos e initialYPos sejam alteradas, o desenho se movera proporcionalmente a elas. (Ao invés de utilizar variáveis e constantes, eu sei que poderia ter transformado tudo em parametros de uma função mas deu aquela preguiça hehe).

               const tela = creeper;
                let pincel = tela.getContext('2d');

                const canvasWidth = 350;
                const canvasHeight = 300;

                let initialXPos = 0;
                const eyeWidth = 90;
                const noseWidth = 70;
                const mouthWidth = 40;
                const creeperWidth = eyeWidth * 2 + noseWidth;

                let initialYPos = 0;
                const eyeHeight = 90;
                const noseHeight = 100;
                const mouthHeight = 110;
                const creeperHeight = eyeHeight + mouthWidth + mouthHeight;


                function drawRect(color, x, y, width, height) {
                    pincel.fillStyle = color;
                    pincel.fillRect(x, y, width, height);
                };

                initialXPos = (canvasWidth / 2) - (creeperWidth / 2);
                // initialYPos =  (canvasHeight / 2 ) - (creeperHeight / 2);

                drawRect("darkgreen", 0, 0, canvasWidth, creeperHeight);

                drawRect("#000", initialXPos, initialYPos, eyeWidth, eyeHeight);

                drawRect("#000", initialXPos + eyeWidth + noseWidth, initialYPos, eyeWidth, eyeHeight);

                drawRect("#000", initialXPos + eyeWidth, initialYPos + eyeHeight, noseWidth, noseHeight);

                drawRect("#000", initialXPos + eyeWidth - mouthWidth, initialYPos + eyeHeight + mouthWidth, mouthWidth, mouthHeight);

                drawRect("#000", initialXPos + eyeWidth + noseWidth, initialYPos + eyeHeight + mouthWidth, mouthWidth, mouthHeight);
2 respostas

Olá, tudo certo com o código, um creeper é desenhado. Eu sei que é um exercício para aprendizado, mas com essa quantidade toda de variáveis e constantes declaradas uma foi errada:

const creeperHeight = eyeHeight + mouthWidth + mouthHeight;

Acredito que o correto seria "mouthHeight" correto?

Nesse caso não pois a altura do creeper se dá pela altura do olho + altura da boca + largura da boca.

pois, se dá pelo ponto inicial(olho) mais o final (altura da boca) mais o espaço entre o olho e a boca que se vc perceber vai formar um quadrado se você completar com a largura da boca, ou seja, são iguais e por isso eu somo a largura da boca também.