0% found this document useful (0 votes)
8 views

Level1 Unity Game

lfkbndflbnkldfnbl

Uploaded by

pavankalyan4292
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Level1 Unity Game

lfkbndflbnkldfnbl

Uploaded by

pavankalyan4292
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 14

// using UnityEngine;

// using UnityEngine.UI;
// using TMPro;
// using System.Collections;
// using UnityEngine.EventSystems;

// public class SubtractionGame : MonoBehaviour


// {
// public GameObject level1Panel;
// public GameObject gamePanel;
// public GameObject endPanel;
// public GameObject incorrectAnswerModal;

// public TMP_Text randomNum1;


// public TMP_Text randomNum2;
// public TMP_InputField userInput;
// public TMP_Text scoreText;
// public TMP_Text timerText;
// public Image feedbackImage;
// public TMP_Text correctAnswerText;

// public TMP_Text finalScoreText;


// public TMP_Text finalTimeText;

// public Slider timeSlider;


// public float maxTime = 60f;

// public Transform carTransform;


// public float carMoveSpeed = 1f;
// public Slider carSlider;
// private int maxQuestions = 20;

// private int num1;


// private int num2;
// private int score = 0;
// private float timer = 0;
// private bool gameRunning = false;
// private float timeLeft;
// private int correctAnswers = 0;
// private int totalQuestions = 0;
// private int incorrectAnswers = 0;

// public Image correctAnswerImage;


// public Image wrongAnswerImage;

// void Start()
// {
// HideAllPanels();
// level1Panel.SetActive(true);
// timeLeft = maxTime;
// correctAnswerText.text = "";
// feedbackImage.color = Color.clear;
// timeSlider.maxValue = maxTime;
// carSlider.maxValue = maxQuestions;
// carSlider.value = 0;
// userInput.caretBlinkRate = 0.85f;
// userInput.Select();
// correctAnswerImage.gameObject.SetActive(false);
// wrongAnswerImage.gameObject.SetActive(false);
// }

// public void OnStartButtonClick()


// {
// HideAllPanels();
// gamePanel.SetActive(true);
// gameRunning = true;
// timer = 0;
// score = 0;
// correctAnswers = 0;
// totalQuestions = 0;
// incorrectAnswers = 0;
// GenerateRandomNumbers();
// FocusInputField();
// }

// void Update()
// {
// if (gameRunning)
// {
// timer += Time.deltaTime;
// timerText.text = "Time: " + Mathf.FloorToInt(timer).ToString();

// if (timeLeft > 0)
// {
// timeLeft -= Time.deltaTime;
// timeSlider.value = timeLeft;
// }
// else
// {
// GameOver();
// }

// carTransform.position = Vector3.Lerp(carTransform.position, new


Vector3(correctAnswers * 2f, carTransform.position.y, carTransform.position.z),
Time.deltaTime * carMoveSpeed);

// if (Input.GetKeyDown(KeyCode.Return))
// {
// OnSubmitButtonClick();
// }
// }
// }

// void GenerateRandomNumbers()
// {
// if (totalQuestions >= maxQuestions)
// {
// GameOver();
// return;
// }

// num1 = Random.Range(1, 20);


// int maxDifference = 5;
// num2 = Random.Range(Mathf.Max(0, num1 - maxDifference), num1);

// randomNum1.text = num1.ToString();
// randomNum2.text = num2.ToString();
// feedbackImage.color = Color.clear;
// correctAnswerText.text = "";
// userInput.text = "";
// FocusInputField();

// correctAnswerImage.gameObject.SetActive(false);
// wrongAnswerImage.gameObject.SetActive(false);
// }

// private void FocusInputField()


// {
// // Clear any currently selected object in the event system
// EventSystem.current.SetSelectedGameObject(null);

// // Set the input field as the currently selected object


// EventSystem.current.SetSelectedGameObject(userInput.gameObject);

// // On mobile platforms, open the native keyboard


// #if UNITY_IOS || UNITY_ANDROID
// TouchScreenKeyboard.Open("", TouchScreenKeyboardType.NumberPad);
// #endif
// }

// public void OnSubmitButtonClick()


// {
// if (string.IsNullOrEmpty(userInput.text))
// {
// correctAnswerText.text = "Please enter a number.";
// return;
// }

