
Game by marketing2advertising
π Car Dodging Game on the π£οΈ
Use the arrow keys to dodge the red cars and survive as long as you can!
Score: 0
const canvas = document.getElementById(‘carGame’);
const ctx = canvas.getContext(‘2d’);
const scoreDisplay = document.getElementById(‘score’);
let player = { x: 130, y: 400, width: 40, height: 70 };
let obstacles = [];
let speed = 4;
let score = 0;
let gameOver = false;
// Draw emoji car for player
function drawPlayerCar(x, y) {
ctx.font = ’60px serif’;
ctx.textAlign = ‘center’;
ctx.textBaseline = ‘middle’;
ctx.fillText(‘π’, x + player.width/2, y + player.height/2 + 5);
}
function drawRoad() {
ctx.fillStyle = ‘#444’; // Darker road background
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = ‘#fff’;
ctx.lineWidth = 4;
ctx.setLineDash([20, 20]);
ctx.beginPath();
ctx.moveTo(canvas.width / 2, 0);
ctx.lineTo(canvas.width / 2, canvas.height);
ctx.stroke();
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawRoad();
drawPlayerCar(player.x, player.y);
ctx.fillStyle = ‘red’;
obstacles.forEach(obs => {
ctx.fillRect(obs.x, obs.y, obs.width, obs.height);
});
obstacles.forEach(obs => {
obs.y += speed;
if (checkCollision(player, obs)) {
gameOver = true;
}
});
obstacles = obstacles.filter(obs => obs.y < canvas.height);
score++;
scoreDisplay.textContent = Math.floor(score / 10);
}
function checkCollision(a, b) {
return (
a.x b.x &&
a.y b.y
);
}
function gameLoop() {
if (!gameOver) {
draw();
if (Math.random() < 0.03) {
obstacles.push({
x: Math.random() {
if (e.key === ‘ArrowLeft’ && player.x > 60) player.x -= 70;
if (e.key === ‘ArrowRight’ && player.x < 200) player.x += 70;
});
gameLoop();