بتل کد Battle Codeدارد باز می‌شود…

جلسه‌ی 2: شکل‌ها و رنگ‌ها Shapes

با قاب، دایره و متن نقاشی می‌کنی: از چند شکل ساده یک شخصیت می‌سازی و اسم بازی‌ات را روی صحنه می‌نویسی.

در این جلسه یاد می‌گیری

درس

fillRect مستطیل را پر می‌کند. برادرش strokeRect فقط دورِ مستطیل را می‌کشد، مثل قاب عکس، و وسطش خالی می‌ماند. رنگ قاب را strokeStyle می‌گوید (نه fillStyle!) و کلفتی خطش را lineWidth.

<script>
  const ctx = salam.canvas(360, 480);
  ctx.fillStyle = "skyblue";
  ctx.fillRect(0, 0, 360, 480);
  ctx.strokeStyle = "navy";
  ctx.lineWidth = 8;
  ctx.strokeRect(60, 100, 240, 180);
</script>

دایره سه قدم دارد: beginPath() یعنی «یک شکل تازه شروع کن»، arc(x, y, r, 0, Math.PI * 2) دایره را می‌سازد و fill() رنگش می‌کند. اینجا x و y وسطِ دایره‌اند، نه گوشه‌اش، و r شعاع است: فاصله‌ی وسط تا لبه.

<script>
  const ctx = salam.canvas(360, 480);
  ctx.fillStyle = "skyblue";
  ctx.fillRect(0, 0, 360, 480);
  ctx.fillStyle = "gold";
  ctx.beginPath();
  ctx.arc(280, 80, 40, 0, Math.PI * 2);
  ctx.fill();
</script>

دو عدد آخرِ arc می‌گویند دایره از کجا شروع شود و تا کجا برود. 0 تا Math.PI * 2 یعنی یک دور کامل. اگر فقط تا Math.PI بروی، نصف دایره می‌کشی (نیمه‌ی پایینی!). به جای fill() هم می‌توانی stroke() بنویسی تا فقط دورِ دایره کشیده شود، مثل یک حلقه.

<script>
  const ctx = salam.canvas(360, 480);
  ctx.fillStyle = "skyblue";
  ctx.fillRect(0, 0, 360, 480);

  ctx.fillStyle = "tomato";
  ctx.beginPath();
  ctx.arc(100, 200, 50, 0, Math.PI);  // only half
  ctx.fill();

  ctx.strokeStyle = "navy";
  ctx.lineWidth = 6;
  ctx.beginPath();
  ctx.arc(250, 200, 50, 0, Math.PI * 2);
  ctx.stroke();  // only the ring
</script>

برای نوشتن روی بوم، اول با font اندازه و شکل حروف را بگو؛ مثلاً "bold 36px sans-serif" یعنی حروف کلفت و ۳۶ پیکسلی. بعد بنویس fillText(متن, x, y). رنگ متن هم همان fillStyle است. حواست باشد: y خطِ زیرِ حروف است، نه بالایشان.

<script>
  const ctx = salam.canvas(360, 480);
  ctx.fillStyle = "skyblue";
  ctx.fillRect(0, 0, 360, 480);
  ctx.fillStyle = "white";
  ctx.font = "bold 36px sans-serif";
  ctx.fillText("Star Catcher", 40, 80);
</script>

می‌خواهی متن درست وسط باشد؟ textAlign = "center" بگذار؛ از آن به بعد x وسطِ متن است. پس با x = 180 متن، هر قدر هم بلند باشد، وسط بوم می‌نشیند. متن می‌تواند از یک متغیر هم بیاید، و با + می‌شود متن و عدد را به هم چسباند.

<script>
  const ctx = salam.canvas(360, 480);
  ctx.fillStyle = "navy";
  ctx.fillRect(0, 0, 360, 480);

  const title = "Star Catcher";
  let score = 0;
  ctx.fillStyle = "white";
  ctx.textAlign = "center";
  ctx.font = "bold 40px sans-serif";
  ctx.fillText(title, 180, 200);
  ctx.font = "24px sans-serif";
  ctx.fillText("Score: " + score, 180, 260);
</script>

حالا همه را کنار هم بگذار! این ربات از دو مستطیل، سه دایره و یک قاب ساخته شده. اول شکل‌های بزرگ را بکش، بعد جزئیات را رویشان: چشم روی سر، نه سر روی چشم!

<script>
  const ctx = salam.canvas(360, 480);
  ctx.fillStyle = "skyblue";
  ctx.fillRect(0, 0, 360, 480);

  ctx.fillStyle = "silver";
  ctx.fillRect(130, 110, 100, 80);     // head
  ctx.fillStyle = "steelblue";
  ctx.fillRect(110, 200, 140, 150);    // body
  ctx.strokeStyle = "navy";
  ctx.lineWidth = 6;
  ctx.strokeRect(110, 200, 140, 150);  // frame

  ctx.fillStyle = "cyan";
  ctx.beginPath();
  ctx.arc(155, 145, 12, 0, Math.PI * 2);
  ctx.fill();
  ctx.beginPath();
  ctx.arc(205, 145, 12, 0, Math.PI * 2);
  ctx.fill();

  ctx.fillStyle = "red";
  ctx.beginPath();
  ctx.arc(180, 95, 10, 0, Math.PI * 2);  // antenna
  ctx.fill();
</script>

اشتباه‌های رایج: fill() را یادت برود و دایره‌ای نبینی؛ beginPath() را جا بیندازی و دایره‌ی قبلی هم رنگِ تازه بگیرد؛ یا برای قاب fillStyle را عوض کنی به جای strokeStyle. در کد زیر چرا خورشید دیده نمی‌شود؟ درستش کن.

<script>
  const ctx = salam.canvas(360, 480);
  ctx.fillStyle = "skyblue";
  ctx.fillRect(0, 0, 360, 480);
  ctx.fillStyle = "gold";
  ctx.beginPath();
  ctx.arc(280, 80, 40, 0, Math.PI * 2);
  // something is missing here...
</script>

همین در پایتون: در Pygame Zero دایره را با screen.draw.filled_circle((x, y), r, رنگ) می‌کشی؛ اینجا هم (x, y) وسط دایره است. قاب screen.draw.rect است و متن screen.draw.text، که با center= وسط می‌نشیند. (دوره‌ی «بازی‌سازی با پایتون»، جلسه‌ی ۲)

import pgzrun

WIDTH = 360
HEIGHT = 480

def draw():
    screen.fill("skyblue")
    screen.draw.filled_circle((280, 80), 40, "gold")
    screen.draw.rect(Rect((60, 100), (240, 180)), "navy")
    screen.draw.text("Star Catcher", center=(180, 400), fontsize=40, color="white")

pgzrun.go()

مثال‌های این جلسه

چالش‌ها

  1. Sunny Day (آسان)
  2. Picture Frame (آسان)
  3. The Game's Name (آسان)
  4. Happy Face (متوسط)
  5. Half a Sun (متوسط)
  6. Score Board (متوسط)
  7. Target (سخت)
  8. Robot Friend (سخت)

بیشتر بخوان

← جلسه‌ی 1: صحنه‌ی بازی جلسه‌ی 3: عکس و شخصیت →