krying
Version:
krying — games hub, AI, movies, and tools
503 lines (422 loc) • 16.4 kB
HTML
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Kobran - Breakout</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: linear-gradient(135deg, #581c87, #1e3a8a, #3730a3);
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 1rem;
}
.game-container {
background: rgba(255, 255, 255, 0.1);
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 0.75rem;
padding: 1.5rem;
backdrop-filter: blur(10px);
}
.game-title {
color: white;
text-align: center;
font-size: 1.5rem;
font-weight: bold;
margin-bottom: 1rem;
}
.game-stats {
display: flex;
justify-content: space-between;
color: white;
font-weight: bold;
margin-bottom: 1rem;
padding: 0 1rem;
}
#gameCanvas {
border: 2px solid white;
background: #000;
display: block;
margin: 0 auto 1rem;
}
.controls {
display: flex;
justify-content: center;
gap: 1rem;
margin-bottom: 1rem;
}
.btn {
background: rgba(255, 255, 255, 0.2);
border: 1px solid rgba(255, 255, 255, 0.3);
color: white;
padding: 0.5rem 1rem;
border-radius: 0.375rem;
cursor: pointer;
font-weight: 500;
}
.btn:hover {
background: rgba(255, 255, 255, 0.3);
}
.instructions {
text-align: center;
color: rgba(255, 255, 255, 0.6);
font-size: 0.875rem;
line-height: 1.4;
}
.game-over {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(0, 0, 0, 0.9);
color: white;
padding: 2rem;
border-radius: 0.5rem;
text-align: center;
display: none;
}
.power-ups {
margin-top: 1rem;
text-align: center;
}
.power-up {
display: inline-block;
margin: 0 0.5rem;
padding: 0.25rem 0.5rem;
background: rgba(255, 255, 255, 0.2);
border-radius: 0.25rem;
font-size: 0.75rem;
}
</style>
</head>
<body>
<div class="game-container">
<h1 class="game-title">Breakout</h1>
<div class="game-stats">
<span>Score: <span id="score">0</span></span>
<span>Lives: <span id="lives">3</span></span>
<span>Level: <span id="level">1</span></span>
</div>
<canvas id="gameCanvas" width="480" height="320"></canvas>
<div class="controls">
<button class="btn" onclick="startGame()" id="startBtn">Start Game</button>
<button class="btn" onclick="pauseGame()" id="pauseBtn" style="display: none;">Pause</button>
<button class="btn" onclick="resetGame()">Reset</button>
</div>
<div class="power-ups">
<div class="power-up">🔵 Multi-Ball</div>
<div class="power-up">📏 Wide Paddle</div>
<div class="power-up">⚡ Fast Ball</div>
<div class="power-up">💎 Bonus Points</div>
</div>
<div class="instructions">
<p>Use arrow keys or A/D to move paddle</p>
<p>Break all bricks to advance to the next level!</p>
</div>
</div>
<div class="game-over" id="gameOver">
<h2 id="gameOverTitle">Game Over!</h2>
<p>Final Score: <span id="finalScore">0</span></p>
<p>Level Reached: <span id="finalLevel">1</span></p>
<button class="btn" onclick="resetGame()">Play Again</button>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
let gameRunning = false;
let gameLoop = null;
const game = {
score: 0,
lives: 3,
level: 1
};
const paddle = {
x: canvas.width / 2 - 40,
y: canvas.height - 20,
width: 80,
height: 10,
speed: 6,
normalWidth: 80
};
let balls = [{
x: canvas.width / 2,
y: canvas.height - 40,
radius: 8,
velocityX: 4,
velocityY: -4,
speed: 4
}];
let bricks = [];
let powerUps = [];
const brickConfig = {
rows: 5,
cols: 10,
width: 44,
height: 20,
padding: 2,
offsetTop: 60,
offsetLeft: 20
};
const colors = ['#ff6b6b', '#4ecdc4', '#45b7d1', '#f9ca24', '#f0932b'];
const keys = {};
document.addEventListener('keydown', (e) => {
keys[e.key.toLowerCase()] = true;
});
document.addEventListener('keyup', (e) => {
keys[e.key.toLowerCase()] = false;
});
function initBricks() {
bricks = [];
for (let r = 0; r < brickConfig.rows; r++) {
for (let c = 0; c < brickConfig.cols; c++) {
const brick = {
x: c * (brickConfig.width + brickConfig.padding) + brickConfig.offsetLeft,
y: r * (brickConfig.height + brickConfig.padding) + brickConfig.offsetTop,
width: brickConfig.width,
height: brickConfig.height,
color: colors[r % colors.length],
destroyed: false,
points: (5 - r) * 10
};
bricks.push(brick);
}
}
}
function createPowerUp(x, y) {
if (Math.random() < 0.15) {
const types = ['multiBall', 'widePaddle', 'fastBall', 'bonusPoints'];
const type = types[Math.floor(Math.random() * types.length)];
powerUps.push({
x: x,
y: y,
width: 20,
height: 20,
type: type,
velocityY: 2,
duration: 0
});
}
}
function update() {
if (!gameRunning) return;
if (keys['arrowleft'] || keys['a']) {
paddle.x = Math.max(0, paddle.x - paddle.speed);
}
if (keys['arrowright'] || keys['d']) {
paddle.x = Math.min(canvas.width - paddle.width, paddle.x + paddle.speed);
}
balls.forEach((ball, ballIndex) => {
ball.x += ball.velocityX;
ball.y += ball.velocityY;
if (ball.x - ball.radius <= 0 || ball.x + ball.radius >= canvas.width) {
ball.velocityX = -ball.velocityX;
}
if (ball.y - ball.radius <= 0) {
ball.velocityY = -ball.velocityY;
}
if (ball.y + ball.radius >= paddle.y &&
ball.x >= paddle.x &&
ball.x <= paddle.x + paddle.width &&
ball.velocityY > 0) {
ball.velocityY = -Math.abs(ball.velocityY);
const hitPos = (ball.x - paddle.x) / paddle.width;
ball.velocityX = (hitPos - 0.5) * 8;
}
bricks.forEach(brick => {
if (!brick.destroyed &&
ball.x >= brick.x &&
ball.x <= brick.x + brick.width &&
ball.y >= brick.y &&
ball.y <= brick.y + brick.height) {
brick.destroyed = true;
ball.velocityY = -ball.velocityY;
game.score += brick.points;
createPowerUp(brick.x + brick.width / 2, brick.y + brick.height / 2);
updateDisplay();
}
});
if (ball.y > canvas.height) {
balls.splice(ballIndex, 1);
}
});
if (balls.length === 0) {
game.lives--;
updateDisplay();
if (game.lives <= 0) {
endGame();
} else {
resetBall();
}
}
powerUps.forEach((powerUp, index) => {
powerUp.y += powerUp.velocityY;
if (powerUp.y + powerUp.height >= paddle.y &&
powerUp.x >= paddle.x &&
powerUp.x <= paddle.x + paddle.width &&
powerUp.y <= paddle.y + paddle.height) {
activatePowerUp(powerUp.type);
powerUps.splice(index, 1);
}
if (powerUp.y > canvas.height) {
powerUps.splice(index, 1);
}
});
if (bricks.every(brick => brick.destroyed)) {
nextLevel();
}
}
function activatePowerUp(type) {
switch (type) {
case 'multiBall':
if (balls.length > 0) {
const originalBall = balls[0];
balls.push({
x: originalBall.x,
y: originalBall.y,
radius: originalBall.radius,
velocityX: -originalBall.velocityX,
velocityY: originalBall.velocityY,
speed: originalBall.speed
});
}
break;
case 'widePaddle':
paddle.width = paddle.normalWidth * 1.5;
setTimeout(() => {
paddle.width = paddle.normalWidth;
}, 10000);
break;
case 'fastBall':
balls.forEach(ball => {
ball.velocityX *= 1.5;
ball.velocityY *= 1.5;
});
setTimeout(() => {
balls.forEach(ball => {
ball.velocityX /= 1.5;
ball.velocityY /= 1.5;
});
}, 8000);
break;
case 'bonusPoints':
game.score += 100;
updateDisplay();
break;
}
}
function draw() {
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
bricks.forEach(brick => {
if (!brick.destroyed) {
ctx.fillStyle = brick.color;
ctx.fillRect(brick.x, brick.y, brick.width, brick.height);
ctx.fillStyle = 'rgba(255, 255, 255, 0.3)';
ctx.fillRect(brick.x, brick.y, brick.width, 3);
}
});
ctx.fillStyle = '#fff';
ctx.fillRect(paddle.x, paddle.y, paddle.width, paddle.height);
ctx.fillStyle = 'rgba(255, 255, 255, 0.7)';
ctx.fillRect(paddle.x, paddle.y, paddle.width, 2);
balls.forEach(ball => {
ctx.beginPath();
ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2);
ctx.fillStyle = '#fff';
ctx.fill();
});
powerUps.forEach(powerUp => {
ctx.fillStyle = getPowerUpColor(powerUp.type);
ctx.fillRect(powerUp.x, powerUp.y, powerUp.width, powerUp.height);
});
}
function getPowerUpColor(type) {
switch (type) {
case 'multiBall': return '#4ecdc4';
case 'widePaddle': return '#f9ca24';
case 'fastBall': return '#ff6b6b';
case 'bonusPoints': return '#45b7d1';
default: return '#fff';
}
}
function gameStep() {
update();
draw();
}
function startGame() {
if (gameRunning) return;
gameRunning = true;
document.getElementById('startBtn').style.display = 'none';
document.getElementById('pauseBtn').style.display = 'inline-block';
gameLoop = setInterval(gameStep, 1000 / 60);
}
function pauseGame() {
gameRunning = false;
clearInterval(gameLoop);
document.getElementById('startBtn').style.display = 'inline-block';
document.getElementById('pauseBtn').style.display = 'none';
}
function resetGame() {
gameRunning = false;
clearInterval(gameLoop);
game.score = 0;
game.lives = 3;
game.level = 1;
resetBall();
initBricks();
powerUps = [];
paddle.width = paddle.normalWidth;
paddle.x = canvas.width / 2 - 40;
updateDisplay();
document.getElementById('startBtn').style.display = 'inline-block';
document.getElementById('pauseBtn').style.display = 'none';
document.getElementById('gameOver').style.display = 'none';
draw();
}
function resetBall() {
balls = [{
x: canvas.width / 2,
y: canvas.height - 40,
radius: 8,
velocityX: 4 * (Math.random() > 0.5 ? 1 : -1),
velocityY: -4,
speed: 4
}];
}
function nextLevel() {
game.level++;
game.score += 200;
balls.forEach(ball => {
ball.speed += 0.5;
ball.velocityX *= 1.1;
ball.velocityY *= 1.1;
});
initBricks();
resetBall();
updateDisplay();
}
function updateDisplay() {
document.getElementById('score').textContent = game.score;
document.getElementById('lives').textContent = game.lives;
document.getElementById('level').textContent = game.level;
}
function endGame() {
gameRunning = false;
clearInterval(gameLoop);
document.getElementById('finalScore').textContent = game.score;
document.getElementById('finalLevel').textContent = game.level;
document.getElementById('gameOver').style.display = 'block';
}
resetGame();
</script>
</body>
</html>