ساخت یک بازی shooter پیشرفته مخصوص موبایل با استفاده از HTML5 Canvas، JavaScript و CSS امکانپذیر است. در زیر یک نمونه ساده از چنین بازیای را ارائه میدهم که میتوانید آن را به یک فایل HTML ذخیره کرده و در مرورگر موبایل خود اجرا کنید.
html Copy code
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>بازی Shooter موبایل</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #000;
}
#gameCanvas {
display: block;
background: #111;
}
#score {
position: absolute;
top: 10px;
left: 50%;
transform: translateX(-50%);
color: #fff;
font-family: Arial, sans-serif;
font-size: 20px;
z-index: 1;
}
/* دکمههای کنترل */
.controls {
position: absolute;
bottom: 20px;
width: 100%;
display: flex;
justify-content: space-around;
z-index: 1;
}
.control-button {
width: 60px;
height: 60px;
background: rgba(255, 255, 255, 0.2);
border: none;
border-radius: 50%;
touch-action: none;
}
.control-button:active {
background: rgba(255, 255, 255, 0.4);
}
</style>
</head>
<body>
<div id="score">امتیاز: 0</div>
<canvas id="gameCanvas"></canvas>
<div class="controls">
<button id="left" class="control-button">←</button>
<button id="shoot" class="control-button">•</button>
<button id="right" class="control-button">→</button>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let keys = {};
let bullets = [];
let enemies = [];
let score = 0;
// بازیکن
const player = {
x: canvas.width / 2,
y: canvas.height - 60,
width: 50,
height: 50,
speed: 5,
color: 'white',
draw() {
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
};
// گلولهها
class Bullet {
constructor(x, y) {
this.x = x;
this.y = y;
this.radius = 5;
this.speed = 7;
this.color = 'yellow';
}
update() {
this.y -= this.speed;
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
ctx.closePath();
}
}
// دشمنها
class Enemy {
constructor() {
this.width = 50;
this.height = 50;
this.x = Math.random() * (canvas.width - this.width);
this.y = -this.height;
this.speed = 2 + Math.random() * 3;
this.color = 'red';
}
update() {
this.y += this.speed;
}
draw() {
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
// کنترلهای تاچ
document.getElementById('left').addEventListener('touchstart', () => keys['ArrowLeft'] = true);
document.getElementById('left').addEventListener('touchend', () => keys['ArrowLeft'] = false);
document.getElementById('right').addEventListener('touchstart', () => keys['ArrowRight'] = true);
document.getElementById('right').addEventListener('touchend', () => keys['ArrowRight'] = false);
document.getElementById('shoot').addEventListener('touchstart', shootBullet);
// شلیک گلوله
function shootBullet() {
bullets.push(new Bullet(player.x + player.width / 2, player.y));
}
// ایجاد دشمنها به صورت تصادفی
function spawnEnemies() {
if (Math.random() < 0.02) {
enemies.push(new Enemy());
}
}
// برخورد کردن گلولهها و دشمنها
function checkCollisions() {
bullets.forEach((bullet, bIndex) => {
enemies.forEach((enemy, eIndex) => {
if (
bullet.x > enemy.x &&
bullet.x < enemy.x + enemy.width &&
bullet.y > enemy.y &&
bullet.y < enemy.y + enemy.height
) {
// حذف گلوله و دشمن
bullets.splice(bIndex, 1);
enemies.splice(eIndex, 1);
score++;
scoreElement.textContent = 'امتیاز: ' + score;
}
});
});
// برخورد دشمن با بازیکن
enemies.forEach(enemy => {
if (
player.x < enemy.x + enemy.width &&
player.x + player.width > enemy.x &&
player.y < enemy.y + enemy.height &&
player.y + player.height > enemy.y
) {
endGame();
}
});
}
// پایان بازی
function endGame() {
alert('بازی تمام شد! امتیاز شما: ' + score);
window.location.reload();
}
// حلقه اصلی بازی
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
player.draw();
// حرکت بازیکن
if (keys['ArrowLeft'] && player.x > 0) {
player.x -= player.speed;
}
if (keys['ArrowRight'] && player.x + player.width < canvas.width) {
player.x += player.speed;
}
// بهروزرسانی و نمایش گلولهها
bullets.forEach((bullet, index) => {
bullet.update();
bullet.draw();
// حذف گلولههای خارج از صفحه
if (bullet.y < 0) {
bullets.splice(index, 1);
}
});
// بهروزرسانی و نمایش دشمنها
enemies.forEach((enemy, index) => {
enemy.update();
enemy.draw();
// حذف دشمنهای خارج از صفحه
if (enemy.y > canvas.height) {
enemies.splice(index, 1);
}
});
spawnEnemies();
checkCollisions();
requestAnimationFrame(gameLoop);
}
// شروع بازی
gameLoop();
// جلوگیری از کشیدن صفحه هنگام لمسی
document.body.addEventListener('touchmove', function(e) {
e.preventDefault();
}, { passive: false });
</script>
</body>
</html>