Core Gameplay Math
teguhikhlas
csharp
3 years ago
4.7 kB
8
Indexable
// Import required libraries
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MathGame : MonoBehaviour
{
// Declare variables
int random, velocity = 1, point = 0;
float increment = 0.1f;
int num1, num2, result, wrongResult;
// Start is called before the first frame update
void Start()
{
// Generate random integers for num1 and num2
num1 = Random.Range(1, 11);
num2 = Random.Range(1, 11);
// Generate random integer for selecting operation
random = Random.Range(1, 5);
// Check if random integer is 1 or 2 for addition or subtraction operation
if (random == 1 || random == 2)
{
// Check if random integer is 1 for addition operation or 2 for subtraction operation
if (random == 1)
{
result = num1 + num2; // Calculate correct result for addition
}
else if (random == 2)
{
result = num1 - num2; // Calculate correct result for subtraction
}
// Generate random integer for selecting correct or wrong answer
int randomPlusOrMinus = Random.Range(1, 3);
// Generate random integer for selecting wrong number to add or subtract
int randomNumWrong = Random.Range(1, 3);
// Check if random integer is 1 for adding wrong number or 2 for subtracting wrong number
if (randomPlusOrMinus == 1)
{
wrongResult = result + randomNumWrong; // Calculate wrong result by adding wrong number
}
else
{
wrongResult = result - randomNumWrong; // Calculate wrong result by subtracting wrong number
}
}
// Check if random integer is 3 or 4 for multiplication or division operation
if (random == 3 || random == 4)
{
// Check if random integer is 3 for multiplication operation or 4 for division operation
if (random == 3)
{
result = num1 * num2; // Calculate correct result for multiplication
int randomNum = 1; // Set random number to 1
// Generate random integer for selecting whether to add or subtract wrong number
int randomPlusOrMinus = Random.Range(1, 3);
// Check if random integer is 1 for adding wrong number or 2 for subtracting wrong number
if (randomPlusOrMinus == 1)
{
int wrongNum = num2 + randomNum; // Calculate wrong number by adding random number
wrongResult = num1 * wrongNum; // Calculate wrong result by multiplying wrong number
}
else
{
int wrongNum = num2 - randomNum; // Calculate wrong number by subtracting random number
wrongResult = num1 * wrongNum; // Calculate wrong result by multiplying wrong number
}
}
else if (random == 4)
{
result = num1 * num2; // Calculate correct result for multiplication
num1 = result; // Set num1 to correct result for division
result = result / num2; // Calculate correct result for division
// Generate random integer for selecting whether to add or subtract wrong number
int randomPlusOrMinus = Random.Range(1, 3);
// Generate random integer for selecting wrong number to add or subtract
int randomNumWrong = Random.Range(1, 3);
// Check if random integer is 1 for adding wrong number
if (randomPlusOrMinus == 1)
{
wrongResult = result + randomNumWrong; // Calculate wrong result by adding wrong number
}
else
{
wrongResult = result - randomNumWrong; // Calculate wrong result by subtracting wrong number
}
}
}
}
// Update is called once per frame
void Update()
{
// Check if answer is correct
bool isCorrect = CheckAnswer();
// Check if answer is correct or wrong
if (isCorrect)
{
velocity += increment; // Increase velocity by increment
point += 1; // Increase point by 1
}
else
{
gameOver(); // Call gameOver function if answer is wrong
}
}
// Check if answer is correct or wrong
bool CheckAnswer()
{
// Check if user input is equal to correct result or wrong result
}
// Game over function to end game
void gameOver()
{
// Reset velocity and point to initial values
velocity = 1;
point = 0;
// Display game over message
Debug.Log("Game Over!");
}
Editor is loading...