Certainly! Below is a complete HTML simulation of a robot assembling a Bubble Sort algorithm in Python, piece by piece, with step-by-step explanations. The UI is designed to be clean, modern, and engaging, with smooth animations and clear instructions.
Features:
- Robot arm animation "picks" each code piece.
- Code pieces appear one by one in the code editor area.
- Step-by-step explanation updates dynamically.
- Clean, responsive design with a futuristic robot theme.
- Syntax-highlighted Python code.
- Smooth transitions and user-friendly layout.
How to use:
Open the HTML file in any modern browser. The robot will start assembling the Bubble Sort code automatically, explaining each part as it goes.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Robot Assembles Bubble Sort Algorithm</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Roboto+Mono&display=swap');
/* Reset & base */
* {
box-sizing: border-box;
}
body {
margin: 0;
background: #0f111a;
color: #e0e6f0;
font-family: 'Roboto Mono', monospace;
display: flex;
flex-direction: column;
height: 100vh;
overflow: hidden;
}
header {
padding: 1rem 2rem;
background: #1a1f2e;
font-size: 1.5rem;
font-weight: 700;
letter-spacing: 1.2px;
text-align: center;
color: #7fffd4;
user-select: none;
}
main {
flex: 1;
display: flex;
gap: 2rem;
padding: 2rem;
background: #121622;
}
/* Left panel: Robot + workspace */
#robot-workspace {
flex: 1;
position: relative;
background: #1a1f2e;
border-radius: 12px;
box-shadow: 0 0 20px #0ff;
display: flex;
flex-direction: column;
align-items: center;
padding: 1rem;
}
/* Robot arm container */
#robot-arm {
position: relative;
width: 180px;
height: 300px;
margin-bottom: 1rem;
}
/* Robot arm parts */
.arm-part {
position: absolute;
background: linear-gradient(135deg, #00fff7, #008080);
border-radius: 8px;
box-shadow: 0 0 10px #00fff7;
}
#base {
bottom: 0;
left: 50%;
transform: translateX(-50%);
width: 80px;
height: 40px;
border-radius: 20px 20px 8px 8px;
background: linear-gradient(135deg, #004d4d, #007777);
box-shadow: 0 0 15px #00fff7;
}
#lower-arm {
bottom: 40px;
left: 50%;
transform-origin: top center;
width: 30px;
height: 120px;
border-radius: 15px;
background: linear-gradient(135deg, #00b3b3, #004d4d);
box-shadow: 0 0 10px #00fff7;
transform: rotate(0deg);
}
#upper-arm {
bottom: 160px;
left: 50%;
transform-origin: bottom center;
width: 25px;
height: 100px;
border-radius: 15px;
background: linear-gradient(135deg, #00e6e6, #006666);
box-shadow: 0 0 10px #00fff7;
transform: rotate(0deg);
}
#claw {
bottom: 260px;
left: 50%;
width: 60px;
height: 40px;
transform-origin: center;
display: flex;
justify-content: space-between;
align-items: center;
}
.claw-finger {
width: 15px;
height: 40px;
background: linear-gradient(135deg, #00ffff, #004d4d);
border-radius: 8px;
box-shadow: 0 0 8px #00fff7;
transform-origin: top center;
transition: transform 0.3s ease;
}
.claw-finger.left {
transform: rotate(20deg);
}
.claw-finger.right {
transform: rotate(-20deg);
}
/* Code pieces */
#code-pieces {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
gap: 10px;
flex-wrap: wrap;
max-width: 300px;
}
.code-piece {
background: #002b3a;
border: 2px solid #00fff7;
border-radius: 8px;
padding: 8px 12px;
font-size: 0.9rem;
cursor: pointer;
user-select: none;
box-shadow: 0 0 8px #00fff7;
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.code-piece:hover {
transform: scale(1.1);
box-shadow: 0 0 15px #00fff7;
}
/* Right panel: Code editor + explanation */
#code-explanation {
flex: 1.2;
background: #1a1f2e;
border-radius: 12px;
box-shadow: 0 0 20px #0ff;
display: flex;
flex-direction: column;
padding: 1.5rem;
}
#code-editor {
flex: 1;
background: #0f111a;
border-radius: 8px;
padding: 1rem;
font-family: 'Roboto Mono', monospace;
font-size: 1.1rem;
color: #7fffd4;
overflow-y: auto;
box-shadow: inset 0 0 10px #00fff7;
white-space: pre-wrap;
line-height: 1.5;
}
#explanation {
margin-top: 1rem;
background: #002b3a;
border-radius: 8px;
padding: 1rem;
font-size: 1rem;
color: #a0e7e5;
min-height: 100px;
box-shadow: inset 0 0 10px #00fff7;
}
/* Syntax highlighting */
.keyword { color: #ff79c6; font-weight: 700; }
.function { color: #50fa7b; }
.variable { color: #8be9fd; }
.number { color: #bd93f9; }
.comment { color: #6272a4; font-style: italic; }
.operator { color: #ffb86c; }
.string { color: #f1fa8c; }
/* Animations */
@keyframes pick-move {
0% { transform: translate(0, 0) rotate(0deg); }
50% { transform: translate(0, -150px) rotate(15deg); }
100% { transform: translate(300px, -150px) rotate(15deg); }
}
/* Responsive */
@media (max-width: 900px) {
main {
flex-direction: column;
padding: 1rem;
}
#robot-workspace, #code-explanation {
width: 100%;
flex: none;
margin-bottom: 1.5rem;
}
}
</style>
</head>
<body>
<header>🤖 Robot Assembles Bubble Sort Algorithm in Python</header>
<main>
<section id="robot-workspace" aria-label="Robot workspace">
<div id="robot-arm" aria-hidden="true">
<div id="base" class="arm-part"></div>
<div id="lower-arm" class="arm-part"></div>
<div id="upper-arm" class="arm-part"></div>
<div id="claw">
<div class="claw-finger left"></div>
<div class="claw-finger right"></div>
</div>
</div>
<div id="code-pieces" aria-label="Code pieces to assemble">
<!-- Pieces will be dynamically inserted here -->
</div>
</section>
<section id="code-explanation" aria-label="Code editor and explanation">
<pre id="code-editor" aria-live="polite" aria-atomic="true"></pre>
<div id="explanation" aria-live="polite" aria-atomic="true"></div>
</section>
</main>
<script>
// Bubble Sort code pieces with explanations
const codeSteps = [
{
code: "def bubble_sort(arr):",
explanation: "Define a function named bubble_sort that takes a list 'arr' as input."
},
{
code: " n = len(arr)",
explanation: "Calculate the length of the list and store it in variable 'n'."
},
{
code: " for i in range(n):",
explanation: "Start an outer loop that runs from 0 to n-1, representing each pass."
},
{
code: " for j in range(0, n - i - 1):",
explanation: "Start an inner loop to compare adjacent elements up to the unsorted part."
},
{
code: " if arr[j] > arr[j + 1]:",
explanation: "If the current element is greater than the next element, swap them."
},
{
code: " arr[j], arr[j + 1] = arr[j + 1], arr[j]",
explanation: "Swap the two elements to move the larger one towards the end."
},
{
code: " return arr",
explanation: "After sorting, return the sorted list."
}
];
// References to DOM elements
const codePiecesContainer = document.getElementById('code-pieces');
const codeEditor = document.getElementById('code-editor');
const explanationBox = document.getElementById('explanation');
// Robot arm parts
const lowerArm = document.getElementById('lower-arm');
const upperArm = document.getElementById('upper-arm');
const claw = document.getElementById('claw');
const clawLeft = claw.querySelector('.claw-finger.left');
const clawRight = claw.querySelector('.claw-finger.right');
// State
let assembledCode = "";
let currentStep = 0;
// Create code pieces visually
function createCodePieces() {
codeSteps.forEach((step, i) => {
const piece = document.createElement('div');
piece.classList.add('code-piece');
piece.textContent = step.code.trim();
piece.dataset.index = i;
codePiecesContainer.appendChild(piece);
});
}
// Syntax highlight function (basic)
function syntaxHighlight(line) {
// Escape HTML
line = line.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
// Keywords
const keywords = /\b(def|for|in|if|return|range)\b/g;
line = line.replace(keywords, '<span class="keyword">$1</span>');
// Function names (def ...)
line = line.replace(/def (\w+)/, 'def <span class="function">$1</span>');
// Variables (simple heuristic)
line = line.replace(/\b(arr|n|i|j)\b/g, '<span class="variable">$1</span>');
// Numbers
line = line.replace(/\b(\d+)\b/g, '<span class="number">$1</span>');
// Comments
line = line.replace(/(#.*)/g, '<span class="comment">$1</span>');
// Strings
line = line.replace(/('.*?'|".*?")/g, '<span class="string">$1</span>');
// Operators
line = line.replace(/(=|>|,|\+|-|\*|\/)/g, '<span class="operator">$1</span>');
return line;
}
// Animate robot arm to pick a piece and place it in code editor
async function animatePickAndPlace(pieceElement, stepIndex) {
return new Promise((resolve) => {
// Calculate positions
const pieceRect = pieceElement.getBoundingClientRect();
const clawRect = claw.getBoundingClientRect();
// Calculate translation needed for claw to reach piece
const dx = pieceRect.left + pieceRect.width / 2 - (clawRect.left + clawRect.width / 2);
const dy = pieceRect.top + pieceRect.height / 2 - (clawRect.top + clawRect.height / 2);
// Animate arm to piece
lowerArm.style.transition = 'transform 1s ease';
upperArm.style.transition = 'transform 1s ease';
claw.style.transition = 'transform 1s ease';
// Open claw
clawLeft.style.transform = 'rotate(40deg)';
clawRight.style.transform = 'rotate(-40deg)';
// Move arm down and towards piece
lowerArm.style.transform = 'rotate(40deg)';
upperArm.style.transform = 'rotate(-30deg)';
claw.style.transform = `translate(${dx}px, ${dy}px) rotate(0deg)`;
setTimeout(() => {
// Close claw (grab)
clawLeft.style.transform = 'rotate(10deg)';
clawRight.style.transform = 'rotate(-10deg)';
// Lift arm up with piece
lowerArm.style.transform = 'rotate(0deg)';
upperArm.style.transform = 'rotate(0deg)';
claw.style.transform = `translate(${dx}px, ${dy - 150}px) rotate(0deg)`;
setTimeout(() => {
// Move claw to code editor position
const editorRect = codeEditor.getBoundingClientRect();
const clawCenterX = clawRect.left + clawRect.width / 2;
const clawCenterY = clawRect.top + clawRect.height / 2;
const targetX = editorRect.left + 20 - clawCenterX;
const targetY = editorRect.top + 20 - clawCenterY;
claw.style.transition = 'transform 1s ease';
claw.style.transform = `translate(${targetX}px, ${targetY}px) rotate(0deg)`;
setTimeout(() => {
// Open claw to drop piece
clawLeft.style.transform = 'rotate(40deg)';
clawRight.style.transform = 'rotate(-40deg)';
setTimeout(() => {
// Reset claw position
claw.style.transition = 'transform 1s ease';
claw.style.transform = 'translate(0, 0) rotate(0deg)';
// Open claw fully
clawLeft.style.transform = 'rotate(20deg)';
clawRight.style.transform = 'rotate(-20deg)';
// Reset arm
lowerArm.style.transform = 'rotate(0deg)';
upperArm.style.transform = 'rotate(0deg)';
resolve();
}, 700);
}, 1000);
}, 1000);
}, 1000);
});
}
// Add code piece to editor with syntax highlight
function addCodeToEditor(codeLine) {
assembledCode += codeLine + "\n";
const highlighted = assembledCode.split('\n').map(line => syntaxHighlight(line)).join('\n');
codeEditor.innerHTML = highlighted;
}
// Show explanation text
function showExplanation(text) {
explanationBox.textContent = text;
}
// Main assembly loop
async function assembleCode() {
const pieces = Array.from(codePiecesContainer.children);
for (let i = 0; i < codeSteps.length; i++) {
const piece = pieces[i];
piece.style.pointerEvents = 'none'; // Disable interaction during animation
showExplanation("Robot is picking up the next piece...");
await animatePickAndPlace(piece, i);
// Add code line to editor
addCodeToEditor(codeSteps[i].code);
showExplanation(codeSteps[i].explanation);
// Fade out the piece after placing
piece.style.transition = 'opacity 0.5s ease';
piece.style.opacity = '0';
await new Promise(r => setTimeout(r, 700));
}
showExplanation("Bubble Sort algorithm assembled successfully! 🎉");
}
// Initialize
createCodePieces();
assembleCode();
</script>
</body>
</html>
Explanation:
- The robot arm is built with simple divs styled and animated to simulate picking and placing.
- The code pieces appear at the bottom of the robot workspace.
- The robot arm moves to each piece, "grabs" it (claw animation), lifts it, and moves it to the code editor.
- The code editor shows the Python code with basic syntax highlighting.
- The explanation box updates with a clear, concise explanation of each code piece.
- The entire process is automated and visually engaging.
Open this file in your browser and watch the robot assemble the Bubble Sort algorithm step by step with explanations! If you want, I can help you extend this with more interactivity or polish.