I'll create a Geometry Dash-style game for you in a single HTML file with SVG graphics. Here's a complete implementation:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Geometry Dash Clone</title>
<style>
body {
margin: 0;
padding: 0;
background: linear-gradient(to bottom, #1a1a2e, #16213e);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
font-family: Arial, sans-serif;
overflow: hidden;
}
#gameCanvas {
border: 2px solid #0f3460;
background: linear-gradient(to bottom, #0f3460, #16213e);
position: relative;
overflow: hidden;
}
#gameOverlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.7);
display: none;
justify-content: center;
align-items: center;
flex-direction: column;
color: white;
z-index: 1000;
}
#gameOverlay h2 {
font-size: 48px;
margin: 20px;
text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
}
#restartBtn {
padding: 15px 30px;
font-size: 20px;
background: #e94560;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
transition: transform 0.2s;
}
#restartBtn:hover {
transform: scale(1.1);
}
#score {
position: absolute;
top: 20px;
right: 20px;
color: white;
font-size: 24px;
font-weight: bold;
text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
}
#instructions {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
color: white;
font-size: 18px;
text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
}
</style>
</head>
<body>
<div style="position: relative;">
<canvas id="gameCanvas" width="800" height="400"></canvas>
<div id="score">Score: 0</div>
<div id="instructions">Press SPACE or Click to Jump</div>
<div id="gameOverlay">
<h2>Game Over!</h2>
<p style="font-size: 24px; margin: 10px;">Score: <span id="finalScore">0</span></p>
<button id="restartBtn">Restart</button>
</div>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const gameOverlay = document.getElementById('gameOverlay');
const scoreElement = document.getElementById('score');
const finalScoreElement = document.getElementById('finalScore');
const restartBtn = document.getElementById('restartBtn');
// Game variables
let gameRunning = true;
let score = 0;
let gameSpeed = 5;
let groundHeight = 50;
// Player object
const player = {
x: 100,
y: canvas.height - groundHeight - 40,
width: 40,
height: 40,
velocityY: 0,
jumping: false,
rotation: 0,
grounded: true
};
// Obstacles array
let obstacles = [];
let particles = [];
let stars = [];
// Create star background
for (let i = 0; i < 50; i++) {
stars.push({
x: Math.random() * canvas.width,
y: Math.random() * (canvas.height - groundHeight),
size: Math.random() * 2 + 1,
speed: Math.random() * 2 + 1
});
}
// SVG creation functions
function createPlayerSVG() {
const svg = `<svg width="40" height="40" xmlns="http://www.w3.org/2000/svg">
<rect x="0" y="0" width="40" height="40" fill="#f39c12" stroke="#e67e22" stroke-width="2"/>
<rect x="8" y="8" width="10" height="10" fill="#2c3e50"/>
<rect x="22" y="8" width="10" height="10" fill="#2c3e50"/>
<rect x="10" y="25" width="20" height="5" fill="#2c3e50"/>
</svg>`;
return svg;
}
function createSpikeSVG() {
const svg = `<svg width="40" height="40" xmlns="http://www.w3.org/2000/svg">
<polygon points="20,5 35,35 5,35" fill="#e74c3c" stroke="#c0392b" stroke-width="2"/>
</svg>`;
return svg;
}
// Convert SVG to image
function svgToImage(svgString, callback) {
const blob = new Blob([svgString], { type: 'image/svg+xml' });
const url = URL.createObjectURL(blob);
const img = new Image();
img.onload = function() {
callback(img);
URL.revokeObjectURL(url);
};
img.src = url;
}
// Load images
let playerImg, spikeImg;
svgToImage(createPlayerSVG(), (img) => { playerImg = img; });
svgToImage(createSpikeSVG(), (img) => { spikeImg = img; });
// Game functions
function jump() {
if (player.grounded && gameRunning) {
player.velocityY = -15;
player.jumping = true;
player.grounded = false;
}
}
function createObstacle() {
obstacles.push({
x: canvas.width,
y: canvas.height - groundHeight - 40,
width: 40,
height: 40,
type: 'spike'
});
}
function createParticle(x, y) {
for (let i = 0; i < 10; i++) {
particles.push({
x: x,
y: y,
vx: (Math.random() - 0.5) * 10,
vy: (Math.random() - 0.5) * 10,
life: 30,
color: `hsl(${Math.random() * 60 + 15}, 100%, 50%)`
});
}
}
function updatePlayer() {
if (!gameRunning) return;
// Apply gravity
player.velocityY += 0.8;
player.y += player.velocityY;
// Ground collision
if (player.y > canvas.height - groundHeight - player.height) {
player.y = canvas.height - groundHeight - player.height;
player.velocityY = 0;
player.jumping = false;
player.grounded = true;
player.rotation = Math.floor(player.rotation / 90) * 90;
}
// Rotation while jumping
if (player.jumping) {
player.rotation += 8;
}
}
function updateObstacles() {
for (let i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].x -= gameSpeed;
// Remove off-screen obstacles
if (obstacles[i].x + obstacles[i].width < 0) {
obstacles.splice(i, 1);
score += 10;
scoreElement.textContent = `Score: ${score}`;
}
}
}
function updateParticles() {
for (let i = particles.length - 1; i >= 0; i--) {
particles[i].x += particles[i].vx;
particles[i].y += particles[i].vy;
particles[i].life--;
if (particles[i].life <= 0) {
particles.splice(i, 1);
}
}
}
function updateStars() {
stars.forEach(star => {
star.x -= star.speed;
if (star.x < 0) {
star.x = canvas.width;
}
});
}
function checkCollision() {
for (let obstacle of obstacles) {
if (player.x < obstacle.x + obstacle.width - 10 &&
player.x + player.width - 10 > obstacle.x &&
player.y < obstacle.y + obstacle.height - 10 &&
player.y + player.height - 10 > obstacle.y) {
gameOver();
}
}
}
function gameOver() {
gameRunning = false;
finalScoreElement.textContent = score;
gameOverlay.style.display = 'flex';
createParticle(player.x + player.width / 2, player.y + player.height / 2);
}
function restart() {
gameRunning = true;
score = 0;
scoreElement.textContent = 'Score: 0';
player.y = canvas.height - groundHeight - 40;
player.velocityY = 0;
player.jumping = false;
player.rotation = 0;
player.grounded = true;
obstacles = [];
particles = [];
gameOverlay.style.display = 'none';
gameSpeed = 5;
}
function draw() {
// Clear canvas
ctx.fillStyle = 'rgba(15, 52, 96, 0.1)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw stars
ctx.fillStyle = 'white';
stars.forEach(star => {
ctx.beginPath();
ctx.arc(star.x, star.y, star.size, 0, Math.PI * 2);
ctx.fill();
});
// Draw ground
const gradient = ctx.createLinearGradient(0, canvas.height - groundHeight, 0, canvas.height);
gradient.addColorStop(0, '#1e3c72');
gradient.addColorStop(1, '#2a5298');
ctx.fillStyle = gradient;
ctx.fillRect(0, canvas.height - groundHeight, canvas.width, groundHeight);
// Draw ground line
ctx.strokeStyle = '#3498db';
ctx.lineWidth = 3;
ctx.beginPath();
ctx.moveTo(0, canvas.height - groundHeight);
ctx.lineTo(canvas.width, canvas.height - groundHeight);
ctx.stroke();
// Draw particles
particles.forEach(particle => {
ctx.globalAlpha = particle.life / 30;
ctx.fillStyle = particle.color;
ctx.fillRect(particle.x - 2, particle.y - 2, 4, 4);
});
ctx.globalAlpha = 1;
// Draw player
ctx.save();
ctx.translate(player.x + player.width / 2, player.y + player.height / 2);
ctx.rotate(player.rotation * Math.PI / 180);
if (playerImg) {
ctx.drawImage(playerImg, -player.width / 2, -player.height / 2, player.width, player.height);
} else {
ctx.fillStyle = '#f39c12';
ctx.fillRect(-player.width / 2, -player.height / 2, player.width, player.height);
}
ctx.restore();
// Draw obstacles
obstacles.forEach(obstacle => {
if (spikeImg) {
ctx.drawImage(spikeImg, obstacle.x, obstacle.y, obstacle.width, obstacle.height);
} else {
ctx.fillStyle = '#e74c3c';
ctx.beginPath();
ctx.moveTo(obstacle.x + obstacle.width / 2, obstacle.y);
ctx.lineTo(obstacle.x + obstacle.width, obstacle.y + obstacle.height);
ctx.lineTo(obstacle.x, obstacle.y + obstacle.height);
ctx.closePath();
ctx.fill();
}
});
}
function gameLoop() {
updatePlayer();
updateObstacles();
updateParticles();
updateStars();
checkCollision();
draw();
// Increase difficulty
if (score > 0 && score % 100 === 0) {
gameSpeed = Math.min(gameSpeed + 0.5, 12);
}
requestAnimationFrame(gameLoop);
}
// Event listeners
document.addEventListener('keydown', (e) => {
if (e.code === 'Space') {
e.preventDefault();
jump();
}
});
canvas.addEventListener('click', jump);
restartBtn.addEventListener('click', restart);
// Create obstacles periodically
setInterval(() => {
if (gameRunning && Math.random() < 0.7) {
createObstacle();
}
}, 1500);
// Start game
gameLoop();
</script>
</body>
</html>
This game features:
- Player Character: A yellow square with a simple face that rotates when jumping
- Obstacles: Red triangular spikes that appear randomly
- Physics: Gravity, jumping mechanics, and smooth movement
- Visual Effects:
- Particle effects on game over
- Starfield background
- Gradient backgrounds
- Trail effect
- Game Mechanics:
- Score increases as you avoid obstacles
- Difficulty increases as score goes up (speed increases)
- Collision detection
- Game over screen with restart
- Controls: Press SPACE or click to jump
The game uses SVG graphics embedded directly in the code for the player and obstacles. Everything is contained in a single HTML file as requested. The gameplay is similar to Geometry Dash where you need to time your jumps to avoid obstacles.