pbInfo.ro
Probleme
Probleme - clasa a IX-a
Probleme - clasa a X-a
Probleme - clasa a XI-a
Probleme din concursuri
Căutare problemă
Exerciții
Programează cu Blockly
Desenează cu Processing
Exersează SQL
Soluţii
Resurse
Resurse pentru clasa a IX-a
Resurse pentru clasa a X-a
Resurse pentru clasa a XI-a
Articole recomandate
Subiecte bacalaureat
Ajutor
Autentificare
Înregistrare
Autentificare
Utilizator sau email
Parola
Acest site foloseşte cookies. Navigând în continuare, vă exprimaţi acordul asupra folosirii cookie-urilor.
Îti place pbInfo? Atunci acceptă-l cu totul! Dezactivează modulul de blocare a reclamelor!
Script Nou - Processing
Lista scripturi
Script Nou
Ajutor
ID
Autor
Duplicat din
Ultima modificare
#7240
7B-Necsulescu Stefan Matei (Stefan_Matei_Necsulescu)
-
Luni, 18 mai 2026, 10:31
// ===================================================== // ULTRA MINECRAFT p5.js ENGINE // Massive upgraded version with MANY features // ===================================================== let cam; const BLOCK = 40; const CHUNK = 8; const RENDER_DISTANCE = 3; let chunks = {}; let particles = []; let clouds = []; let stars = []; let rain = []; let mobs = []; let selected = "grass"; const blockTypes = [ "grass", "dirt", "stone", "wood", "leaf", "sand", "glass", "water", "lava" ]; const colors = { grass: [95, 159, 53], dirt: [121, 85, 58], stone: [120, 120, 125], wood: [102, 81, 51], leaf: [60, 120, 60], sand: [194, 178, 128], glass: [220, 240, 255, 70], water: [40, 100, 220, 140], lava: [255, 90, 0] }; let player = { pos: null, vel: null, yaw: 0, pitch: 0, grounded: false, sprint: false, crouch: false, health: 20, hunger: 20, xp: 0, level: 1 }; let inventory = {}; let weather = "clear"; let time = 0; function setup() { createCanvas(windowWidth, windowHeight, WEBGL); cam = createCamera(); noiseDetail(5, 0.5); player.pos = createVector(0, -300, 0); player.vel = createVector(0, 0, 0); for (let t of blockTypes) inventory[t] = 99; // CLOUDS for (let i = 0; i < 40; i++) { clouds.push({ x: random(-6000, 6000), y: random(-1200, -700), z: random(-6000, 6000), size: random(120, 400) }); } // STARS for (let i = 0; i < 1000; i++) { stars.push({ x: random(-8000, 8000), y: random(-8000, 0), z: random(-8000, 8000), s: random(1, 4) }); } // RAIN for (let i = 0; i < 600; i++) { rain.push({ x: random(-3000, 3000), y: random(-3000, 0), z: random(-3000, 3000) }); } // MOBS for (let i = 0; i < 15; i++) { mobs.push({ pos: createVector( random(-2000, 2000), -200, random(-2000, 2000) ), vel: createVector(), type: random(["cow", "zombie"]), hp: 10 }); } document.oncontextmenu = () => false; requestPointerLock(); } function draw() { updateTime(); drawSky(); updateLighting(); handleInput(); physics(); updateCamera(); generateChunks(); renderChunks(); drawClouds(); drawStars(); drawWeather(); updateMobs(); updateParticles(); drawTargetOutline(); drawHUD(); } // ===================================================== // TIME + SKY // ===================================================== function updateTime() { time += 0.0007; } function drawSky() { let day = map(sin(time), -1, 1, 0, 1); let skyR = lerp(10, 130, day); let skyG = lerp(10, 190, day); let skyB = lerp(25, 255, day); background(skyR, skyG, skyB); // SUN push(); translate( sin(time) * 4000, cos(time) * 2000, -5000 ); emissiveMaterial(255, 230, 120); sphere(160); pop(); // MOON push(); translate( -sin(time) * 4000, -cos(time) * 2000, -5000 ); emissiveMaterial(180); sphere(100); pop(); } function updateLighting() { let day = map(sin(time), -1, 1, 0, 1); ambientLight(20 + day * 180); directionalLight( 255 * day, 240 * day, 220 * day, 0.3, 1, 0.2 ); } // ===================================================== // WORLD // ===================================================== function generateChunks() { let pcx = floor(player.pos.x / (CHUNK * BLOCK)); let pcz = floor(player.pos.z / (CHUNK * BLOCK)); for (let cx = pcx - RENDER_DISTANCE; cx <= pcx + RENDER_DISTANCE; cx++) { for (let cz = pcz - RENDER_DISTANCE; cz <= pcz + RENDER_DISTANCE; cz++) { let key = `${cx},${cz}`; if (!chunks[key]) { chunks[key] = generateChunk(cx, cz); } } } } function generateChunk(cx, cz) { let arr = []; for (let x = 0; x < CHUNK; x++) { for (let z = 0; z < CHUNK; z++) { let wx = (cx * CHUNK + x) * BLOCK; let wz = (cz * CHUNK + z) * BLOCK; // BIOME let biome = noise(wx * 0.0003, wz * 0.0003); // HEIGHTMAP let hNoise = noise(wx * 0.002, wz * 0.002) * 0.7 + noise(wx * 0.01, wz * 0.01) * 0.3; let h = floor(hNoise * 20) * BLOCK; let topType = "grass"; if (biome < 0.3) topType = "sand"; if (biome > 0.75) topType = "stone"; // TOP arr.push({ pos: createVector(wx, h, wz), type: topType }); // DIRT for (let d = 1; d <= 3; d++) { arr.push({ pos: createVector(wx, h + d * BLOCK, wz), type: "dirt" }); } // STONE + CAVES for (let d = 4; d <= 15; d++) { let cave = noise( wx * 0.02, wz * 0.02, d * 0.1 ); if (cave > 0.57) continue; arr.push({ pos: createVector(wx, h + d * BLOCK, wz), type: "stone" }); } // TREES if ( topType === "grass" && random() < 0.03 ) { makeTree(arr, wx, h - BLOCK, wz); } // WATER if (h > 200) { arr.push({ pos: createVector(wx, 240, wz), type: "water" }); } // LAVA if (random() < 0.002) { arr.push({ pos: createVector(wx, h + 10 * BLOCK, wz), type: "lava" }); } } } return arr; } function makeTree(arr, x, y, z) { let height = floor(random(4, 8)); for (let i = 0; i < height; i++) { arr.push({ pos: createVector(x, y - i * BLOCK, z), type: "wood" }); } for (let lx = -2; lx <= 2; lx++) { for (let lz = -2; lz <= 2; lz++) { for (let ly = -2; ly <= 1; ly++) { if (lx*lx + lz*lz + ly*ly < 7) { arr.push({ pos: createVector( x + lx * BLOCK, y - height * BLOCK + ly * BLOCK, z + lz * BLOCK ), type: "leaf" }); } } } } } // ===================================================== // RENDER // ===================================================== function renderChunks() { for (let key in chunks) { for (let b of chunks[key]) { drawBlock(b); } } } function drawBlock(b) { let d = dist( player.pos.x, player.pos.z, b.pos.x, b.pos.z ); if (d > 1400) return; push(); translate( b.pos.x, b.pos.y, b.pos.z ); let c = colors[b.type]; let alpha = map(d, 0, 1400, 255, 30); if (b.type === "water") { fill(c[0], c[1], c[2], alpha); stroke(255, 40); } else if (b.type === "glass") { fill(c[0], c[1], c[2], alpha); stroke(255, 80); } else { ambientMaterial( c[0], c[1], c[2], alpha ); noStroke(); } // Fake grass top if (b.type === "grass") { fill(121, 85, 58); box(BLOCK); translate(0, -BLOCK/2 + 2, 0); fill(95,159,53); box(BLOCK * 1.01, 4, BLOCK * 1.01); } else { box(BLOCK); } pop(); } // ===================================================== // PLAYER // ===================================================== function handleInput() { player.sprint = keyIsDown(SHIFT); let speed = player.sprint ? 9 : 5; let dx = 0; let dz = 0; if (keyIsDown(87)) { dx += sin(player.yaw); dz += cos(player.yaw); } if (keyIsDown(83)) { dx -= sin(player.yaw); dz -= cos(player.yaw); } if (keyIsDown(65)) { dx += cos(player.yaw); dz -= sin(player.yaw); } if (keyIsDown(68)) { dx -= cos(player.yaw); dz += sin(player.yaw); } player.vel.x = lerp(player.vel.x, dx * speed, 0.2); player.vel.z = lerp(player.vel.z, dz * speed, 0.2); player.pos.x += player.vel.x; if (collide()) { player.pos.x -= player.vel.x; } player.pos.z += player.vel.z; if (collide()) { player.pos.z -= player.vel.z; } } function physics() { player.vel.y += 0.5; player.pos.y += player.vel.y; if (collide()) { player.pos.y -= player.vel.y; player.vel.y = 0; player.grounded = true; } else { player.grounded = false; } } function collide() { for (let key in chunks) { for (let b of chunks[key]) { if ( abs(player.pos.x - b.pos.x) < 20 && abs(player.pos.y - b.pos.y) < 60 && abs(player.pos.z - b.pos.z) < 20 ) { return true; } } } return false; } // ===================================================== // CAMERA // ===================================================== function updateCamera() { let bob = sin(frameCount * 0.15) * 2; cam.setPosition( player.pos.x, player.pos.y - 40 + bob, player.pos.z ); cam.lookAt( player.pos.x + sin(player.yaw), player.pos.y - 40 + player.pitch * 100, player.pos.z + cos(player.yaw) ); } function mouseMoved() { player.yaw += movedX * 0.002; player.pitch += movedY * 0.002; player.pitch = constrain( player.pitch, -1.5, 1.5 ); } // ===================================================== // TARGET // ===================================================== function getTargetBlock() { let dir = createVector( sin(player.yaw), player.pitch, cos(player.yaw) ); for (let d = 0; d < 240; d += 8) { let p = p5.Vector.add( player.pos, p5.Vector.mult(dir, d) ); for (let key in chunks) { for (let i = 0; i < chunks[key].length; i++) { let b = chunks[key][i]; if ( dist( p.x, p.y, p.z, b.pos.x, b.pos.y, b.pos.z ) < 25 ) { return { block: b, list: chunks[key], index: i }; } } } } return null; } function drawTargetOutline() { let hit = getTargetBlock(); if (!hit) return; push(); translate( hit.block.pos.x, hit.block.pos.y, hit.block.pos.z ); noFill(); stroke(255); strokeWeight(2); box(BLOCK + 3); pop(); } // ===================================================== // BLOCK INTERACTION // ===================================================== function mousePressed() { // DESTROY if (mouseButton === LEFT) { let hit = getTargetBlock(); if (hit) { inventory[hit.block.type]++; for (let i = 0; i < 20; i++) { particles.push( new Particle( hit.block.pos.x, hit.block.pos.y, hit.block.pos.z, hit.block.type ) ); } hit.list.splice(hit.index, 1); } } // PLACE if (mouseButton === RIGHT) { placeBlock(); } } function placeBlock() { if (inventory[selected] <= 0) return; let dir = createVector( sin(player.yaw), player.pitch, cos(player.yaw) ); let p = p5.Vector.add( player.pos, p5.Vector.mult(dir, 120) ); p.x = round(p.x / BLOCK) * BLOCK; p.y = round(p.y / BLOCK) * BLOCK; p.z = round(p.z / BLOCK) * BLOCK; let key = `${floor(p.x / (CHUNK * BLOCK))},${floor(p.z / (CHUNK * BLOCK))}`; if (!chunks[key]) return; chunks[key].push({ pos: createVector(p.x, p.y, p.z), type: selected }); inventory[selected]--; } // ===================================================== // PARTICLES // ===================================================== class Particle { constructor(x, y, z, type) { this.pos = createVector(x, y, z); this.vel = p5.Vector.random3D() .mult(random(2, 7)); this.life = 255; this.type = type; } update() { this.pos.add(this.vel); this.vel.y += 0.15; this.life -= 7; } draw() { push(); translate( this.pos.x, this.pos.y, this.pos.z ); let c = colors[this.type]; fill(c[0], c[1], c[2], this.life); noStroke(); box(6); pop(); } } function updateParticles() { for (let i = particles.length - 1; i >= 0; i--) { particles[i].update(); particles[i].draw(); if (particles[i].life <= 0) { particles.splice(i, 1); } } } // ===================================================== // CLOUDS // ===================================================== function drawClouds() { noStroke(); fill(255, 230); for (let c of clouds) { push(); translate(c.x, c.y, c.z); ellipsoid( c.size, 50, c.size * 0.5 ); pop(); c.x += 0.2; if (c.x > 6000) c.x = -6000; } } // ===================================================== // STARS // ===================================================== function drawStars() { let day = map(sin(time), -1, 1, 0, 1); if (day > 0.4) return; noStroke(); fill(255); for (let s of stars) { push(); translate(s.x, s.y, s.z); sphere(s.s); pop(); } } // ===================================================== // WEATHER // ===================================================== function drawWeather() { if (weather !== "rain") { if (random() < 0.0005) weather = "rain"; return; } stroke(180, 180, 255); for (let r of rain) { line( r.x, r.y, r.z, r.x, r.y + 30, r.z ); r.y += 40; if (r.y > player.pos.y + 300) { r.y = player.pos.y - 1000; } } if (random() < 0.001) { weather = "clear"; } } // ===================================================== // MOBS // ===================================================== function updateMobs() { for (let m of mobs) { push(); translate( m.pos.x, m.pos.y, m.pos.z ); if (m.type === "cow") { fill(120, 70, 50); } else { fill(60, 130, 60); } box(40, 60, 30); pop(); // RANDOM WALK m.pos.x += random(-1, 1); m.pos.z += random(-1, 1); } } // ===================================================== // HUD // ===================================================== function drawHUD() { resetMatrix(); // CROSSHAIR stroke(255); strokeWeight(2); circle(0, 0, 12); line(-14, 0, -5, 0); line(14, 0, 5, 0); line(0, -14, 0, -5); line(0, 14, 0, 5); // HOTBAR translate(-width / 2 + 20, height / 2 - 90); fill(0, 150); noStroke(); rect(0, 0, 500, 70, 10); fill(255); textSize(18); text( `[1]Grass [2]Dirt [3]Stone [4]Wood [5]Leaf [6]Sand [7]Glass [8]Water [9]Lava`, 20, 30 ); text(`Selected: ${selected}`, 20, 55); text(`Health: ${player.health}`, 350, 30); text(`Hunger: ${player.hunger}`, 350, 55); // HAND push(); translate(width/2 + 260, height/2 + 150); rotate(-0.3); fill(220, 180, 150); box(40, 120, 40); pop(); } // ===================================================== // INPUT // ===================================================== function keyPressed() { if (key === " " && player.grounded) { player.vel.y = -12; } if (key >= "1" && key <= "9") { selected = blockTypes[int(key) - 1]; } } function windowResized() { resizeCanvas(windowWidth, windowHeight); }
Duplicare
Executare
Cod
Cod HTML
<iframe sandbox="allow-scripts" src="/p5js/index.php?id=7240" style="width:408px; height:408px;border:solid 1px gray; overflow: scroll;"></iframe>
Duplicare script
Denumirea noului script
Du-te sus!