首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单问答游戏

简单问答游戏
EN

Code Review用户
提问于 2016-04-27 09:29:39
回答 3查看 1.1K关注 0票数 4

我只想得到一些批评,我的程序,使我更好。我在团结中做了一个游戏,只是一个简单的问答游戏。

演示

代码语言:javascript
复制
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using UnityEditor.VersionControl;
using System.Threading;
using System;

[System.Serializable]
class Question
{
    public string question;

    public bool answer;

    public Question(string question, bool answer)
    {
        this.answer = answer;
        this.question = question;
    }

    public string getQuestion()
    {
        return question;
    }
    public void setQuestion(string msg)
    {
        question = msg;
    }

    public bool getAnswer()
    {
        return answer;
    }


}

class NoMoreQuestionsException : Exception
{
    public NoMoreQuestionsException()
    {
    }
} 


public class GameManager : MonoBehaviour {

    Question currentQuestion;
    [SerializeField]
    float buttonMoveSpeed;

    int points = 0;

    bool isPlayerCorrect;

    bool changedAfterAnswer = false;

    bool moveComplete = false;

    int answer = 0;

    int currentQuestionNumber;

    [SerializeField]
    float waitBetweenQuestions;

    //List of questions
    [SerializeField]
    Question[] questions;

    List<Question> unansweredQuestions = new List<Question>();

    //Variables
    [SerializeField]
    GameObject questionText;

    [SerializeField]
    GameObject falseButton;

    [SerializeField]
    GameObject trueButton;

    [SerializeField]
    Text trueAnswerText;

    [SerializeField]
    Text falseAnswerText;

    [SerializeField]
    Text pointsText;

    public void Answered (bool playerAnswer)
    {
        if (playerAnswer)
        {
            answer = 1;
        } else
        {
            answer = -1;
        }
    }

    void NewRound()
    {
        if (unansweredQuestions.Count < 2)
        {
            Debug.Log("Error! No more questions to answer!");
            throw new NoMoreQuestionsException();
            return;
        }
        //Remove old question
        unansweredQuestions.RemoveAt(currentQuestionNumber);
        //Check if there are any more questions to answer
        Debug.Log("New Round!");
        //Generate new question number
        System.Random ran = new System.Random();
        currentQuestionNumber = (ran.Next(unansweredQuestions.Count - 1));
        //Set current question
        currentQuestion = unansweredQuestions[currentQuestionNumber];
        //Set question text to the new question
        Text txt = questionText.GetComponent<Text>();
        txt.text = currentQuestion.question;
        //Reset players result
        answer = 0;
        //Set the buttons back to centre
        falseButton.transform.position = new Vector3(1012.5f, 218.5f, -107.9f);
        trueButton.transform.position = new Vector3(283.5f, 218.5f, -107.9f);
        changedAfterAnswer = false;
    }

    void Start () {

        //Load points
        System.Random ran1 = new System.Random();
        points = ran1.Next(100);
        pointsText.text = points.ToString();

        //Se the level

        //Set unaswered questions to the main questions
        unansweredQuestions = questions.ToList();
        //Check if there are any questions to answer
        if (unansweredQuestions.Count < 1)
        {
            Debug.Log("Error! No questions to answer!");
        }
        Debug.Log("New Round!");
        //Generate new question number
        System.Random ran = new System.Random();
        currentQuestionNumber = (ran.Next(unansweredQuestions.Count - 1));
        //Set current question
        currentQuestion = unansweredQuestions[currentQuestionNumber];
        //Set question text to the new question
        Text txt = questionText.GetComponent<Text>();
        txt.text = currentQuestion.question;
        //Reset players result
        answer = 0;
        //Set the buttons back to centre
        falseButton.transform.position = new Vector3(1012.5f, 218.5f, -107.9f);
        trueButton.transform.position = new Vector3(283.5f, 218.5f, -107.9f);
        changedAfterAnswer = false;
        Debug.Log("Question Number: " + currentQuestionNumber);
    }

    void moveFalse()
    {
        if (falseButton.transform.position.x > 1330)
        {
            moveComplete = true;
            return;
        }

        Vector3 pos = falseButton.transform.position;

        pos.x += buttonMoveSpeed;

        falseButton.transform.position = pos;
        moveComplete = false;
    }

    void moveTrue()
    {
        if (trueButton.transform.position.x < -25)
        {
            moveComplete = true;
            return;
        }

        Vector3 pos = trueButton.transform.position;

        pos.x -= buttonMoveSpeed;

        trueButton.transform.position = pos;
        moveComplete = false;
    }

    //Check if users answer is correct
    bool isCorrect(bool playerAnswer)
    {
        if (currentQuestion.getAnswer() == playerAnswer)
        {
            points += 1;
            pointsText.text = points.ToString();
            return true;
        }
        return false;
    }

    void setAnswerText(bool answer, string text)
    {
        if (!answer)
        {
            trueAnswerText.text = text;
        } else
        {
            falseAnswerText.text = text;
        }
    }

    IEnumerator StartWait()
    {
        yield return new WaitForSeconds(waitBetweenQuestions);
        WaitForNewQuestion();
    }

