此处涉及到的知识点有
- 利用flex实现页面布局,包括水平垂直分布及order排序
- 使用requestAnimationFrame实现图形的连续旋转和停止(也可使用css实现)
- 设置文档的键盘事件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>2-player reaction game</title>
<style>
/* General styles */
html {
background-color: white;
height: 100%;
font-family: sans-serif;
}
body {
height: inherit;
background-color: red;
margin: 0;
}
* {
box-sizing: border-box;
}
/* UI Layout */
section {
width: 100%;
height: inherit;
padding: 30px;
}
.topbar {
height: 50%;
display: flex;
justify-content: space-between;
}
.topbar p, button {
margin: 0;
font-size: 1.5rem;
border: 5px solid;
border-radius: 20px;
padding: 10px 20px;
}
.p1, .p2 {
align-self: flex-start;
}
.topbar .p1 {
order: 0;
border-color: yellow;
color: yellow;
}
.topbar .p2 {
order: 2;
border-color: cyan;
color: cyan;
}
.topbar .middlebar {
order: 1;
}
.middlebar {
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
}
/* Button-specific styling */
button {
border: 0;
padding: 12.75px 20px;
background-color: #ddd;
cursor: pointer;
}
button:hover, button:focus {
background-color: #eee;
}
button:active {
background-color: #fff;
}
/* spinner-specific styling */
.spinner {
position: absolute;
z-index: 1;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.spinner div {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.spinner p {
margin: 0;
font-size: 10rem;
}
</style>
</head>
<body>
<div class="spinner"><div><p>↻</p></div></div>
<section class="ui">
<div class="topbar">
<p class="p1">Player 1: "A"</p>
<p class="p2">Player 2: "L"</p>
<div class="middlebar">
<button>Start game</button>
<p class="result"></p>
</div>
</div>
</section>
<script>
</script>
</body>
</html>
js代码
声明变量
const spinner = document.querySelector('.spinner p');
const spinnerContainer = document.querySelector('.spinner');
let rotateCount = 0; // 旋转角度计数
let startTime = null; // 开始旋转时间
let rAF; //存储requestAnimationFrame
const btn = document.querySelector('button');
const result = document.querySelector('.result');
// 返回[min, max)的随机整数
function random(min,max) {
var num = Math.floor(Math.random()*(max-min)) + min;
return num;
}
function draw(timestamp) {
if(!startTime) {
startTime = timestamp;
}
rotateCount = (timestamp - startTime) / 3;
if(rotateCount > 359) {
rotateCount %= 360;
}
spinner.style.transform = 'rotate(' + rotateCount + 'deg)';
rAF = requestAnimationFrame(draw);
}
result.style.display = 'none';
spinnerContainer.style.display = 'none';
// 重置游戏
function reset() {
btn.style.display = 'block';
result.textContent = '';
result.style.display = 'none';
}
// 开始按钮处理函数
btn.addEventListener('click', start);
function start() {
// Start the spinner spinning
draw();
// Show the spinner and hide the button
spinnerContainer.style.display = 'block';
btn.style.display = 'none';
// run the setEndgame() function after a random number of seconds between 5 and 10
setTimeout(setEndgame, random(5000,10000));
}
// 结束游戏处理函数
function setEndgame() {
cancelAnimationFrame(rAF);
spinnerContainer.style.display = 'none';
result.style.display = 'block';
result.textContent = 'PLAYERS GO!!';
// 设置键盘事件
document.addEventListener('keydown', keyHandler);
function keyHandler(e) {
let isOver = false;
console.log(e.key);
if (e.key === "a") {
result.textContent = 'Player 1 won!!';
isOver = true;
} else if (e.key === "l") {
result.textContent = 'Player 2 won!!';
isOver = true;
}
if (isOver) {
document.removeEventListener('keydown', keyHandler);
setTimeout(reset, 5000);
}
};
}