🛠️ مشروع القسم: امسك النجمة
القسم 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 تتحوّل النجمة إلى 🏆.
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.

