سلام!
در اینجا یک کد ساده برای ایجاد یک بازی فوتبال در فایل index.html آوردهام. این بازی با استفاده از HTML، CSS و JavaScript نوشته شده و تمام کدها در یک فایل قرار دارند.
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<title>بازی فوتبال ساده</title>
<style>
body {
margin: 0;
overflow: hidden;
}
canvas {
background: green;
display: block;
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<script>
// تنظیمات اولیه
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// تنظیم اندازه بوم به اندازه صفحه نمایش
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// تعریف توپ
let ball = {
x: canvas.width / 2,
y: canvas.height / 2,
radius: 15,
dx: 5,
dy: 4
};
// رسم توپ
function drawBall() {
ctx.beginPath();
ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI*2);
ctx.fillStyle = 'white';
ctx.fill();
ctx.closePath();
}
// بهروزرسانی موقعیت توپ
function updateBall() {
ball.x += ball.dx;
ball.y += ball.dy;
// برخورد با دیوارهها
if(ball.x + ball.radius > canvas.width || ball.x - ball.radius < 0) {
ball.dx = -ball.dx;
}
if(ball.y + ball.radius > canvas.height || ball.y - ball.radius < 0) {
ball.dy = -ball.dy;
}
}
// تعریف بازیکن
let player = {
x: canvas.width / 2 - 50,
y: canvas.height - 30,
width: 100,
height: 20,
dx: 0
};
// رسم بازیکن
function drawPlayer() {
ctx.fillStyle = 'blue';
ctx.fillRect(player.x, player.y, player.width, player.height);
}
// بهروزرسانی موقعیت بازیکن
function updatePlayer() {
player.x += player.dx;
// جلوگیری از خروج بازیکن از صفحه
if(player.x < 0) {
player.x = 0;
}
if(player.x + player.width > canvas.width) {
player.x = canvas.width - player.width;
}
}
// کنترل ورودی کاربر
document.addEventListener('keydown', function(e) {
if(e.key === 'ArrowRight' || e.key === 'Right') {
player.dx = 7;
} else if(e.key === 'ArrowLeft' || e.key === 'Left') {
player.dx = -7;
}
});
document.addEventListener('keyup', function(e) {
if(e.key === 'ArrowRight' || e.key === 'Right' || e.key === 'ArrowLeft' || e.key === 'Left') {
player.dx = 0;
}
});
// لمس برای موبایل
let touchX = null;
canvas.addEventListener('touchstart', function(e) {
touchX = e.touches[0].clientX;
});
canvas.addEventListener('touchmove', function(e) {
if(touchX !== null) {
let newTouchX = e.touches[0].clientX;
let deltaX = newTouchX - touchX;
player.x += deltaX;
touchX = newTouchX;
}
});
canvas.addEventListener('touchend', function(e) {
touchX = null;
});
// حلقه بازی
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
updateBall();
drawPlayer();
updatePlayer();
// برخورد توپ با بازیکن
if(ball.x + ball.radius > player.x && ball.x - ball.radius < player.x + player.width && ball.y + ball.radius > player.y) {
ball.dy = -ball.dy;
ball.y = player.y - ball.radius;
}
// برخورد با کف صفحه
if(ball.y + ball.radius > canvas.height) {
alert('بازی تمام شد!');
document.location.reload();
}
requestAnimationFrame(gameLoop);
}
// شروع بازی
gameLoop();
// تنظیم مجدد اندازه بوم هنگام تغییر اندازه صفحه
window.addEventListener('resize', function() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
player.y = canvas.height - 30;
});
</script>
</body>
</html>
توضیحات:
- HTML: ساختار اصلی صفحه را تعریف میکند و یک
canvas برای رسم بازی داریم.
- CSS: ظاهر صفحه را تنظیم میکند، از جمله حذف حاشیهها و تنظیم پسزمینه سبز برای زمین فوتبال.
- JavaScript:
- توپ: یک شیء برای توپ تعریف شده که موقعیت و سرعت آن را نگه میدارد. توپ در صفحه حرکت کرده و با دیوارهها و بازیکن برخورد میکند.
- بازیکن: یک مستطیل در پایین صفحه که کاربر میتواند آن را حرکت دهد.
- کنترلها: برای دسکتاپ از کلیدهای چپ و راست و برای موبایل از لمس صفحه برای حرکت بازیکن استفاده میشود.
- حلقه بازی: تابع
gameLoop که به صورت مداوم اجرا شده و بازی را بهروزرسانی میکند.
نحوه اجرا:
- فایل را با نام
index.html ذخیره کنید.
- فایل را در مرورگر موبایل یا دسکتاپ خود باز کنید.
- با استفاده از کلیدهای جهت یا لمس صفحه، بازیکن را حرکت دهید و سعی کنید توپ را از افتادن جلوگیری کنید.
نکته: این یک بازی ساده است و میتوانید آن را با افزودن ویژگیهایی مانند امتیازدهی، مراحل مختلف، موانع و ... توسعه دهید.
امیدوارم این کد برای شما مفید باشد!