製作一個用聲音猜數字的遊戲
成品:
成品網址
成品功能:
- 詢問是否可以使用麥克風
- 判斷數字是否正確
- 顯示離目前數字的位置 - 喊得太高或是太低
- 如果喊得不是數字會顯示相對字樣
- 當遊戲成功會顯示按鈕讓遊戲重來
HTML
上 CSS 之前的 HTML:
html 程式碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <body>
<img src="img/mic.png" alt="Speak"> <h1>Guess a Number Between 1 - 100</h1>
<h3>Speak the number into your microphone</h3>
<div id="msg" class="msg"> <div>You said:</div> <span class="box">20</span> <div>GO HIGHER</div> </div> <script src="script.js"></script> </body>
|
CSS:
CSS 設置好的樣式
CSS 完整程式碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| * { box-sizing: border-box; }
body { background: #2f3542 url('img/bg.jpg') no-repeat left center/cover; color: #fff; min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; margin: 0; font-family: Arial, Helvetica, sans-serif; }
h1, h3 { margin-bottom: 0; }
p { line-height: 1.5; margin: 0; }
.play-again { padding: 8px 15px; border: 0; background: #f4f4f4; border-radius: 5px; margin-top: 10px; }
.msg { font-size: 1.5em; margin-top: 40px; }
.box { border: 1px solid #dedede; display: inline-block; font-size: 30px; margin: 20px; padding: 10px; }
|
JS:
前置作業
後方 webkit 部分是為了瀏覽器相容使用的
1
| window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition
|
變數設置
本篇唯一一個變數裡面會放入:
const msgEl = document.getElementById("msg");
functions:
randomNum()
產生題目(隨機數字 1-100 內)
抓取聲音產生的值
把抓取到聲音的值推到 DOM 上
randomNum()
產生題目(隨機數字 1-100 內)
1 2 3 4 5 6
| const randomNum = Math.floor(Math.random() * 100) + 1;
|
onSpeak()
抓取聲音產生的值
抓取後:
- 把抓取到聲音的值推到 DOM 上
writeMessage(msg)
- 辨別使用者聲音答案是否符合題目
checkNumber(msg)
1 2 3 4 5 6 7 8 9
| function onSpeak(e) { const msg = e.results[0][0].transcript;
writeMessage(msg); checkNumber(msg); }
|
writeMessage()
把抓取到聲音的值推到 DOM 上
1 2 3 4 5 6
| function writeMessage(msg) { msgEl.innerHTML = ` <div>You said:</div> <span class="box">${msg}</span>`; }
|
checkNumber()
辨別使用者聲音答案是否符合題目:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| function checkNumber(msg) { const num = +msg;
if (Number.isNaN(num)) {
msgEl.innerHTML += ` <div>This is not a valid number</div>`;
return; }
if (num > 100 || num < 1) { msgEl.innerHTML += `<div>Number must be between 1 - 100</div>`; return; }
if (num === randomNum) {
document.body.innerHTML = `<h2>Congrats! You have guessed the number! <br>It was ${num}</h2><button class="play-again" id="play-again">Play Again</button>` } else if (num > randomNum) { msgEl.innerHTML += '<div>GO LOWER</div>'; } else { msgEl.innerHTML += '<div>GO HIGHER</div>'; } }
|
事件監聽
使用聲音辨識 API 可以先把它設置為物件並且指派給recognition
做變數使用比較好操作
SpeechRecognition 專屬的事件
- result- 當 SpeechRecognition 回傳一個值的時候觸發用來抓取使用者輸入的數字
- end- 當 SpeechRecognition 服務停止時觸發,用來繼續遊戲
- start- 開始接收音檔
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| let recognition = new SpeechRecognition();
recognition.start();
recognition.addEventListener('result', onSpeak)
recognition.addEventListener('end', () => recognition.start());
document.body.addEventListener('click', (e) => {
if (e.target.id == 'play-again') { window.location.reload(); } })
|
小補充:
SpeechRecognition MDN
JS 完整程式碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| const msgEl = document.getElementById("msg");
function onSpeak(e) { const msg = e.results[0][0].transcript;
writeMessage(msg); checkNumber(msg); }
function writeMessage(msg) { msgEl.innerHTML = ` <div>You said:</div> <span class="box">${msg}</span>`; }
function checkNumber(msg) { const num = +msg;
if (Number.isNaN(num)) {
msgEl.innerHTML += ` <div>This is not a valid number</div>`; return; }
if (num > 100 || num < 1) { msgEl.innerHTML += `<div>Number must be between 1 - 100</div>`; return; }
if (num === randomNum) { document.body.innerHTML = `<h2>Congrats! You have guessed the number! <br>It was ${num}</h2><button class="play-again" id="play-again">Play Again</button>` } else if (num > randomNum) { msgEl.innerHTML += '<div>GO LOWER</div>'; } else { msgEl.innerHTML += '<div>GO HIGHER</div>'; } }
const randomNum = Math.floor(Math.random() * 100) + 1;
console.log(randomNum);
window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition
let recognition = new SpeechRecognition();
recognition.start();
recognition.addEventListener('result', onSpeak)
recognition.addEventListener('end', () => recognition.start());
document.body.addEventListener('click', (e) => {
if (e.target.id == 'play-again') { window.location.reload(); } })
|