Text
Text
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2D Action Combat Game</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #333;
}
canvas {
background-color: #eee;
display: block;
margin: auto;
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Variables du joueur
const player = {
x: 50,
y: canvas.height - 100,
width: 50,
height: 50,
color: 'blue',
speed: 5,
dx: 0,
dy: 0,
isAttacking: false
};
// Variables de l'ennemi
const enemy = {
x: canvas.width - 100,
y: canvas.height - 100,
width: 50,
height: 50,
color: 'red'
};
// Dessine le joueur
function drawPlayer() {
ctx.fillStyle = player.color;
ctx.fillRect(player.x, player.y, player.width, player.height);
}
// Dessine l'ennemi
function drawEnemy() {
ctx.fillStyle = enemy.color;
ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
}
// Déplace le joueur
function movePlayer() {
player.x += player.dx;
player.y += player.dy;
// Limites du canvas
if (player.x < 0) player.x = 0;
if (player.x + player.width > canvas.width) player.x = canvas.width -
player.width;
}
// Réinitialise le jeu
function resetGame() {
player.x = 50;
player.y = canvas.height - 100;
player.isAttacking = false;
}
// Efface le canvas
function clearCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
requestAnimationFrame(updateGame);
}
// Démarre le jeu
updateGame();
</script>
</body>
</html>