如何在游戏开始前加载所有图像? (JavaScript,预加载器)
How to load all images before the game starts? (JavaScript, Preloader)
我设计了这个游戏,这是我的第一个项目。这是 JavaScript 课程中“The Pig Game”的衍生产品。我为 UI 调整了 Pig Game 的 HTML 和 CSS 模板,但我从头开始进行游戏设计和编码。您可以在这里玩游戏:https://jeffparadox.000webhostapp.com/
我有一些问题,如果有人关心:
你怎么看,你觉得有什么问题吗?有什么比现在更清楚的了吗(尤其是 UI)?
游戏在我的电脑上运行很快。但是当我访问该站点时,图像并没有立即开始旋转;大约需要 30 秒才能开始看到明显旋转的图像。我认为这是因为浏览器正在加载图像但代码运行速度更快。有没有办法在代码中预加载这些图像,以便游戏正常启动?或者,如果我清理我的代码,游戏加载速度是否会更快而不需要预加载图像?
这是我的 JS 代码。如果有人有兴趣检查它并告诉我可以清理和优化哪些部分,我将非常感激。提前致谢:
"use strict";
// Selecting elements
const player0El = document.querySelector(".player--0");
const player1El = document.querySelector(".player--1");
const tries0El = document.getElementById("tries--0");
const tries1El = document.getElementById("tries--1");
const current0El = document.getElementById("current--0");
const current1El = document.getElementById("current--1");
const animalEl = document.querySelector(".animal");
const btnSpin = document.querySelector(".btn--spin");
const btnReset = document.querySelector(".btn--reset");
const btnRestart = document.querySelector(".btn--restart");
const youWin0El = document.querySelector("#you-win--0");
const youWin1El = document.querySelector("#you-win--1");
const highScore0El = document.querySelector(".high-score--0");
const highScore1El = document.querySelector(".high-score--1");
// Declare let variables
let triesLeft,
playerScores,
highScores,
activePlayer,
round,
currentScore,
playing;
// Starting conditions
const init = function () {
youWin0El.classList.add("hidden");
youWin1El.classList.add("hidden");
youWin1El.textContent = "You Win! ";
youWin0El.textContent = "You Win! ";
currentScore = 0;
triesLeft = [10, 10];
playerScores = [0, 0];
highScores = [0, 0];
activePlayer = 0;
round = 3;
playing = true;
btnRestart.textContent = ` ROUND: ${round}`;
tries0El.textContent = 10;
tries1El.textContent = 10;
current0El.textContent = 0;
current1El.textContent = 0;
animalEl.src = "noAnimal.jpg";
player0El.classList.remove("player--winner");
player1El.classList.remove("player--winner");
player0El.classList.add("player--active");
player1El.classList.remove("player--active");
};
// Initialize game
init();
// ***GAME FUNCTIONS***
// Switch players
const switchPlayer = function () {
activePlayer = activePlayer === 0 ? 1 : 0;
player0El.classList.toggle("player--active");
player1El.classList.toggle("player--active");
};
// Check how many rounds left
const checkRound = function () {
btnRestart.textContent = ` ROUND: ${round}`;
if (round < 1) {
gameOver();
} else if (triesLeft[activePlayer] < 1 && round > 0) {
if (triesLeft[0] === 0 && triesLeft[1] === 0) {
triesLeft[0] = 10;
triesLeft[1] = 10;
tries0El.textContent = 10;
tries1El.textContent = 10;
}
switchPlayer();
}
};
// End of game
const gameOver = function () {
playing = false;
if (playerScores[0] > playerScores[1]) {
youWin0El.classList.remove("hidden");
} else if (playerScores[0] < playerScores[1]) {
youWin1El.classList.remove("hidden");
} else if (playerScores[0] === playerScores[1]) {
youWin1El.textContent = "It's a Tie ";
youWin0El.textContent = "It's a Tie ";
youWin1El.classList.remove("hidden");
youWin0El.classList.remove("hidden");
}
};
// Check the rabbit, increase and log the score
const checkRabbit = function () {
if (imageNumber === 0) {
currentScore =
Number(document.getElementById(`current--${activePlayer}`).textContent) +
1;
playerScores[activePlayer] = currentScore;
document.getElementById(
`current--${activePlayer}`
).textContent = currentScore;
}
};
// Update tries left
const triesUpdate = function () {
triesLeft[activePlayer] -= 1;
document.getElementById(`tries--${activePlayer}`).textContent =
triesLeft[activePlayer];
};
// Update high scores
const registerHighScore = function () {
if (playerScores[activePlayer] > highScores[activePlayer]) {
highScores[activePlayer] = playerScores[activePlayer];
document.getElementById(
`high-score--${activePlayer}`
).textContent = `High Score: ${highScores[activePlayer]}`;
}
};
// ***GAME ENGINE***
// Declare game engine variables
let interval, imageNumber;
// Spinning images
btnSpin.addEventListener("click", function () {
if (playing) {
// Change button to Stop
btnSpin.textContent = `⛔ STOP!`;
// Stop the spinning (Runs only when interval is declared)
if (interval) {
clearInterval(interval);
interval = null;
btnSpin.textContent = ` SPIN!`;
triesUpdate();
checkRabbit();
registerHighScore();
if (triesLeft[0] < 1 && triesLeft[1] < 1) {
round -= 1;
}
checkRound();
// Start the spinning (Runs only when interval is null or undefined)
} else {
// Loop with time intervals
interval = setInterval(function () {
// Genarate image number
imageNumber = Math.trunc(Math.random() * 10);
// Show image with the generated number
animalEl.src = `animal-${imageNumber}.jpg`;
}, 5);
}
}
});
// ***RESET GAME***
btnReset.addEventListener("click", init);
您可以通过将其放入 <head>
中为您使用的每个图像预加载图像:
<link rel="preload" as="image" href="animal-1.png">
此处有更多文档:https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content
至于其他问题,它们可能更适合 SE Code Review:https://codereview.stackexchange.com/
我设计了这个游戏,这是我的第一个项目。这是 JavaScript 课程中“The Pig Game”的衍生产品。我为 UI 调整了 Pig Game 的 HTML 和 CSS 模板,但我从头开始进行游戏设计和编码。您可以在这里玩游戏:https://jeffparadox.000webhostapp.com/
我有一些问题,如果有人关心:
你怎么看,你觉得有什么问题吗?有什么比现在更清楚的了吗(尤其是 UI)?
游戏在我的电脑上运行很快。但是当我访问该站点时,图像并没有立即开始旋转;大约需要 30 秒才能开始看到明显旋转的图像。我认为这是因为浏览器正在加载图像但代码运行速度更快。有没有办法在代码中预加载这些图像,以便游戏正常启动?或者,如果我清理我的代码,游戏加载速度是否会更快而不需要预加载图像?
这是我的 JS 代码。如果有人有兴趣检查它并告诉我可以清理和优化哪些部分,我将非常感激。提前致谢:
"use strict";
// Selecting elements
const player0El = document.querySelector(".player--0");
const player1El = document.querySelector(".player--1");
const tries0El = document.getElementById("tries--0");
const tries1El = document.getElementById("tries--1");
const current0El = document.getElementById("current--0");
const current1El = document.getElementById("current--1");
const animalEl = document.querySelector(".animal");
const btnSpin = document.querySelector(".btn--spin");
const btnReset = document.querySelector(".btn--reset");
const btnRestart = document.querySelector(".btn--restart");
const youWin0El = document.querySelector("#you-win--0");
const youWin1El = document.querySelector("#you-win--1");
const highScore0El = document.querySelector(".high-score--0");
const highScore1El = document.querySelector(".high-score--1");
// Declare let variables
let triesLeft,
playerScores,
highScores,
activePlayer,
round,
currentScore,
playing;
// Starting conditions
const init = function () {
youWin0El.classList.add("hidden");
youWin1El.classList.add("hidden");
youWin1El.textContent = "You Win! ";
youWin0El.textContent = "You Win! ";
currentScore = 0;
triesLeft = [10, 10];
playerScores = [0, 0];
highScores = [0, 0];
activePlayer = 0;
round = 3;
playing = true;
btnRestart.textContent = ` ROUND: ${round}`;
tries0El.textContent = 10;
tries1El.textContent = 10;
current0El.textContent = 0;
current1El.textContent = 0;
animalEl.src = "noAnimal.jpg";
player0El.classList.remove("player--winner");
player1El.classList.remove("player--winner");
player0El.classList.add("player--active");
player1El.classList.remove("player--active");
};
// Initialize game
init();
// ***GAME FUNCTIONS***
// Switch players
const switchPlayer = function () {
activePlayer = activePlayer === 0 ? 1 : 0;
player0El.classList.toggle("player--active");
player1El.classList.toggle("player--active");
};
// Check how many rounds left
const checkRound = function () {
btnRestart.textContent = ` ROUND: ${round}`;
if (round < 1) {
gameOver();
} else if (triesLeft[activePlayer] < 1 && round > 0) {
if (triesLeft[0] === 0 && triesLeft[1] === 0) {
triesLeft[0] = 10;
triesLeft[1] = 10;
tries0El.textContent = 10;
tries1El.textContent = 10;
}
switchPlayer();
}
};
// End of game
const gameOver = function () {
playing = false;
if (playerScores[0] > playerScores[1]) {
youWin0El.classList.remove("hidden");
} else if (playerScores[0] < playerScores[1]) {
youWin1El.classList.remove("hidden");
} else if (playerScores[0] === playerScores[1]) {
youWin1El.textContent = "It's a Tie ";
youWin0El.textContent = "It's a Tie ";
youWin1El.classList.remove("hidden");
youWin0El.classList.remove("hidden");
}
};
// Check the rabbit, increase and log the score
const checkRabbit = function () {
if (imageNumber === 0) {
currentScore =
Number(document.getElementById(`current--${activePlayer}`).textContent) +
1;
playerScores[activePlayer] = currentScore;
document.getElementById(
`current--${activePlayer}`
).textContent = currentScore;
}
};
// Update tries left
const triesUpdate = function () {
triesLeft[activePlayer] -= 1;
document.getElementById(`tries--${activePlayer}`).textContent =
triesLeft[activePlayer];
};
// Update high scores
const registerHighScore = function () {
if (playerScores[activePlayer] > highScores[activePlayer]) {
highScores[activePlayer] = playerScores[activePlayer];
document.getElementById(
`high-score--${activePlayer}`
).textContent = `High Score: ${highScores[activePlayer]}`;
}
};
// ***GAME ENGINE***
// Declare game engine variables
let interval, imageNumber;
// Spinning images
btnSpin.addEventListener("click", function () {
if (playing) {
// Change button to Stop
btnSpin.textContent = `⛔ STOP!`;
// Stop the spinning (Runs only when interval is declared)
if (interval) {
clearInterval(interval);
interval = null;
btnSpin.textContent = ` SPIN!`;
triesUpdate();
checkRabbit();
registerHighScore();
if (triesLeft[0] < 1 && triesLeft[1] < 1) {
round -= 1;
}
checkRound();
// Start the spinning (Runs only when interval is null or undefined)
} else {
// Loop with time intervals
interval = setInterval(function () {
// Genarate image number
imageNumber = Math.trunc(Math.random() * 10);
// Show image with the generated number
animalEl.src = `animal-${imageNumber}.jpg`;
}, 5);
}
}
});
// ***RESET GAME***
btnReset.addEventListener("click", init);
您可以通过将其放入 <head>
中为您使用的每个图像预加载图像:
<link rel="preload" as="image" href="animal-1.png">
此处有更多文档:https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content
至于其他问题,它们可能更适合 SE Code Review:https://codereview.stackexchange.com/