انتقل للمحتوى
Robo EgyptRobo Egypt
☰ فهرس الكورس
روبو ويب: الصفحة الأولى
0. جولة في المختبر1
1. HTML: محتوى الصفحة7
2. CSS: شكل الصفحة6
3. JavaScript: الحركة7
4. المشروع: قائمة المهام3
5. CSS المحترفين: ظلال وحركة5
6. المصفوفات وبناء الصفحة بالكود3
7. الأحداث والنماذج3
8. الكائنات والوقت3
9. Canvas: الرسم والألعاب3
10. المشروع النهائي: لعبة الذاكرة3

🛠️ مشروع القسم: امسك النجمة

القسم 3: JavaScript: الحركةالدرس 3.6 · ⏱ 12 دقائق
🎯 هدف المشروع
لعبة: كلما ضغطت على النجمة تزيد النقاط 1، وتقفز النجمة إلى مكان عشوائي.
HTML
<p>Score: <b id="score">0</b></p>
<div id="star">⭐</div>
CSS
body { font-family: Tahoma; margin: 0; height: 100vh; background: #1E1B4B; color: white; overflow: hidden; }
p { font-size: 22px; padding: 10px 16px; margin: 0; }
#star { position: absolute; left: 100px; top: 100px; font-size: 54px; cursor: pointer; }
JavaScript
let score = 0;
const star = document.getElementById("star");

star.onclick = function () {
  score = score + 1;
  document.getElementById("score").textContent = score;
  star.style.left = Math.random() * 80 + "%";
  star.style.top = Math.random() * 80 + "%";
};
🖥️ الصفحة في روبو ويب:الصفحة في روبو ويب
💡 نصيحة
Math.random() رقم عشوائي من 0 إلى 1، فإذا ضربناه في 80 صار من 0 إلى 80.

📌 مثال آخر: امسك النجمة: الفوز عند 5

عندما تصل النقاط إلى 5 تتحوّل النجمة إلى 🏆.

🌐 امسك النجمة: الفوز عند 5
HTML
<p>النقاط: <b id="score">0</b></p>
<div id="star">⭐</div>
CSS
body { font-family: Tahoma, sans-serif; margin: 0; height: 100vh; background: #1E1B4B; color: white; overflow: hidden; }
p { font-size: 22px; padding: 10px 16px; margin: 0; }
#star { position: absolute; left: 100px; top: 100px; font-size: 54px; cursor: pointer; }
JavaScript
let score = 0;
const star = document.getElementById("star");

star.onclick = function () {
  score++;
  document.getElementById("score").textContent = score;
  if (score === 5) star.textContent = "🏆";
  star.style.left = Math.random() * 80 + "%";
  star.style.top = Math.random() * 80 + "%";
};
🖥️ الصفحة في روبو ويب:الصفحة في روبو ويب
✍️ طبّق بنفسك
امسك النجمة.
✍️ طبّق هنا في الدرسأو افتح المعمل في صفحة جديدة ↗

🎯 تحدَّ نفسك

سهل

غيّر النجمة إلى 🦋 والخلفية إلى أخضر غامق.

👀 اعرض الحل
🌐 الحل
HTML
<p>النقاط: <b id="score">0</b></p>
<div id="star">🦋</div>
CSS
body { font-family: Tahoma, sans-serif; margin: 0; height: 100vh; background: #14532D; color: white; overflow: hidden; }
p { font-size: 22px; padding: 10px 16px; margin: 0; }
#star { position: absolute; left: 100px; top: 100px; font-size: 54px; cursor: pointer; }
JavaScript
let score = 0;
const star = document.getElementById("star");

star.onclick = function () {
  score++;
  document.getElementById("score").textContent = score;
  star.style.left = Math.random() * 80 + "%";
  star.style.top = Math.random() * 80 + "%";
};
🖥️ الصفحة في روبو ويب:الصفحة في روبو ويب

النتيجة: فراشة على أخضر.

متوسط

كل ضغطة تجعل النجمة أصغر قليلًا.

👀 اعرض الحل
🌐 الحل
HTML
<p>النقاط: <b id="score">0</b></p>
<div id="star">⭐</div>
CSS
body { font-family: Tahoma, sans-serif; margin: 0; height: 100vh; background: #1E1B4B; color: white; overflow: hidden; }
p { font-size: 22px; padding: 10px 16px; margin: 0; }
#star { position: absolute; left: 100px; top: 100px; font-size: 54px; cursor: pointer; }
JavaScript
let score = 0;
let size = 54;
const star = document.getElementById("star");

star.onclick = function () {
  score++;
  document.getElementById("score").textContent = score;
  size = size - 6;
  star.style.fontSize = size + "px";
  star.style.left = Math.random() * 80 + "%";
  star.style.top = Math.random() * 80 + "%";
};
🖥️ الصفحة في روبو ويب:الصفحة في روبو ويب

النتيجة: أصغر بعد كل ضغطة.

صعب

الضغط على الخلفية (خارج النجمة) يُنقص نقطة.

👀 اعرض الحل
🌐 الحل
HTML
<p>النقاط: <b id="score">0</b></p>
<div id="star">⭐</div>
CSS
body { font-family: Tahoma, sans-serif; margin: 0; height: 100vh; background: #1E1B4B; color: white; overflow: hidden; }
p { font-size: 22px; padding: 10px 16px; margin: 0; }
#star { position: absolute; left: 100px; top: 100px; font-size: 54px; cursor: pointer; }
JavaScript
let score = 0;
const star = document.getElementById("star");
function show() { document.getElementById("score").textContent = score; }

star.onclick = function (e) {
  e.stopPropagation();   // so the page under it does not count this click
  score++;
  show();
  star.style.left = Math.random() * 80 + "%";
  star.style.top = Math.random() * 80 + "%";
};
document.body.onclick = function () {
  score--;
  show();
};
🖥️ الصفحة في روبو ويب:الصفحة في روبو ويب

النتيجة: 3 نجوم وضغطة خطأ: 2.

أنهيت الدرس؟ علّم عليه ليُحسب تقدّمك.