Challenge/Youtube

Chrome Dinosaur Game (2)

Deviloper๐Ÿ˜ˆ 2021. 10. 3. 13:59

์–ด์ œ ๊ณต๋ฃก(dino)์€ ๋ฉˆ์ถฐ์žˆ๊ณ  ์—ฌ๋Ÿฌ ์žฅ์• ๋ฌผ(cactus)์ด ๋‹ค๊ฐ€์˜ค๋Š” ์ƒํ™ฉ๊นŒ์ง€ ๊ตฌํ˜„ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์˜ค๋Š˜์€ ์ŠคํŽ˜์ด์Šค๋ฐ”๋ฅผ ๋ˆ„๋ฅด๋ฉด ๊ณต๋ฃก์ด ์ ํ”„๋ฅผ ํ•˜๊ณ  ๊ณต๋ฃก๊ณผ ์žฅ์• ๋ฌผ์˜ ์ถฉ๋Œ ๊ฐ์ง€(Collision Detection) ๋ถ€๋ถ„์„ ๊ตฌํ˜„ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์ด ์–ด๋ ค์šธ ๊ฒƒ์ด๋ผ๊ณ  ์˜ˆ์ƒํ–ˆ์—ˆ๋Š”๋ฐ ๊ฐ ๊ฐ์ฒด์˜ ์œ„์น˜๋งŒ ๊ด€๋ฆฌํ•˜๋ฉด ๊ฐ€๋Šฅํ–ˆ์—ˆ๋˜ ๊ฒƒ์ด์–ด์„œ ์ƒ๊ฐ์™ธ๋กœ ๋น ๋ฅด๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๊ธฐ๋ณธ์ ์ธ ๊ธฐ๋Šฅ ๊ตฌํ˜„์€ ๋งˆ์ณค์œผ๋‹ˆ ์ƒ๊ฐ๋‚˜๋Š” ์•„์ด๋””์–ด๊ฐ€ ์ƒ๊ธธ ๋•Œ๋งˆ๋‹ค ๊ธฐ์กด์— ๋งŒ๋“ค์—ˆ๋˜ ๊ฒŒ์ž„์— ์กฐ๊ธˆ์”ฉ ์ถ”๊ฐ€ํ•ด๋ณด๋ ค๊ตฌ์š”! ๋˜ํ•œ ์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด Java Script ๋งŒ์œผ๋กœ๋„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋“ค์ด ๋ฌด๊ถ๋ฌด์ง„ํ•˜๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ฒฝํ—˜์„ ์‚ด๋ ค์„œ ๋‹ค์Œ ๊ฒŒ์ž„ ํ”„๋กœ์ ํŠธ๋„ ๋ชฉ์—…๋Œ€๋กœ ์ž˜ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์—ˆ์œผ๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค๐Ÿ™๐Ÿ˜Š

 

์˜ค๋Š˜ ๋ฐฐ์› ๋˜ ๋‚ด์šฉ๋“ค์€ ์ฝ”๋“œ ์•ˆ์— ์ฃผ์„์ฒ˜๋ฆฌ ํ–ˆ์Šต๋‹ˆ๋‹ค!

 

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext('2d');

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;



var img2 = new Image();
img2.src = 'dinosaur.png'; // make an object to place a downloaded image
var dino = {
  x: 10,
  y: 200,
  width: 50,
  height: 50,
  draw() {
    ctx.drawImage(img2, this.x, this.y, this.width+10, this.height+10);
  }
} 



var img1 = new Image();
img1.src = 'cactus.png';
class Cactus {
  constructor() {
    this.x = 800;
    this.y = 200;
    this.width = 50;
    this.height = 50;
  }
  draw() {
    ctx.drawImage(img1,this.x,this.y, this.width, this.height);
  }
}


var timer = 0;
var cacti;
var animation;
var jumping = false;
var jumpTimer = 0;



function perFrame() {
  animation = requestAnimationFrame(perFrame);
  timer++;
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  
  if(timer % 60 === 0) { // a new cactus is created every 60 frames
    var cactus = new Cactus();
    cacti.push(cactus);
  }
  
  cacti.forEach((a,idx,o) => { // o is the Array 'Cacti'
  	if (a.x < 0) {
      o.splice(idx,1); // a cactus disappears after its x-value is smaller than 0
    }
    a.x -= 8;
    collisionDetection(dino, a);
    a.draw();
  })
  
  if(jumping === true) { // dino goes to the top when jumping is true
    dino.y -= 5; 
    jumpTimer += 3;
  }
  
  if(jumping === false) {
    if(dino.y < 200) {
      dino.y += 5;
    }
  }
  
  if (jumpTimer > 100) { // dino goes to the bottom when jumping is false
  	jumping = false;
  }
  
  if(dino.y === 200) {
    jumpTimer = 0;
  }
  
  dino.draw();
}



perFrame();



function collisionDetection(dino, cactus) {
  var xDifference = cactus.x - (dino.x + dino.width);  
  var yDifference = cactus.y - (dino.y + dino.height);
  if(xDifference < 0 && yDifference < 0) { 
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    cancelAnimationFrame(animation); 
    //This method cancels an animation request previously scheduled through a call to requestAnimationFrame
  }
}



var jumping = false;

document.addEventListener('keydown', function(e) {
  if(e.code === 'Space') {
    jumping = true;
  }
}) //When spacebar is pressed, the event listener is executed