How to make a super smash Bros melee on HTML, CSS and Javascript
- User
How to make a super smash Bros melee on HTML, CSS and Javascript
- Assistant
- User
Mobile controls friendly
- Assistant
- User
Coded sample updated combined
- Assistant
How to make a super smash Bros melee on HTML, CSS and Javascript
Mobile controls friendly
Coded sample updated combined
Creating a full-fledged Super Smash Bros. Melee clone using just HTML, CSS, and JavaScript is a very complex project because Melee is a sophisticated 3D fighting game with complex physics, animations, AI, and network play. However, you can definitely create a simple 2D fighting game inspired by Super Smash Bros., focusing on basic movement, jumping, attacking, and knockback mechanics.
Below is a high-level guide and a very basic example to get you started with a minimal 2D version. You can gradually expand upon this foundation.
index.html – The main HTML file.style.css – Styles for layout and characters.script.js – Game logic, rendering, and input handling.You need:
requestAnimationFrame)<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Simple Smash Bros</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<canvas id="gameCanvas" width="800" height="450"></canvas>
<script src="script.js"></script>
</body>
</html>
body {
margin: 0;
background: #87ceeb; /* sky blue */
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
canvas {
background: #68a357; /* grass platform color */
border: 2px solid black;
}
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const gravity = 0.5;
const floorY = canvas.height - 50;
// Player class
class Player {
constructor(x, color, controls) {
this.x = x;
this.y = floorY;
this.vx = 0;
this.vy = 0;
this.width = 40;
this.height = 60;
this.color = color;
this.onGround = true;
this.damage = 0;
this.facing = 1; // 1 right, -1 left
this.isAttacking = false;
this.controls = controls;
this.attackCooldown = 0;
}
update() {
// Apply gravity
this.vy += gravity;
this.x += this.vx;
this.y += this.vy;
// floor collision
if (this.y + this.height > floorY) {
this.y = floorY - this.height;
this.vy = 0;
this.onGround = true;
} else {
this.onGround = false;
}
// Friction
this.vx *= 0.8;
if (this.attackCooldown > 0) {
this.attackCooldown--;
}
}
draw() {
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
// Indicate attacking with a rectangle in front of player
if (this.isAttacking) {
ctx.fillStyle = "orange";
const attackWidth = 20;
const attackHeight = 20;
const attackX = this.facing > 0 ? this.x + this.width : this.x - attackWidth;
const attackY = this.y + this.height / 2 - attackHeight / 2;
ctx.fillRect(attackX, attackY, attackWidth, attackHeight);
}
}
attack() {
if (this.attackCooldown === 0) {
this.isAttacking = true;
this.attackCooldown = 30; // cooldown frames
setTimeout(() => {
this.isAttacking = false;
}, 200);
}
}
applyKnockback(forceX, forceY) {
this.vx += forceX * (1 + this.damage / 100);
this.vy += forceY * (1 + this.damage / 100);
}
}
// Controls
const controlsP1 = { left: "a", right: "d", jump: "w", attack: "s" };
const controlsP2 = { left: "ArrowLeft", right: "ArrowRight", jump: "ArrowUp", attack: "ArrowDown" };
// Create players
const player1 = new Player(100, "red", controlsP1);
const player2 = new Player(600, "blue", controlsP2);
// Input states
const keys = {};
window.addEventListener("keydown", e => {
keys[e.key.toLowerCase()] = true;
});
window.addEventListener("keyup", e => {
keys[e.key.toLowerCase()] = false;
});
function checkAttackHits(attacker, defender) {
if (!attacker.isAttacking) return;
const attackRange = 20;
const attackBox = {
x: attacker.facing > 0 ? attacker.x + attacker.width : attacker.x - attackRange,
y: attacker.y + attacker.height / 2 - 10,
width: attackRange,
height: 20,
};
const defenderBox = {
x: defender.x,
y: defender.y,
width: defender.width,
height: defender.height,
};
// AABB collision detection
if (
attackBox.x < defenderBox.x + defenderBox.width &&
attackBox.x + attackBox.width > defenderBox.x &&
attackBox.y < defenderBox.y + defenderBox.height &&
attackBox.y + attackBox.height > defenderBox.y
) {
// On hit, increase damage and apply knockback
defender.damage += 10;
const knockbackForceX = 8 * attacker.facing;
const knockbackForceY = -5;
defender.applyKnockback(knockbackForceX, knockbackForceY);
attacker.isAttacking = false; // prevent multiple hits in one attack
}
}
function handleInput(player) {
if (keys[player.controls.left]) {
player.vx = -5;
player.facing = -1;
} else if (keys[player.controls.right]) {
player.vx = 5;
player.facing = 1;
}
if (keys[player.controls.jump] && player.onGround) {
player.vy = -12;
player.onGround = false;
}
if (keys[player.controls.attack]) {
player.attack();
}
}
function drawDamage(player) {
ctx.fillStyle = "black";
ctx.font = "16px Arial";
ctx.fillText(`Damage: ${player.damage.toFixed(0)}%`, player.x, player.y - 10);
}
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw floor/platform
ctx.fillStyle = "#4b773d";
ctx.fillRect(0, floorY, canvas.width, 50);
// Players update
handleInput(player1);
handleInput(player2);
player1.update();
player2.update();
checkAttackHits(player1, player2);
checkAttackHits(player2, player1);
player1.draw();
player2.draw();
drawDamage(player1);
drawDamage(player2);
requestAnimationFrame(gameLoop);
}
gameLoop();
A (left), D (right), W (jump), S (attack)Even this basic example serves as a good foundation to build on. Games like Smash Bros have decades of development behind the scenes, so building a full clone in a browser is a multi-month (or even multi-year) project for a team. But starting small with JavaScript games is a great way to learn!
If you want, I can help you build out a specific feature or improve this starter code. Just ask!
To make the simple Smash Bros-like game I shared mobile-friendly, you need to add on-screen touch controls because mobile devices don’t have keyboards.
Create on-screen buttons for each action:
Listen for touch events on those buttons:
touchstart to activatetouchend to deactivateModify the game input reading to consider both physical keyboard and touch button inputs.
Add this inside <body> but below the canvas:
<div id="mobile-controls">
<button id="left-btn" class="control-btn">◀</button>
<button id="right-btn" class="control-btn">▶</button>
<button id="jump-btn" class="control-btn">▲</button>
<button id="attack-btn" class="control-btn">●</button>
</div>
Add this to your style.css:
#mobile-controls {
position: fixed;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
width: 320px;
display: flex;
justify-content: space-between;
pointer-events: auto;
z-index: 1000;
}
.control-btn {
width: 70px;
height: 70px;
border-radius: 35px;
border: none;
background: rgba(0,0,0,0.6);
color: white;
font-size: 32px;
font-weight: bold;
user-select: none;
touch-action: none;
}
.control-btn:active {
background: rgba(255,165,0,0.9);
}
/* Hide controls on large screens (desktop) */
@media(min-width: 600px) {
#mobile-controls {
display: none;
}
}
script.js to set and clear input states via touchAdd this near your existing keyboard input code (below your keys object):
const touchKeys = {
left: false,
right: false,
jump: false,
attack: false
};
function setupButton(id, keyName) {
const button = document.getElementById(id);
// Prevent scrolling while touching buttons
button.addEventListener('touchstart', e => {
e.preventDefault();
touchKeys[keyName] = true;
}, {passive: false});
button.addEventListener('touchend', e => {
e.preventDefault();
touchKeys[keyName] = false;
});
button.addEventListener('touchcancel', e => {
e.preventDefault();
touchKeys[keyName] = false;
});
}
// Initialize all buttons
setupButton("left-btn", "left");
setupButton("right-btn", "right");
setupButton("jump-btn", "jump");
setupButton("attack-btn", "attack");
handleInput() function so the player can be controlled by keyboard OR touch buttons:Find your handleInput(player) function and replace it with:
function handleInput(player) {
// Determine input states from keyboard or touch buttons
const leftPressed = keys[player.controls.left] || touchKeys.left;
const rightPressed = keys[player.controls.right] || touchKeys.right;
const jumpPressed = keys[player.controls.jump] || touchKeys.jump;
const attackPressed = keys[player.controls.attack] || touchKeys.attack;
if (leftPressed) {
player.vx = -5;
player.facing = -1;
} else if (rightPressed) {
player.vx = 5;
player.facing = 1;
}
if (jumpPressed && player.onGround) {
player.vy = -12;
player.onGround = false;
}
if (attackPressed) {
player.attack();
}
}
A common approach is to make mobile controls control only player 1 to simplify UI.
For now, if you want to keep both players, mobile controls will control both (or you can extend it to have two sets of controls).
That’s it!
When you load your game on a mobile device, you should see 4 large buttons at the bottom of the screen for left, right, jump, and attack. The player can be controlled via keyboard on desktop or via these on-screen buttons on mobile.
If you'd like, I can help you integrate this into the full source code example for clarity!
Certainly! Below is the full combined and updated code for a simple 2D Smash Bros-style game with mobile-friendly on-screen controls, keyboard controls, and basic attack/knockback mechanics.
index.html<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Simple Smash Bros - Mobile Friendly</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<canvas id="gameCanvas" width="800" height="450"></canvas>
<!-- Mobile Controls -->
<div id="mobile-controls">
<button id="left-btn" class="control-btn">◀</button>
<button id="right-btn" class="control-btn">▶</button>
<button id="jump-btn" class="control-btn">▲</button>
<button id="attack-btn" class="control-btn">●</button>
</div>
<script src="script.js"></script>
</body>
</html>
style.cssbody {
margin: 0;
background: #87ceeb; /* sky blue */
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
-webkit-tap-highlight-color: transparent; /* remove tap highlight on buttons */
user-select: none;
}
canvas {
background: #68a357; /* grass platform color */
border: 2px solid black;
}
/* Mobile Controls Styles */
#mobile-controls {
position: fixed;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
width: 320px;
display: flex;
justify-content: space-between;
pointer-events: auto;
z-index: 1000;
}
.control-btn {
width: 70px;
height: 70px;
border-radius: 35px;
border: none;
background: rgba(0,0,0,0.6);
color: white;
font-size: 32px;
font-weight: bold;
user-select: none;
touch-action: none;
}
.control-btn:active {
background: rgba(255,165,0,0.9);
}
/* Hide controls on large screens (desktop) */
@media(min-width: 600px) {
#mobile-controls {
display: none;
}
}
script.jsconst canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const gravity = 0.5;
const floorY = canvas.height - 50;
// Player Class
class Player {
constructor(x, color, controls) {
this.x = x;
this.y = floorY - 60;
this.vx = 0;
this.vy = 0;
this.width = 40;
this.height = 60;
this.color = color;
this.onGround = false;
this.damage = 0;
this.facing = 1; // 1 right, -1 left
this.isAttacking = false;
this.controls = controls;
this.attackCooldown = 0;
}
update() {
// Apply gravity
this.vy += gravity;
this.x += this.vx;
this.y += this.vy;
// floor collision
if (this.y + this.height > floorY) {
this.y = floorY - this.height;
this.vy = 0;
this.onGround = true;
} else {
this.onGround = false;
}
// Friction slows horizontal movement
this.vx *= 0.8;
// Keep player inside canvas horizontally
if(this.x < 0) this.x = 0;
if(this.x + this.width > canvas.width) this.x = canvas.width - this.width;
if (this.attackCooldown > 0) {
this.attackCooldown--;
}
}
draw() {
// Draw player rectangle
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
// Draw attack hitbox (orange rectangle)
if (this.isAttacking) {
ctx.fillStyle = "orange";
const attackWidth = 20;
const attackHeight = 20;
const attackX = this.facing > 0 ? this.x + this.width : this.x - attackWidth;
const attackY = this.y + this.height / 2 - attackHeight / 2;
ctx.fillRect(attackX, attackY, attackWidth, attackHeight);
}
}
attack() {
if (this.attackCooldown === 0) {
this.isAttacking = true;
this.attackCooldown = 30; // cooldown frames
setTimeout(() => {
this.isAttacking = false;
}, 200);
}
}
applyKnockback(forceX, forceY) {
// Knockback force increases with damage %
this.vx += forceX * (1 + this.damage / 100);
this.vy += forceY * (1 + this.damage / 100);
}
}
// Controls mapping for each player
const controlsP1 = { left: "a", right: "d", jump: "w", attack: "s" };
const controlsP2 = { left: "arrowleft", right: "arrowright", jump: "arrowup", attack: "arrowdown" };
// Create players
const player1 = new Player(100, "red", controlsP1);
const player2 = new Player(600, "blue", controlsP2);
// Keyboard input state
const keys = {};
window.addEventListener("keydown", e => {
keys[e.key.toLowerCase()] = true;
});
window.addEventListener("keyup", e => {
keys[e.key.toLowerCase()] = false;
});
// Touch input state for mobile controls
const touchKeys = {
left: false,
right: false,
jump: false,
attack: false
};
function setupButton(id, keyName) {
const button = document.getElementById(id);
// Prevent default scrolling on touch buttons
button.addEventListener('touchstart', e => {
e.preventDefault();
touchKeys[keyName] = true;
}, {passive: false});
button.addEventListener('touchend', e => {
e.preventDefault();
touchKeys[keyName] = false;
});
button.addEventListener('touchcancel', e => {
e.preventDefault();
touchKeys[keyName] = false;
});
}
// Setup mobile control buttons
setupButton("left-btn", "left");
setupButton("right-btn", "right");
setupButton("jump-btn", "jump");
setupButton("attack-btn", "attack");
// Handle inputs for player (keyboard OR touch)
function handleInput(player) {
// Determine if keys or touch buttons are pressed depending on player controls
const leftPressed = keys[player.controls.left] || touchKeys.left;
const rightPressed = keys[player.controls.right] || touchKeys.right;
const jumpPressed = keys[player.controls.jump] || touchKeys.jump;
const attackPressed = keys[player.controls.attack] || touchKeys.attack;
if (leftPressed) {
player.vx = -5;
player.facing = -1;
} else if (rightPressed) {
player.vx = 5;
player.facing = 1;
}
if (jumpPressed && player.onGround) {
player.vy = -12;
player.onGround = false;
}
if (attackPressed) {
player.attack();
}
}
// Simple AABB collision detection for attack hitbox hit detection
function checkAttackHits(attacker, defender) {
if (!attacker.isAttacking) return;
const attackRange = 20;
const attackBox = {
x: attacker.facing > 0 ? attacker.x + attacker.width : attacker.x - attackRange,
y: attacker.y + attacker.height / 2 - 10,
width: attackRange,
height: 20,
};
const defenderBox = {
x: defender.x,
y: defender.y,
width: defender.width,
height: defender.height,
};
// AABB collision detection
if (
attackBox.x < defenderBox.x + defenderBox.width &&
attackBox.x + attackBox.width > defenderBox.x &&
attackBox.y < defenderBox.y + defenderBox.height &&
attackBox.y + attackBox.height > defenderBox.y
) {
// On hit: increase damage and apply knockback
defender.damage += 10;
const knockbackForceX = 8 * attacker.facing;
const knockbackForceY = -5;
defender.applyKnockback(knockbackForceX, knockbackForceY);
// Prevent multiple hits per attack
attacker.isAttacking = false;
}
}
function drawDamage(player) {
ctx.fillStyle = "black";
ctx.font = "16px Arial";
ctx.fillText(`Damage: ${player.damage.toFixed(0)}%`, player.x, player.y - 10);
}
function gameLoop() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw floor/platform
ctx.fillStyle = "#4b773d";
ctx.fillRect(0, floorY, canvas.width, 50);
// Update and draw players
handleInput(player1);
handleInput(player2);
player1.update();
player2.update();
checkAttackHits(player1, player2);
checkAttackHits(player2, player1);
player1.draw();
player2.draw();
drawDamage(player1);
drawDamage(player2);
requestAnimationFrame(gameLoop);
}
gameLoop();
On desktop/laptop, you can use keyboard controls:
On mobile, you see four bottom-center buttons controlling left, right, jump, and attack that control Player 1 only. You can modify the code to add a second set if you want a second mobile player.
If you'd like me to help you extend this (adding multiple sets of mobile controls, better physics, animations, etc.) just let me know!
Not yet included in a published release.