class Brick {
constructor(left, top, right, bottom, color) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
this.isAlive = true;
this.color = color;
}
draw() {
if (this.isAlive)
{
context.rect(this.left, this.top, brickWidth, brickHeight);
context.fillStyle = this.color;
context.fill();
}
}
}
class MovingBrick extends Brick{
movingAction () {
this.left++;
}
}
const canvas = document.getElementById('myCanvas');
const context = canvas.getContext('2d');
canvas.style.backgroundColor = "aqua";
let deadBricksCount = 0;
const brickWidth = 50;
const brickHeight = 25;
const brickColumn = 4;
const brickRow = 5;
let bricks ;
const wallWidth = 50;
const wallHeight = 20;
let wallPosX = 200;
let wallPosY = 200;
let wallMoveDirX = -1;
let wallMoveSpeed = 1;
let wall = {
left:200, right:250, top:200, bottom:220,
};
const arcRadius = 20;
let arcPosX = 200;
let arcPosY = 370;
let arcMoveDirX = -1;
let arcMoveDirY = 1;
let arcMoveSpeed = 3;
let ball = {
left:0, right:0, top:0, bottom:0,
};
const barWidth = 200;
const barHeight = 20;
let barPosX = canvas.width / 2 - barWidth / 2;
let barPosY = canvas.height - barHeight;
let barMoveSpeed = 20;
let paddle = {
left:0, right:0, top:0, bottom:0,
};
document.addEventListener('keydown', keyDownEventHandler);
document.addEventListener('keyup', keyUpEventHandler);
function keyDownEventHandler(e)
{
if (e.key == 'ArrowRight')
{
if (barPosX + barWidth < canvas.width)
{
barPosX += barMoveSpeed;
}
}
else if (e.key == 'ArrowLeft')
{
if (barPosX > 0)
{
barPosX -= barMoveSpeed;
}
}
paddle.left = barPosX;
paddle.right = barPosX + barWidth;
paddle.top = barPosY;
paddle.bottom = barPosY + barWidth;
}
function keyUpEventHandler()
{
}
function gameWin ()
{
if(brickCount == 0)
{
window.location.reload(true);
alert('gamewin');
}
}
function gameOver ()
{
if(arcPosY > 370)
{
window.location.reload(true);
alert('gameover');
}
}
function update()
{
if (arcPosX - arcRadius < 0)
{
arcMoveDirX = 1;
}
else if (arcPosX + arcRadius > canvas.width)
{
arcMoveDirX = -1;
}
if (arcPosY - arcRadius < 0)
{
arcMoveDirY = 1;
}
else if (arcPosY + arcRadius > canvas.width)
{
arcMoveDirY = -1;
}
if (wallPosX + 50 > canvas.width)
{
wallMoveDirX = -1;
}
else if (wallPosX < 0){
wallMoveDirX = 1;
}
arcPosX += arcMoveDirX * arcMoveSpeed;
arcPosY += arcMoveDirY * arcMoveSpeed;
wallPosX += wallMoveDirX * wallMoveSpeed;
ball.left = arcPosX - (arcRadius);
ball.right = arcPosX + (arcRadius);
ball.top = arcPosY - (arcRadius);
ball.bottom = arcPosY + (arcRadius);
wall.left = wallPosX ;
wall.right = wallPosX + 50 ;
if (isCollisionRectToRect(ball, paddle))
{
arcMoveDirY = -1;
arcPosY = paddle.top - arcRadius;
}
if (isCollisionRectToRect(ball, wall))
{
arcMoveDirY = -1 * arcMoveDirY;
arcMoveDirX = -1 * arcMoveDirX;
}
for (let i = 0; i < brickRow; i ++)
{
for(let j = 0; j < brickColumn; j++)
{
if (bricks[i][j].isAlive && isCollisionRectToRect(ball, bricks[i][j]))
{
bricks[i][j].isAlive = false;
arcMoveDirY = -arcMoveDirY;
deadBricksCount++;
checkToWin();
break;
}
}
gameOver();
}
}
function checkToWin()
{
let flatBricks = bricks.flat();
let deadBricks = bricks.flat().filter(brick => brick.isAlive == false);
if(deadBricks.length == brickRow * brickColumn)
{
alert("게임클리어");
}
}
function isCollisionRtToRect(rectA, rectB)
{
if (rectA.left > rectB.right ||
rectA.right < rectB.left ||
rectA.top > rectB.bottom ||
rectA.bottom < rectB.top )
{
return false;
}
return true;
}
function draw()
{
context.clearRect(0, 0, canvas.width, canvas.height);
drawCanvas();
drawRect();
drawArc();
drawBricks();
drawWall();
}
function drawCanvas() {
context.beginPath();
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = '#CA9B89';
context.fill();
context.closePath();
}
function drawArc()
{
context.beginPath();
context.arc(arcPosX, arcPosY, arcRadius, 0, 2 * Math.PI);
context.fillStyle = 'blue';
context.fill();
context.closePath();
}
function drawWall()
{
context.beginPath();
context.rect(wallPosX, wallPosY, wallWidth, wallHeight);
context.fillStyle = 'black';
context.fill();
context.closePath();
}
function drawRect()
{
context.beginPath();
context.rect(barPosX, barPosY, barWidth, barHeight);
context.fillStyle = 'red';
context.fill();
context.closePath();
}
function setBricks()
{
bricks = [];
for(let i = 0; i < brickRow; i++)
{
bricks[i] = [];
for(let j = 0; j < brickColumn; j++)
{
bricks[i][j] = new Brick( 55 + j * (brickWidth + 30),
30 + i * (brickHeight + 10),
55 + j * (brickWidth + 30) + 50,
30 + i * (brickHeight + 10) + 25,
'green'
);
}
}
}
function drawBricks()
{
context.beginPath();
for(let i = 0; i < brickRow; i++)
{
for(let j = 0; j < brickColumn; j++)
{
bricks[i][j].draw();
}
}
context.closePath();
}
setBricks();
setInterval(update, 10);
setInterval(draw, 10);
const wallWidth = 50;
const wallHeight = 20;
let wallPosX = 200;
let wallPosY = 200;
let wallMoveDirX = -1;
let wallMoveSpeed = 1;