Create and click on 9 image holes, keep score and time limit
HTML
<h1>🧩 Robotime Puzzle Challenge</h1>
<div id="timer">Starting in 3</div>
<div id="puzzle"></div>
<div id="message"></div>
CSS
body{
font-family: Arial;
background:#f5f5f5;
text-align:center;
margin:0;
padding:20px;
}
h1{
margin-bottom:10px;
}
#timer{
font-size:24px;
font-weight:bold;
margin-bottom:20px;
}
#puzzle{
width:510px;
height:510px;
margin:auto;
display:flex;
flex-wrap:wrap;
border:3px solid #333;
}
.tile{
width:100px;
height:100px;
border:1px solid white;
box-sizing:border-box;
background-image:url('https://www.robotimeonline.com/cdn/shop/files/SecretBloomyNook-5.jpg?v=1780637655&width=1200');
background-size:500px 500px;
cursor:pointer;
transition:0.2s;
}
.tile:hover{
transform:scale(0.98);
}
#message{
margin-top:20px;
font-size:28px;
color:green;
font-weight:bold;
}
JavaScript
const puzzle =
document.getElementById("puzzle");
const message =
document.getElementById("message");
const timerText =
document.getElementById("timer");
let startTime;
let timerInterval;
/* 321倒计时 */
let countdown = 3;
const countdownInterval =
setInterval(()=>{
countdown--;
if(countdown > 0){
timerText.innerHTML =
"Starting in " + countdown;
}else{
clearInterval(
countdownInterval);
startGame();
}
},1000);
/* 开始游戏 */
function startGame(){
startTime = Date.now();
timerInterval = setInterval(()=>{
let now = Date.now();
let seconds =
((now - startTime)/1000)
.toFixed(1);
timerText.innerHTML =
"⏱ Time: " +
seconds +
"s";
},100);
render();
}
/* 5x5 */
let positions = [
0,1,2,3,4,
5,6,7,8,9,
10,11,12,13,14,
15,16,17,18,19,
20,21,22,23,24
];
positions.sort(() =>
Math.random() - 0.5);
/* 渲染 */
function render(){
puzzle.innerHTML = "";
positions.forEach((pos,index)=>{
const tile =
document.createElement("div");
tile.className = "tile";
let x =
(pos % 5) * -100;
let y =
Math.floor(pos / 5) * -100;
tile.style.backgroundPosition =
`${x}px ${y}px`;
tile.onclick =
()=>swap(index);
puzzle.appendChild(tile);
});
checkWin();
}
let first = null;
function swap(index){
if(first === null){
first = index;
return;
}
[positions[first],positions[index]] =
[positions[index],positions[first]];
first = null;
render();
}
/* 胜利 */
function checkWin(){
let win = true;
for(let i=0;i<positions.length;i++){
if(positions[i] !== i){
win = false;
break;
}
}
if(win){
...