o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
Write a Geometry Dash clone in HTML canvas (without external assets, nor music).
- User
Write a Geometry Dash clone in HTML canvas (without external assets, nor music).
- Assistant
Write a Geometry Dash clone in HTML canvas (without external assets, nor music).
Sure! Below is a simple implementation of a Geometry Dash clone using HTML Canvas and JavaScript. This version does not use any external assets or music, and all graphics are drawn using the Canvas API.
Here's how the game works:
Spacebar or click to make the player jump.You can copy and paste the following code into a single HTML file and open it in a web browser to play the game.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Geometry Dash Clone</title>
<style>
canvas {
background-color: #222;
display: block;
margin: 0 auto;
}
body {
margin: 0;
overflow: hidden;
}
#score {
color: #fff;
font-family: Arial, sans-serif;
position: absolute;
top: 10px;
left: 50%;
transform: translateX(-50%);
}
#game-over {
color: #fff;
font-family: Arial, sans-serif;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: none;
text-align: center;
}
#game-over button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="800" height="400"></canvas>
<div id="score">Score: 0</div>
<div id="game-over">
<h1>Game Over</h1>
<p>Your Score: <span id="final-score">0</span></p>
<button onclick="restartGame()">Restart</button>
</div>
<script>
// Get the canvas and context
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Game variables
let gameSpeed = 5;
let gravity = 0.5;
let obstacles = [];
let score = 0;
let gameOver = false;
// Player object
const player = {
x: 50,
y: canvas.height - 70,
width: 50,
height: 50,
dy: 0,
jumpForce: 15,
originalHeight: 50,
grounded: false,
jumpTimer: 0
};
// Key handling
let keys = {};
document.addEventListener('keydown', function(e) {
keys[e.code] = true;
if (e.code === 'Space' || e.code === 'ArrowUp') {
e.preventDefault();
}
});
document.addEventListener('keyup', function(e) {
keys[e.code] = false;
});
// Mouse handling for jump
document.addEventListener('mousedown', function(e) {
keys['MouseClick'] = true;
});
document.addEventListener('mouseup', function(e) {
keys['MouseClick'] = false;
});
// Obstacle class
class Obstacle {
constructor() {
this.width = 20 + Math.random() * 30;
this.height = 20 + Math.random() * 50;
this.x = canvas.width + this.width;
this.y = canvas.height - this.height;
this.color = '#ff0000';
}
update() {
this.x -= gameSpeed;
this.draw();
}
draw() {
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
// Spawn obstacles at intervals
function spawnObstacle() {
const obstacle = new Obstacle();
obstacles.push(obstacle);
}
let obstacleTimer = 0;
let obstacleInterval = 2000; // milliseconds
let lastTime = 0;
// Game loop
function update(timeStamp) {
if (gameOver) {
return;
}
const deltaTime = timeStamp - lastTime;
lastTime = timeStamp;
obstacleTimer += deltaTime;
if (obstacleTimer > obstacleInterval) {
spawnObstacle();
obstacleTimer = 0;
}
// Clear the canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw player
ctx.fillStyle = '#00ff00';
ctx.fillRect(player.x, player.y, player.width, player.height);
// Player jump mechanics
if ((keys['Space'] || keys['ArrowUp'] || keys['MouseClick']) && player.grounded) {
player.dy = -player.jumpForce;
player.grounded = false;
}
// Apply gravity
player.dy += gravity;
player.y += player.dy;
// Ground collision
if (player.y + player.height >= canvas.height) {
player.y = canvas.height - player.height;
player.dy = 0;
player.grounded = true;
}
// Update obstacles
obstacles.forEach((obstacle, index) => {
obstacle.update();
// Remove off-screen obstacles
if (obstacle.x + obstacle.width < 0) {
obstacles.splice(index, 1);
score++;
document.getElementById('score').innerText = 'Score: ' + score;
}
// Collision detection
if (
player.x < obstacle.x + obstacle.width &&
player.x + player.width > obstacle.x &&
player.y < obstacle.y + obstacle.height &&
player.y + player.height > obstacle.y
) {
gameOver = true;
cancelAnimationFrame(animationId);
endGame();
}
});
// Move ground (optional ground effect)
ctx.fillStyle = '#555';
ctx.fillRect(0, canvas.height - 10, canvas.width, 10);
// Request next frame
if (!gameOver) {
animationId = requestAnimationFrame(update);
}
}
let animationId = requestAnimationFrame(update);
// End game function
function endGame() {
document.getElementById('final-score').innerText = score;
document.getElementById('game-over').style.display = 'block';
}
// Restart game function
function restartGame() {
gameSpeed = 5;
gravity = 0.5;
obstacles = [];
score = 0;
gameOver = false;
player.y = canvas.height - player.height;
player.dy = 0;
player.grounded = false;
document.getElementById('score').innerText = 'Score: 0';
document.getElementById('game-over').style.display = 'none';
lastTime = 0;
obstacleTimer = 0;
animationId = requestAnimationFrame(update);
}
</script>
</body>
</html>
Spacebar, Arrow Up key, or click the mouse to make the player jump.requestAnimationFrame for smooth animations.keydown and keyup events to detect when the player presses or releases keys.mousedown and mouseup events to detect mouse clicks.gameSpeed to make the game faster.obstacleInterval to spawn obstacles more frequently.draw() methods to customize the look.player.jumpForce and gravity for different jump dynamics.Obstacle class's width and height ranges for varied obstacle sizes.Enjoy playing your custom Geometry Dash clone!
Not yet included in a published release.