Below is a simple yet fun platformer game built with HTML, CSS, and JavaScript. The player can control a character using the arrow keys to move left, right, and jump. The game includes obstacles like moving platforms, spikes, and coins to collect. Let's dive in!
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fun Platformer Game</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="game-container">
<div id="player"></div>
<div class="platform" style="left: 50px; bottom: 50px;"></div>
<div class="platform moving-platform" style="left: 200px; bottom: 150px;"></div>
<div class="spikes" style="left: 350px; bottom: 50px;"></div>
<div class="coin" style="left: 450px; bottom: 150px;"></div>
<div class="platform" style="left: 550px; bottom: 50px;"></div>
<div id="score">Score: 0</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS (styles.css)
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #87CEEB; /* Sky blue background */
}
#game-container {
position: relative;
width: 800px;
height: 400px;
background-color: #4A7C59; /* Green ground */
overflow: hidden;
border: 2px solid #000;
}
#player {
position: absolute;
width: 30px;
height: 50px;
background-color: #FFD700; /* Gold character */
bottom: 50px;
left: 50px;
}
.platform {
position: absolute;
width: 100px;
height: 20px;
background-color: #8B4513; /* Brown platform */
}
.moving-platform {
animation: movePlatform 4s linear infinite;
}
@keyframes movePlatform {
0% { left: 200px; }
50% { left: 400px; }
100% { left: 200px; }
}
.spikes {
position: absolute;
width: 50px;
height: 20px;
background-image: linear-gradient(45deg, #000 25%, transparent 25%, transparent 50%, #000 50%, #000 75%, transparent 75%, transparent);
background-size: 20px 20px;
}
.coin {
position: absolute;
width: 20px;
height: 20px;
background-color: #FFD700; /* Gold coin */
border-radius: 50%;
animation: spinCoin 1s linear infinite;
}
@keyframes spinCoin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
#score {
position: absolute;
top: 10px;
left: 10px;
color: #FFF;
font-family: Arial, sans-serif;
}
JavaScript (script.js)
const player = document.getElementById('player');
const gameContainer = document.getElementById('game-container');
const scoreDisplay = document.getElementById('score');
let score = 0;
let isJumping = false;
let gravity = 0.9;
let positionY = 0;
let positionX = 0;
function jump() {
if (isJumping) return;
isJumping = true;
let jumpCount = 0;
const jumpInterval = setInterval(() => {
if (jumpCount >= 15) {
clearInterval(jumpInterval);
let fallInterval = setInterval(() => {
if (positionY <= 50) {
clearInterval(fallInterval);
isJumping = false;
}
positionY -= gravity;
player.style.bottom = positionY + 'px';
}, 20);
} else {
positionY += gravity;
player.style.bottom = positionY + 'px';
jumpCount++;
}
}, 20);
}
function moveLeft() {
positionX -= 5;
player.style.left = positionX + 'px';
checkCollision();
}
function moveRight() {
positionX += 5;
player.style.left = positionX + 'px';
checkCollision();
}
function checkCollision() {
const playerRect = player.getBoundingClientRect();
const coins = document.getElementsByClassName('coin');
const spikes = document.getElementsByClassName('spikes');
const platforms = document.getElementsByClassName('platform');
// Check coin collision
Array.from(coins).forEach(coin => {
const coinRect = coin.getBoundingClientRect();
if (playerRect.left < coinRect.right && playerRect.right > coinRect.left && playerRect.bottom > coinRect.top) {
coin.remove();
score += 10;
scoreDisplay.textContent = `Score: ${score}`;
}
});
// Check spikes collision
Array.from(spikes).forEach(spike => {
const spikeRect = spike.getBoundingClientRect();
if (playerRect.left < spikeRect.right && playerRect.right > spikeRect.left && playerRect.bottom > spikeRect.top) {
alert('Game Over! Your score: ' + score);
location.reload();
}
});
// Check platform collision
let isOnPlatform = false;
Array.from(platforms).forEach(platform => {
const platformRect = platform.getBoundingClientRect();
if (playerRect.left < platformRect.right && playerRect.right > platformRect.left && playerRect.bottom > platformRect.top && playerRect.top < platformRect.bottom) {
isOnPlatform = true;
positionY = platformRect.top - playerRect.height;
player.style.bottom = positionY + 'px';
}
});
// Fall if not on platform
if (!isOnPlatform && !isJumping) {
positionY -= gravity;
player.style.bottom = positionY + 'px';
if (positionY <= 0) {
alert('Game Over! Your score: ' + score);
location.reload();
}
}
}
document.addEventListener('keydown', (e) => {
if (e.key === 'ArrowLeft') moveLeft();
if (e.key === 'ArrowRight') moveRight();
if (e.key === 'ArrowUp') jump();
});
setInterval(checkCollision, 100);
How It Works
-
Controls:
- Left Arrow: Move left.
- Right Arrow: Move right.
- Up Arrow: Jump.
-
Game Elements:
- Platforms: Static and moving platforms to jump on.
- Spikes: Touching spikes ends the game.
- Coins: Collect coins to increase your score.
-
Gameplay:
- Avoid spikes and falling off the screen.
- Collect coins to maximize your score.
- The game resets if you die.
How to Run
- Save the HTML, CSS, and JavaScript code into separate files (
index.html, styles.css, script.js).
- Open
index.html in a browser.
- Use the arrow keys to play!
Feel free to customize the game by adding more levels, obstacles, or power-ups. Have fun! 🎮