undefined / index.html
igorq212's picture
Создай простую браузерную игру на HTML5 Canvas и JavaScript.
103e064 verified
Raw History Blame Contribute Delete
14.6 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Alina's Academic Adventure</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/feather-icons"></script>
<style>
#gameCanvas {
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
border-radius: 12px;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
}
.character {
transition: all 0.3s ease;
}
.obstacle {
animation: float 3s infinite ease-in-out;
}
@keyframes float {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-10px); }
}
</style>
</head>
<body class="bg-gradient-to-br from-purple-100 to-blue-100 min-h-screen flex flex-col items-center justify-center p-4">
<header class="text-center mb-8">
<h1 class="text-4xl font-bold text-purple-800 mb-2">Alina's Academic Adventure</h1>
<p class="text-purple-600">Help Alina the kitten reach her university!</p>
</header>
<div class="relative w-full max-w-2xl">
<canvas id="gameCanvas" width="800" height="500" class="w-full h-auto"></canvas>
<div id="startScreen" class="absolute inset-0 flex flex-col items-center justify-center bg-black bg-opacity-70 rounded-lg">
<div class="bg-white p-8 rounded-lg text-center max-w-md">
<h2 class="text-2xl font-bold text-purple-800 mb-4">Ready for school?</h2>
<p class="text-gray-700 mb-6">Help Alina navigate through obstacles to reach her university. Use arrow keys to move!</p>
<button id="startButton" class="bg-purple-600 hover:bg-purple-700 text-white font-bold py-3 px-6 rounded-full transition-all transform hover:scale-105">
Start Journey
</button>
</div>
</div>
<div id="gameOverScreen" class="absolute inset-0 hidden flex-col items-center justify-center bg-black bg-opacity-70 rounded-lg">
<div class="bg-white p-8 rounded-lg text-center max-w-md">
<h2 id="resultText" class="text-2xl font-bold text-purple-800 mb-4">Oh no!</h2>
<p id="scoreText" class="text-gray-700 mb-6">You didn't make it to school today.</p>
<button id="restartButton" class="bg-purple-600 hover:bg-purple-700 text-white font-bold py-3 px-6 rounded-full transition-all transform hover:scale-105">
Try Again
</button>
</div>
</div>
</div>
<div class="mt-8 flex items-center gap-4">
<div class="bg-white p-4 rounded-lg shadow-md">
<h3 class="text-lg font-semibold text-purple-800 mb-2">Controls</h3>
<div class="flex gap-2">
<div class="bg-gray-100 p-2 rounded"><i data-feather="arrow-up"></i></div>
<div class="bg-gray-100 p-2 rounded"><i data-feather="arrow-down"></i></div>
<div class="bg-gray-100 p-2 rounded"><i data-feather="arrow-left"></i></div>
<div class="bg-gray-100 p-2 rounded"><i data-feather="arrow-right"></i></div>
</div>
</div>
<div class="bg-white p-4 rounded-lg shadow-md">
<h3 class="text-lg font-semibold text-purple-800 mb-2">Score</h3>
<p id="currentScore" class="text-2xl font-bold text-purple-600">0</p>
</div>
</div>
<footer class="mt-12 text-center text-purple-500">
<p>Made with ❤️ for Alina's academic journey</p>
</footer>
<script>
feather.replace();
// Game variables
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const startButton = document.getElementById('startButton');
const restartButton = document.getElementById('restartButton');
const resultText = document.getElementById('resultText');
const scoreText = document.getElementById('scoreText');
const currentScore = document.getElementById('currentScore');
let gameRunning = false;
let score = 0;
// Character properties
const character = {
x: 50,
y: canvas.height / 2,
width: 50,
height: 50,
speed: 5,
color: '#FF6B6B'
};
// University properties
const university = {
x: canvas.width - 100,
y: canvas.height / 2 - 25,
width: 80,
height: 100,
color: '#4ECDC4'
};
// Array of obstacles
let obstacles = [];
const obstacleTypes = ['homework', 'sleep', 'distractions'];
// Keyboard controls
const keys = {
ArrowUp: false,
ArrowDown: false,
ArrowLeft: false,
ArrowRight: false
};
// Event listeners
startButton.addEventListener('click', startGame);
restartButton.addEventListener('click', startGame);
document.addEventListener('keydown', (e) => {
if (keys.hasOwnProperty(e.key)) {
keys[e.key] = true;
}
});
document.addEventListener('keyup', (e) => {
if (keys.hasOwnProperty(e.key)) {
keys[e.key] = false;
}
});
// Start game function
function startGame() {
gameRunning = true;
score = 0;
currentScore.textContent = score;
character.x = 50;
character.y = canvas.height / 2;
obstacles = [];
startScreen.classList.add('hidden');
gameOverScreen.classList.add('hidden');
// Start game loop
requestAnimationFrame(gameLoop);
}
// Game loop
function gameLoop() {
if (!gameRunning) return;
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update character position
updateCharacter();
// Update obstacles
updateObstacles();
// Draw game elements
drawGame();
// Check for collisions or win condition
checkGameStatus();
// Continue game loop
requestAnimationFrame(gameLoop);
}
// Update character position based on keyboard input
function updateCharacter() {
if (keys.ArrowUp && character.y > 0) {
character.y -= character.speed;
}
if (keys.ArrowDown && character.y < canvas.height - character.height) {
character.y += character.speed;
}
if (keys.ArrowLeft && character.x > 0) {
character.x -= character.speed;
}
if (keys.ArrowRight && character.x < canvas.width - character.width) {
character.x += character.speed;
}
}
// Update obstacles
function updateObstacles() {
// Randomly add new obstacles
if (Math.random() < 0.02) {
const type = obstacleTypes[Math.floor(Math.random() * obstacleTypes.length)];
const size = Math.random() * 30 + 30;
obstacles.push({
x: canvas.width,
y: Math.random() * (canvas.height - size),
width: size,
height: size,
speed: Math.random() * 3 + 2,
type: type,
color: getObstacleColor(type)
});
}
// Move obstacles
obstacles.forEach(obstacle => {
obstacle.x -= obstacle.speed;
});
// Remove obstacles that are off screen
obstacles = obstacles.filter(obstacle => obstacle.x + obstacle.width > 0);
}
// Get obstacle color based on type
function getObstacleColor(type) {
switch(type) {
case 'homework': return '#FFD166';
case 'sleep': return '#06D6A0';
case 'distractions': return '#EF476F';
default: return '#118AB2';
}
}
// Draw game elements
function drawGame() {
// Draw university
ctx.fillStyle = university.color;
ctx.fillRect(university.x, university.y, university.width, university.height);
// Draw university details
ctx.fillStyle = '#FFFFFF';
ctx.fillRect(university.x + 10, university.y + 20, 15, 60);
ctx.fillRect(university.x + 30, university.y + 10, 15, 70);
ctx.fillRect(university.x + 50, university.y + 30, 15, 50);
// Draw character (kitten)
ctx.fillStyle = character.color;
ctx.beginPath();
ctx.arc(character.x + character.width/2, character.y + character.height/2, character.width/2, 0, Math.PI * 2);
ctx.fill();
// Draw kitten face
ctx.fillStyle = '#FFFFFF';
ctx.beginPath();
ctx.arc(character.x + character.width/2 - 10, character.y + character.height/2 - 5, 5, 0, Math.PI * 2);
ctx.arc(character.x + character.width/2 + 10, character.y + character.height/2 - 5, 5, 0, Math.PI * 2);
ctx.fill();
// Draw kitten mouth
ctx.strokeStyle = '#FFFFFF';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.arc(character.x + character.width/2, character.y + character.height/2 + 5, 8, 0, Math.PI);
ctx.stroke();
// Draw kitten ears
ctx.fillStyle = character.color;
ctx.beginPath();
ctx.moveTo(character.x + character.width/2 - 15, character.y + character.height/2 - 25);
ctx.lineTo(character.x + character.width/2 - 5, character.y + character.height/2 - 15);
ctx.lineTo(character.x + character.width/2 - 25, character.y + character.height/2 - 5);
ctx.fill();
ctx.beginPath();
ctx.moveTo(character.x + character.width/2 + 15, character.y + character.height/2 - 25);
ctx.lineTo(character.x + character.width/2 + 5, character.y + character.height/2 - 15);
ctx.lineTo(character.x + character.width/2 + 25, character.y + character.height/2 - 5);
ctx.fill();
// Draw obstacles
obstacles.forEach(obstacle => {
ctx.fillStyle = obstacle.color;
// Different shapes for different obstacles
if (obstacle.type === 'homework') {
// Draw stack of papers
ctx.fillRect(obstacle.x, obstacle.y, obstacle.width, obstacle.height/3);
ctx.fillRect(obstacle.x + 5, obstacle.y + obstacle.height/3, obstacle.width - 10, obstacle.height/3);
ctx.fillRect(obstacle.x + 10, obstacle.y + 2*obstacle.height/3, obstacle.width - 20, obstacle.height/3);
} else if (obstacle.type === 'sleep') {
// Draw pillow
ctx.beginPath();
ctx.ellipse(obstacle.x + obstacle.width/2, obstacle.y + obstacle.height/2,
obstacle.width/2, obstacle.height/2, 0, 0, Math.PI * 2);
ctx.fill();
// Draw Zzz
ctx.fillStyle = '#FFFFFF';
ctx.font = 'bold 16px Arial';
ctx.fillText('Zzz', obstacle.x + obstacle.width/2 - 10, obstacle.y + obstacle.height/2 + 5);
} else {
// Draw phone for distractions
ctx.fillRect(obstacle.x, obstacle.y, obstacle.width, obstacle.height);
ctx.fillStyle = '#000000';
ctx.fillRect(obstacle.x + 5, obstacle.y + 5, obstacle.width - 10, obstacle.height - 25);
ctx.fillStyle = '#FFFFFF';
ctx.fillRect(obstacle.x + 10, obstacle.y + 10, obstacle.width - 20, obstacle.height - 30);
}
});
}
// Check game status (collisions or win)
function checkGameStatus() {
// Check for collision with obstacles
for (const obstacle of obstacles) {
if (
character.x < obstacle.x + obstacle.width &&
character.x + character.width > obstacle.x &&
character.y < obstacle.y + obstacle.height &&
character.y + character.height > obstacle.y
) {
gameOver(false);
return;
}
}
// Check if reached university
if (
character.x < university.x + university.width &&
character.x + character.width > university.x &&
character.y < university.y + university.height &&
character.y + character.height > university.y
) {
gameOver(true);
return;
}
// Increment score
score++;
currentScore.textContent = Math.floor(score / 10);
}
// Game over function
function gameOver(success) {
gameRunning = false;
if (success) {
resultText.textContent = "Congratulations!";
scoreText.textContent = `Alina made it to university with a score of ${Math.floor(score / 10)}!`;
} else {
resultText.textContent = "Oh no!";
scoreText.textContent = `Alina couldn't make it today. Your score: ${Math.floor(score / 10)}`;
}
gameOverScreen.classList.remove('hidden');
}
</script>
</body>
</html>