C# 在程序中使用实例库(库?)
我目前正在制作一个文本游戏,作为C#(WPF)的学校项目。对于必须使用哪些结构和OOP原则(类、继承、异常等),存在一些限制,因此我最终创建了3个基本类: 风景(应该是我要的场景) 游戏本身是在wpf窗口中进行的,combobox用于用户输入,textbox用于显示所有文本,listbox带有清单。这一切都很好,但是游戏的初始化非常混乱和不可追踪 我想知道是否有类似实例管理器(使用VisualStudio2012)的东西,或者是否可以以更简单的方式完成。我也在考虑自己为游戏创建一个实例管理器,使其成为一个可用的引擎,但没有足够的时间,而且我还不太擅长编程来实现这一点(仅在C#6周内工作) 有什么想法吗C# 在程序中使用实例库(库?),c#,wpf,multiple-instances,C#,Wpf,Multiple Instances,我目前正在制作一个文本游戏,作为C#(WPF)的学校项目。对于必须使用哪些结构和OOP原则(类、继承、异常等),存在一些限制,因此我最终创建了3个基本类: 风景(应该是我要的场景) 游戏本身是在wpf窗口中进行的,combobox用于用户输入,textbox用于显示所有文本,listbox带有清单。这一切都很好,但是游戏的初始化非常混乱和不可追踪 我想知道是否有类似实例管理器(使用VisualStudio2012)的东西,或者是否可以以更简单的方式完成。我也在考虑自己为游戏创建一个实例管理器,使
PS:一个类可以有多少构造函数是有限制的吗?我的UsableItem类最终有24个,涵盖了所有类型的usableitems,对我来说似乎太多了…您可以像这样使用初始化:
Dictionary<string, Node> list = new Dictionary<string, string>
{
{ "One", new Node("One") },
{ "Two", new Node("Two") }
};
“实例管理器”是什么意思?听起来更像是要将安装文件写入外部格式。XML听起来是个不错的选择,然后您可以编辑XML文件来更改游戏结构,而无需每次更改游戏时都重新编译。您可能希望研究序列化,或者甚至只需要一个负责读取XML文件和创建初始游戏状态的game manager类。NET中有大量的XML读取工具(XmlDocument很适合使用XPath查询查询XML DOM,这样您就不需要按顺序读取),为了进一步了解这一点,一个对象上的24个构造函数太多了(框架中可能有具有这么多构造函数的对象,但这是一个很好的理由)-创建对象时,可以使用成员初始化器语法初始化对象,而无需为所需的每个值组合创建构造函数。构造函数负责设置对象的基本状态,但不应负责检索数据(除了最琐碎的情况)
{
public class Person
{
/* VARIABLES */
// basic identifier
internal string _ID;
public string ID { get { return _ID; } }
// keys are keywords the person knows about
// values are answers given when asking about a specific keyword
internal Dictionary<string, string> _answers;
public bool Answering { get { if (_answers == null) return false; else return true; } }
// tree-like structure used to have a dialog with this person
internal Node _talk;
public bool Talking { get { if (_talk == null) return false; else return true; } }
// phrase to be written out when you start talking to the person
// when reseting/looping is set to current _talk value
private string _firstPhrase;
public string FirstPhrase { get { return _firstPhrase; } }
// talking interface window
internal Dialog _dialog;
public Dialog Dialog { get { return _dialog; } }
/* CONSTRUCTORS */
/* METHODS */
{
public class Item
{
/* VARIABLES */
// identifier, used for writing out general messages and identifying user input
private string _ID;
public string ID { get { return _ID; } }
// indicates if this item can be broken
protected bool _breakable;
// message to be written out after this item is broken
protected string _breakPhrase;
public string BreakPhrase { get { return _breakPhrase; } }
// list of item got after breaking this item
protected Dictionary<string, Item> _afterBreak;
public Dictionary<string, Item> AfterBreak { get { return _afterBreak; } }
public string AfterBreakPrint
{
get
{
string toPrint = "";
foreach (string key in _afterBreak.Keys) toPrint += '\n' + key;
return toPrint;
}
}
// indicates if this item can be investigated
private bool _investigate;
public bool Investigate { get { return _investigate; } }
// reference to the investigation scene
private Scenery _investigation;
public Scenery Investigation { get { return _investigation; } }
/* CONSTRUCTORS */
/* METHODS */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Markup;
namespace Textova_Hra
{
public static class GameData
{
public static Scenery start; // starting location
public static List<string> NA_ID_LIST = new List<string>(); // list of IDs in format 'x na y'
// used in Addons -> GetTwoItems
// Basically compiles the whole game returning the starting point
// Declarations must be written from the inside out
// Hra je pro češtinu, ID objektů zadávejte ve 4.pádě, jinak nebude správně fungovat!
// First goes items and people "containing" only strings(and numbers)
// Then goes lists of these items which you want to use in other items etc.
// Last goes sceneries with directions arrays declared first, then scenery declarations,
// then filling corresponding directions arrays
// Finaly the starting scene is saved into the start variable and returned
// The game may begin!
public static Scenery make_start()
{
Dictionary<string, Node> alisTalk1 = new Dictionary<string, Node>();
Dictionary<string, Node> alisLoop = new Dictionary<string, Node>();
Dictionary<string, string> alisAnswers = new Dictionary<string, string>();
alisAnswers.Add("cestu", "Tady zrovna žádné cesty nevedou.");
Node alisTalk11 = new Node("Tak to je skvělé. Už musím běžet.");
Node alisTalk12 = new Node("To je ale velice smutné. No nic, už budu muset jít.", alisLoop, true);
alisTalk1.Add("dobře", alisTalk11);
alisTalk1.Add("špatně", alisTalk12);
Node alisTalk = new Node("Ahoj, jak se máš?", alisTalk1, false);
alisLoop.Add("reset", alisTalk);
Person Alis = new Person("Alis", alisAnswers, alisTalk);
Dictionary<string, Person> start_people = new Dictionary<string, Person>();
start_people.Add("Alis", Alis);
Dictionary<string, Item> canBreak = new Dictionary<string, Item>();
UsableItem hammer = new UsableItem("kladivo", "Sebrali jste pevné železné kladivo. Určitě se jen tak nerozbije.", -1, "", canBreak);
Dictionary<string, Item> flowerVaseAfter = new Dictionary<string, Item>();
Item flowerVase = new Item("váza s květinou", false, "Váza se s ohlušujícím třískotem rozbila na střepy, vypadly z ní mince. Květina leží uvadle na zemi.", flowerVaseAfter);
Dictionary<string, Tuple<string, Item>> flowerChange = new Dictionary<string, Tuple<string, Item>>();
Tuple<string, Item> flowervaset = new Tuple<string, Item>("vázu s květinou", flowerVase);
flowerChange.Add("vázu", flowervaset);
Dictionary<string, string> flowerPhrase = new Dictionary<string, string>();
flowerPhrase.Add("vázu", "Dali jste květinu do vázy. Toto aranžmá místnost poněkud oživilo.");
UsableItem flower = new UsableItem("květina", "Sebrali jste zvláštní namodralou květinu, poněkud uvadlou.", 1, "Květina je nyní ve váze.", flowerChange, flowerPhrase);
PickableItem fragments = new PickableItem("střepy", "Sebrali jste hromádku střepů, třeba se budou ještě k něčemu hodit.");
PickableItem coins = new PickableItem("mince", "Sebrali jste pár zlatých mincí, kterým jste zkrátka nemohli ododlat.");
Dictionary<string, Item> afterVase = new Dictionary<string, Item>();
afterVase.AddMany(fragments, coins);
flowerVaseAfter.AddRange(afterVase);
Scenery[] vaseBack = new Scenery[4];
Scenery vaseInvestigate = new Scenery("Zkoumáte vázu", "Ve váze vidíte několik mincí, ale hrdlo je příliš úzké, aby se daly vysypat.", vaseBack);
Item vase = new Item("váza", true, "Váza se s ohlušujícím třískotem rozbila na střepy a vypadly z ní mince.", afterVase, vaseInvestigate);
canBreak.Add("vázu", vase);
canBreak.Add("vázu s květinou", flowerVase);
Dictionary<string, Item> startItems = new Dictionary<string, Item>();
startItems.Add("vázu", vase);
startItems.Add("květinu", flower);
startItems.Add("kladivo", hammer);
Scenery[] startDirs = new Scenery[4];
Scenery x = new Scenery("Úvod", "Vítejte v nové hře." + '\n' + "Jste v místnosti v malém domku. Mítnost je poloprázdná, je zde akorát malý stůl s vázou, vedle vázy leží květina a pod stolem někdo zapomněl kladivo.", startItems, start_people, startDirs);
vaseBack[0] = x;
start = x;
return start;
}
}
}
public MainWindow()
{
InitializeComponent();
/* GameData - static class containing declarations of all game instances + helpful variables
method make_start inicializes all game instances and returns a starting point */
current = GameData.make_start();
// print out starting scene description
tbStory.Text = current.Phrase;
lblSceneName.Content = current.ID;
Application.Current.MainWindow.Closed += new EventHandler(MainWindow_Closed);
}
Dictionary<string, Node> list = new Dictionary<string, string>
{
{ "One", new Node("One") },
{ "Two", new Node("Two") }
};
public class Test1
{
public Test1() { }
public Test1(int i) { }
public Test1(int i) { } /* Syntax error 'Test already defines a member called Test with the same parameter types */
}
public class Test2
{
public Test2() { }
public Test2(int i) { }
public Test2(int i, int j) { }
}