// int userAnswer;
// if (int.TryParse(userInput.text, out userAnswer))
// {
// int correctAnswer = num1 - num2;

// if (userAnswer == correctAnswer)
// {
// correctAnswerImage.gameObject.SetActive(true);
// wrongAnswerImage.gameObject.SetActive(false);
// score++;
// correctAnswers++;
// carSlider.value = correctAnswers;
// correctAnswerText.text = "";
// }
// else
// {
// incorrectAnswers++;
// correctAnswerText.text = $"<color=#364951>The correct answer
is:</color>\n<color=#97de14>{num1} - {num2} = {correctAnswer}</color>";
// wrongAnswerImage.gameObject.SetActive(true);
// correctAnswerImage.gameObject.SetActive(false);
// StartCoroutine(ShowCorrectAnswerAndNextQuestion(1.5f));

// if (incorrectAnswers >= 10)


// {
// ShowIncorrectAnswerModal();
// return;
// }
// return;
// }

// scoreText.text = "Score: " + score.ToString();


// totalQuestions++;
// StartCoroutine(DelayNextQuestion(0.5f));
// }
// else
// {
// feedbackImage.color = Color.yellow;
// correctAnswerText.text = "Please enter a valid number.";
// }
// }

// private IEnumerator DelayNextQuestion(float delayDuration)


// {
// yield return new WaitForSeconds(delayDuration);
// GenerateRandomNumbers();
// FocusInputField();
// }

// private IEnumerator HideFeedbackImages(float duration)


// {
// yield return new WaitForSeconds(duration);
// correctAnswerImage.gameObject.SetActive(false);
// wrongAnswerImage.gameObject.SetActive(false);
// }

// private IEnumerator ShowCorrectAnswerAndNextQuestion(float displayDuration)


// {
// correctAnswerText.text = $"<color=#364951>The correct answer
is:</color>\n<color=#97de14>{num1} - {num2} = {(num1 - num2).ToString()}</color>";
// yield return new WaitForSeconds(displayDuration);
// GenerateRandomNumbers();
// FocusInputField();
// }

// private void ShowIncorrectAnswerModal()


// {
// gameRunning = false;
// HideAllPanels();
// incorrectAnswerModal.SetActive(true);
// }

// public void OnPlayAgainButtonClick()


// {
// incorrectAnswers = 0;
// incorrectAnswerModal.SetActive(false);
// Start();
// }

// public void OnQuitButtonClick()


// {
// Application.Quit();
// }

// public void GameOver()


// {
// gameRunning = false;
// HideAllPanels();
// endPanel.SetActive(true);
// finalScoreText.text = "Final Score: " + score;
// finalTimeText.text = "Final Time: " + Mathf.FloorToInt(timer).ToString()
+ " seconds";
// }

// // Method to hide all panels


// private void HideAllPanels()
// {
// level1Panel.SetActive(false);
// gamePanel.SetActive(false);
// endPanel.SetActive(false);
// incorrectAnswerModal.SetActive(false);
// }
// }

// using UnityEngine;
// using UnityEngine.UI;
// using TMPro;
// using System.Collections;
// using UnityEngine.EventSystems;

// public class SubtractionGame : MonoBehaviour


// {
// public GameObject level1Panel;
// public GameObject gamePanel;
// public GameObject endPanel;
// public GameObject incorrectAnswerModal;

// public TMP_Text randomNum1;


// public TMP_Text randomNum2;
// public TMP_InputField userInput;
// public TMP_Text scoreText;
// public TMP_Text timerText;
// public Image feedbackImage;
// public TMP_Text correctAnswerText;

// public TMP_Text finalScoreText;


// public TMP_Text finalTimeText;

// public Slider timeSlider;


// public float maxTime = 60f;

// public Transform carTransform;


// public float carMoveSpeed = 1f;
// public Slider carSlider;
// private int maxQuestions = 20;

// private int num1;


// private int num2;
// private int score = 0;
// private float timer = 0;
// private bool gameRunning = false;
// private float timeLeft;
// private int correctAnswers = 0;
// private int totalQuestions = 0;
// private int incorrectAnswers = 0;

// public Image correctAnswerImage;


// public Image wrongAnswerImage;

// private TouchScreenKeyboard keyboard;

