313 lines
8.7 KiB
C#
313 lines
8.7 KiB
C#
|
|
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using System.Collections.Generic;
|
|
|
|
public class GuessGame : MonoBehaviour
|
|
{
|
|
[System.Serializable]
|
|
public class Soal
|
|
{
|
|
public string textSoal; // Teks soal
|
|
public Sprite gambarA; // Gambar untuk opsi A
|
|
public Sprite gambarB; // Gambar untuk opsi B
|
|
public int idJawaban; // 0 untuk A, 1 untuk B
|
|
}
|
|
|
|
public List<Soal> listSoal; // List berisi semua soal
|
|
public Text uiTextSoal; // UI Text untuk menampilkan soal
|
|
public Button buttonA; // UI Button untuk opsi A
|
|
public Button buttonB; // UI Button untuk opsi B
|
|
public Text uiTextSkor; // UI Text untuk menampilkan skor
|
|
public GameObject uiBenar; // UI Image untuk feedback benar
|
|
public GameObject uiSalah; // UI Image untuk feedback salah
|
|
|
|
public GameObject panelGameOver; // Panel untuk Game Over / Final Screen
|
|
public Text uiTextSkorAkhir; // UI Text untuk menampilkan skor akhir
|
|
public GameObject panelSoal; // Panel yang berisi soal dan jawaban
|
|
|
|
public Image uiGambarBintang; // Gambar bintang akhir
|
|
public Sprite star0, star1, star2, star3; // Gambar-gambar bintang
|
|
|
|
public AudioClip audioBenar; // Audio untuk jawaban benar
|
|
public AudioClip audioSalah; // Audio untuk jawaban salah
|
|
private AudioSource audioSource; // Komponen AudioSource
|
|
|
|
private int skor = 0;
|
|
private int indexSoal;
|
|
|
|
void Start()
|
|
{
|
|
// Inisialisasi AudioSource
|
|
audioSource = gameObject.AddComponent<AudioSource>();
|
|
|
|
uiBenar.SetActive(false);
|
|
uiSalah.SetActive(false);
|
|
panelGameOver.SetActive(false);
|
|
TampilkanSoalBaru();
|
|
}
|
|
|
|
void TampilkanSoalBaru()
|
|
{
|
|
if (indexSoal >= listSoal.Count)
|
|
{
|
|
TampilkanGameOver();
|
|
return;
|
|
}
|
|
|
|
uiTextSoal.text = listSoal[indexSoal].textSoal;
|
|
buttonA.image.sprite = listSoal[indexSoal].gambarA;
|
|
buttonB.image.sprite = listSoal[indexSoal].gambarB;
|
|
|
|
buttonA.onClick.RemoveAllListeners();
|
|
buttonA.onClick.AddListener(() => CekJawaban(0));
|
|
|
|
buttonB.onClick.RemoveAllListeners();
|
|
buttonB.onClick.AddListener(() => CekJawaban(1));
|
|
}
|
|
|
|
void CekJawaban(int jawaban)
|
|
{
|
|
if (jawaban == listSoal[indexSoal].idJawaban)
|
|
{
|
|
skor += 10;
|
|
uiBenar.SetActive(true);
|
|
uiSalah.SetActive(false);
|
|
|
|
if (audioBenar != null)
|
|
audioSource.PlayOneShot(audioBenar);
|
|
}
|
|
else
|
|
{
|
|
uiSalah.SetActive(true);
|
|
uiBenar.SetActive(false);
|
|
|
|
if (audioSalah != null)
|
|
audioSource.PlayOneShot(audioSalah);
|
|
}
|
|
|
|
uiTextSkor.text = "Total Skor: " + skor;
|
|
|
|
Invoke("HilangkanFeedback", 1f);
|
|
indexSoal++;
|
|
Invoke("TampilkanSoalBaru", 2f);
|
|
}
|
|
|
|
void HilangkanFeedback()
|
|
{
|
|
uiBenar.SetActive(false);
|
|
uiSalah.SetActive(false);
|
|
}
|
|
|
|
void TampilkanGameOver()
|
|
{
|
|
panelGameOver.SetActive(true);
|
|
uiTextSkorAkhir.text = "Total Skor: " + skor;
|
|
|
|
if (panelSoal != null)
|
|
panelSoal.SetActive(false);
|
|
|
|
buttonA.interactable = false;
|
|
buttonB.interactable = false;
|
|
|
|
// Menampilkan gambar bintang sesuai skor
|
|
if (skor >= 80)
|
|
{
|
|
uiGambarBintang.sprite = star3;
|
|
}
|
|
else if (skor >= 60)
|
|
{
|
|
uiGambarBintang.sprite = star2;
|
|
}
|
|
else if (skor >= 30)
|
|
{
|
|
uiGambarBintang.sprite = star1;
|
|
}
|
|
else
|
|
{
|
|
uiGambarBintang.sprite = star0;
|
|
}
|
|
}
|
|
|
|
public void UlangGame()
|
|
{
|
|
skor = 0;
|
|
indexSoal = 0;
|
|
|
|
panelGameOver.SetActive(false);
|
|
|
|
if (panelSoal != null)
|
|
panelSoal.SetActive(true);
|
|
|
|
buttonA.interactable = true;
|
|
buttonB.interactable = true;
|
|
|
|
// Reset bintang
|
|
uiGambarBintang.sprite = null;
|
|
|
|
TampilkanSoalBaru();
|
|
uiTextSkor.text = "Total Skor: 0";
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// using UnityEngine;
|
|
// using UnityEngine.UI;
|
|
// using System.Collections.Generic;
|
|
|
|
// public class GuessGame : MonoBehaviour
|
|
// {
|
|
// [System.Serializable]
|
|
// public class Soal
|
|
// {
|
|
// public string textSoal; // Teks soal
|
|
// public Sprite gambarA; // Gambar untuk opsi A
|
|
// public Sprite gambarB; // Gambar untuk opsi B
|
|
// public int idJawaban; // 0 untuk A, 1 untuk B
|
|
// }
|
|
|
|
// public List<Soal> listSoal; // List berisi semua soal
|
|
// public Text uiTextSoal; // UI Text untuk menampilkan soal
|
|
// public Button buttonA; // UI Button untuk opsi A
|
|
// public Button buttonB; // UI Button untuk opsi B
|
|
// public Text uiTextSkor; // UI Text untuk menampilkan skor
|
|
// public GameObject uiBenar; // UI Image untuk feedback benar
|
|
// public GameObject uiSalah; // UI Image untuk feedback salah
|
|
|
|
// public GameObject panelGameOver; // Panel untuk Game Over / Final Screen
|
|
// public Text uiTextSkorAkhir; // UI Text untuk menampilkan skor akhir di Game Over
|
|
|
|
// public GameObject panelSoal; // Panel yang berisi soal dan jawaban (untuk dinonaktifkan saat Game Over)
|
|
|
|
// public AudioClip audioBenar; // Audio untuk jawaban benar
|
|
// public AudioClip audioSalah; // Audio untuk jawaban salah
|
|
// private AudioSource audioSource; // Komponen AudioSource
|
|
|
|
// private int skor = 0;
|
|
// private int indexSoal;
|
|
|
|
// void Start()
|
|
// {
|
|
// // Inisialisasi AudioSource
|
|
// audioSource = gameObject.AddComponent<AudioSource>();
|
|
|
|
// uiBenar.SetActive(false);
|
|
// uiSalah.SetActive(false);
|
|
// panelGameOver.SetActive(false); // Sembunyikan panel Game Over saat mulai
|
|
// TampilkanSoalBaru();
|
|
// }
|
|
|
|
// void TampilkanSoalBaru()
|
|
// {
|
|
// // Cek apakah semua soal telah dijawab
|
|
// if (indexSoal >= listSoal.Count)
|
|
// {
|
|
// TampilkanGameOver();
|
|
// return;
|
|
// }
|
|
|
|
// // Set teks soal
|
|
// uiTextSoal.text = listSoal[indexSoal].textSoal;
|
|
|
|
// // Set gambar untuk opsi A dan B
|
|
// buttonA.image.sprite = listSoal[indexSoal].gambarA;
|
|
// buttonB.image.sprite = listSoal[indexSoal].gambarB;
|
|
|
|
// // Set listener untuk button A dan B
|
|
// buttonA.onClick.RemoveAllListeners();
|
|
// buttonA.onClick.AddListener(() => CekJawaban(0)); // 0 untuk A
|
|
|
|
// buttonB.onClick.RemoveAllListeners();
|
|
// buttonB.onClick.AddListener(() => CekJawaban(1)); // 1 untuk B
|
|
// }
|
|
|
|
// void CekJawaban(int jawaban)
|
|
// {
|
|
// if (jawaban == listSoal[indexSoal].idJawaban)
|
|
// {
|
|
// skor += 10;
|
|
// uiBenar.SetActive(true);
|
|
// uiSalah.SetActive(false);
|
|
|
|
// // Mainkan audio benar
|
|
// if (audioBenar != null)
|
|
// {
|
|
// audioSource.PlayOneShot(audioBenar);
|
|
// }
|
|
// }
|
|
// else
|
|
// {
|
|
// uiSalah.SetActive(true);
|
|
// uiBenar.SetActive(false);
|
|
|
|
// // Mainkan audio salah
|
|
// if (audioSalah != null)
|
|
// {
|
|
// audioSource.PlayOneShot(audioSalah);
|
|
// }
|
|
// }
|
|
|
|
// // Update skor
|
|
// uiTextSkor.text = "Total Skor: " + skor;
|
|
|
|
// // Hilangkan feedback setelah 1 detik
|
|
// Invoke("HilangkanFeedback", 1f);
|
|
|
|
// // Lanjut ke soal berikutnya setelah 2 detik
|
|
// indexSoal++; // Pindah ke soal berikutnya
|
|
// Invoke("TampilkanSoalBaru", 2f);
|
|
// }
|
|
|
|
// void HilangkanFeedback()
|
|
// {
|
|
// uiBenar.SetActive(false);
|
|
// uiSalah.SetActive(false);
|
|
// }
|
|
|
|
// void TampilkanGameOver()
|
|
// {
|
|
// // Tampilkan panel Game Over
|
|
// panelGameOver.SetActive(true);
|
|
|
|
// // Tampilkan skor akhir
|
|
// uiTextSkorAkhir.text = "Total Skor: " + skor;
|
|
|
|
// // Nonaktifkan panel soal dan jawaban
|
|
// if (panelSoal != null)
|
|
// {
|
|
// panelSoal.SetActive(false);
|
|
// }
|
|
|
|
// // Nonaktifkan tombol A dan B
|
|
// buttonA.interactable = false;
|
|
// buttonB.interactable = false;
|
|
// }
|
|
|
|
// // Fungsi untuk mengulang game
|
|
// public void UlangGame()
|
|
// {
|
|
// // Reset skor dan index soal
|
|
// skor = 0;
|
|
// indexSoal = 0;
|
|
|
|
// // Sembunyikan panel Game Over
|
|
// panelGameOver.SetActive(false);
|
|
|
|
// // Aktifkan kembali panel soal dan jawaban
|
|
// if (panelSoal != null)
|
|
// {
|
|
// panelSoal.SetActive(true);
|
|
// }
|
|
|
|
// // Aktifkan tombol A dan B
|
|
// buttonA.interactable = true;
|
|
// buttonB.interactable = true;
|
|
|
|
// // Tampilkan soal baru
|
|
// TampilkanSoalBaru();
|
|
// }
|
|
// }
|
|
|