// This function gets the action from the model, and adds rewards/penalties if // any public override void OnActionReceived(float[] vectorAction) { // create a controlSignal vector and set x value to the first value in the vectorAction array(modified for time and sensitivity) Vector3 controlSignal = new Vector3(0, 0, 0); controlSignal.x = vectorAction[0] * Time.deltaTime * sens; // move the paddle based off the controlSignal gameObject.transform.Translate(controlSignal); // give reward for the paddle hitting the ball if (ballScript.GetPaddleCollision()) { AddReward(0.05f); } // Ball Fell; give large penalty if (ballScript.GetClearBall()) { AddReward(-1.0f); } // Ball Moving; give small reward if (my_ball.transform.localPosition.y > gameObject.transform.localPosition.y) { AddReward(0.01f); } // give rewards based off change in score(bricks destroyed) int prevScore = curScore; curScore = Int32.Parse(scoreUGUI.text); if (curScore != prevScore) { float scoreDiff = curScore - prevScore; AddReward(scoreDiff / 10.0f); } // end the episode if training and the bricks are destroyed or the player has 0 lives if (training) { // end episode when lose a live, or all bricks are destroyed if (curScore % 448 == 0 && curScore != 0) { EndEpisode(); } // if the lives reaches 0, then end the training episode curLives = Int32.Parse(livesUGUI.text); if (curLives == 0) { EndEpisode(); } } }
// Update is called once per frame void Update() { // if both players have 0 lives, then load the game over scene if (Int32.Parse(playerLivesUGUI.text) <= 0 && Int32.Parse(machineLivesUGUI.text) <= 0) { UnityEngine.SceneManagement.SceneManager.LoadScene("Game Over"); } // if player hits space or clicks the mouse if ((Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.Mouse0)) && Int32.Parse(playerLivesUGUI.text) > 0) { // clear the prompt displayUGUI.text = ""; // set buttonSpawn variable of machineBallScript to false so balls spawn constantly machineBallScript.buttonSpawn = false; } // if the player loses a ball, then display the prompt to the user again if (playerBallScript.GetClearBall() && Int32.Parse(playerLivesUGUI.text) > 0) { displayUGUI.text = prompt; } // if machine loses all of their lives before the player and the machine has a lower score if (Int32.Parse(machineLivesUGUI.text) <= 0 && Int64.Parse(machineScoreUGUI.text) < Int64.Parse(playerScoreUGUI.text)) { // display that the user wins secondPromptUGUI.text = "Player Wins!"; } // if player loses all of their lives, then display game over on screen for the player if (Int32.Parse(playerLivesUGUI.text) <= 0) { displayUGUI.text = "Game Over"; // if the player has lost all of their lives, and their score is less than the machine if (Int64.Parse(machineScoreUGUI.text) > Int64.Parse(playerScoreUGUI.text)) { // display prompts to the user displayUGUI.text = "Press Space to Quit"; secondPromptUGUI.text = "Machine Wins!"; // if the player hits space or clicks, then load game over scene if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.Mouse0)) { UnityEngine.SceneManagement.SceneManager.LoadScene("Game Over"); } } } }