// void Start()
// {
// HideAllPanels();
// level1Panel.SetActive(true);
// timeLeft = maxTime;
// correctAnswerText.text = "";
// feedbackImage.color = Color.clear;
// timeSlider.maxValue = maxTime;
// carSlider.maxValue = maxQuestions;
// carSlider.value = 0;
// userInput.caretBlinkRate = 0.85f;
// userInput.Select();
// correctAnswerImage.gameObject.SetActive(false);
// wrongAnswerImage.gameObject.SetActive(false);
// }

// public void OnStartButtonClick()


// {
// HideAllPanels();
// gamePanel.SetActive(true);
// gameRunning = true;
// timer = 0;
// score = 0;
// correctAnswers = 0;
// totalQuestions = 0;
// incorrectAnswers = 0;
// GenerateRandomNumbers();
// FocusInputField();
// }

// void Update()
// {
// if (gameRunning)
// {
// timer += Time.deltaTime;
// timerText.text = "Time: " + Mathf.FloorToInt(timer).ToString();

// if (timeLeft > 0)
// {
// timeLeft -= Time.deltaTime;
// timeSlider.value = timeLeft;
// }
// else
// {
// GameOver();
// }

// carTransform.position = Vector3.Lerp(carTransform.position, new


Vector3(correctAnswers * 2f, carTransform.position.y, carTransform.position.z),
Time.deltaTime * carMoveSpeed);

// if (Input.GetKeyDown(KeyCode.Return))
// {
// OnSubmitButtonClick();
// }
// }
// }

// void GenerateRandomNumbers()
// {
// if (totalQuestions >= maxQuestions)
// {
// GameOver();
// return;
// }

// num1 = Random.Range(1, 20);


// int maxDifference = 5;
// num2 = Random.Range(Mathf.Max(0, num1 - maxDifference), num1);

// randomNum1.text = num1.ToString();
// randomNum2.text = num2.ToString();
// feedbackImage.color = Color.clear;
// correctAnswerText.text = "";
// userInput.text = "";
// FocusInputField();

// correctAnswerImage.gameObject.SetActive(false);
// wrongAnswerImage.gameObject.SetActive(false);
// }

// private void FocusInputField()


// {
// System.Diagnostics.Process.Start("OSK.exe");//tabe
// // Clear the currently selected object in the event system
// EventSystem.current.SetSelectedGameObject(null);

// // Set the input field as the selected object


// EventSystem.current.SetSelectedGameObject(userInput.gameObject);

// // For mobile platforms, open the native keyboard


// // #if UNITY_IOS || UNITY_ANDROID
// // if (keyboard == null || !keyboard.active)
// // {
// // keyboard = TouchScreenKeyboard.Open("",
TouchScreenKeyboardType.NumberPad, false, false, false, false);
// // }
// // #endif

// // #if UNITY_IOS || UNITY_ANDROID


// // if (keyboard == null || !TouchScreenKeyboard.visible)
// // {
// // keyboard = TouchScreenKeyboard.Open("",
TouchScreenKeyboardType.NumberPad);
// // }
// // #endif
// }
// public void OnSubmitButtonClick()
// {
// if (string.IsNullOrEmpty(userInput.text))
// {
// correctAnswerText.text = "Please enter a number.";
// return;
// }

// int userAnswer;
// if (int.TryParse(userInput.text, out userAnswer))
// {
// int correctAnswer = num1 - num2;

// if (userAnswer == correctAnswer)
// {
// correctAnswerImage.gameObject.SetActive(true);
// wrongAnswerImage.gameObject.SetActive(false);
// score++;
// correctAnswers++;
// carSlider.value = correctAnswers;
// correctAnswerText.text = "";
// }
// else
// {
// incorrectAnswers++;
// correctAnswerText.text = $"<color=#364951>The correct answer
is:</color>\n<color=#97de14>{num1} - {num2} = {correctAnswer}</color>";
// wrongAnswerImage.gameObject.SetActive(true);
// correctAnswerImage.gameObject.SetActive(false);
// StartCoroutine(ShowCorrectAnswerAndNextQuestion(1.5f));

// if (incorrectAnswers >= 10)


// {
// ShowIncorrectAnswerModal();
// return;
// }
// return;
// }

// scoreText.text = "Score: " + score.ToString();