    void WaitForNewQuestion()
    {

        NewRound();
        startedTimer = false;
    }

    bool startedTimer = false;

    void Update () {
        //Checking if the player has clicked a button
        if (answer == 0)
        {
            return;
        }
        //Check is user has pressed the true button.
        if (answer == 1)
        {
            //Check if player is correct and change the answer text to show.
            if (!changedAfterAnswer)
            {
                if (isCorrect(true))
                {
                    setAnswerText(true, "Correct!");
                }
                else
                {
                    setAnswerText(true, "Incorrect");
                }
                changedAfterAnswer = true;
            }
            //Move the false button
            moveFalse();
            if (!startedTimer)
            {
                Debug.Log("STARTED WA8T");
                startedTimer = true;
                StartCoroutine(StartWait());
            }
        }
        //Check is user has pressed the false button.
        if (answer == -1)
        {
            //Check if player is correct and change the answer text to show.
            if (!changedAfterAnswer)
            {
                if (isCorrect(false))
                {
                    setAnswerText(false, "Correct!");
                }
                else
                {
                    setAnswerText(false, "Incorrect");
                }
                changedAfterAnswer = true;
            }
            //Move the true button
            moveTrue();
            if (!startedTimer)
            {
                Debug.Log("STARTED WA8T");
                startedTimer = true;
                StartCoroutine(StartWait());
            }
        }

    }
}
EN

回答 3

Code Review用户

发布于 2016-04-27 10:26:58

您应该避免代码的重复。

例如:

1.你可以提取

代码语言:javascript
复制
    if (falseButton.transform.position.x > 1330)
    {
        moveComplete = true;
        return;
    }

    Vector3 pos = falseButton.transform.position;

    pos.x += buttonMoveSpeed;

    falseButton.transform.position = pos;
    moveComplete = false;

将1330或-25作为参数传递给一个方法,并将其重用,而不是两次使用几乎完全相同的代码。

2.你可以提取

代码语言:javascript
复制
        if (!changedAfterAnswer)
        {
            if (isCorrect(true))
            {
                setAnswerText(true, "Correct!");
            }
            else
            {
                setAnswerText(true, "Incorrect");
            }
            changedAfterAnswer = true;
        }
        //Move the false button
        moveFalse();
        if (!startedTimer)
        {
            Debug.Log("STARTED WA8T");
            startedTimer = true;
            StartCoroutine(StartWait());
        }

将true或false作为参数传递给一个方法,并重用它,而不是两次使用几乎完全相同的代码。

票数 5
EN

Code Review用户

发布于 2016-04-27 13:47:36

在C#中进行开发时,可以使用属性而不是getter/setter属性。

它们不仅看上去更好看,而且更容易使用。所以,而不是这个

代码语言:javascript
复制
// If you are using attributes, then you should always make them private,
// if you don't there is no sense behind getters and setters.
// public string question;
// public bool answer;

private string question;
private bool answer;

public string getQuestion()
{
    return question;
}
public void setQuestion(string msg)
{
    question = msg;
}

public bool getAnswer()
{
    return answer;
}

您可以使用以下内容:

代码语言:javascript
复制
// Note: Properties start with a capital letter
public string Question { get; set; }
public bool Answer { get; }

之后,您可以使用yourObject.QuestionyourObject.Answer访问那些。

否则,我只能同意MrSmith先生的回答,即您可能希望创建更多的方法,以使代码更简单,并避免重复。

此外,我觉得你还没有完全理解OOP的概念,所以你可能想读一下它。

票数 4
EN

Code Review用户

发布于 2016-04-27 16:57:30

幻数

你有很多神奇的数字会让人困惑。见下面的例子。

  • new Vector3(1012.5f, 218.5f, -107.9f)
  • if (falseButton.transform.position.x > 1330)
  • if (trueButton.transform.position.x < -25)

这些可能现在是有意义的,但在未来(比如说从现在起的2周内,当你已经深入到游戏代码的另一部分时),你选择这些数字的原因可能还不太清楚。

因此,为了使这段代码更具可读性和可维护性,我建议将它们设置为常量(或者至少是变量,取决于它们是否被更改或计算)。

  • private static readonly Vector3 FalseButtonOrigin = new Vector3(1012.5f, 218.5f, -107.9f);
  • private const int FalseButtonDestination = 1330;
  • private const int TrueButtonDestination = -25;

我可能弄错了神奇数字的目的,但我认为你明白重点。让他们有个有意义的名字。在最坏的情况下,您可以给他们尽可能好的名称,并在描述其用途的const/variable声明上放置一个注释。

小建议

  • Update() --也许把你必须的3条if-语句切换到一个开关语句可能是有用的吗?
  • Start() -您有一个声明的Text txt = questionText.GetComponent<Text>();,但您似乎只使用它一次。变量确实会占用内存,这些内存可以加起来。这是比我更需要思考的事情,告诉你要改变它。一方面,您的游戏可能占用大量内存,您想要减少它;另一方面,您可能需要对该变量添加另一个调用,从而使声明更有用,维护也更少。所以,只要指出这一点,你就可以记住这些事情。
票数 4
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/126825

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档