Newer
Older
2023-Yoshimiya / mogura.js
@YOSHIMIYA YOSHIMIYA on 17 Nov 2023 1 KB add mogura
#!/usr/bin/env js

(() => {
    var timer = null, count = 10; point = 0; ana=6;
    var tataki = ana+1; tataki2 = ana+1; //存在しないdivs添字
    var msgArea = null; ptArea = null;
    function countdown() {
	msgArea.innerText = `残り ${count}秒`;
	if (count<=0) {
	    stop();
	} else {
	    timer = setTimeout(countdown, 1000);   //1秒待つ
	}
	count --;
    }
    function start() {
	startButton.disabled = true;
	rand();
	countdown();
    }
    function stop() {
	alert("しゅーーりょーー");
	ptArea.innerText = `今回のスコア: ${point}回`; //ptArea
	ptArea.style.textDecoration = "underline";
	//atari.innerHTML = ""     //もぐら画像削除
    }
    function whack() {     // whack-a-mole(英):モグラ叩き
	if (count>=0) {     // 時間すぎると加算されない
	    point ++;
	    ptArea.innerText = `現在: ${point}回`;
	    ptArea.style.textDecoration = "none";
	    atari.innerHTML = "";     //もぐら画像削除
	    atari.removeEventListener('click', whack);   //イベントリスナー削除
	    rand();
	}
    }
    function rand() {
	var divs = document.querySelectorAll("div");
	tataki2 = Math.floor(Math.random()*ana);
	if (count>=0) {
	    if (tataki == tataki2) {   //数字がかぶったらやり直し
		rand();
	    } else {
		tataki = tataki2;
		atari = divs[tataki];
		atari.innerHTML = "<img src='mogura3.png'>"; //もぐら画像表示
		atari.addEventListener('click', whack);
	    }
	console.log(`${tataki}`);
	}
    }
    function init() {
	var divs = document.querySelectorAll("div");
	//for (i=0;i<ana;i++) {divs[i].innerText=`${i}`;}   //divs添字確認用
	msgArea = document.getElementById("message");
	ptArea = document.getElementById("point");
	startButton = document.getElementById("start");
	msgArea.innerText = `制限時間: ${count}秒`;
	startButton.addEventListener('click', start);
    };

    document.addEventListener('DOMContentLoaded', init);
})();