// totalQuestions++;
// StartCoroutine(DelayNextQuestion(0.5f));
// }
// else
// {
// feedbackImage.color = Color.yellow;
// correctAnswerText.text = "Please enter a valid number.";
// }
// }

// private IEnumerator DelayNextQuestion(float delayDuration)


// {
// yield return new WaitForSeconds(delayDuration);
// GenerateRandomNumbers();
// FocusInputField();
// }

// private IEnumerator HideFeedbackImages(float duration)


// {
// yield return new WaitForSeconds(duration);
// correctAnswerImage.gameObject.SetActive(false);
// wrongAnswerImage.gameObject.SetActive(false);
// }

// private IEnumerator ShowCorrectAnswerAndNextQuestion(float displayDuration)


// {
// correctAnswerText.text = $"<color=#364951>The correct answer
is:</color>\n<color=#97de14>{num1} - {num2} = {(num1 - num2).ToString()}</color>";
// yield return new WaitForSeconds(displayDuration);
// GenerateRandomNumbers();
// FocusInputField();
// }

// private void ShowIncorrectAnswerModal()


// {
// gameRunning = false;
// HideAllPanels();
// incorrectAnswerModal.SetActive(true);
// }

// public void OnPlayAgainButtonClick()


// {
// incorrectAnswers = 0;
// incorrectAnswerModal.SetActive(false);
// Start();
// }

// public void OnQuitButtonClick()


// {
// Application.Quit();
// }

// public void GameOver()


// {
// gameRunning = false;
// HideAllPanels();
// endPanel.SetActive(true);
// finalScoreText.text = "Final Score: " + score;
// finalTimeText.text = "Final Time: " + Mathf.FloorToInt(timer).ToString()
+ " seconds";
// }

// // Method to hide all panels


// private void HideAllPanels()
// {
// level1Panel.SetActive(false);
// gamePanel.SetActive(false);
// endPanel.SetActive(false);
// incorrectAnswerModal.SetActive(false);
// }
// }

using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Collections;
using UnityEngine.EventSystems;

public class SubtractionGame : MonoBehaviour


