o4-mini-2025-04-16· OpenAI
LMArena Human Preference 140K
construa, em um unico html, um jogo 2d estilo terraria. monte o melhor jogo que vc…
- User
construa, em um unico html, um jogo 2d estilo terraria. monte o melhor jogo que vc conseguir
- Assistant
Abaixo está um exemplo completo em um único arquivo HTML de um “mini‐Terraria” 2D com geração procedural de terreno, física simples, movimentação, e destruição/colocação de blocos. Só copie tudo para um arquivo
index.htmle abra no seu navegador!<!DOCTYPE html> <html lang="pt-BR"> <head> <meta charset="UTF-8"/> <title>Mini-Terraria 2D</title> <style> html, body { margin: 0; padding: 0; overflow: hidden; } canvas { display: block; background: #87CEEB; } /* céu */ #info { position: absolute; top: 8px; left: 8px; padding: 4px 8px; background: rgba(0,0,0,0.5); color: #fff; font-family: sans-serif; font-size: 14px; } </style> </head> <body> <canvas id="c"></canvas> <div id="info"> Blocos: [1] Terra 🟫 [2] Grama 🟩<br/> Selecione com 1/2 • Esquerdo: quebrar • Direito: colocar </div> <script> (() => { // —––––– Configurações –––––—— const tileSize = 32; const worldW = 100, worldH = 50; const gravity = 0.5; const moveSpeed = 2, jumpSpeed = 10; // —––––– Canvas & Context –––––—— const c = document.getElementById('c'); const ctx = c.getContext('2d'); function resize(){ c.width = innerWidth; c.height = innerHeight; } window.addEventListener('resize', resize); resize(); // —––––– Mundo (grid) –––––—— const world = []; // Gera terreno por "random walk" let height = Math.floor(worldH/2); for(let x=0; x<worldW; x++){ height += Math.floor(Math.random()*3)-1; height = Math.max(10, Math.min(worldH-6, height)); for(let y=0; y<worldH; y++){ if(y < height) world[y] = world[y]||[] , world[y][x] = 0; else if(y === height) world[y][x] = 2; // grama else world[y][x] = 1; // terra } } // —––––– Player –––––—— const player = { x: worldW*tileSize/2, y: 0, w: tileSize*0.8, h: tileSize*1.6, vx: 0, vy: 0, onGround: false }; // —––––– Câmera –––––—— function applyCamera(){ const camX = player.x - c.width/2 + player.w/2; const camY = player.y - c.height/2 + player.h/2; ctx.setTransform(1,0,0,1,-camX,-camY); } // —––––– Input –––––—— const keys = {}; let mouse = { x:0, y:0, down: false, button:0 }; let selectedBlock = 1; window.addEventListener('keydown', e=>{ keys[e.key.toLowerCase()] = true; if(e.key==='1') selectedBlock=1; if(e.key==='2') selectedBlock=2; }); window.addEventListener('keyup', e=>{ keys[e.key.toLowerCase()] = false; }); c.addEventListener('mousemove', e=>{ mouse.x = e.clientX; mouse.y = e.clientY; }); c.addEventListener('mousedown', e=>{ mouse.down = true; mouse.button = e.button; }); window.addEventListener('mouseup', e=>{ mouse.down = false; }); c.oncontextmenu = e=>e.preventDefault(); // —––––– Jogo – física e lógica –––––—— function update(){ // Movimento horizontal if(keys['a'] || keys['arrowleft']) player.vx = -moveSpeed; else if(keys['d'] || keys['arrowright']) player.vx = moveSpeed; else player.vx = 0; // Pulo if((keys['w']||keys[' ']||keys['arrowup']) && player.onGround){ player.vy = -jumpSpeed; player.onGround=false; } // Gravidade player.vy += gravity; // Predição e colisão simples tile-based let nx = player.x + player.vx; let ny = player.y + player.vy; // Função utilitária de colisão com tiles sólidos function collides(x, y){ const gx = Math.floor(x/tileSize); const gy = Math.floor(y/tileSize); return ( gy>=0 && gy<worldH && gx>=0 && gx<worldW && world[gy][gx] > 0 ); } // X colisão if(!collides(nx, player.y) && !collides(nx+player.w-1, player.y) && !collides(nx, player.y+player.h-1) && !collides(nx+player.w-1, player.y+player.h-1)) { player.x = nx; } else player.vx = 0; // Y colisão if(!collides(player.x, ny) && !collides(player.x+player.w-1, ny) && !collides(player.x, ny+player.h-1) && !collides(player.x+player.w-1, ny+player.h-1)) { player.y = ny; player.onGround = false; } else { // Se estiver caindo, acertou o chão if(player.vy > 0) player.onGround = true; player.vy = 0; } // Quebrar/colocar bloco if(mouse.down){ // converte coords de tela em coords mundo const camX = player.x - c.width/2 + player.w/2; const camY = player.y - c.height/2 + player.h/2; const mx = Math.floor((mouse.x+camX)/tileSize); const my = Math.floor((mouse.y+camY)/tileSize); if(mx>=0 && mx<worldW && my>=0 && my<worldH){ if(mouse.button===0){ // botão esquerdo: quebrar world[my][mx] = 0; } else if(mouse.button===2){ // direito: colocar if(world[my][mx] === 0){ world[my][mx] = selectedBlock; } } mouse.down = false; // evita clique contínuo } } } // —––––– Renderização –––––—— function draw(){ ctx.clearRect(0,0,c.width,c.height); applyCamera(); // Tiles for(let y=0; y<worldH; y++){ for(let x=0; x<worldW; x++){ const t = world[y][x]; if(t===1){ ctx.fillStyle = "#8B4513"; // terra } else if(t===2){ ctx.fillStyle = "#228B22"; // grama } else continue; ctx.fillRect(x*tileSize, y*tileSize, tileSize, tileSize); } } // Player ctx.fillStyle = "#2451A1"; ctx.fillRect(player.x, player.y, player.w, player.h); } // —––––– Loop principal –––––—— function loop(){ update(); draw(); requestAnimationFrame(loop); } loop(); })(); </script> </body> </html>Como utilizar:
- Salve tudo em um
index.html. - Abra no navegador.
- Use
A/Dou←/→para mover,W/↑/Spacepara pular. - Clique com mouse esquerdo em um bloco para remover, botão direito para colocar o bloco selecionado (1=terra, 2=grama).
Divirta-se expandindo este código com mais tipos de bloco, inimigos, inventário… 😉
- Salve tudo em um
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.