Meta Description" name="description" />
<!DOCTYPE html>
<html lang="ar">
<head>
<meta charset="UTF-8">
<title>لعبة تكسير القوالب</title>
<style>
body { background: #222; display: flex; justify-content: center; align-items: center; height: 100vh; }
canvas { background: #000; border: 2px solid #fff; }
</style>
</head>
<body>
<canvas id="gameCanvas" width="480" height="320"></canvas>
<script>
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
// الكرة
let ballRadius = 10;
let x = canvas.width / 2;
let y = canvas.height - 30;
let dx = 2;
let dy = -2;
// المضرب
const paddleHeight = 10;
const paddleWidth = 60;
let paddleX = (canvas.width - paddleWidth) / 2;
let rightPressed = false;
let leftPressed = false;
// القوالب
const brickRowCount = 3;
const brickColumnCount = 5;
const brickWidth = 60;
const brickHeight = 10;
const brickPadding = 10;
const brickOffsetTop = 10;
const brickOffsetLeft = 10;
let bricks = [];
for(let c=0; c<brickColumnCount; c++) {
bricks[c] = [];
for(let r=0; r<brickRowCount; r++) {
bricks[c][r] = { x: 0, y: 0, status: 1 };
}
}
let score = 0;
document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);
function keyDownHandler(e) {
if(e.key === "Right" || e.key === "ArrowRight") rightPressed = true;
else if(e.key === "Left" || e.key === "ArrowLeft") leftPressed = true;
}
function keyUpHandler(e) {
if(e.key === "Right" || e.key === "ArrowRight") rightPressed = false;
else if(e.key === "Left" || e.key === "ArrowLeft") leftPressed = false;
}
function collisionDetection() {
for(let c=0; c<brickColumnCount; c++) {
for(let r=0; r<brickRowCount; r++) {
let b = bricks[c][r];
if(b.status === 1) {
if(x > b.x && x < b.x+brickWidth && y > b.y && y < b.y+brickHeight) {
dy = -dy;
b.status = 0;
score+++;
if(score === brickRowCount*brickColumnCount) {
alert(" 🎉");
document.location.reload();
}
}
}
}
}
}
function drawBall() {
ctx.beginPath();
ctx.arc(x, y, ballRadius, 0, Math.PI*2);
ctx.fillStyle = "#00ff00";
ctx.fill();
ctx.closePath();
}
function drawPaddle() {
ctx.beginPath();
ctx.rect(paddleX, canvas.height-paddleHeight, paddleWidth, paddleHeight);
ctx.fillStyle = "#0095DD";
ctx.fill();
ctx.closePath();
}
function drawBricks() {
for(let c=0; c<brickColumnCount; c++) {
for(let r=0; r<brickRowCount; r++) {
if(bricks[c][r].status === 1) {
let brickX = (c*(brickWidth+brickPadding))+brickOffsetLeft;
let brickY = (r*(brickHeight+brickPadding))+brickOffsetTop;
bricks[c][r].x = brickX;
bricks[c][r].y = brickY;
ctx.beginPath();
ctx.rect(brickX, brickY, brickWidth, brickHeight);
ctx.fillStyle = "#ff0000";
ctx.fill();
ctx.closePath();
}
}
}
}
function drawScore() {
ctx.font = "12px Arial";
ctx.fillStyle = "#fff";
ctx.fillText("النقاط: "+score, 8, 20);
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBricks();
drawBall();
drawPaddle();
drawScore();
collisionDetection();
if(x + dx > canvas.width-ballRadius || x + dx < ballRadius) dx = -dx;
if(y + dy < ballRadius) dy = -dy;
else if(y + dy > canvas.height-ballRadius) {
if(x > paddleX && x < paddleX + paddleWidth) dy = -dy;
else {
alert(" finished 😢");
document.location.reload();
}
}
if(rightPressed && paddleX < canvas.width-paddleWidth) paddleX += 7;
else if(leftPressed && paddleX > 0) paddleX -= 7;
x += dx;
y += dy;
requestAnimationFrame(draw);
}
draw();
</script>
</body>
</html>1
1
5KB
5KB
128.0ms
200.0ms
128.0ms