{
public GameObject level1Panel;
public GameObject gamePanel;
public GameObject endPanel;
public GameObject incorrectAnswerModal;

public TMP_Text randomNum1;


public TMP_Text randomNum2;
public TMP_InputField userInput;
public TMP_Text scoreText;
public TMP_Text timerText;
public Image feedbackImage;
public TMP_Text correctAnswerText;

public TMP_Text finalScoreText;


public TMP_Text finalTimeText;

public Slider timeSlider;


public float maxTime = 60f;

public Transform carTransform;


public float carMoveSpeed = 1f;
public Slider carSlider;
private int maxQuestions = 20;

private int num1;


private int num2;
private int score = 0;
private float timer = 0;
private bool gameRunning = false;
private float timeLeft;
private int correctAnswers = 0;
private int totalQuestions = 0;
private int incorrectAnswers = 0;

public Image correctAnswerImage;


public Image wrongAnswerImage;

private TouchScreenKeyboard keyboard;

void Start()
{
HideAllPanels();
level1Panel.SetActive(true);
timeLeft = maxTime;
correctAnswerText.text = "";
feedbackImage.color = Color.clear;
timeSlider.maxValue = maxTime;
carSlider.maxValue = maxQuestions;
carSlider.value = 0;
userInput.caretBlinkRate = 0.85f;
userInput.Select();

// Set keyboard for mobile devices


#if UNITY_IOS || UNITY_ANDROID
keyboard = TouchScreenKeyboard.Open("", TouchScreenKeyboardType.NumberPad);
#endif

correctAnswerImage.gameObject.SetActive(false);
wrongAnswerImage.gameObject.SetActive(false);
}

public void OnStartButtonClick()


{
HideAllPanels();
gamePanel.SetActive(true);
gameRunning = true;
timer = 0;
score = 0;
correctAnswers = 0;
totalQuestions = 0;
incorrectAnswers = 0;
GenerateRandomNumbers();
FocusInputField();
}

void Update()
{
if (gameRunning)
{
timer += Time.deltaTime;
timerText.text = "Time: " + Mathf.FloorToInt(timer).ToString();

if (timeLeft > 0)
{
timeLeft -= Time.deltaTime;
timeSlider.value = timeLeft;
}
else
{
GameOver();
}

carTransform.position = Vector3.Lerp(carTransform.position, new


Vector3(correctAnswers * 2f, carTransform.position.y, carTransform.position.z),
Time.deltaTime * carMoveSpeed);

if (Input.GetKeyDown(KeyCode.Return))
{
OnSubmitButtonClick();
}
}
}

void GenerateRandomNumbers()
{
if (totalQuestions >= maxQuestions)
{
GameOver();
return;
}

num1 = Random.Range(1, 20);


int maxDifference = 5;
num2 = Random.Range(Mathf.Max(0, num1 - maxDifference), num1);

randomNum1.text = num1.ToString();
randomNum2.text = num2.ToString();
feedbackImage.color = Color.clear;
correctAnswerText.text = "";
userInput.text = "";
FocusInputField();

correctAnswerImage.gameObject.SetActive(false);
wrongAnswerImage.gameObject.SetActive(false);
}

private void FocusInputField()


{
// Clear the currently selected object in the event system
EventSystem.current.SetSelectedGameObject(null);

// Set the input field as the selected object


EventSystem.current.SetSelectedGameObject(userInput.gameObject);

// For mobile platforms, open the native keyboard


#if UNITY_IOS || UNITY_ANDROID
if (keyboard == null || !TouchScreenKeyboard.visible)
{
keyboard = TouchScreenKeyboard.Open("",
TouchScreenKeyboardType.NumberPad);
}
#endif
}

public void OnSubmitButtonClick()


{
if (string.IsNullOrEmpty(userInput.text))
{
correctAnswerText.text = "Please enter a number.";
return;
}

int userAnswer;
if (int.TryParse(userInput.text, out userAnswer))
{
int correctAnswer = num1 - num2;

if (userAnswer == correctAnswer)
{
correctAnswerImage.gameObject.SetActive(true);
wrongAnswerImage.gameObject.SetActive(false);
score++;
correctAnswers++;
carSlider.value = correctAnswers;
correctAnswerText.text = "";
}
else
{
incorrectAnswers++;
correctAnswerText.text = $"<color=#364951>The correct answer
is:</color>\n<color=#97de14>{num1} - {num2} = {correctAnswer}</color>";
wrongAnswerImage.gameObject.SetActive(true);
correctAnswerImage.gameObject.SetActive(false);
StartCoroutine(ShowCorrectAnswerAndNextQuestion(1.5f));

if (incorrectAnswers >= 10)


{
ShowIncorrectAnswerModal();
return;
}
return;
}

scoreText.text = "Score: " + score.ToString();


totalQuestions++;
StartCoroutine(DelayNextQuestion(0.5f));
}
else
{
feedbackImage.color = Color.yellow;
correctAnswerText.text = "Please enter a valid number.";
}
}

private IEnumerator DelayNextQuestion(float delayDuration)


{
yield return new WaitForSeconds(delayDuration);
GenerateRandomNumbers();
FocusInputField();
}

private IEnumerator HideFeedbackImages(float duration)


{
yield return new WaitForSeconds(duration);
correctAnswerImage.gameObject.SetActive(false);
wrongAnswerImage.gameObject.SetActive(false);
}

private IEnumerator ShowCorrectAnswerAndNextQuestion(float displayDuration)


{
correctAnswerText.text = $"<color=#364951>The correct answer is:</color>\
n<color=#97de14>{num1} - {num2} = {(num1 - num2).ToString()}</color>";
yield return new WaitForSeconds(displayDuration);
GenerateRandomNumbers();
FocusInputField();
}

private void ShowIncorrectAnswerModal()


{
gameRunning = false;
HideAllPanels();
incorrectAnswerModal.SetActive(true);
}

public void OnPlayAgainButtonClick()


{
incorrectAnswers = 0;
incorrectAnswerModal.SetActive(false);
Start();
}
public void OnQuitButtonClick()
{
Application.Quit();
}

public void GameOver()


{
gameRunning = false;
HideAllPanels();
endPanel.SetActive(true);
finalScoreText.text = "Final Score: " + score;
finalTimeText.text = "Final Time: " + Mathf.FloorToInt(timer).ToString() +
" seconds";
}

private void HideAllPanels()


{
level1Panel.SetActive(false);
gamePanel.SetActive(false);
endPanel.SetActive(false);
incorrectAnswerModal.SetActive(false);
}
}

You might also like