Điểm cao nhất
Bài học 19 trên 21 trong khóa học Trò chơi Unity: đường đua trên trời của Coddy.
Điểm cao nhất tạo mục tiêu để bạn vượt qua trong mỗi lượt chơi. PlayerPrefs lưu các giá trị nhỏ giữa những lần chơi, theo một tên:
int best = PlayerPrefs.GetInt("BestScore", 0);
PlayerPrefs.SetInt("BestScore", best);Đối số thứ hai của GetInt là giá trị sẽ dùng khi chưa có gì được lưu. Khi một lượt chơi kết thúc, hãy so sánh điểm của lượt đó với điểm cao nhất: nếu cao hơn, điểm sẽ được lưu và ăn mừng; nếu thấp hơn, điểm sẽ hiển thị bên cạnh điểm cao nhất.
Thử thách
DễLưu điểm cao nhất giữa các lượt chơi.
- Trong
GameOver, đọc điểm cao nhất. - Nếu
Score()cao hơn, hãy lưu điểm đó và tạo dòng"New best: " + best. - Nếu không, dòng đó là
"Score: " + Score() + "\nBest: " + best. - Đặt
messageText.text = "Game over\n" + line;.
Nhấn Chơi và va chạm hai lần: thông báo lần thứ hai hiển thị điểm của bạn và điểm cao nhất. Sau đó nhấn Kiểm tra.
Tự làm thử
using UnityEngine;
public class Runner : MonoBehaviour
{
public float laneWidth = 2.5f;
public float laneSpeed = 16f;
public float jumpHeight = 1.6f;
public float gravity = -40f;
public float slideTime = 0.7f;
public float magnetTime = 8f;
public int lane;
public float magnet;
public bool shield;
private CharacterController controller;
private GameManager game;
private Renderer body;
private float fallSpeed;
private float slide;
private Color color;
void Start()
{
controller = GetComponent<CharacterController>();
game = FindFirstObjectByType<GameManager>();
body = GetComponent<Renderer>();
color = body.material.color;
}
void Update()
{
if (!game.over)
{
if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)) lane = Mathf.Max(-1, lane - 1);
if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow)) lane = Mathf.Min(1, lane + 1);
bool jump = Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Space);
if (jump && controller.isGrounded)
{
fallSpeed = Mathf.Sqrt(jumpHeight * -2f * gravity);
StopSlide();
}
if ((Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow)) && controller.isGrounded)
{
slide = slideTime;
Resize(0.5f);
}
}
if (slide > 0f)
{
slide -= Time.deltaTime;
if (slide <= 0f) StopSlide();
}
if (magnet > 0f) magnet -= Time.deltaTime;
if (controller.isGrounded && fallSpeed < 0f) fallSpeed = -2f;
fallSpeed += gravity * Time.deltaTime;
float targetX = lane * laneWidth;
float x = Mathf.MoveTowards(transform.position.x, targetX, laneSpeed * Time.deltaTime);
Vector3 move = new Vector3(x - transform.position.x, fallSpeed * Time.deltaTime, 0f);
controller.Move(move);
body.material.color = shield ? new Color(0.3f, 0.9f, 1f) : color;
ShowPower();
}
void StopSlide()
{
if (transform.localScale.y < 1f) Resize(1f);
slide = 0f;
}
void Resize(float height)
{
controller.enabled = false;
transform.position += Vector3.up * (height - transform.localScale.y);
transform.localScale = new Vector3(1f, height, 1f);
controller.enabled = true;
}
void ShowPower()
{
string text = "";
if (magnet > 0f) text = "Magnet " + Mathf.CeilToInt(magnet);
if (shield) text += (text == "" ? "" : " ") + "Shield";
game.powerText.text = text == "" ? " " : text;
}
void OnTriggerEnter(Collider other)
{
if (game.over) return;
if (other.CompareTag("Obstacle"))
{
if (shield)
{
shield = false;
Destroy(other.gameObject);
return;
}
game.GameOver();
}
else if (other.CompareTag("Coin"))
{
game.AddCoin();
Destroy(other.gameObject);
}
else if (other.CompareTag("Magnet"))
{
magnet = magnetTime;
Destroy(other.gameObject);
}
else if (other.CompareTag("Shield"))
{
shield = true;
Destroy(other.gameObject);
}
}
}
Tất cả bài học trong Trò chơi Unity: đường đua trên trời
Tự luyện tập: Playground Unity