generate the html, css and js all in a single document for a mario-like game
- User
generate the html, css and js all in a single document for a mario-like game
- Assistant
Here’s a self-contained “Mario-like” demo in one HTML file. It uses an HTML5 canvas, inline CSS for styling, and JavaScript for a simple tile-based world, a player you can move/jump, gravity, and rudimentary collision detection. Copy & paste this into a file (e.g.
index.html) and open in your browser:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Mini Mario-Style Demo</title> <style> /* center the canvas and set a background */ body { margin: 0; background: #87CEEB; /* sky blue */ display: flex; align-items: center; justify-content: center; height: 100vh; overflow: hidden; } canvas { border: 4px solid #333; background: #5C94FC; /* deeper sky behind */ } </style> </head> <body> <canvas id="game" width="640" height="320"></canvas> <script> // ----- SETUP ----- const canvas = document.getElementById('game'); const ctx = canvas.getContext('2d'); const TILE = 32; const COLS = canvas.width / TILE; const ROWS = canvas.height / TILE; // simple level: 0 = empty, 1 = ground block const level = [ // 20 columns each row [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], // sky rows [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0], [0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0], [0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0], [0,0,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], // ground [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] ]; // player state const player = { x: TILE*2, y: TILE*5, w: TILE*0.8, h: TILE*0.9, vx: 0, vy: 0, speed: 2.2, jumpPower: 7, onGround: false }; const keys = {}; window.addEventListener('keydown', e => keys[e.code] = true); window.addEventListener('keyup', e => keys[e.code] = false); function tileAt(col, row) { if (row < 0 || row >= ROWS || col < 0 || col >= COLS) return 1; return level[row][col]; } // ----- GAME LOOP ----- function update() { // horizontal movement if (keys['ArrowLeft']) { player.vx = -player.speed; } else if (keys['ArrowRight']) { player.vx = player.speed; } else { player.vx = 0; } // jump if (keys['Space'] || keys['ArrowUp']) { if (player.onGround) { player.vy = -player.jumpPower; player.onGround = false; } } // gravity player.vy += 0.3; if (player.vy > 12) player.vy = 12; // tentative new position let nx = player.x + player.vx; let ny = player.y + player.vy; // collision detection helper function collides(x, y) { // check 4 corners const pts = [ [x, y], [x+player.w, y], [x, y+player.h], [x+player.w, y+player.h] ]; return pts.some(([px,py]) => tileAt(Math.floor(px/TILE), Math.floor(py/TILE)) === 1 ); } // horizontal collision if (!collides(nx, player.y)) { player.x = nx; } else { player.vx = 0; } // vertical collision if (!collides(player.x, ny)) { player.y = ny; player.onGround = false; } else { // if we were falling, we hit the ground if (player.vy > 0) player.onGround = true; player.vy = 0; } } function draw() { // clear ctx.clearRect(0,0,canvas.width,canvas.height); // draw tiles for (let row=0; row<ROWS; row++) { for (let col=0; col<COLS; col++) { if (level[row][col] === 1) { ctx.fillStyle = '#654321'; // brown blocks ctx.fillRect(col*TILE, row*TILE, TILE, TILE); ctx.strokeStyle = '#492E1B'; ctx.strokeRect(col*TILE, row*TILE, TILE, TILE); } } } // draw player ctx.fillStyle = '#FF0000'; ctx.fillRect(player.x, player.y, player.w, player.h); // simple face ctx.fillStyle = '#000'; ctx.fillRect(player.x+player.w*0.2, player.y+player.h*0.2, 2, 2); ctx.fillRect(player.x+player.w*0.6, player.y+player.h*0.2, 2, 2); ctx.beginPath(); ctx.arc(player.x+player.w/2, player.y+player.h*0.7, 3, 0, Math.PI); ctx.stroke(); } function loop() { update(); draw(); requestAnimationFrame(loop); } // start loop(); </script> </body> </html>How it works:
• A 10×20 tile grid defines the world (1 = block, 0 = empty).
• A red “player” rectangle can move left/right (←,→) and jump (↑ or Space).
• Basic AABB collision with tilemap keeps the player on platforms.
• Gravity, jumping, tile rendering, and a simple face complete the vibe.Feel free to swap in your own tile images or tweak physics (speed, gravity, level layout, etc.). Have fun expanding it! 🚀
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.