[javascript] 테트리스게임 다시시작 버튼 spacebar로 만들기

Jaho·2022년 5월 30일
0

javascript

목록 보기
5/6


GameOver 화면이 나타나고 space를 눌렀을때 이벤트 발생

function reStart() {
    playground.innerHTML = ""; // innerHTML초기화
    gameText.style.display = "none"; // 게임종료 화면 삭제
    scoreDisplay.innerText = 0;
    init();
}

restartBtn.addEventListener("click", () => {
    reStart();
})

// keyCode 32 = 스페이스바
document.addEventListener("keydown", (e) => {
    if (gameText.style.display === "flex" && e.keyCode === 32) {
        reStart();
    }
})

초기화하는 코드를 함수로 만들었고 게임종료 화면이 나타나며 스페이스바 버튼을 눌렀을 때를 조건문으로 주었다.

profile
개발 옹알이 부터

0개의 댓글