جواب «Play Again» چالش 5 از جلسهی 11
سؤال
بازی «Game Over» میشود، ولی بعدش هیچ راهی برای دوباره بازی کردن نیست.
در salam.tap، اگر state برابر "over" است: توپ را به بالا برگردان (ball.y = 100) و state را "play" کن. از else if استفاده کن.
راهنما
if (state === "start") { state = "play"; } else if (state === "over") { ball.y = 100; state = "play"; }
یک جواب درست
<script>
const ctx = salam.canvas(360, 480);
const ball = { x: 170, y: 100, w: 20, h: 20 };
let state = "start";
salam.tap(function (x, y) {
if (state === "start") {
state = "play";
} else if (state === "over") {
ball.y = 100;
state = "play";
}
});
salam.loop(function (dt) {
if (state === "play") {
ball.y = ball.y + 240 * dt;
if (ball.y > 480) state = "over";
}
ctx.fillStyle = "#1b1440";
ctx.fillRect(0, 0, 360, 480);
ctx.fillStyle = "white";
ctx.font = "28px sans-serif";
if (state === "start") {
ctx.fillText("Tap to start", 100, 240);
}
if (state === "play") {
ctx.fillRect(ball.x, ball.y, ball.w, ball.h);
}
if (state === "over") {
ctx.fillText("Game Over", 100, 220);
ctx.fillText("Tap to play again", 70, 270);
}
});
</script>
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن