Spaces:
Running
Running
File size: 31,142 Bytes
2654fd5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 | <!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>پینگ پنگ جادویی | Magic Pong</title>
<!-- فونت فارسی وزیرمتن -->
<link href="https://cdn.jsdelivr.net/gh/rastikerdar/vazirmatn@v33.003/Vazirmatn-font-face.css" rel="stylesheet" type="text/css" />
<!-- آیکونها -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
:root {
--bg-color: #0f0f13;
--text-color: #ffffff;
--primary-color: #00f2ff; /* آبی نئونی */
--secondary-color: #ff0055; /* قرمز نئونی */
--accent-color: #bc13fe; /* بنفش */
--glass-bg: rgba(255, 255, 255, 0.05);
--font-main: 'Vazirmatn', sans-serif;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
user-select: none; /* جلوگیری از انتخاب متن */
}
body {
font-family: var(--font-main);
background-color: var(--bg-color);
color: var(--text-color);
overflow: hidden; /* جلوگیری از اسکرول */
height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
/* هدر */
header {
position: absolute;
top: 0;
width: 100%;
padding: 1rem 2rem;
display: flex;
justify-content: space-between;
align-items: center;
background: linear-gradient(180deg, rgba(0,0,0,0.8) 0%, rgba(0,0,0,0) 100%);
z-index: 10;
}
.logo {
font-size: 1.5rem;
font-weight: bold;
text-shadow: 0 0 10px var(--primary-color);
display: flex;
align-items: center;
gap: 10px;
}
.logo i {
color: var(--primary-color);
}
.anycoder-link {
font-size: 0.9rem;
color: var(--text-color);
text-decoration: none;
opacity: 0.7;
transition: opacity 0.3s;
border: 1px solid rgba(255,255,255,0.2);
padding: 5px 10px;
border-radius: 20px;
}
.anycoder-link:hover {
opacity: 1;
border-color: var(--primary-color);
box-shadow: 0 0 10px var(--primary-color);
}
/* کانتینر اصلی بازی */
#game-container {
position: relative;
width: 90%;
max-width: 1000px;
aspect-ratio: 16/9;
background: radial-gradient(circle at center, #1a1a24 0%, #000000 100%);
border: 2px solid rgba(255, 255, 255, 0.1);
border-radius: 12px;
box-shadow: 0 0 30px rgba(0, 242, 255, 0.1);
overflow: hidden;
}
canvas {
display: block;
width: 100%;
height: 100%;
}
/* لایه رابط کاربری (امتیاز و منو) */
#ui-layer {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none; /* کلیکها از روی آن رد شوند مگر روی دکمهها */
display: flex;
flex-direction: column;
justify-content: space-between;
padding: 20px;
}
.score-board {
display: flex;
justify-content: center;
gap: 50px;
font-size: 3rem;
font-weight: 900;
text-shadow: 0 0 20px rgba(255, 255, 255, 0.5);
z-index: 5;
}
.score-p1 { color: var(--primary-color); }
.score-p2 { color: var(--secondary-color); }
/* منوی بازی (شروع/توقف/پایان) */
#menu-overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.7);
backdrop-filter: blur(5px);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
pointer-events: auto;
transition: opacity 0.3s;
z-index: 20;
}
#menu-overlay.hidden {
opacity: 0;
pointer-events: none;
}
h1 {
font-size: 3rem;
margin-bottom: 1rem;
background: linear-gradient(45deg, var(--primary-color), var(--accent-color));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
text-shadow: 0 0 30px rgba(188, 19, 254, 0.5);
}
.btn {
background: transparent;
color: var(--text-color);
border: 2px solid var(--primary-color);
padding: 12px 30px;
font-size: 1.2rem;
font-family: var(--font-main);
border-radius: 50px;
cursor: pointer;
margin: 10px;
transition: all 0.3s ease;
box-shadow: 0 0 10px rgba(0, 242, 255, 0.2);
display: flex;
align-items: center;
gap: 10px;
}
.btn:hover {
background: var(--primary-color);
color: #000;
box-shadow: 0 0 25px var(--primary-color);
transform: scale(1.05);
}
.btn-secondary {
border-color: var(--secondary-color);
}
.btn-secondary:hover {
background: var(--secondary-color);
box-shadow: 0 0 25px var(--secondary-color);
}
.controls-info {
margin-top: 20px;
font-size: 0.9rem;
color: #aaa;
text-align: center;
line-height: 1.6;
background: rgba(0,0,0,0.5);
padding: 15px;
border-radius: 10px;
}
.key {
display: inline-block;
background: #333;
padding: 2px 8px;
border-radius: 4px;
border-bottom: 2px solid #111;
margin: 0 3px;
font-family: monospace;
color: #fff;
}
/* پیام وضعیت */
#status-message {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 2rem;
font-weight: bold;
color: #fff;
text-shadow: 0 0 10px var(--accent-color);
opacity: 0;
transition: opacity 0.5s;
pointer-events: none;
}
/* فوتر */
footer {
margin-top: 10px;
font-size: 0.8rem;
color: #666;
text-align: center;
}
/* ریسپانسیو برای موبایل */
@media (max-width: 768px) {
h1 { font-size: 2rem; }
.score-board { font-size: 2rem; gap: 30px; }
.controls-info { display: none; } /* در موبایل کنترلها را مخفی کنیم چون تاچ است */
#game-container { width: 100%; border-radius: 0; border: none; }
.mobile-controls-hint {
display: block;
color: #aaa;
font-size: 0.8rem;
margin-top: 10px;
}
}
</style>
</head>
<body>
<header>
<div class="logo">
<i class="fa-solid fa-gamepad"></i>
<span>پینگ پنگ جادویی</span>
</div>
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" class="anycoder-link">
Built with anycoder <i class="fa-solid fa-external-link-alt" style="font-size: 0.7em;"></i>
</a>
</header>
<main id="game-container">
<canvas id="gameCanvas"></canvas>
<div id="ui-layer">
<div class="score-board">
<span class="score-p1" id="scoreP1">0</span>
<span class="score-p2" id="scoreP2">0</span>
</div>
<div id="status-message"></div>
</div>
<div id="menu-overlay">
<h1 id="menu-title">پینگ پنگ جادویی</h1>
<p id="winner-text" style="margin-bottom: 20px; font-size: 1.2rem; display: none;"></p>
<button class="btn" id="startBtn">
<i class="fa-solid fa-play"></i> شروع بازی
</button>
<button class="btn btn-secondary" id="modeBtn">
<i class="fa-solid fa-users"></i> حالت: تک نفره (با هوش مصنوعی)
</button>
<div class="controls-info">
<p><strong>بازیکن ۱ (چپ):</strong> <span class="key">W</span> بالا <span class="key">S</span> پایین</p>
<p><strong>بازیکن ۲ (راست):</strong> <span class="key">↑</span> بالا <span class="key">↓</span> پایین</p>
<p><strong>قابلیت جادویی:</strong> توپهای رنگی قدرتهای خاصی دارند!</p>
</div>
</div>
</main>
<div class="mobile-controls-hint" style="display: none;">
برای کنترل در موبایل، صفحه را لمس کنید و بالا و پایین ببرید.
</div>
<footer>
طراحی شده با HTML5 Canvas و JavaScript مدرن
</footer>
<script>
// --- تنظیمات و متغیرهای بازی ---
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreP1El = document.getElementById('scoreP1');
const scoreP2El = document.getElementById('scoreP2');
const menuOverlay = document.getElementById('menu-overlay');
const startBtn = document.getElementById('startBtn');
const modeBtn = document.getElementById('modeBtn');
const menuTitle = document.getElementById('menu-title');
const winnerText = document.getElementById('winner-text');
const statusMsg = document.getElementById('status-message');
let animationId;
let lastTime = 0;
let isGameRunning = false;
let isTwoPlayerMode = false; // پیشفرض تک نفره
// تنظیمات صوتی (سنتزایزر ساده برای جلوگیری از لود فایل)
const AudioContext = window.AudioContext || window.webkitAudioContext;
const audioCtx = new AudioContext();
function playSound(type) {
if (audioCtx.state === 'suspended') audioCtx.resume();
const osc = audioCtx.createOscillator();
const gain = audioCtx.createGain();
osc.connect(gain);
gain.connect(audioCtx.destination);
const now = audioCtx.currentTime;
if (type === 'hit') {
osc.type = 'sine';
osc.frequency.setValueAtTime(400, now);
osc.frequency.exponentialRampToValueAtTime(800, now + 0.1);
gain.gain.setValueAtTime(0.3, now);
gain.gain.exponentialRampToValueAtTime(0.01, now + 0.1);
osc.start(now);
osc.stop(now + 0.1);
} else if (type === 'wall') {
osc.type = 'square';
osc.frequency.setValueAtTime(200, now);
gain.gain.setValueAtTime(0.1, now);
gain.gain.exponentialRampToValueAtTime(0.01, now + 0.05);
osc.start(now);
osc.stop(now + 0.05);
} else if (type === 'score') {
osc.type = 'triangle';
osc.frequency.setValueAtTime(600, now);
osc.frequency.linearRampToValueAtTime(1200, now + 0.2);
gain.gain.setValueAtTime(0.3, now);
gain.gain.linearRampToValueAtTime(0, now + 0.5);
osc.start(now);
osc.stop(now + 0.5);
} else if (type === 'powerup') {
osc.type = 'sine';
osc.frequency.setValueAtTime(800, now);
osc.frequency.linearRampToValueAtTime(1600, now + 0.3);
gain.gain.setValueAtTime(0.2, now);
gain.gain.linearRampToValueAtTime(0, now + 0.3);
osc.start(now);
osc.stop(now + 0.3);
}
}
// --- اشیاء بازی ---
// راکتها
const paddleWidth = 15;
const paddleHeight = 100;
const player1 = {
x: 20,
y: 0, // در init تنظیم میشود
width: paddleWidth,
height: paddleHeight,
color: '#00f2ff',
score: 0,
dy: 0,
speed: 8,
originalHeight: paddleHeight
};
const player2 = {
x: 0, // در init تنظیم میشود
y: 0,
width: paddleWidth,
height: paddleHeight,
color: '#ff0055',
score: 0,
dy: 0,
speed: 8,
originalHeight: paddleHeight
};
// توپ
const ball = {
x: 0,
y: 0,
radius: 8,
speed: 7,
dx: 7,
dy: 7,
color: '#ffffff',
trail: [] // برای افکت دنباله
};
// سیستم ذرات (Particles)
let particles = [];
class Particle {
constructor(x, y, color) {
this.x = x;
this.y = y;
this.color = color;
this.radius = Math.random() * 3 + 1;
this.velocity = {
x: (Math.random() - 0.5) * 8,
y: (Math.random() - 0.5) * 8
};
this.alpha = 1;
this.decay = Math.random() * 0.03 + 0.01;
}
draw() {
ctx.save();
ctx.globalAlpha = this.alpha;
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
ctx.restore();
}
update() {
this.x += this.velocity.x;
this.y += this.velocity.y;
this.alpha -= this.decay;
}
}
// قدرتها (Power-ups)
let powerUps = [];
let powerUpTimer = 0;
const POWERUP_TYPES = [
{ type: 'bigPaddle', color: '#00ff00', label: 'G' }, // بزرگ کردن راکت
{ type: 'smallPaddle', color: '#ffff00', label: 'S' }, // کوچک کردن حریف
{ type: 'fastBall', color: '#ff00ff', label: 'F' } // توپ سریع
];
class PowerUp {
constructor() {
this.x = canvas.width / 2;
this.y = Math.random() * (canvas.height - 60) + 30;
this.radius = 15;
const data = POWERUP_TYPES[Math.floor(Math.random() * POWERUP_TYPES.length)];
this.type = data.type;
this.color = data.color;
this.label = data.label;
this.active = true;
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.shadowBlur = 15;
ctx.shadowColor = this.color;
ctx.fill();
ctx.shadowBlur = 0;
ctx.fillStyle = '#000';
ctx.font = 'bold 12px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(this.label, this.x, this.y);
}
}
// --- توابع کمکی ---
function resizeCanvas() {
// دریافت ابعاد کانتینر
const container = document.getElementById('game-container');
canvas.width = container.clientWidth;
canvas.height = container.clientHeight;
// بازنشانی موقعیتها
resetPositions();
}
function resetPositions() {
player1.y = canvas.height / 2 - player1.height / 2;
player2.x = canvas.width - 20 - paddleWidth;
player2.y = canvas.height / 2 - player2.height / 2;
resetBall();
}
function resetBall() {
ball.x = canvas.width / 2;
ball.y = canvas.height / 2;
ball.speed = 7;
// جهت تصادفی
ball.dx = (Math.random() > 0.5 ? 1 : -1) * ball.speed;
ball.dy = (Math.random() * 2 - 1) * ball.speed;
ball.trail = [];
}
function createExplosion(x, y, color) {
for (let i = 0; i < 15; i++) {
particles.push(new Particle(x, y, color));
}
}
function showMessage(text) {
statusMsg.innerText = text;
statusMsg.style.opacity = 1;
setTimeout(() => {
statusMsg.style.opacity = 0;
}, 1500);
}
// --- منطق بازی ---
function update(deltaTime) {
if (!isGameRunning) return;
// حرکت بازیکن ۱
player1.y += player1.dy;
// محدود کردن به صفحه
if (player1.y < 0) player1.y = 0;
if (player1.y + player1.height > canvas.height) player1.y = canvas.height - player1.height;
// حرکت بازیکن ۲ (یا هوش مصنوعی)
if (isTwoPlayerMode) {
player2.y += player2.dy;
if (player2.y < 0) player2.y = 0;
if (player2.y + player2.height > canvas.height) player2.y = canvas.height - player2.height;
} else {
// هوش مصنوعی ساده
const targetY = ball.y - player2.height / 2;
// واکنشپذیری AI (سرعت کمتر از توپ)
const aiSpeed = 5.5;
if (targetY > player2.y + 10) {
player2.y += aiSpeed;
} else if (targetY < player2.y - 10) {
player2.y -= aiSpeed;
}
// محدود کردن
if (player2.y < 0) player2.y = 0;
if (player2.y + player2.height > canvas.height) player2.y = canvas.height - player2.height;
}
// حرکت توپ
ball.x += ball.dx;
ball.y += ball.dy;
// برخورد با بالا و پایین
if (ball.y - ball.radius < 0 || ball.y + ball.radius > canvas.height) {
ball.dy *= -1;
playSound('wall');
// جلوگیری از گیر کردن در دیوار
if (ball.y - ball.radius < 0) ball.y = ball.radius + 1;
if (ball.y + ball.radius > canvas.height) ball.y = canvas.height - ball.radius - 1;
}
// برخورد با راکتها
let player = (ball.x < canvas.width / 2) ? player1 : player2;
if (collision(ball, player)) {
// نقطه برخورد
let collidePoint = (ball.y - (player.y + player.height / 2));
// نرمالسازی
collidePoint = collidePoint / (player.height / 2);
// زاویه برخورد (حداکثر 45 درجه)
let angleRad = (Math.PI / 4) * collidePoint;
// جهت جدید
let direction = (ball.x < canvas.width / 2) ? 1 : -1;
// افزایش سرعت با هر برخورد
ball.speed += 0.2;
if (ball.speed > 16) ball.speed = 16;
ball.dx = direction * ball.speed * Math.cos(angleRad);
ball.dy = ball.speed * Math.sin(angleRad);
// افکتهای صوتی و بصری
playSound('hit');
createExplosion(ball.x, ball.y, player.color);
// تغییر رنگ توپ به رنگ بازیکنی که به آن برخورد کرده
ball.color = player.color;
}
// امتیازدهی
if (ball.x - ball.radius < 0) {
player2.score++;
scoreP2El.innerText = player2.score;
playSound('score');
checkWin();
resetBall();
} else if (ball.x + ball.radius > canvas.width) {
player1.score++;
scoreP1El.innerText = player1.score;
playSound('score');
checkWin();
resetBall();
}
// مدیریت ذرات
particles.forEach((p, index) => {
if (p.alpha <= 0) {
particles.splice(index, 1);
} else {
p.update();
}
});
// مدیریت قدرتها
handlePowerUps();
// ذخیره دنباله توپ
ball.trail.push({x: ball.x, y: ball.y});
if (ball.trail.length > 10) ball.trail.shift();
}
function collision(b, p) {
p.top = p.y;
p.bottom = p.y + p.height;
p.left = p.x;
p.right = p.x + p.width;
b.top = b.y - b.radius;
b.bottom = b.y + b.radius;
b.left = b.x - b.radius;
b.right = b.x + b.radius;
return p.left < b.right && p.top < b.bottom && p.right > b.left && p.bottom > b.top;
}
function handlePowerUps() {
// تولید قدرت
powerUpTimer++;
if (powerUpTimer > 600) { // هر 10 ثانیه تقریبا
if (Math.random() > 0.5) {
powerUps.push(new PowerUp());
}
powerUpTimer = 0;
}
powerUps.forEach((p, index) => {
// برخورد توپ با قدرت
const dist = Math.hypot(ball.x - p.x, ball.y - p.y);
if (dist < ball.radius + p.radius) {
applyPowerUp(p.type);
playSound('powerup');
createExplosion(p.x, p.y, p.color);
powerUps.splice(index, 1);
}
});
}
function applyPowerUp(type) {
if (type === 'bigPaddle') {
// آخرین بازیکنی که توپ را زده بزرگ میشود (تشخیص از روی رنگ توپ)
if (ball.color === player1.color) {
player1.height = player1.originalHeight * 1.5;
setTimeout(() => player1.height = player1.originalHeight, 5000);
showMessage("قدرت: راکت بزرگ!");
} else {
player2.height = player2.originalHeight * 1.5;
setTimeout(() => player2.height = player2.originalHeight, 5000);
showMessage("قدرت: راکت بزرگ دشمن!");
}
} else if (type === 'smallPaddle') {
// حریف کوچک میشود
if (ball.color === player1.color) {
player2.height = player2.originalHeight * 0.6;
setTimeout(() => player2.height = player2.originalHeight, 5000);
showMessage("جادو: کوچک کردن حریف!");
} else {
player1.height = player1.originalHeight * 0.6;
setTimeout(() => player1.height = player1.originalHeight, 5000);
}
} else if (type === 'fastBall') {
ball.speed *= 1.5;
showMessage("توپ سریع!");
}
}
function checkWin() {
const winScore = 5;
if (player1.score >= winScore) {
gameOver("بازیکن ۱ برنده شد! 🎉");
} else if (player2.score >= winScore) {
gameOver(isTwoPlayerMode ? "بازیکن ۲ برنده شد! 🎉" : "هوش مصنوعی برنده شد! 🤖");
}
}
function gameOver(msg) {
isGameRunning = false;
menuTitle.innerText = "پایان بازی";
winnerText.innerText = msg;
winnerText.style.display = "block";
startBtn.innerHTML = '<i class="fa-solid fa-rotate-right"></i> بازی مجدد';
menuOverlay.classList.remove('hidden');
}
function draw() {
// پاک کردن صفحه با شفافیت برای ایجاد رد حرکت (Motion Blur خفیف)
ctx.fillStyle = 'rgba(15, 15, 19, 0.4)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// خط وسط
ctx.beginPath();
ctx.setLineDash([10, 15]);
ctx.moveTo(canvas.width / 2, 0);
ctx.lineTo(canvas.width / 2, canvas.height);
ctx.strokeStyle = 'rgba(255, 255, 255, 0.1)';
ctx.lineWidth = 2;
ctx.stroke();
ctx.setLineDash([]);
// رسم ذرات
particles.forEach(p => p.draw());
// رسم قدرتها
powerUps.forEach(p => p.draw());
// رسم دنباله توپ
ball.trail.forEach((pos, i) => {
const ratio = i / ball.trail.length;
ctx.beginPath();
ctx.arc(pos.x, pos.y, ball.radius * ratio, 0, Math.PI * 2);
ctx.fillStyle = ball.color;
ctx.globalAlpha = ratio * 0.5;
ctx.fill();
ctx.globalAlpha = 1;
});
// رسم توپ
ctx.beginPath();
ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2);
ctx.fillStyle = ball.color;
ctx.shadowBlur = 15;
ctx.shadowColor = ball.color;
ctx.fill();
ctx.shadowBlur = 0;
// رسم راکتها
drawRect(player1.x, player1.y, player1.width, player1.height, player1.color);
drawRect(player2.x, player2.y, player2.width, player2.height, player2.color);
}
function drawRect(x, y, w, h, color) {
ctx.fillStyle = color;
ctx.shadowBlur = 10;
ctx.shadowColor = color;
// گوشههای گرد
ctx.beginPath();
ctx.roundRect(x, y, w, h, 5);
ctx.fill();
ctx.shadowBlur = 0;
// درخشش داخلی
ctx.fillStyle = "rgba(255,255,255,0.2)";
ctx.fillRect(x, y, w/2, h);
}
function gameLoop(timestamp) {
let deltaTime = timestamp - lastTime;
lastTime = timestamp;
update(deltaTime);
draw();
animationId = requestAnimationFrame(gameLoop);
}
// --- ورودیها ---
window.addEventListener('keydown', (e) => {
if (!isGameRunning) return;
switch(e.key) {
case 'w':
case 'W':
player1.dy = -player1.speed;
break;
case 's':
case 'S':
player1.dy = player1.speed;
break;
case 'ArrowUp':
if (isTwoPlayerMode) player2.dy = -player2.speed;
break;
case 'ArrowDown':
if (isTwoPlayerMode) player2.dy = player2.speed;
break;
}
});
window.addEventListener('keyup', (e) => {
switch(e.key) {
case 'w':
case 'W':
case 's':
case 'S':
player1.dy = 0;
break;
case 'ArrowUp':
case 'ArrowDown':
if (isTwoPlayerMode) player2.dy = 0;
break;
}
});
// کنترل لمسی برای موبایل
canvas.addEventListener('touchmove', (e) => {
if (!isGameRunning) return;
e.preventDefault();
const touch = e.touches[0];
const rect = canvas.getBoundingClientRect();
const relativeY = touch.clientY - rect.top;
// بازیکن ۱ با لمس هر جایی کنترل میشود (سادهسازی برای موبایل)
player1.y = relativeY - player1.height / 2;
// محدود کردن
if (player1.y < 0) player1.y = 0;
if (player1.y + player1.height > canvas.height) player1.y = canvas.height - player1.height;
}, { passive: false });
// دکمههای رابط کاربری
startBtn.addEventListener('click', () => {
// شروع بازی
player1.score = 0;
player2.score = 0;
scoreP1El.innerText = '0';
scoreP2El.innerText = '0';
powerUps = [];
player1.height = player1.originalHeight;
player2.height = player2.originalHeight;
menuOverlay.classList.add('hidden');
isGameRunning = true;
resetPositions();
// اگر صدای مرورگر معلق است، آن را فعال کن
if (audioCtx.state === 'suspended') {
audioCtx.resume();
}
});
modeBtn.addEventListener('click', () => {
isTwoPlayerMode = !isTwoPlayerMode;
if (isTwoPlayerMode) {
modeBtn.innerHTML = '<i class="fa-solid fa-user"></i> حالت: دونفره';
modeBtn.classList.add('btn-secondary');
} else {
modeBtn.innerHTML = '<i class="fa-solid fa-robot"></i> حالت: تک نفره (با هوش مصنوعی)';
modeBtn.classList.remove('btn-secondary');
}
});
window.addEventListener('resize', resizeCanvas);
// --- شروع اولیه ---
resizeCanvas();
requestAnimationFrame(gameLoop);
</script>
</body>
</html> |