Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
wuchenchina authored Nov 11, 2023
1 parent d0de275 commit 5f26629
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
14 changes: 14 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>反应力测试</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="game-container"></div>
<button id="start-button">开始测试</button>
<script src="script.js"></script>
</body>
</html>
46 changes: 46 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
document.getElementById('start-button').addEventListener('click', startGame);

let startTime;
let reactionTimes = [];

function startGame() {
document.getElementById('start-button').classList.add('hidden');
showRandomShape();
}

function showRandomShape() {
const shape = document.createElement('div');
shape.classList.add('shape');
shape.style.top = `${Math.random() * (window.innerHeight - 50)}px`;
shape.style.left = `${Math.random() * (window.innerWidth - 50)}px`;
shape.style.backgroundColor = getRandomColor();
shape.onclick = shapeClicked;

document.body.appendChild(shape);
startTime = new Date();
}

function shapeClicked() {
const endTime = new Date();
const reactionTime = (endTime - startTime) / 1000;
reactionTimes.push(reactionTime);
this.remove();

if (reactionTimes.length < 10) {
setTimeout(showRandomShape, Math.random() * 2000);
} else {
endGame();
}
}

function getRandomColor() {
const colors = ['red', 'blue', 'green', 'yellow', 'purple'];
return colors[Math.floor(Math.random() * colors.length)];
}

function endGame() {
const average = reactionTimes.reduce((a, b) => a + b, 0) / reactionTimes.length;
alert(`测试完成!\n平均反应时间:${average.toFixed(2)} 秒`);
document.getElementById('start-button').classList.remove('hidden');
reactionTimes = [];
}
45 changes: 45 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
body, html {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
overflow: hidden;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-family: Arial, sans-serif;
background-color: #f0f0f0;
}

#start-button {
padding: 10px 20px;
font-size: 16px;
color: white;
background-color: #007bff;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s, transform 0.2s;
outline: none;
}

#start-button:hover {
background-color: #0056b3;
transform: scale(1.05);
}

#start-button:active {
transform: scale(0.95);
}

.shape {
width: 50px;
height: 50px;
position: fixed;
cursor: pointer;
}

.hidden {
display: none;
}

0 comments on commit 5f26629

Please sign in to comment.