Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="ru">
- <head>
- <meta charset="UTF-8">
- <title>TRON: ARCANOID</title>
- <style>
- body { margin: 0; overflow: hidden; background: #050505; font-family: 'Courier New', monospace; }
- canvas { display: block; }
- #ui { position: absolute; top: 20px; left: 20px; color: #0ff; font-size: 18px; text-shadow: 0 0 10px #0ff; pointer-events: none; }
- </style>
- </head>
- <body>
- <div id="ui">SCORE: <span id="score">0</span> | LIVES: <span id="lives">3</span></div>
- <canvas id="game"></canvas>
- <script>
- const canvas = document.getElementById('game');
- const ctx = canvas.getContext('2d');
- let width, height;
- let score = 0;
- let lives = 3;
- let gameRunning = true;
- let audioCtx;
- const PADDLE_COLOR = '#0ff'; // Cyan
- const BALL_COLOR = '#fff'; // White
- const BRICK_COLORS = ['#f0f', '#f00', '#0ff']; // Magenta, Red, Cyan
- const GLOW = 15;
- function initAudio() {
- if (!audioCtx) audioCtx = new (window.AudioContext || window.webkitAudioContext)();
- }
- function playTone(freq, type, duration) {
- if (!audioCtx) return;
- const osc = audioCtx.createOscillator();
- const gain = audioCtx.createGain();
- osc.type = type;
- osc.frequency.setValueAtTime(freq, audioCtx.currentTime);
- gain.gain.setValueAtTime(0.1, audioCtx.currentTime);
- gain.gain.exponentialRampToValueAtTime(0.001, audioCtx.currentTime + duration);
- osc.connect(gain);
- gain.connect(audioCtx.destination);
- osc.start();
- osc.stop(audioCtx.currentTime + duration);
- }
- function playHit() { playTone(400, 'sawtooth', 0.1); }
- function playPaddle() { playTone(200, 'square', 0.15); }
- function playBrick() { playTone(Math.random() * 300 + 600, 'triangle', 0.1); }
- function playDeath() { playTone(100, 'sawtooth', 0.5); }
- const paddle = { x: 0, y: 0, w: 100, h: 15 };
- let ball = { x: 0, y: 0, r: 6, dx: 0, dy: 0, speed: 7 };
- let bricks = [];
- const brickRows = 5;
- const brickCols = 8;
- function resize() {
- width = canvas.width = window.innerWidth;
- height = canvas.height = window.innerHeight;
- paddle.y = height - 40;
- paddle.x = width / 2 - paddle.w / 2;
- }
- function initBricks() {
- bricks = [];
- const w = (width - 40) / brickCols;
- for (let r = 0; r < brickRows; r++) {
- for (let c = 0; c < brickCols; c++) {
- bricks.push({
- x: 20 + c * w,
- y: 80 + r * 30,
- w: w - 4,
- h: 25,
- color: BRICK_COLORS[r % 3],
- active: true
- });
- }
- }
- }
- function resetBall() {
- ball.x = paddle.x + paddle.w / 2;
- ball.y = paddle.y - 20;
- ball.dx = (Math.random() * 4 - 2) * ball.speed;
- ball.dy = -ball.speed;
- }
- canvas.addEventListener('mousemove', (e) => {
- paddle.x = e.clientX - paddle.w / 2;
- });
- canvas.addEventListener('mousedown', () => {
- if (!gameRunning && lives > 0) resetBall(); // Start on click after death
- initAudio();
- });
- function update() {
- if (!gameRunning) return;
- ball.x += ball.dx;
- ball.y += ball.dy;
- if (ball.x < 0 || ball.x > width) { ball.dx *= -1; playTone(300, 'sine', 0.05); }
- if (ball.y < 0) { ball.dy *= -1; playTone(300, 'sine', 0.05); }
- if (ball.y + ball.r > paddle.y && ball.x > paddle.x && ball.x < paddle.x + paddle.w) {
- ball.dy = -Math.abs(ball.dy);
- let hitPoint = ball.x - (paddle.x + paddle.w / 2);
- ball.dx = hitPoint * 0.15;
- playPaddle();
- }
- for (let b of bricks) {
- if (!b.active) continue;
- if (ball.x > b.x && ball.x < b.x + b.w && ball.y > b.y && ball.y < b.y + b.h) {
- ball.dy *= -1;
- b.active = false;
- score += 10;
- document.getElementById('score').innerText = score;
- playBrick();
- createExplosion(b.x + b.w/2, b.y + b.h/2, b.color);
- break;
- }
- }
- if (ball.y > height) {
- lives--;
- document.getElementById('lives').innerText = lives;
- playDeath();
- if (lives <= 0) {
- gameRunning = false;
- alert("GAME OVER. Score: " + score);
- location.reload();
- } else {
- resetBall();
- }
- }
- }
- let particles = [];
- function createExplosion(x, y, color) {
- for (let i = 0; i < 8; i++) {
- particles.push({
- x: x, y: y,
- dx: (Math.random() - 0.5) * 5,
- dy: (Math.random() - 0.5) * 5,
- life: 1.0,
- color: color
- });
- }
- }
- function drawParticles() {
- particles.forEach((p, i) => {
- p.x += p.dx;
- p.y += p.dy;
- p.life -= 0.02;
- ctx.globalAlpha = p.life;
- ctx.fillStyle = p.color;
- ctx.fillRect(p.x, p.y, 3, 3);
- if (p.life <= 0) particles.splice(i, 1);
- });
- ctx.globalAlpha = 1.0;
- }
- function draw() {
- ctx.fillStyle = '#050505';
- ctx.fillRect(0, 0, width, height);
- ctx.strokeStyle = '#111';
- ctx.lineWidth = 1;
- for(let x=0; x<width; x+=40) { ctx.beginPath(); ctx.moveTo(x,0); ctx.lineTo(x,height); ctx.stroke(); }
- for(let y=0; y<height; y+=40) { ctx.beginPath(); ctx.moveTo(0,y); ctx.lineTo(width,y); ctx.stroke(); }
- ctx.shadowBlur = GLOW;
- ctx.shadowColor = PADDLE_COLOR;
- ctx.fillStyle = PADDLE_COLOR;
- ctx.fillRect(paddle.x, paddle.y, paddle.w, paddle.h);
- ctx.shadowColor = BALL_COLOR;
- ctx.beginPath();
- ctx.arc(ball.x, ball.y, ball.r, 0, Math.PI * 2);
- ctx.fillStyle = BALL_COLOR;
- ctx.fill();
- bricks.forEach(b => {
- if (!b.active) return;
- ctx.shadowColor = b.color;
- ctx.fillStyle = b.color;
- ctx.fillRect(b.x, b.y, b.w, b.h);
- ctx.strokeStyle = '#fff';
- ctx.lineWidth = 2;
- ctx.strokeRect(b.x + 4, b.y + 4, b.w - 8, b.h - 8);
- });
- drawParticles();
- ctx.shadowBlur = 5;
- ctx.strokeStyle = '#fff';
- ctx.lineWidth = 1;
- ctx.beginPath();
- ctx.moveTo(ball.x, ball.y);
- ctx.lineTo(ball.x - ball.dx * 2, ball.y - ball.dy * 2);
- ctx.stroke();
- requestAnimationFrame(loop);
- }
- function loop() {
- update();
- draw();
- }
- window.addEventListener('resize', resize);
- resize();
- initBricks();
- resetBall();
- loop();
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment