let canvas = document.querySelector('#canvas');
let ctx = canvas.getContext('2d');
canvas.width = window.innerWidth - 100;
canvas.height = window.innerHeight - 100;
let dino = {
x : 100,
y : 300,
width : 50,
height : 50,
draw(){
ctx.fillStyle = 'green';
ctx.fillRect(this.x, this.y, this.width, this.height);
}
};
class Cactus {
constructor(){
this.x = 500,
this.y = 300,
this.width = 50,
this.height = 50
}
draw(){
ctx.fillStyle = 'red';
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
let timer = 0;
let cactus들 = [];
let 점프중 = false;
let 점프timer = 0;
let animation;
function frameWork(){
animation = requestAnimationFrame(frameWork)
timer++;
ctx.clearRect(0,0,canvas.width,canvas.height);
if( timer % 200 == 0){
let cactus = new Cactus();
cactus들.push(cactus);
}
cactus들.forEach((a)=>{
if (a.x < 0){
cactus들.shift();
}
a.x -=2;
충돌하냐(dino, a);
a.draw()
});
//충돌 check
function 충돌하냐(dino, cactus){
let x축차이 = cactus.x - ( dino.x + dino.width);
let y축차이 = cactus.y - ( dino.y + dino.height);
if(x축차이 < 0 && y축차이 < 0 ){
cancelAnimationFrame(animation)
}
}
//점프관련
if(점프중 == true){
점프timer++;
dino.y -= 4;
}
if(점프중 == false && dino.y<300){
dino.y += 4;
}
if(점프timer > 30){
점프중 = false;
점프timer = 0;
console.log('점프끝')
}
dino.draw()
}
frameWork()
document.addEventListener('keydown' , function(e){
if(e.code === 'Space'){
점프중 = true;
}
console.log(점프중)
})