const canvas = document.getElementById('myCanvas');
const context = canvas.getContext('2d');
canvas.style.backgroundColor = "white";
class Brick {
constructor(left, top, right, bottom, color)
{
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
this.color = color;
}
draw()
{
context.rect(this.left, this.top, rectWidth, rectHeight);
context.fillStyle = this.color;
context.fill();
}
}
class Monster {
constructor(left, top, right, bottom, color)
{
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
this.color = color;
}
draw()
{
context.rect(this.left, this.top, monsterWidth, monsterWidth);
context.fillStyle = this.color;
context.fill();
}
}
let monsterWidth = 40;
let monsterHeight = 40;
let monsterPosX = 0;
let monsterPosY = 0;
let monsters = [];
let mobs = {
left:0, right:0, top:0, bottom:0,
};
let rectWidth = 40;
let rectHeight = 40;
let rectPosX = 0;
let rectPosY = 0;
let endCanvas = 369;
let paddle = {
left:0, right:rectPosX, top:0, bottom:rectPosY,
};
let tileWidth = 40;
let tileHeight = 40;
let tileColumn = 10;
let tileRow = 10;
let tiles = [];
let colors = ["green", "brown", "lightgreen"];
let gameState = 0;
let shopWidth = 40;
let shopHeight = 40;
let shopPosX = canvas.width / 2;
let shopPosY = canvas.height / 2;
let rightPressed = false;
let leftPressed = false;
let upPressed = false;
let downPressed = false;
document.addEventListener('keydown', keyDownHandler, false);
function keyDownHandler(e) {
if(gameState == 0)
{
if(e.key == "ArrowRight") {
if(rectPosX < endCanvas) {
rectPosX += 41;
paddle.left = rectPosX;
paddle.right = rectPosX + 40;
}
}
else if(e.key == "ArrowLeft") {
if(rectPosX > 0) {
rectPosX -= 41;
paddle.left = rectPosX;
paddle.right = rectPosX + 40;
}
}
else if(e.key == "ArrowUp") {
if(rectPosY > 0) {
rectPosY -= 41;
paddle.top = rectPosY;
paddle.bottom = rectPosY + 40;
}
}
else if(e.key == "ArrowDown") {
if(rectPosY < endCanvas) {
rectPosY += 41;
paddle.top = rectPosY;
paddle.bottom = rectPosY + 40;
}
}
if(!flag) flag = true;
}
gameWin(1000);
}
let flag = true;
function update()
{
player = new Brick(rectPosX, rectPosY, 0, 0, 'white');
for(let i = 0; i < monsters.length; i++)
{
mobs.left = monsters[i].left;
mobs.top = monsters[i].top;
mobs.right = monsters[i].right;
mobs.bottom = monsters[i].bottom;
if(isCollisionRectToRect(player, mobs)){
if (flag) {
gameState = 1
document.getElementById('rspBtn').style.display="block";
if(monsters[i].color == 'green') {
document.getElementById('mon_1').style.display="block";
}
if(monsters[i].color == 'brown') {
document.getElementById('mon_2').style.display="block";
}
if(monsters[i].color == 'lightgreen') {
document.getElementById('mon_3').style.display="block";
}
}
}
}
}
function isCollisionRectToRect(rectA, rectB)
{
if (rectA.left == rectB.left &&
rectA.top == rectB.top )
{
return true;
}
return false;
}
function setTiles()
{
for(let i = 0; i < tileRow; i++)
{
tiles[i] = [];
for(let j = 0; j < tileColumn; j++)
{
tiles[i][j] = {
left:0 + j * (tileWidth + 1),
right:0 + j * (tileWidth + 1) + 50,
top:0 + i * (tileHeight + 1),
bottom:0 + i * (tileHeight + 1) + 25,
column:i, row:j,
isAlive:true,
};
}
}
}
function disPlayMonsterImage(eleMentId, displayType) {
document.getElementById(eleMentId).style.display=displayType;
}
function rspGame(i) {
let computer = Math.round(Math.random() * 2)
let me = i
if((me - computer + 2) % 3 == 0) {
console.log('이김');
gameState = 0;
flag = false;
document.getElementById('rspBtn').style.visibility="hidden";
document.getElementById('coin').innerHTML = parseInt(document.getElementById('coin').innerHTML) + Math.floor(Math.random() * 100);
disPlayMonsterImage('mon_1','none');
disPlayMonsterImage('mon_2','none');
disPlayMonsterImage('mon_3','none');
}
else if ((me - computer + 2 ) % 3 == 1) {
console.log('패배');
gameState = 0;
flag = false;
document.getElementById('rspBtn').style.visibility="hidden";
document.getElementById('hp').innerHTML = parseInt(document.getElementById('hp').innerHTML) -1;
disPlayMonsterImage('mon_1','none');
disPlayMonsterImage('mon_2','none');
disPlayMonsterImage('mon_3','none');
}else {
console.log('비김');
}
}
async function gameWin(timeout)
{
if((rectPosY >= endCanvas) && (rectPosX >= endCanvas)) {
setTimeout(()=> {
window.location.reload(true);
alert('gamewin')
},timeout);
}
}
let coords = [];
let monsterCount = Math.floor(Math.random() * 11) + 20;
function setMonster() {
for (let i = 0; i < monsterCount; i++) {
let x = Math.round(Math.random()*9)
let y = Math.ceil(Math.random()*9)
if (coords.includes(y * 10 + x)) {
console.log(x,y);
}else{
coords.push(y * 10 + x);
let mob = new Monster(
x * (monsterWidth+1),
y * (monsterHeight+1),
x * (monsterWidth+1) + monsterWidth,
y * (monsterHeight+1) + monsterHeight,
colors[Math.floor(Math.random() * 3)]
)
monsters.push(mob)
}
}
}
function drawMonster()
{
for(let i = 0; i < monsters.length; i++) {
context.beginPath();
monsters[i].draw();
context.closePath();
}
}
function drawPlayer() {
context.beginPath();
player.draw();
context.closePath();
}
function drawTile()
{
for(let i = 0; i < tileRow; i++)
{
for(let j = 0; j < tileColumn; j++)
{
if (tiles[i][j].isAlive) {
context.beginPath();
context.rect(tiles[i][j].left, tiles[i][j].top, tileWidth, tileHeight);
context.fillStyle = 'gray';
context.fill();
context.closePath();
}
}
}
}
function draw()
{
context.clearRect(0, 0, canvas.width, canvas.height);
drawTile();
drawPlayer();
drawMonster();
}
setTiles();
setMonster();
setInterval(update, 10);
setInterval(draw, 10);