Canvas 제작
<html>
<canvas width="600" height="600" id="gameFiled"></canvas>
<script src="./escapeGame.js"></script>
</html>
JS code
<script>
var interval = setInterval( () => ( update ), 10);
const canvas = document.getElementById('gameFiled');
const context = canvas.getContext('2d');
const filedTileWidth = 60;
const filedTileHeight = 60;
let tiles;
let isForestMonsterColor = '#043301';
let isLandMonsterColor = '#423c20';
let forestColor = "#043301";
let landColor = "#423c20";
let forestTileNumber = 50;
let landTileNumber = 100;
const goalWidth = 60;
const goalHeight = 60;
const halfGoalWidth = goalWidth / 2;
const halfGoalHeight = goalHeight / 2;
let goalX = (canvas.width/ 2) - halfGoalWidth;
let goalY = (canvas.height / 2) - halfGoalHeight;
let player = { left : 0, right : 0, top : 0, bottom : 0 }
const playerWidth = 60;
const playerHeight = 60;
const halfPlayerWidth = playerWidth / 2;
const halfPlayerHeight = playerHeight / 2;
let playerX = 0;
let playerY = canvas.height-playerHeight;
const playerMoveSpeed = playerWidth /2;
const hpSpan = document.getElementById('hp');
const cashSpan = document.getElementById('cash');
let randomNumber;
let landMonsterNum = 5;
let forestMonsterNum = 10;
let monsterNum = 10;
let monster;
let landMonsterX;
let landMonsterY;
let forestMonsterX;
let forestMonsterY;
let battleResult;
let battleNumber;
var inputNumber;
let battleCoverage;
let storeWidth = 60;
let storeHeight = 60;
const halfStoreWidth = storeWidth / 2;
const halfStoreHeight = storeHeight / 2;
let storeX = 0;
let storeY = 0;
let buyPotion;
let inputResult;
function draw(){
context.clearRect(0,0,canvas.width,canvas.height)
drawGameFiled();
goalRect();
storeRect();
playerRect();
}
class Tile {
constructor(left, top, right, bottom, color, number, isForestMonster, isLandMonster,isForest, isLand, isStore) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
this.color = color;
this.number = number;
this.isForestMonster = isForestMonster;
this.isLandMonster = isLandMonster;
this.isForest = isForest;
this.isLand = isLand;
this.isStore = isStore
}
draw() {
context.rect(this.left, this.top, filedTileWidth, filedTileHeight);
context.fillStyle = this.color;
context.fill();
}
}
class Player {
constructor(hp, cash){
this.hp = hp;
this.cash = cash;
}
}
function tileColor() {
randomNumber = Math.floor(Math.random() * 100 + 1)
if(randomNumber < landMonsterNum) {
return isLandMonsterColor
}
else if (randomNumber > landMonsterNum && randomNumber < forestMonsterNum){
return isForestMonsterColor
}
else if (randomNumber > forestMonsterNum && randomNumber < forestTileNumber ){
return forestColor
} else {
return landColor
}
}
function setGameFiled(){
tiles = [];
for ( let i = 0; i <= 10; i++ ) {
tiles[i]=[];
for (let j = 0; j <= 10; j++) {
tiles[i][j] = new Tile ( i*60 , j*60 , i*60 , j*60 , tileColor(), randomNumber, false, false, false, false )
if(randomNumber <= landMonsterNum) {
landMonsterX = tiles[i][j].left
landMonsterY = tiles[i][j].top
console.log("landMonsterX : " + landMonsterX + ", landMonsterY : " + landMonsterY);
tiles[i][j].isLandMonster = true;
} else if (randomNumber > landMonsterNum && randomNumber <= forestMonsterNum){
forestMonsterX = tiles[i][j].left
forestMonsterY = tiles[i][j].top
console.log("forestMonsterX : " + forestMonsterX + ", forestMonsterY : " + forestMonsterY);
tiles[i][j].isForestMonster = true;
} else if (randomNumber > forestMonsterNum && randomNumber <= forestTileNumber ){
tiles[i][j].isForest = true;
} else if(randomNumber > forestTileNumber && randomNumber <= landTileNumber ){
tiles[i][j].isLand = true;
}
}
}
}
setGameFiled()
playerState = new Player (100, 0)
function drawGameFiled()
{
for(let i = 0; i < 10; i++)
{
for(let j = 0; j < 10; j++)
{
context.beginPath();
tiles[i][j].draw();
context.closePath();
}
}
}
function goalRect(){
context.beginPath();
context.rect(goalX,goalY,goalWidth,goalHeight);
context.fillStyle = "gold";
context.fill();
context.closePath();
}
function playerRect(){
context.beginPath();
context.rect(playerX,playerY,playerWidth,playerHeight);
context.fillStyle = "yellow";
context.fill();
context.closePath();
}
function storeRect(){
context.beginPath();
context.rect(storeX,storeY,storeWidth,storeHeight);
context.fillStyle = "silver";
context.fill();
context.closePath();
}
document.addEventListener('keydown', keyDownEventHandler);
function keyDownEventHandler(e) {
if( e.key == 'ArrowRight' ) {
if(playerX + playerWidth < canvas.width){
playerX += playerMoveSpeed;
}
}
else if(e.key == "ArrowLeft") {
if(playerX > 0 ) {
playerX -= playerMoveSpeed;
}
}
else if(e.key == "ArrowDown") {
if(playerY + playerHeight < canvas.height) {
playerY += playerMoveSpeed;
}
}
else if(e.key == "ArrowUp") {
if(playerY > 0) {
playerY -= playerMoveSpeed;
}
}
player.left = playerX;
player.right = playerX + playerWidth;
player.top = playerY;
player.bottom = playerY + playerHeight;
}
function gameClear(){
setTimeout( ()=> {
window.location.reload(),
alert("게임 클리어!")
},10)
}
function gameOver(){
clearInterval(interval)
alert("게임 오버!")
}
function battle_RockPaperScissors(inputNumber){
battleNumber = Math.floor((Math.random() * 10)) % 3
console.log("battleNumber" + battleNumber);
console.log("inputNumber" + inputNumber);
battleCoverage = Math.floor(Math.random()*100)
if(((inputNumber - battleNumber)+2)%3 == 0){
alert("승리하였습니다")
battleButtonDisapear();
playerState.cash += battleCoverage;
cashSpan.innerText = `cash : ${playerState.cash}`;
alert(battleCoverage + "의 캐쉬를 획득하였습니다!")
}
else if(((inputNumber - battleNumber)+2)%3 == 1){
alert("패배하였습니다")
battleButtonDisapear();
playerState.hp -= 1;
console.log(playerState.hp)
hpSpan.innerText = `hp : ${playerState.hp}`;
alert(playerState.hp + "의 hp가 남았습니다.")
}
else {alert("무승부입니다"), battleButtonDisapear()
}
}
function store(inputResult){
if( inputResult == 1) {
alert('10 캐쉬를 소진하여 HP를 충전하였습니다')
playerState.hp += 100
playerState.cash -= 10
hpSpan.innerText = `hp : ${playerState.hp}`;
cashSpan.innerText = `cash : ${playerState.cash}`;
} else if ( inputResult == 0) {
alert('상점을 나왔습니다.')
storeButtonDisapear();
}
}
document.getElementById("battleButton").style.display = "none";
function battleButtonDisapear(){
document.getElementById("battleButton").style.display = "none";
}
function battleButtonApear(){
document.getElementById("battleButton").style.display = "block";
alert('몬스터를 만났습니다! 가위바위보 배틀을 시작합니다')
}
document.getElementById("store").style.display = "none";
function storeButtonDisapear(){
document.getElementById("store").style.display = "none";
}
function storeButtonApear(){
document.getElementById("store").style.display = "block";
alert('상점으로 들어왔습니다.')
}
function update(){
if(playerX == goalX && playerY == goalY) {
gameClear()
setTimeout( ()=> {
playerX = 0,
playerY = canvas.height-playerHeight
},2000)
}
for ( let i = 0; i <= 10; i++) {
for (let j = 0; j <= 10; j++) {
if(playerX == tiles[i][j].left &&
playerY == tiles[i][j].top &&
(tiles[i][j].isForestMonster == true ||
tiles[i][j].isLandMonster == true )) {
setTimeout( ()=> {
playerY -= 30
},1)
battleButtonApear()
}
}
}
if(playerX == storeX && playerY == storeY) {
setTimeout( ()=> {
console.log('상점 입장!')
playerY += 60;
},1)
storeButtonApear();
}
}
draw();
setInterval(update, 10);
setInterval(draw, 10);
</script>