מהר יותר ויותר
שיעור 13 מתוך 14 בקורס משחק Unity: נחש בתלת־ממד של Coddy.
המשחק Snake נעשה קשה יותר ככל שהוא גדל: כל מזון מקצר מעט את משך הטיק, ולכן הנחש מתקדם מהר יותר. חסם תחתון שומר על המשחקיות: משך הטיק הקצר ביותר שאליו הוא יכול להגיע.
Mathf.Max מחזירה את הגדול מבין שני מספרים, ולכן היא משמשת כחסם תחתון: Mathf.Max(value, floor) לעולם לא תהיה קטנה מ-floor. צריח שיורה מהר יותר עם כל שדרוג, אך לעולם לא יותר מפעם אחת בכל 0.2 שניות:
cooldown = Mathf.Max(cooldown - 0.05f, 0.2f);Mathf.Min עושה את ההפך ומשמשת כחסם עליון. קל להתבלבל ביניהן: עם Min, זמן ההמתנה שלמעלה היה יורד ל-0.2 כבר בשדרוג הראשון. הטיימר שב-Update() קורא את tick בכל פריים, ולכן טיק קצר יותר מתחיל להיספר מהצעד הבא ואילך.
אתגר
קשה- הוסף ל־
Snakeאתpublic float speedUp = 0.005f;ואתpublic float minTick = 0.08f;. - כשהנחש אוכל, קצר את
tickב־speedUp, אך לעולם אל תקצר אותו מתחת ל־minTick.
לחץ על Play ואכול: הנחש מאיץ. לאחר מכן לחץ על Check.
נסו בעצמכם
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Snake : MonoBehaviour
{
public float tick = 0.15f; // שניות בין שני צעדים
public int edge = 7; // תאי הלוח משתרעים מ־-edge עד edge
public bool alive = true;
public List<Transform> tail = new List<Transform>();
public Transform segmentPrefab;
private float timer;
private Vector3 direction = Vector3.right; // הכיוון שבו הראש נע בצעד האחרון שלו
private Vector3 nextDirection = Vector3.right; // הפנייה הממתינה לצעד הבא
void Update()
{
if (!alive) return;
Keyboard keyboard = Keyboard.current;
if (keyboard != null)
{
if (keyboard.upArrowKey.wasPressedThisFrame || keyboard.wKey.wasPressedThisFrame) Turn(Vector3.forward);
if (keyboard.downArrowKey.wasPressedThisFrame || keyboard.sKey.wasPressedThisFrame) Turn(Vector3.back);
if (keyboard.leftArrowKey.wasPressedThisFrame || keyboard.aKey.wasPressedThisFrame) Turn(Vector3.left);
if (keyboard.rightArrowKey.wasPressedThisFrame || keyboard.dKey.wasPressedThisFrame) Turn(Vector3.right);
}
timer += Time.deltaTime;
if (timer >= tick)
{
timer -= tick;
Step();
}
}
void Turn(Vector3 newDirection)
{
if (newDirection != -direction)
{
nextDirection = newDirection;
}
}
void Step()
{
direction = nextDirection;
Vector3 next = transform.position + direction;
if (Mathf.Abs(next.x) > edge || Mathf.Abs(next.z) > edge)
{
Die();
return;
}
for (int i = 0; i < tail.Count - 1; i++)
{
if (tail[i].position == next)
{
Die();
return;
}
}
Vector3 tailEnd = tail.Count > 0 ? tail[tail.Count - 1].position : transform.position;
for (int i = tail.Count - 1; i > 0; i--)
{
tail[i].position = tail[i - 1].position;
}
if (tail.Count > 0)
{
tail[0].position = transform.position;
}
transform.position = next;
if (GameManager.Instance.IsFood(next))
{
tail.Add(Instantiate(segmentPrefab, tailEnd, Quaternion.identity));
GameManager.Instance.Eat();
}
}
public bool Occupies(Vector3 cell)
{
if (transform.position == cell) return true;
foreach (Transform segment in tail)
{
if (segment.position == cell) return true;
}
return false;
}
void Die()
{
alive = false;
GameManager.Instance.GameOver();
}
}
השיעור הזה כולל חידון קצר. התחילו את השיעור כדי לענות עליו ולעקוב אחרי ההתקדמות.