Oto zmodyfikowany kod JavaScript dla gry scroller "Quake Skacze", w którym wysokość przeszkód jest ograniczona do poziomu skoku postaci, a monety generują się w zakresie wysokości skoku.
Zmiany w kodzie:
- Ograniczenie wysokości przeszkód: W funkcji
addObstacle(), wysokość przeszkody jest ograniczona do maksymalnej wysokości skoku postaci.
- Generowanie monet w zakresie skoku: W funkcji
addCoin(), wysokość monety jest generowana w zakresie wysokości skoku postaci.
Zmodyfikowany kod JavaScript (script.js):
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreDisplay = document.getElementById('scoreDisplay');
const finalScoreDisplay = document.getElementById('finalScore');
const gameFrame = document.getElementById('gameFrame');
const restartButton = document.getElementById('restartButton');
let playerX = 50;
let playerY = 500;
let playerWidth = 16;
let playerHeight = 32;
let gravity = 0.5;
let jumpStrength = -15;
let isJumping = false;
let obstacles = [];
let coins = [];
let score = 0;
let gameRunning = true;
let maxJumpHeight = 300; // Maksymalna wysokość skoku postaci
const playerImage = new Image();
playerImage.src = 'character.png';
const obstacleImage = new Image();
obstacleImage.src = 'obstacle.png';
const coinImage = new Image();
coinImage.src = 'coin.png';
function addObstacle() {
if (!gameRunning) return;
const obstacle = {
x: canvas.width,
y: Math.floor(Math.random() * Math.min(maxJumpHeight, canvas.height - obstacleImage.height)), // Ograniczenie wysokości
width: obstacleImage.width,
height: obstacleImage.height,
speed: 3 + (score / 10)
};
obstacles.push(obstacle);
}
function addCoin() {
if (!gameRunning) return;
const coin = {
x: canvas.width,
y: Math.floor(Math.random() * Math.min(maxJumpHeight, canvas.height - coinImage.height)), // Generowanie w zakresie skoku
width: coinImage.width,
height: coinImage.height,
speed: 3
};
coins.push(coin);
}
function drawPlayer() {
ctx.drawImage(playerImage, playerX, playerY, playerWidth, playerHeight);
}
function drawObstacle(obstacle) {
ctx.drawImage(obstacleImage, obstacle.x, obstacle.y, obstacle.width, obstacle.height);
}
function drawCoin(coin) {
ctx.drawImage(coinImage, coin.x, coin.y, coin.width, coin.height);
}
function updatePlayer() {
if (!gameRunning) return;
if (isJumping) {
playerY += jumpStrength;
jumpStrength += gravity;
if (playerY + playerHeight >= canvas.height) {
playerY = canvas.height - playerHeight;
isJumping = false;
}
} else {
playerY = canvas.height - playerHeight;
}
}
function updateObstacles() {
if (!gameRunning) return;
for (let i = 0; i < obstacles.length; i++) {
obstacles[i].x -= obstacles[i].speed;
if (obstacles[i].x < -obstacles[i].width) obstacles.splice(i, 1);
}
}
function updateCoins() {
if (!gameRunning) return;
for (let i = 0; i < coins.length; i++) {
coins[i].x -= coins[i].speed;
if (coins[i].x < -coins[i].width) coins.splice(i, 1);
}
}
function checkCollision() {
if (!gameRunning) return;
for (let i = 0; i < obstacles.length; i++) {
if (
playerX < obstacles[i].x + obstacles[i].width &&
playerX + playerWidth > obstacles[i].x &&
playerY + playerHeight > obstacles[i].y &&
playerY < obstacles[i].y + obstacles[i].height
) {
gameRunning = false;
finalScoreDisplay.textContent = score;
gameFrame.style.display = 'block';
return;
}
}
}
function collectCoins() {
if (!gameRunning) return;
for (let i = 0; i < coins.length; i++) {
if (
playerX < coins[i].x + coins[i].width &&
playerX + playerWidth > coins[i].x &&
playerY + playerHeight > coins[i].y &&
playerY < coins[i].y + coins[i].height
) {
score++;
coins.splice(i, 1);
}
}
}
function resetGame() {
playerX = 50;
playerY = 500;
isJumping = false;
obstacles = [];
coins = [];
score = 0;
gameRunning = true;
gameFrame.style.display = 'none';
gameLoop();
}
restartButton.addEventListener('click', resetGame);
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (Math.random() < 0.02) addObstacle();
if (Math.random() < 0.01) addCoin();
updatePlayer();
updateObstacles();
updateCoins();
checkCollision();
collectCoins();
drawPlayer();
obstacles.forEach(drawObstacle);
coins.forEach(drawCoin);
scoreDisplay.textContent = 'Wynik: ' + score;
requestAnimationFrame(gameLoop);
}
document.addEventListener('keydown', (event) => {
if (event.key === ' ' && !isJumping && gameRunning) {
isJumping = true;
jumpStrength = -15;
}
});
gameLoop();
Wyjaśnienie zmian:
maxJumpHeight: Zmienna przechowująca maksymalną wysokość skoku postaci.
Math.min(maxJumpHeight, canvas.height - obstacleImage.height): W funkcji addObstacle(), ta funkcja ogranicza wysokość przeszkody do maxJumpHeight lub wysokości canvas minus wysokość przeszkody, w zależności od tego, która wartość jest mniejsza.
Math.min(maxJumpHeight, canvas.height - coinImage.height): W funkcji addCoin(), ta funkcja ogranicza wysokość monety do maxJumpHeight lub wysokości canvas minus wysokość monety, w zależności od tego, która wartość jest mniejsza.
Pamiętaj, aby użyć tego zmodyfikowanego kodu JavaScript w swoim projekcie. Powinno to zapewnić, że przeszkody i monety generują się w odpowiednim zakresie wysokości, co poprawi rozgrywkę.