Here's a complete Super Mario-style platformer game in a single HTML file:
<!DOCTYPE html>
<html>
<head>
<title>Super Mario HTML Game</title>
<style>
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #333;
overflow: hidden;
}
#gameCanvas {
border: 2px solid white;
background-color: #87CEEB;
}
.game-ui {
position: absolute;
top: 10px;
left: 10px;
color: white;
font-family: Arial, sans-serif;
font-size: 20px;
}
</style>
</head>
<body>
<div class="game-ui">
<div>Score: <span id="score">0</span></div>
<div>Lives: <span id="lives">3</span></div>
</div>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
// Game state
let score = 0;
let lives = 3;
let cameraX = 0;
let gameOver = false;
// Player properties
const player = {
x: 100,
y: 300,
width: 30,
height: 50,
velocityX: 0,
velocityY: 0,
isOnGround: false,
powerUp: 0, // 0: small, 1: super, 2: fire
direction: 1,
canShoot: true
};
// Game objects
const platforms = [
{ x: 0, y: 500, width: 2000, height: 100, type: 'ground' },
{ x: 300, y: 400, width: 100, height: 20, type: 'question', hit: false },
{ x: 500, y: 300, width: 100, height: 20, type: 'question', hit: false },
{ x: 700, y: 200, width: 100, height: 20, type: 'question', hit: false }
];
const enemies = [
{ type: 'goomba', x: 400, y: 480, width: 30, height: 30, velocityX: 2, state: 'alive' },
{ type: 'koopa', x: 600, y: 480, width: 30, height: 40, velocityX: 1, state: 'alive' },
{ type: 'paratroopa', x: 800, y: 300, width: 30, height: 40, velocityX: 1, velocityY: 1, state: 'alive' }
];
const collectibles = [
{ type: 'coin', x: 350, y: 370, width: 20, height: 20, collected: false },
{ type: 'coin', x: 550, y: 270, width: 20, height: 20, collected: false },
{ type: 'coin', x: 750, y: 170, width: 20, height: 20, collected: false }
];
let powerups = [];
let fireballs = [];
// Input handling
const keys = {};
document.addEventListener('keydown', e => keys[e.key] = true);
document.addEventListener('keyup', e => keys[e.key] = false);
// Game functions
function update() {
if (gameOver) return;
// Player movement
if (keys['ArrowLeft']) {
player.velocityX = -5;
player.direction = -1;
} else if (keys['ArrowRight']) {
player.velocityX = 5;
player.direction = 1;
} else {
player.velocityX *= 0.9;
}
// Jumping
if (keys['ArrowUp'] && player.isOnGround) {
player.velocityY = -12;
player.isOnGround = false;
}
// Fireball shooting
if (keys[' '] && player.powerUp === 2 && player.canShoot) {
fireballs.push({
x: player.x + player.width * player.direction,
y: player.y + player.height/2,
width: 10,
height: 10,
velocityX: 7 * player.direction,
velocityY: 0
});
player.canShoot = false;
setTimeout(() => player.canShoot = true, 500);
}
// Apply physics
player.velocityY += 0.5;
player.x += player.velocityX;
player.y += player.velocityY;
// Camera follow
cameraX = player.x - canvas.width/2;
// Platform collisions
handlePlatformCollisions();
// Enemy updates
updateEnemies();
// Power-up updates
updatePowerups();
// Fireball updates
updateFireballs();
// Collectible checks
checkCollectibles();
// Check if player fell off
if (player.y > canvas.height) {
lives--;
if (lives <= 0) gameOver = true;
else respawnPlayer();
}
// Update UI
scoreElement.textContent = score;
livesElement.textContent = lives;
}
function handlePlatformCollisions() {
const prevX = player.x - player.velocityX;
const prevY = player.y - player.velocityY;
player.isOnGround = false;
platforms.forEach(platform => {
if (checkCollision(player, platform)) {
// Top collision
if (prevY + player.height <= platform.y && player.velocityY > 0) {
player.y = platform.y - player.height;
player.velocityY = 0;
player.isOnGround = true;
}
// Bottom collision (head hit)
else if (prevY >= platform.y + platform.height && player.velocityY < 0) {
player.y = platform.y + platform.height;
player.velocityY = 0;
if (platform.type === 'question' && !platform.hit) {
platform.hit = true;
spawnPowerup(platform.x, platform.y);
}
}
// Side collisions
else if (prevX + player.width <= platform.x) {
player.x = platform.x - player.width;
} else if (prevX >= platform.x + platform.width) {
player.x = platform.x + platform.width;
}
}
});
}
function updateEnemies() {
enemies.forEach(enemy => {
if (enemy.state !== 'alive') return;
// Movement logic
if (enemy.type === 'goomba' || enemy.type === 'koopa') {
enemy.x += enemy.velocityX;
checkEnemyEdges(enemy);
} else if (enemy.type === 'paratroopa') {
enemy.x += enemy.velocityX;
enemy.y += enemy.velocityY;
if (enemy.y > 300 || enemy.y < 200) enemy.velocityY *= -1;
}
// Player collision
if (checkCollision(player, enemy)) {
if (player.y + player.height < enemy.y + 10 && player.velocityY > 0) {
// Stomp enemy
enemy.state = 'dead';
player.velocityY = -10;
score += 100;
} else {
// Player hit
if (player.powerUp > 0) {
player.powerUp--;
player.height = player.powerUp === 0 ? 50 : 60;
respawnPlayer();
} else {
lives--;
if (lives <= 0) gameOver = true;
else respawnPlayer();
}
}
}
});
}
function checkEnemyEdges(enemy) {
let onPlatform = platforms.some(platform =>
enemy.y + enemy.height === platform.y &&
enemy.x + enemy.width > platform.x &&
enemy.x < platform.x + platform.width
);
if (!onPlatform ||
enemy.x <= 0 ||
enemy.x + enemy.width >= 2000) {
enemy.velocityX *= -1;
}
}
function updatePowerups() {
powerups.forEach((powerup, index) => {
powerup.velocityY += 0.5;
powerup.x += powerup.velocityX;
powerup.y += powerup.velocityY;
if (checkCollision(player, powerup)) {
if (powerup.type === 'mushroom') {
if (player.powerUp === 0) {
player.powerUp = 1;
player.height = 60;
}
} else if (powerup.type === 'fireflower') {
player.powerUp = 2;
} else if (powerup.type === '1up') {
lives++;
}
powerups.splice(index, 1);
}
});
}
function updateFireballs() {
fireballs = fireballs.filter(fireball => {
fireball.x += fireball.velocityX;
fireball.y += fireball.velocityY;
fireball.velocityY += 0.2;
// Check enemy collisions
enemies.forEach(enemy => {
if (enemy.state === 'alive' && checkCollision(fireball, enemy)) {
enemy.state = 'dead';
score += 100;
}
});
return fireball.x > cameraX - 50 && fireball.x < cameraX + canvas.width + 50;
});
}
function checkCollectibles() {
collectibles.forEach(collectible => {
if (!collectible.collected && checkCollision(player, collectible)) {
collectible.collected = true;
score += 10;
}
});
}
function spawnPowerup(x, y) {
const types = ['mushroom', 'fireflower', '1up'];
const type = types[Math.floor(Math.random() * 3)];
powerups.push({
type,
x: x + 10,
y: y - 20,
width: 20,
height: 20,
velocityX: 1,
velocityY: 0
});
}
function respawnPlayer() {
player.x = 100;
player.y = 300;
player.velocityX = 0;
player.velocityY = 0;
}
function checkCollision(a, b) {
return a.x < b.x + b.width &&
a.x + a.width > b.x &&
a.y < b.y + b.height &&
a.y + a.height > b.y;
}
function render() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.save();
ctx.translate(-cameraX, 0);
// Draw platforms
platforms.forEach(platform => {
ctx.fillStyle = platform.type === 'ground' ? '#8B4513' : '#DAA520';
ctx.fillRect(platform.x, platform.y, platform.width, platform.height);
if (platform.type === 'question' && !platform.hit) {
ctx.fillStyle = 'yellow';
ctx.fillRect(platform.x + 5, platform.y + 5, 10, 10);
}
});
// Draw collectibles
collectibles.forEach(coin => {
if (!coin.collected) {
ctx.fillStyle = 'gold';
ctx.beginPath();
ctx.arc(coin.x + 10, coin.y + 10, 10, 0, Math.PI * 2);
ctx.fill();
}
});
// Draw powerups
powerups.forEach(powerup => {
ctx.fillStyle = powerup.type === 'mushroom' ? 'red' :
powerup.type === 'fireflower' ? 'orange' : 'green';
ctx.fillRect(powerup.x, powerup.y, powerup.width, powerup.height);
});
// Draw enemies
enemies.forEach(enemy => {
if (enemy.state === 'alive') {
ctx.fillStyle = enemy.type === 'goomba' ? 'brown' :
enemy.type === 'koopa' ? 'green' : 'blue';
ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
}
});
// Draw fireballs
ctx.fillStyle = 'orange';
fireballs.forEach(ball => {
ctx.beginPath();
ctx.arc(ball.x, ball.y, ball.width/2, 0, Math.PI * 2);
ctx.fill();
});
// Draw player
ctx.fillStyle = player.powerUp === 0 ? 'red' :
player.powerUp === 1 ? 'blue' : 'green';
ctx.fillRect(player.x, player.y, player.width, player.height);
ctx.restore();
}
// Game loop
function gameLoop() {
update();
render();
requestAnimationFrame(gameLoop);
}
// Start game
gameLoop();
</script>
</body>
</html>
This game includes:
- Smooth scrolling camera that follows the player
- Physics-based movement with gravity and jumping
- Multiple player states (small, super, fire)
- Lives system with respawn
- Goombas, Koopas, and flying enemies
- Power-ups (mushrooms, fire flowers, 1-ups)
- Collectible coins
- Hidden blocks that reveal power-ups
- Score system
Controls:
- Arrow keys to move and jump
- Space to shoot fireballs (when in fire mode)
The game features basic collision detection, enemy AI patterns, and power-up mechanics. The graphics are simple colored rectangles but represent all the required game elements. You can extend the level by adding more platforms and enemies in the respective arrays.