Voici un jeu Minecraft-like complet en un seul fichier HTML avec toutes les fonctionnalités demandées :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>Minecraft-like 3D Game</title>
<style>
body {
margin: 0;
overflow: hidden;
touch-action: none;
font-family: Arial, sans-serif;
}
canvas { display: block; }
#ui {
position: absolute;
bottom: 20px;
width: 100%;
display: flex;
justify-content: space-between;
pointer-events: none;
}
#joystick {
width: 120px;
height: 120px;
background: rgba(255,255,255,0.2);
border: 3px solid rgba(255,255,255,0.4);
border-radius: 50%;
margin-left: 30px;
position: relative;
pointer-events: all;
}
#joystick-knob {
width: 40px;
height: 40px;
background: rgba(255,255,255,0.8);
border-radius: 50%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
transition: none;
}
#buttons {
display: flex;
flex-direction: column;
margin-right: 30px;
gap: 15px;
pointer-events: all;
}
button {
width: 70px;
height: 70px;
background: rgba(255,255,255,0.2);
border: 2px solid rgba(255,255,255,0.4);
border-radius: 15px;
color: white;
font-size: 11px;
font-weight: bold;
cursor: pointer;
user-select: none;
}
#jumpBtn { background: rgba(0,255,0,0.3); }
#viewBtn { background: rgba(0,100,255,0.3); }
button:active {
transform: scale(0.9);
background: rgba(255,255,255,0.4);
}
#info {
position: absolute;
top: 20px;
left: 20px;
color: white;
background: rgba(0,0,0,0.5);
padding: 10px;
border-radius: 5px;
font-size: 14px;
}
</style>
</head>
<body>
<div id="info">
Position: <span id="position">0, 0, 0</span><br>
Vue: <span id="viewMode">1ère personne</span>
</div>
<div id="ui">
<div id="joystick">
<div id="joystick-knob"></div>
</div>
<div id="buttons">
<button id="jumpBtn">SAUT</button>
<button id="viewBtn">VUE</button>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
// Configuration globale
const CHUNK_SIZE = 16;
const RENDER_DISTANCE = 3;
const BLOCK_SIZE = 1;
const PLAYER_SPEED = 5;
const JUMP_FORCE = 12;
const GRAVITY = -25;
// Initialisation de Three.js
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x87CEEB); // Ciel bleu
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.appendChild(renderer.domElement);
// Éclairage
const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(50, 100, 30);
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.width = 2048;
directionalLight.shadow.mapSize.height = 2048;
directionalLight.shadow.camera.near = 0.5;
directionalLight.shadow.camera.far = 200;
directionalLight.shadow.camera.left = -100;
directionalLight.shadow.camera.right = 100;
directionalLight.shadow.camera.top = 100;
directionalLight.shadow.camera.bottom = -100;
scene.add(directionalLight);
// Joueur (rectangle vert plus petit horizontalement)
const playerGeometry = new THREE.BoxGeometry(0.6, 1.8, 0.6);
const playerMaterial = new THREE.MeshLambertMaterial({ color: 0x00ff00 });
const player = new THREE.Mesh(playerGeometry, playerMaterial);
player.castShadow = true;
player.position.set(0, 10, 0);
scene.add(player);
// Variables de jeu
let isFirstPerson = true;
let velocity = new THREE.Vector3(0, 0, 0);
let onGround = false;
let world = new Map();
let loadedChunks = new Set();
// Variables de contrôle
let moveVector = new THREE.Vector2(0, 0);
let cameraRotation = { x: 0, y: 0 };
// Gestion du joystick
const joystick = document.getElementById('joystick');
const joystickKnob = document.getElementById('joystick-knob');
let joystickActive = false;
let joystickCenter = { x: 0, y: 0 };
function initJoystick() {
const rect = joystick.getBoundingClientRect();
joystickCenter = {
x: rect.left + rect.width / 2,
y: rect.top + rect.height / 2
};
}
joystick.addEventListener('touchstart', (e) => {
e.preventDefault();
joystickActive = true;
initJoystick();
});
joystick.addEventListener('touchmove', (e) => {
e.preventDefault();
if (!joystickActive) return;
const touch = e.touches[0];
const deltaX = touch.clientX - joystickCenter.x;
const deltaY = touch.clientY - joystickCenter.y;
const distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
const maxDistance = 40;
if (distance > maxDistance) {
const angle = Math.atan2(deltaY, deltaX);
const x = Math.cos(angle) * maxDistance;
const y = Math.sin(angle) * maxDistance;
joystickKnob.style.transform = `translate(${x}px, ${y}px)`;
moveVector.set(x / maxDistance, -y / maxDistance);
} else {
joystickKnob.style.transform = `translate(${deltaX}px, ${deltaY}px)`;
moveVector.set(deltaX / maxDistance, -deltaY / maxDistance);
}
});
joystick.addEventListener('touchend', (e) => {
e.preventDefault();
joystickActive = false;
joystickKnob.style.transform = 'translate(0px, 0px)';
moveVector.set(0, 0);
});
// Boutons
document.getElementById('jumpBtn').addEventListener('touchstart', (e) => {
e.preventDefault();
if (onGround) {
velocity.y = JUMP_FORCE;
onGround = false;
}
});
document.getElementById('viewBtn').addEventListener('touchstart', (e) => {
e.preventDefault();
isFirstPerson = !isFirstPerson;
document.getElementById('viewMode').textContent = isFirstPerson ? '1ère personne' : '3ème personne';
});
// Génération de blocs avec matériaux différents pour les faces
function createBlockMaterials() {
return [
new THREE.MeshLambertMaterial({ color: 0x999999 }), // droite
new THREE.MeshLambertMaterial({ color: 0x999999 }), // gauche
new THREE.MeshLambertMaterial({ color: 0x666666 }), // haut (plus foncé)
new THREE.MeshLambertMaterial({ color: 0x666666 }), // bas (plus foncé)
new THREE.MeshLambertMaterial({ color: 0x999999 }), // avant
new THREE.MeshLambertMaterial({ color: 0x999999 }) // arrière
];
}
// Génération de terrain avec bruit procédural
function getHeightAt(x, z) {
const scale1 = 0.02;
const scale2 = 0.05;
const scale3 = 0.1;
const height1 = Math.sin(x * scale1) * Math.cos(z * scale1) * 10;
const height2 = Math.sin(x * scale2 + z * scale2) * 5;
const height3 = Math.sin(x * scale3) * Math.cos(z * scale3) * 3;
return Math.floor(height1 + height2 + height3);
}
// Génération de chunk
function generateChunk(chunkX, chunkZ) {
const chunkKey = `${chunkX},${chunkZ}`;
if (loadedChunks.has(chunkKey)) return;
loadedChunks.add(chunkKey);
const blocks = [];
for (let x = 0; x < CHUNK_SIZE; x++) {
for (let z = 0; z < CHUNK_SIZE; z++) {
const worldX = chunkX * CHUNK_SIZE + x;
const worldZ = chunkZ * CHUNK_SIZE + z;
const height = getHeightAt(worldX, worldZ);
// Générer des blocs du sol jusqu'à la hauteur calculée
for (let y = -10; y <= height; y++) {
const blockGeometry = new THREE.BoxGeometry(BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
const blockMaterials = createBlockMaterials();
const block = new THREE.Mesh(blockGeometry, blockMaterials);
block.position.set(worldX, y, worldZ);
block.receiveShadow = true;
block.castShadow = true;
scene.add(block);
blocks.push(block);
// Stocker la position du bloc pour les collisions
const blockKey = `${worldX},${y},${worldZ}`;
world.set(blockKey, block);
}
}
}
}
// Vérification des collisions
function checkCollision(position) {
const playerBox = new THREE.Box3();
const tempPlayer = player.clone();
tempPlayer.position.copy(position);
playerBox.setFromObject(tempPlayer);
// Vérifier collision avec les blocs autour du joueur
const minX = Math.floor(position.x - 1);
const maxX = Math.floor(position.x + 1);
const minY = Math.floor(position.y - 1);
const maxY = Math.floor(position.y + 2);
const minZ = Math.floor(position.z - 1);
const maxZ = Math.floor(position.z + 1);
for (let x = minX; x <= maxX; x++) {
for (let y = minY; y <= maxY; y++) {
for (let z = minZ; z <= maxZ; z++) {
const blockKey = `${x},${y},${z}`;
if (world.has(blockKey)) {
const blockBox = new THREE.Box3();
blockBox.setFromObject(world.get(blockKey));
if (playerBox.intersectsBox(blockBox)) {
return true;
}
}
}
}
}
return false;
}
// Gestion de la physique et du mouvement
function updatePlayer(deltaTime) {
const oldPosition = player.position.clone();
// Mouvement horizontal basé sur le joystick
if (moveVector.length() > 0) {
const moveDirection = new THREE.Vector3();
if (isFirstPerson) {
// En première personne, se baser sur la rotation de la caméra
moveDirection.set(moveVector.x, 0, moveVector.y);
moveDirection.applyQuaternion(camera.quaternion);
} else {
// En troisième personne, mouvement relatif à la caméra
moveDirection.set(moveVector.x, 0, moveVector.y);
}
moveDirection.y = 0;
moveDirection.normalize();
moveDirection.multiplyScalar(PLAYER_SPEED * deltaTime);
// Test collision horizontale
const newPos = oldPosition.clone().add(moveDirection);
if (!checkCollision(newPos)) {
player.position.copy(newPos);
}
}
// Gravité
velocity.y += GRAVITY * deltaTime;
// Test collision verticale
const verticalMovement = new THREE.Vector3(0, velocity.y * deltaTime, 0);
const newPosVertical = player.position.clone().add(verticalMovement);
if (checkCollision(newPosVertical)) {
if (velocity.y < 0) {
onGround = true;
}
velocity.y = 0;
} else {
player.position.add(verticalMovement);
onGround = false;
}
// Limiter la vitesse de chute
velocity.y = Math.max(velocity.y, -50);
}
// Gestion de la caméra
function updateCamera() {
if (isFirstPerson) {
camera.position.copy(player.position);
camera.position.y += 0.8; // Hauteur des yeux
player.visible = false;
} else {
camera.position.set(
player.position.x + 5,
player.position.y + 3,
player.position.z + 5
);
camera.lookAt(player.position);
player.visible = true;
}
}
// Chargement des chunks
function updateChunks() {
const playerChunkX = Math.floor(player.position.x / CHUNK_SIZE);
const playerChunkZ = Math.floor(player.position.z / CHUNK_SIZE);
// Charger les chunks autour du joueur
for (let x = playerChunkX - RENDER_DISTANCE; x <= playerChunkX + RENDER_DISTANCE; x++) {
for (let z = playerChunkZ - RENDER_DISTANCE; z <= playerChunkZ + RENDER_DISTANCE; z++) {
generateChunk(x, z);
}
}
}
// Mise à jour de l'interface
function updateUI() {
const pos = player.position;
document.getElementById('position').textContent =
`${Math.floor(pos.x)}, ${Math.floor(pos.y)}, ${Math.floor(pos.z)}`;
}
// Gestion du redimensionnement
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// Rotation de la caméra par touch (première personne)
let lastTouchX = 0;
let lastTouchY = 0;
let touchRotating = false;
renderer.domElement.addEventListener('touchstart', (e) => {
if (e.touches.length === 1 && isFirstPerson) {
touchRotating = true;
lastTouchX = e.touches[0].clientX;
lastTouchY = e.touches[0].clientY;
}
});
renderer.domElement.addEventListener('touchmove', (e) => {
if (touchRotating && e.touches.length === 1 && isFirstPerson) {
e.preventDefault();
const deltaX = e.touches[0].clientX - lastTouchX;
const deltaY = e.touches[0].clientY - lastTouchY;
cameraRotation.y -= deltaX * 0.005;
cameraRotation.x -= deltaY * 0.005;
cameraRotation.x = Math.max(-Math.PI/2, Math.min(Math.PI/2, cameraRotation.x));
camera.rotation.set(cameraRotation.x, cameraRotation.y, 0);
lastTouchX = e.touches[0].clientX;
lastTouchY = e.touches[0].clientY;
}
});
renderer.domElement.addEventListener('touchend', () => {
touchRotating = false;
});
// Boucle de jeu principale
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const deltaTime = clock.getDelta();
updatePlayer(deltaTime);
updateCamera();
updateChunks();
updateUI();
renderer.render(scene, camera);
}
// Initialisation
initJoystick();
updateChunks(); // Charger les chunks initiaux
animate();
</script>
</body>
</html>
Ce jeu Minecraft-like inclut toutes les fonctionnalités demandées :
Fonctionnalités implémentées :
- ✅ Contrôles tactiles Android : Joystick virtuel pour le mouvement, boutons pour sauter et changer de vue
- ✅ Cubes gris avec bordures foncées : Matériaux différents pour les faces (gris clair sur les côtés, gris foncé en haut/bas)
- ✅ Système de chunks : Chargement dynamique des zones autour du joueur
- ✅ Joueur rectangle vert : Plus petit horizontalement (0.6x1.8x0.6) que les cubes (1x1x1)
- ✅ Basculement 1ère/3ème personne : Bouton "VUE" pour changer de perspective
- ✅ Système de collision/hitbox : Empêche de traverser les blocs
- ✅ Ciel bleu : Arrière-plan bleu ciel
- ✅ Génération procédurale : Terrain avec montagnes et vallées
- ✅ Three.js : Utilise la bibliothèque Three.js
- ✅ Fichier unique : Tout le code dans un seul fichier HTML
Contrôles :
- Joystick virtuel (en bas à gauche) : Déplacement
- Écran tactile : Rotation de la caméra en 1ère personne
- Bouton "SAUT" : Faire sauter le joueur
- Bouton "VUE" : Basculer entre 1ère et 3ème personne
Le jeu génère un monde infini avec des terrains variés et se charge automatiquement quand vous vous déplacez !