简体   繁体   English

C#应用程序/游戏,最佳方法

[英]C# application/game, best approach

I want to create a game something like "Who wants to be millionaire?", where you answer questions by clicking one of the three buttons. 我想创建一个类似“谁想成为百万富翁?”的游戏,您可以通过单击三个按钮之一来回答问题。 I'm kind of noob in c# so I don't know what would be the best approach to do this. 我在C#中有点菜鸟,所以我不知道什么是最好的方法。 I was thinking of making the loop and then creating objects (question - object). 我当时正在考虑进行循环,然后创建对象(问题-对象)。 But how would I connect form buttons with these new objects? 但是如何将表单按钮与这些新对象连接起来? Or maybe there is a better way to accomplish this? 也许有更好的方法可以做到这一点? Thank you all for suggestions! 谢谢大家的建议!

First of all: Unless you need your application to run on other platforms than Windows you should look into Windows Presentation Foundation, it's the successor of Winforms. 首先:除非您需要您的应用程序在Windows以外的其他平台上运行,否则您应该考虑Windows Presentation Foundation,它是Winforms的后继者。

Regarding how to implement this a simple solution would be to have a CurrentQuestion property containing an instance of a Question class with Answers inside. 关于如何实现这一点,一个简单的解决方案是拥有一个CurrentQuestion属性,该属性包含一个内部带有Answers的Question类的实例。

class Question
{
     public string QuestionText {get;}
     public Answer AnswerA { get; }
     public Answer AnswerB { get; }
     public Answer AnswerC { get; }
     public Answer AnswerD { get; }
     public Answer ChosenAnswer { get;set;}
}

class Answer
{
    public string AnswerText { get; }
}

Each buttons Text-property binds to an answer in the CurrentQuestion and when you click the button the answer is chosen and the next question can be processed. 每个按钮文本属性都绑定到CurrentQuestion中的答案,并且当您单击按钮时,答案将被选择,并且下一个问题可以被处理。

I will presume that you will do this in windows Forms or WPF (Windows Presentation Foundation) - an application running on a Windows PC and not a web application. 我假设您将在Windows Forms或WPF(Windows Presentation Foundation)中执行此操作-Windows PC上运行的应用程序而不是Web应用程序。

What I would do is to prepare a complete model (or object) that would have the question and the correct answer which I would use to load a view - a bunch of controls (TextBoxes, Labels, Buttons etc) that would present the information. 我要做的是准备一个完整的模型 (或对象),该模型具有问题和正确的答案(可用于加载视图)-一堆控件(文本框,标签,按钮等),这些控件将显示信息。

Since both Windows Forms and WPF support the notion of events , I would hook up handlers the click events of the three button and take action on a click of any of those buttons. 由于Windows Forms和WPF都支持事件的概念,因此我将联系处理程序中三个按钮的click事件,并对其中任何一个按钮的单击采取操作。 And since I loaded the view with the correct answer, I wouldn't have to load anything on the click event which means I could determine if the answer would be correct immediately. 并且由于我用正确的答案加载了视图,因此不必在click事件上加载任何内容,这意味着我可以立即确定答案是否正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM