Skip to content

Commit

Permalink
resolved #20 add timer
Browse files Browse the repository at this point in the history
  • Loading branch information
fpdjsns committed Mar 2, 2022
1 parent 00023f0 commit 1968b53
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions Tetris/js/constants.js
Expand Up @@ -36,6 +36,7 @@ const BOTTOM_DUPLICATED = 4;

const LINE_COLOR = "gray";

const TIMER_UNIT = 1000; // 1sec
const SPEED = 1000; // 1sec
const BLOCK_BOTTOM_TIMEOUT = 2000; // 4sec
const BLOCK_BOTTOM_TEMP_TIMEOUT = 500; // 0.5sec
Expand Down
24 changes: 24 additions & 0 deletions Tetris/js/timer.js
Expand Up @@ -15,6 +15,8 @@ class Timer {
this.gameoverFunction = gameoverFunction; // 게임 종료시 실행되는 함수.
this.whenDropBlockNextFunction = whenDropBlockNextFunction; // 블럭이 땅에 도달했다고 판단 후 후처리 함수. (ex, 다음 블럭 생성)

this.time = 0;
this.timerId = null;
this.gameTimerId = null;
this.bottomTime = null;
this.bottomTempTime = null;
Expand All @@ -37,6 +39,7 @@ class Timer {

// ==== game ====
startGame() {
this.setTimer();
this.refreshGame(this.speed);
this.setDifficulty();
}
Expand All @@ -59,10 +62,31 @@ class Timer {
}, speed);
}

setTimer() {
const that = this;
that.time = 0;
if (that.timerId)
clearInterval(this.timerId);
return that.timerId = setInterval(function () {
that.time++;
const time = that.time
const min = parseInt((time / 60) % 60)
const hour = parseInt(time / (60 * 60))
const sec = parseInt(time % 60)

const el = document.getElementsByClassName("time-text")[0];
const newText = hour + " : " + min + " : " + sec;
el.innerText = newText;
}, TIMER_UNIT);
}

stopGame() {
if (!this.gameTimerId) return;
clearInterval(this.gameTimerId);
this.gameTimerId = null;
clearInterval(this.timerId);
this.timerId = null;
this.time = 0;
clearInterval(this.difficultyTimerId);
this.difficultyTimerId = null;
this.gameoverFunction();
Expand Down
6 changes: 3 additions & 3 deletions Tetris/main.html
Expand Up @@ -37,7 +37,7 @@
</table>
<button class="btn_open_setting" onclick="openSetting()">설정</button>

<div id="setting" class="layer_popup" hidden>
<span id="setting" class="layer_popup" hidden>
<div class="layer">
<div class="text_area">
<p><input type="text" class="key_input" id="key-up" onkeydown="return false;"/></p>
Expand All @@ -53,8 +53,8 @@
</div>
</div>
<div class="dimmed"></div>
</div>

</span>
<span class="time-text">0 : 0 : 0</span>

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css"
integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ"
Expand Down

0 comments on commit 1968b53

Please sign in to comment.