Untitled
unknown
plain_text
3 months ago
2.6 kB
5
Indexable
<!DOCTYPE html>
<html lang="et">
<head>
<meta charset="UTF-8">
<title>Mini Geometry Dash</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #222;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
canvas {
background: #444;
display: block;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="800" height="400"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
let player = {
x: 50,
y: 300,
width: 30,
height: 30,
dy: 0,
gravity: 0.8,
jumpForce: -12,
grounded: false
};
let obstacles = [];
let score = 0;
let gameOver = false;
// Hüppe funktsioon
function jump() {
if(player.grounded) {
player.dy = player.jumpForce;
player.grounded = false;
}
}
// Loo takistusi
function spawnObstacle() {
let height = Math.random() * 50 + 20;
obstacles.push({x: canvas.width, y: canvas.height - height, width: 20, height: height});
}
// Mängu loop
function update() {
if(gameOver) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Mängija füüsika
player.dy += player.gravity;
player.y += player.dy;
if(player.y + player.height >= canvas.height) {
player.y = canvas.height - player.height;
player.dy = 0;
player.grounded = true;
}
// Joonista mängija
ctx.fillStyle = 'lime';
ctx.fillRect(player.x, player.y, player.width, player.height);
// Takistuste liikumine
for(let i = obstacles.length - 1; i >= 0; i--) {
let obs = obstacles[i];
obs.x -= 5;
ctx.fillStyle = 'red';
ctx.fillRect(obs.x, obs.y, obs.width, obs.height);
// Kontrolli kokkupõrget
if(player.x < obs.x + obs.width &&
player.x + player.width > obs.x &&
player.y < obs.y + obs.height &&
player.y + player.height > obs.y) {
gameOver = true;
alert("Mäng läbi! Skoor: " + score);
}
// Eemalda ekraanilt väljunud takistused
if(obs.x + obs.width < 0) {
obstacles.splice(i, 1);
score++;
}
}
// Skoori näitamine
ctx.fillStyle = 'white';
ctx.font = '20px Arial';
ctx.fillText("Skoor: " + score, 10, 30);
requestAnimationFrame(update);
}
// Spawn takistusi iga 1.5 sekundi tagant
setInterval(spawnObstacle, 1500);
// Kuulaja hüppe jaoks
document.addEventListener('keydown', (e) => {
if(e.code === 'Space') jump();
});
update();
</script>
</body>
</html>Editor is loading...
Leave a Comment