코딩애플님의 간단한 javaScript 게임개발 예제를 따라 작성해보았다.
게임개발 2까지 진도를 나가서, 이중점프 안되게 뭐 다 코드를 작성했다.
드래그도 안되게 했고, 스페이스바를 눌렀을 때 홈페이지 내부에서 동작하지 않도록 처리도 했다.
let $canvas = document.getElementById('canvas');
let $h1 = document.querySelector('h1');
let ctx = $canvas.getContext('2d');
let $pScore = document.getElementById('score');
let $pHScore = document.getElementById('Hscore');
// 드래그 방지
document.onselectstart= () => {return false};
// // space바로 page down 방지 (keyborad 입력 금지)
// document.onkeydown= () => {return false};
$canvas.width = window.innerWidth - 100;
$canvas.height = window.innerHeight - 100;
$h1.style.display = 'none';
$h1.style.position = 'absolute';
// $pHScore.style.float = "right";
// $pScore.style.float = "right";
let line = {
draw(){
ctx.moveTo(-20,450);
ctx.lineTo(2000,450);
ctx.lineWidth = 4;
ctx.stroke();
}
}
let me = {
x : 40,
y : 400,
width : 50,
height : 50,
draw(){
ctx.fillStyle = 'rgb(0,200,0)';
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
let jumpNow = false;
let jumping = false;
let standing = false;
let jumpTimer = 0;
let jumpSpeed = 3;
// 나의 점프
//
let jumpKey = ['Space', 'ArrowUp', 'KeyW'];
let standKey = ['ArrowDown', 'KeyS']
document.addEventListener("keydown", (e) => {
if(jumpKey.includes(e.code)){
if(jumpNow === false){
jumping = true;
standing = false;
}
if(game == false){
game = true;
location.reload();
}
}
else if(standKey.includes(e.code)){
standing = true;
}
})
class Enemy {
constructor(){
this.x = 1600;
this.y = 400;
this.width = 50;
this.height = 50;
}
draw(){
ctx.fillStyle = 'rgb(200,0,0)';
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
let $input = document.querySelector('input');
let score = 0;
if(localStorage.getItem('highScore') === null){
localStorage.setItem('highScore', 0);
}
let highScore = localStorage.getItem('highScore');
let game = true;
let timer = 0;
let enemies = [];
// 빈도 수
const hard = 140;
const normal = 240;
const difficulty = [hard, normal];
const choice = 0;
// 속도
// 충돌 여부
function isCrush(me, enemy){
let xDif;
let yDif = enemy.y - (me.y + me.height);
if(enemy.x > 40){
xDif = enemy.x - (me.x + me.width);
}
else{
xDif = me.x - (enemy.x + enemy.width);
}
if(xDif < 0 && yDif <0){
cancelAnimationFrame(animation);
ctx.clearRect(0,0, $canvas.width, $canvas.height);
game = false;
// $h1.style.position = 'position:relative';
// $h1.style.display = 'block';
//최고기록 갱신
if(score > highScore){
highScore = score;
localStorage.setItem('highScore', highScore);
}
}
}
function excuteEveryFrame(){
animation = requestAnimationFrame(excuteEveryFrame);
timer++;
score++;
ctx.clearRect(0,0, $canvas.width, $canvas.height);
// 적 생성
if(timer % difficulty[1] == 0){
let enemy = new Enemy();
enemies.push(enemy);
}
// 적 소환 및 제거
enemies.forEach((e, i, o) =>{
if(e.x < -40) {
o.splice(i, 1);
}
isCrush(me, e);
e.draw();
e.x -= 3;
})
if(jumping === true && jumpNow === false){
me.y-= (jumpSpeed);
jumpTimer++;
}
if(jumpTimer > 40 || standing == true){
jumping = false;
jumpTimer = 0;
jumpNow = true;
}
if(jumping === false){
if(me.y <= 400){
me.y+= (jumpSpeed+0.3);
if(standing === true){
me.y += (jumpSpeed + 1);
}
// 오차 방지 및 중복 점프 방지
if(me.y > 398){
me.y = 400;
jumpNow = false;
}
}
}
// 선, 자신 생성
me.draw();
line.draw();
$pScore.innerHTML = 'score : ' + score;
$pHScore.innerHTML = 'highscore : ' + highScore;
// 최고기록 실시간 갱신
if(score > highScore){
$pHScore.innerHTML = 'highscore : ' + score;
}
}
excuteEveryFrame();
코드해석은 추후 수정하도록 하겠다!
궁금한 부분은 질문 바란다.








