2 글 보임 - 1 에서 2 까지 (총 2 중에서)
-
글쓴이글
-
2024년 5월 7일 16:07 #122437

송승환참가자var canvas = document.querySelector('canvas'); var ctx = canvas.getContext('2d'); var cactusList = []; var timer = 0; var jumping = false; var jumpingCount = 0;canvas.width = window.innerWidth - 100; canvas.height = window.innerHeight - 100;
var character = { x: 100, y: 200, width: 50, height: 50, draw() { ctx.fillStyle = 'black'; ctx.fillRect(this.x, this.y, this.width, this.height); } }class Enemy { constructor() { this.x = 1400; this.y = 200; this.width = 50; this.height = 50; } draw() { ctx.fillStyle = 'gray'; ctx.fillRect(this.x, this.y, this.width, this.height); } }function frame() { animation = requestAnimationFrame(frame); timer++; ctx.clearRect(0, 0, canvas.width, canvas.height)if (timer % 80 == 0) { var cactus = new Enemy() cactusList.push(cactus); }cactusList.forEach(function (a, i, o) { if(a.x + a.width < 0){ o.splice(i, 1); } a.x -= 10; contect(character, a) a.draw() });if(jumping && jumpingCount == 0){ if(character.y > 50){ character.y -= 10; } } if(!jumping || jumpingCount == 1){ if(character.y < 200){ character.y += 10 } }if(character.y == 200){ jumpingCount = 0 }character.draw() }
document.addEventListener('keydown', function(e){ if(e.code == 'Space'){ jumping = true }})
document.addEventListener('keyup', function(){ jumping = false; jumpingCount++ })frame();
function contect(character, enemy){ var xind = enemy.x - (character.x + character.width); var yind = enemy.y - (character.y + character.height);if(xind < 0 && yind < 0){ ctx.clearRect(0,0,canvas.width, canvas.height) cancelAnimationFrame(animation)} }
-
글쓴이글
2 글 보임 - 1 에서 2 까지 (총 2 중에서)
- 답변은 로그인 후 가능합니다.
