1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
| <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mini Adventure</title>
<style>
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #000;
}
canvas {
display: block;
border: 1px solid #fff;
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<script>
// Variables globales
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Chargement des images
const map = new Image();
map.src = 'assets/map.png';
const collisionMap = new Image();
collisionMap.src = 'assets/map-collision.png';
const sprites = [];
for (let i = 1; i <= 16; i++) {
const img = new Image();
img.src = `assets/sprite_${i}.png`;
sprites.push(img);
}
// Dimensions du canvas
canvas.width = 800;
canvas.height = 600;
// Position et direction initiales du personnage
let player = {
x: 150, // Position initiale (point bleu sur map-collision.png)
y: 150,
width: 32,
height: 32,
frame: 0,
direction: 'down', // Options : 'down', 'up', 'left', 'right'
};
// Variables pour l'animation
let animationFrame = 0;
const frameInterval = 10; // Intervalle entre les frames d'animation
// Zones d'interaction (ex. escaliers)
const interactionZones = [
{ x: 400, y: 300, width: 32, height: 32, action: () => window.location.href = 'https://example.com' },
];
// Détection des collisions
function isColliding(x, y) {
const pixelData = ctx.getImageData(x, y, player.width, player.height).data;
for (let i = 0; i < pixelData.length; i += 4) {
if (pixelData[i] === 255 && pixelData[i + 1] === 0 && pixelData[i + 2] === 0) {
// Collision avec une zone rouge
return true;
}
}
return false;
}
// Dessin du personnage
function drawPlayer() {
const spriteIndex = player.direction === 'down' ? 0 :
player.direction === 'up' ? 4 :
player.direction === 'left' ? 8 :
12;
const frame = spriteIndex + Math.floor(player.frame / frameInterval) % 4;
ctx.drawImage(sprites[frame], player.x, player.y, player.width, player.height);
}
// Dessin de la carte
function drawMap() {
ctx.drawImage(map, 0, 0, canvas.width, canvas.height);
}
// Boucle du jeu
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Dessin de la carte
drawMap();
// Vérification des interactions
interactionZones.forEach(zone => {
if (
player.x < zone.x + zone.width &&
player.x + player.width > zone.x &&
player.y < zone.y + zone.height &&
player.y + player.height > zone.y
) {
// Si le joueur est dans la zone d'interaction et appuie sur 'E'
document.addEventListener('keydown', (e) => {
if (e.key === 'e') {
zone.action();
}
});
}
});
// Dessin du personnage
drawPlayer();
// Animation
animationFrame++;
if (animationFrame >= frameInterval * 4) {
animationFrame = 0;
}
requestAnimationFrame(gameLoop);
}
// Déplacement du joueur
document.addEventListener('keydown', (e) => {
let nextX = player.x;
let nextY = player.y;
if (e.key === 'ArrowUp') {
player.direction = 'up';
nextY -= 5;
} else if (e.key === 'ArrowDown') {
player.direction = 'down';
nextY += 5;
} else if (e.key === 'ArrowLeft') {
player.direction = 'left';
nextX -= 5;
} else if (e.key === 'ArrowRight') {
player.direction = 'right';
nextX += 5;
}
// Vérification des collisions
if (!isColliding(nextX, nextY)) {
player.x = nextX;
player.y = nextY;
}
});
// Chargement initial
map.onload = () => {
collisionMap.onload = () => {
gameLoop();
};
};
</script>
</body>
</html> |
Partager